Use comedi_perror to report error if comedi_open fails, and exit with exit
[comedilib.git] / demo / info.c
1 /*
2    This demo reads information about a comedi device and
3    displays the information in a human-readable form.
4  */
5
6 #include <stdio.h>
7 #include <comedilib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <errno.h>
12 #include <string.h>
13 #include "examples.h"
14
15 void get_command_stuff(comedi_t *it,int s);
16
17 void help(void)
18 {
19         fprintf(stderr,"info </dev/comediN>\n");
20         exit(0);
21 }
22
23 char *tobinary(char *s,int bits,int n);
24
25 char *subdevice_types[]={
26         "unused",
27         "analog input",
28         "analog output",
29         "digital input",
30         "digital output",
31         "digital I/O",
32         "counter",
33         "timer",
34         "memory",
35         "calibration",
36         "processor",
37         "serial digital I/O"
38 };
39
40 comedi_t *it;
41 extern char *filename;
42
43
44 int main(int argc,char *argv[])
45 {
46         int i,j;
47         int n_subdevices,type;
48         int chan,n_chans;
49         int n_ranges;
50         int subdev_flags;
51         comedi_range *rng;
52         
53         parse_options(argc,argv);
54
55         it=comedi_open(filename);
56         if(!it){
57                 comedi_perror(filename);
58                 exit(1);
59         }
60
61         printf("overall info:\n");
62         printf("  version code: 0x%06x\n",comedi_get_version_code(it));
63         printf("  driver name: %s\n",comedi_get_driver_name(it));
64         printf("  board name: %s\n",comedi_get_board_name(it));
65         printf("  number of subdevices: %d\n",n_subdevices=comedi_get_n_subdevices(it));
66         
67         for(i=0;i<n_subdevices;i++){
68                 printf("subdevice %d:\n",i);
69                 type=comedi_get_subdevice_type(it,i);
70                 printf("  type: %d (%s)\n",type,subdevice_types[type]);
71                 if(type==COMEDI_SUBD_UNUSED)
72                         continue;
73                 subdev_flags = comedi_get_subdevice_flags(it, i);
74                 printf("  flags: 0x%08x\n",subdev_flags);
75                 n_chans=comedi_get_n_channels(it,i);
76                 printf("  number of channels: %d\n",n_chans);
77                 if(!comedi_maxdata_is_chan_specific(it,i)){
78                         printf("  max data value: %d\n",comedi_get_maxdata(it,i,0));
79                 }else{
80                         printf("  max data value: (channel specific)\n");
81                         for(chan=0;chan<n_chans;chan++){
82                                 printf("    chan%d: %d\n",chan,
83                                         comedi_get_maxdata(it,i,chan));
84                         }
85                 }
86                 printf("  ranges:\n");
87                 if(!comedi_range_is_chan_specific(it,i)){
88                         n_ranges=comedi_get_n_ranges(it,i,0);
89                         printf("    all chans:");
90                         for(j=0;j<n_ranges;j++){
91                                 rng=comedi_get_range(it,i,0,j);
92                                 printf(" [%g,%g]",rng->min,rng->max);
93                         }
94                         printf("\n");
95                 }else{
96                         for(chan=0;chan<n_chans;chan++){
97                                 n_ranges=comedi_get_n_ranges(it,i,chan);
98                                 printf("    chan%d:",chan);
99                                 for(j=0;j<n_ranges;j++){
100                                         rng=comedi_get_range(it,i,chan,j);
101                                         printf(" [%g,%g]",rng->min,rng->max);
102                                 }
103                                 printf("\n");
104                         }
105                 }
106                 printf("  command:\n");
107                 get_command_stuff(it,i);
108         }
109         
110         return 0;
111 }
112
113 char *tobinary(char *s,int bits,int n)
114 {
115         int bit=1<<n;
116         char *t=s;
117         
118         for(;bit;bit>>=1)
119                 *t++=(bits&bit)?'1':'0';
120         *t=0;
121         
122         return s;
123 }
124
125 void probe_max_1chan(comedi_t *it,int s);
126
127 void get_command_stuff(comedi_t *it,int s)
128 {
129         comedi_cmd cmd;
130         char buf[100];
131
132         if(comedi_get_cmd_src_mask(it,s,&cmd)<0){
133                 printf("    not supported\n");
134         }else{
135                 printf("    start: %s\n",cmd_src(cmd.start_src,buf));
136                 printf("    scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
137                 printf("    convert: %s\n",cmd_src(cmd.convert_src,buf));
138                 printf("    scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
139                 printf("    stop: %s\n",cmd_src(cmd.stop_src,buf));
140         
141                 probe_max_1chan(it,s);
142         }
143 }
144
145 void probe_max_1chan(comedi_t *it,int s)
146 {
147         comedi_cmd cmd;
148         char buf[100];
149
150         printf("  command fast 1chan:\n");
151         if(comedi_get_cmd_generic_timed(it,s,&cmd,1)<0){
152                 printf("    not supported\n");
153         }else{
154                 printf("    start: %s %d\n",
155                         cmd_src(cmd.start_src,buf),cmd.start_arg);
156                 printf("    scan_begin: %s %d\n",
157                         cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
158                 printf("    convert: %s %d\n",
159                         cmd_src(cmd.convert_src,buf),cmd.convert_arg);
160                 printf("    scan_end: %s %d\n",
161                         cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
162                 printf("    stop: %s %d\n",
163                         cmd_src(cmd.stop_src,buf),cmd.stop_arg);
164         }
165 }
166