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