doc: add missing -lm option to command line for compiling tut1
[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 static const char * const 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         "pwm"
39 };
40
41 comedi_t *it;
42 extern char *filename;
43
44
45 int main(int argc,char *argv[])
46 {
47         int i,j;
48         int n_subdevices,type;
49         const char *type_str;
50         int chan,n_chans;
51         int n_ranges;
52         int subdev_flags;
53         comedi_range *rng;
54         struct parsed_options options;
55
56         init_parsed_options(&options);
57         parse_options(&options, argc, argv);
58
59         it = comedi_open(options.filename);
60         if(!it){
61                 comedi_perror(options.filename);
62                 exit(1);
63         }
64
65         printf("overall info:\n");
66         printf("  version code: 0x%06x\n", comedi_get_version_code(it));
67         printf("  driver name: %s\n", comedi_get_driver_name(it));
68         printf("  board name: %s\n", comedi_get_board_name(it));
69         printf("  number of subdevices: %d\n", n_subdevices = comedi_get_n_subdevices(it));
70
71         for(i = 0; i < n_subdevices; i++){
72                 printf("subdevice %d:\n",i);
73                 type = comedi_get_subdevice_type(it, i);
74                 if(type < (int)(sizeof(subdevice_types) / sizeof(subdevice_types[0]))){
75                         type_str = subdevice_types[type];
76                 }else{
77                         type_str = "UNKNOWN";
78                 }
79                 printf("  type: %d (%s)\n",type,type_str);
80                 if(type==COMEDI_SUBD_UNUSED)
81                         continue;
82                 subdev_flags = comedi_get_subdevice_flags(it, i);
83                 printf("  flags: 0x%08x\n",subdev_flags);
84                 n_chans=comedi_get_n_channels(it,i);
85                 printf("  number of channels: %d\n",n_chans);
86                 if(!comedi_maxdata_is_chan_specific(it,i)){
87                         printf("  max data value: %lu\n", (unsigned long)comedi_get_maxdata(it,i,0));
88                 }else{
89                         printf("  max data value: (channel specific)\n");
90                         for(chan=0;chan<n_chans;chan++){
91                                 printf("    chan%d: %lu\n",chan,
92                                         (unsigned long)comedi_get_maxdata(it,i,chan));
93                         }
94                 }
95                 printf("  ranges:\n");
96                 if(!comedi_range_is_chan_specific(it,i)){
97                         n_ranges=comedi_get_n_ranges(it,i,0);
98                         printf("    all chans:");
99                         for(j=0;j<n_ranges;j++){
100                                 rng=comedi_get_range(it,i,0,j);
101                                 printf(" [%g,%g]",rng->min,rng->max);
102                         }
103                         printf("\n");
104                 }else{
105                         for(chan=0;chan<n_chans;chan++){
106                                 n_ranges=comedi_get_n_ranges(it,i,chan);
107                                 printf("    chan%d:",chan);
108                                 for(j=0;j<n_ranges;j++){
109                                         rng=comedi_get_range(it,i,chan,j);
110                                         printf(" [%g,%g]",rng->min,rng->max);
111                                 }
112                                 printf("\n");
113                         }
114                 }
115                 printf("  command:\n");
116                 get_command_stuff(it,i);
117         }
118
119         return 0;
120 }
121
122 char *tobinary(char *s,int bits,int n)
123 {
124         int bit=1<<n;
125         char *t=s;
126
127         for(;bit;bit>>=1)
128                 *t++=(bits&bit)?'1':'0';
129         *t=0;
130
131         return s;
132 }
133
134 void probe_max_1chan(comedi_t *it,int s);
135
136 void get_command_stuff(comedi_t *it,int s)
137 {
138         comedi_cmd cmd;
139         char buf[100];
140
141         if(comedi_get_cmd_src_mask(it,s,&cmd)<0){
142                 printf("    not supported\n");
143         }else{
144                 printf("    start: %s\n",cmd_src(cmd.start_src,buf));
145                 printf("    scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
146                 printf("    convert: %s\n",cmd_src(cmd.convert_src,buf));
147                 printf("    scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
148                 printf("    stop: %s\n",cmd_src(cmd.stop_src,buf));
149
150                 probe_max_1chan(it,s);
151         }
152 }
153
154 void probe_max_1chan(comedi_t *it,int s)
155 {
156         comedi_cmd cmd;
157         char buf[100];
158
159         printf("  command fast 1chan:\n");
160         if(comedi_get_cmd_generic_timed(it, s, &cmd, 1, 1)<0){
161                 printf("    not supported\n");
162         }else{
163                 printf("    start: %s %d\n",
164                         cmd_src(cmd.start_src,buf),cmd.start_arg);
165                 printf("    scan_begin: %s %d\n",
166                         cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
167                 printf("    convert: %s %d\n",
168                         cmd_src(cmd.convert_src,buf),cmd.convert_arg);
169                 printf("    scan_end: %s %d\n",
170                         cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
171                 printf("    stop: %s %d\n",
172                         cmd_src(cmd.stop_src,buf),cmd.stop_arg);
173         }
174 }
175