added lots of more information
[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 <sys/ioctl.h>
11 #include <errno.h>
12
13 void get_command_stuff(comedi_t *it,int s);
14
15 void help(void)
16 {
17         fprintf(stderr,"info </dev/comediN>\n");
18         exit(0);
19 }
20
21 char *tobinary(char *s,int bits,int n);
22
23 char *subdevice_types[]={
24         "unused",
25         "analog input",
26         "analog output",
27         "digital input",
28         "digital output",
29         "digital I/O",
30         "counter",
31         "timer",
32         "memory",
33         "calibration",
34         "processor"
35 };
36
37 comedi_t *it;
38 extern char *filename;
39
40
41 int main(int argc,char *argv[])
42 {
43         int i,j;
44         int n_subdevices,type;
45         int chan,n_chans;
46         int n_ranges;
47         comedi_range *rng;
48         
49         parse_options(argc,argv);
50
51         it=comedi_open(filename);
52         if(!it){
53                 fprintf(stderr,"cannot open %s\n",filename);
54                 exit(0);
55         }
56
57         printf("overall info:\n");
58         printf("  version code: 0x%06x\n",comedi_get_version_code(it));
59         printf("  driver name: %s\n",comedi_get_driver_name(it));
60         printf("  board name: %s\n",comedi_get_board_name(it));
61         printf("  number of subdevices: %d\n",n_subdevices=comedi_get_n_subdevices(it));
62         
63         for(i=0;i<n_subdevices;i++){
64                 printf("subdevice %d:\n",i);
65                 type=comedi_get_subdevice_type(it,i);
66                 printf("  type: %d (%s)\n",type,subdevice_types[type]);
67                 if(type==COMEDI_SUBD_UNUSED)
68                         continue;
69                 n_chans=comedi_get_n_channels(it,i);
70                 printf("  number of channels: %d\n",n_chans);
71                 if(!comedi_maxdata_is_chan_specific(it,i)){
72                         printf("  max data value: %d\n",comedi_get_maxdata(it,i,0));
73                 }else{
74                         printf("  max data value: (channel specific)\n");
75                         for(chan=0;chan<n_chans;chan++){
76                                 printf("    chan%d: %d\n",chan,
77                                         comedi_get_maxdata(it,i,chan));
78                         }
79                 }
80                 printf("  ranges:\n");
81                 if(!comedi_range_is_chan_specific(it,i)){
82                         n_ranges=comedi_get_n_ranges(it,i,0);
83                         printf("    all chans:");
84                         for(j=0;j<n_ranges;j++){
85                                 rng=comedi_get_range(it,i,0,j);
86                                 printf(" [%g,%g]",rng->min,rng->max);
87                         }
88                         printf("\n");
89                 }else{
90                         for(chan=0;chan<n_chans;chan++){
91                                 n_ranges=comedi_get_n_ranges(it,i,chan);
92                                 printf("    chan%d:",chan);
93                                 for(j=0;j<n_ranges;j++){
94                                         rng=comedi_get_range(it,i,chan,j);
95                                         printf(" [%g,%g]",rng->min,rng->max);
96                                 }
97                                 printf("\n");
98                         }
99                 }
100                 printf("  command:\n");
101                 get_command_stuff(it,i);
102         }
103         
104         return 0;
105 }
106
107 char *tobinary(char *s,int bits,int n)
108 {
109         int bit=1<<n;
110         char *t=s;
111         
112         for(;bit;bit>>=1)
113                 *t++=(bits&bit)?'1':'0';
114         *t=0;
115         
116         return s;
117 }
118
119 char *cmd_src(int src,char *buf)
120 {
121         buf[0]=0;
122
123         if(src&TRIG_NONE)strcat(buf,"none|");
124         if(src&TRIG_NOW)strcat(buf,"now|");
125         if(src&TRIG_FOLLOW)strcat(buf,"follow|");
126         if(src&TRIG_TIME)strcat(buf,"time|");
127         if(src&TRIG_TIMER)strcat(buf,"timer|");
128         if(src&TRIG_COUNT)strcat(buf,"count|");
129         if(src&TRIG_EXT)strcat(buf,"ext|");
130         if(src&TRIG_INT)strcat(buf,"int|");
131
132         if(strlen(buf)==0){
133                 sprintf(buf,"unknown(0x%02x)",src);
134         }else{
135                 buf[strlen(buf)-1]=0;
136         }
137
138         return buf;
139 }
140
141 void probe_max_1chan(comedi_t *it,int s);
142
143 int comedi_get_cmd_src_mask(comedi_t *it,unsigned int s,comedi_cmd *cmd)
144 {
145         memset(cmd,0,sizeof(*cmd));
146
147         cmd->subdev = s;
148
149         cmd->flags = 0;
150
151         cmd->start_src = TRIG_ANY;
152         cmd->scan_begin_src = TRIG_ANY;
153         cmd->convert_src = TRIG_ANY;
154         cmd->scan_end_src = TRIG_ANY;
155         cmd->stop_src = TRIG_ANY;
156
157         return comedi_command_test(it,cmd);
158 }
159
160
161 int comedi_get_cmd_fast_1chan(comedi_t *it,unsigned int s,comedi_cmd *cmd)
162 {
163         int ret;
164
165         comedi_get_cmd_src_mask(it,s,cmd);
166
167         cmd->chanlist_len = 1;
168
169         cmd->scan_end_src = TRIG_COUNT;
170         cmd->scan_end_arg = 1;
171
172         if(cmd->convert_src&TRIG_TIMER){
173                 if(cmd->scan_begin_src&TRIG_FOLLOW){
174                         cmd->convert_src = TRIG_TIMER;
175                         cmd->scan_begin_src = TRIG_FOLLOW;
176                 }else{
177                         cmd->convert_src = TRIG_TIMER;
178                         cmd->scan_begin_src = TRIG_TIMER;
179                 }
180         }else{
181                 printf("can't do timed?!?\n");
182                 return;
183         }
184         if(cmd->stop_src&TRIG_COUNT){
185                 cmd->stop_src=TRIG_COUNT;
186                 cmd->stop_arg=2;
187         }else if(cmd->stop_src&TRIG_NONE){
188                 cmd->stop_src=TRIG_NONE;
189                 cmd->stop_arg=0;
190         }else{
191                 printf("can't find a good stop_src\n");
192                 return;
193         }
194
195         ret=comedi_command_test(it,cmd);
196         if(ret==3){
197                 /* good */
198                 ret=comedi_command_test(it,cmd);
199         }
200         if(ret==4 || ret==0){
201                 return 0;
202         }
203         return -1;
204 }
205
206 void get_command_stuff(comedi_t *it,int s)
207 {
208         comedi_cmd cmd;
209         char buf[100];
210
211         if(comedi_get_cmd_src_mask(it,s,&cmd)<0){
212                 printf("    not supported\n");
213         }else{
214                 printf("    start: %s\n",cmd_src(cmd.start_src,buf));
215                 printf("    scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
216                 printf("    convert: %s\n",cmd_src(cmd.convert_src,buf));
217                 printf("    scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
218                 printf("    stop: %s\n",cmd_src(cmd.stop_src,buf));
219         
220                 probe_max_1chan(it,s);
221         }
222 }
223
224 void probe_max_1chan(comedi_t *it,int s)
225 {
226         comedi_cmd cmd;
227         char buf[100];
228
229         printf("  command fast 1chan:\n");
230         if(comedi_get_cmd_fast_1chan(it,s,&cmd)<0){
231                 printf("    not supported\n");
232         }else{
233                 printf("    start: %s %d\n",
234                         cmd_src(cmd.start_src,buf),cmd.start_arg);
235                 printf("    scan_begin: %s %d\n",
236                         cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
237                 printf("    convert: %s %d\n",
238                         cmd_src(cmd.convert_src,buf),cmd.convert_arg);
239                 printf("    scan_end: %s %d\n",
240                         cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
241                 printf("    stop: %s %d\n",
242                         cmd_src(cmd.stop_src,buf),cmd.stop_arg);
243         }
244 }
245