Added new gpct_simple_counting demo (shares some code with
[comedilib.git] / demo / common.c
1 /*
2  * This is a little helper function to parse options that
3  * are common to most of the examples.
4  */
5
6 #include <stdio.h>
7 #include <comedilib.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <getopt.h>
12 #include <ctype.h>
13 #include <malloc.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include "examples.h"
17
18 static char * const default_filename = "/dev/comedi0";
19
20 void init_parsed_options(struct parsed_options *options)
21 {
22         memset(options, 0, sizeof(struct parsed_options));
23         options->filename = default_filename;
24         options->aref = AREF_GROUND;
25         options->n_chan = 4;
26         options->n_scan = 1000;
27         options->freq = 1000.0;
28         options->physical = 0;
29         options->value = 0.;
30 }
31
32 int parse_options(struct parsed_options *options, int argc, char *argv[])
33 {
34         int c;
35
36         while (-1 != (c = getopt(argc, argv, "a:c:s:r:f:n:N:F:pvdgom"))) {
37                 switch (c) {
38                 case 'f':
39                         options->filename = optarg;
40                         break;
41                 case 's':
42                         options->subdevice = strtoul(optarg, NULL, 0);
43                         break;
44                 case 'c':
45                         options->channel = strtoul(optarg, NULL, 0);
46                         break;
47                 case 'a':
48                         options->aref = strtoul(optarg, NULL, 0);
49                         break;
50                 case 'r':
51                         options->range = strtoul(optarg, NULL, 0);
52                         break;
53                 case 'n':
54                         options->n_chan = strtoul(optarg, NULL, 0);
55                         break;
56                 case 'N':
57                         options->n_scan = strtoul(optarg, NULL, 0);
58                         break;
59                 case 'F':
60                         options->freq = strtod(optarg, NULL);
61                         break;
62                 case 'p':
63                         options->physical = 1;
64                         break;
65                 case 'v':
66                         ++options->verbose;
67                         break;
68                 case 'd':
69                         options->aref = AREF_DIFF;
70                         break;
71                 case 'g':
72                         options->aref = AREF_GROUND;
73                         break;
74                 case 'o':
75                         options->aref = AREF_OTHER;
76                         break;
77                 case 'm':
78                         options->aref = AREF_COMMON;
79                         break;
80                 default:
81                         printf("bad option\n");
82                         exit(1);
83                 }
84         }
85         if(optind < argc) {
86                 /* data value */
87                 options->value = strtod(argv[optind++], NULL);
88         }
89
90         return argc;
91 }
92
93 char *cmd_src(int src,char *buf)
94 {
95         buf[0]=0;
96
97         if(src&TRIG_NONE)strcat(buf,"none|");
98         if(src&TRIG_NOW)strcat(buf,"now|");
99         if(src&TRIG_FOLLOW)strcat(buf, "follow|");
100         if(src&TRIG_TIME)strcat(buf, "time|");
101         if(src&TRIG_TIMER)strcat(buf, "timer|");
102         if(src&TRIG_COUNT)strcat(buf, "count|");
103         if(src&TRIG_EXT)strcat(buf, "ext|");
104         if(src&TRIG_INT)strcat(buf, "int|");
105 #ifdef TRIG_OTHER
106         if(src&TRIG_OTHER)strcat(buf, "other|");
107 #endif
108
109         if(strlen(buf)==0){
110                 sprintf(buf,"unknown(0x%08x)",src);
111         }else{
112                 buf[strlen(buf)-1]=0;
113         }
114
115         return buf;
116 }
117
118 void dump_cmd(FILE *out,comedi_cmd *cmd)
119 {
120         char buf[100];
121
122         fprintf(out,"start:      %-8s %d\n",
123                 cmd_src(cmd->start_src,buf),
124                 cmd->start_arg);
125
126         fprintf(out,"scan_begin: %-8s %d\n",
127                 cmd_src(cmd->scan_begin_src,buf),
128                 cmd->scan_begin_arg);
129
130         fprintf(out,"convert:    %-8s %d\n",
131                 cmd_src(cmd->convert_src,buf),
132                 cmd->convert_arg);
133
134         fprintf(out,"scan_end:   %-8s %d\n",
135                 cmd_src(cmd->scan_end_src,buf),
136                 cmd->scan_end_arg);
137
138         fprintf(out,"stop:       %-8s %d\n",
139                 cmd_src(cmd->stop_src,buf),
140                 cmd->stop_arg);
141 }
142
143 int arm(comedi_t *device, unsigned subdevice, lsampl_t source)
144 {
145         comedi_insn insn;
146         lsampl_t data[2];
147         int retval;
148
149         memset(&insn, 0, sizeof(comedi_insn));
150         insn.insn = INSN_CONFIG;
151         insn.subdev = subdevice;
152         insn.chanspec = 0;
153         insn.data = data;
154         insn.n = sizeof(data) / sizeof(data[0]);
155         data[0] = INSN_CONFIG_ARM;
156         data[1] = source;
157
158         retval = comedi_do_insn(device, &insn);
159         if(retval < 0)
160         {
161                 fprintf(stderr, "%s: error:\n", __FUNCTION__);
162                 comedi_perror("comedi_do_insn");
163                 return retval;
164         }
165         return 0;
166 }
167
168 /* This resets the count to zero and disarms the counter.  The counter output
169  is set low. */
170 int reset_counter(comedi_t *device, unsigned subdevice)
171 {
172         comedi_insn insn;
173         lsampl_t data[1];
174         int retval;
175
176         memset(&insn, 0, sizeof(comedi_insn));
177         insn.insn = INSN_CONFIG;
178         insn.subdev = subdevice;
179         insn.chanspec = 0;
180         insn.data = data;
181         insn.n = sizeof(data) / sizeof(data[0]);
182         data[0] = INSN_CONFIG_RESET;
183
184         retval = comedi_do_insn(device, &insn);
185         if(retval < 0)
186         {
187                 fprintf(stderr, "%s: error:\n", __FUNCTION__);
188                 comedi_perror("comedi_do_insn");
189                 return retval;
190         }
191         return 0;
192 }
193
194 int set_counter_mode(comedi_t *device, unsigned subdevice, lsampl_t mode_bits)
195 {
196         comedi_insn insn;
197         lsampl_t data[2];
198         int retval;
199
200         memset(&insn, 0, sizeof(comedi_insn));
201         insn.insn = INSN_CONFIG;
202         insn.subdev = subdevice;
203         insn.chanspec = 0;
204         insn.data = data;
205         insn.n = sizeof(data) / sizeof(data[0]);
206         data[0] = INSN_CONFIG_SET_COUNTER_MODE;
207         data[1] = mode_bits;
208
209         retval = comedi_do_insn(device, &insn);
210         if(retval < 0)
211         {
212                 fprintf(stderr, "%s: error:\n", __FUNCTION__);
213                 comedi_perror("comedi_do_insn");
214                 return retval;
215         }
216         return 0;
217 }
218
219 int set_clock_source(comedi_t *device, unsigned subdevice, lsampl_t clock, lsampl_t period_ns)
220 {
221         comedi_insn insn;
222         lsampl_t data[3];
223         int retval;
224
225         memset(&insn, 0, sizeof(comedi_insn));
226         insn.insn = INSN_CONFIG;
227         insn.subdev = subdevice;
228         insn.chanspec = 0;
229         insn.data = data;
230         insn.n = sizeof(data) / sizeof(data[0]);
231         data[0] = INSN_CONFIG_SET_CLOCK_SRC;
232         data[1] = clock;
233         data[2] = period_ns;
234
235         retval = comedi_do_insn(device, &insn);
236         if(retval < 0)
237         {
238                 fprintf(stderr, "%s: error:\n", __FUNCTION__);
239                 comedi_perror("comedi_do_insn");
240                 return retval;
241         }
242         return 0;
243 }
244
245 int set_gate_source(comedi_t *device, unsigned subdevice, lsampl_t gate_index, lsampl_t gate_source)
246 {
247         comedi_insn insn;
248         lsampl_t data[3];
249         int retval;
250
251         memset(&insn, 0, sizeof(comedi_insn));
252         insn.insn = INSN_CONFIG;
253         insn.subdev = subdevice;
254         insn.chanspec = 0;
255         insn.data = data;
256         insn.n = sizeof(data) / sizeof(data[0]);
257         data[0] = INSN_CONFIG_SET_GATE_SRC;
258         data[1] = gate_index;
259         data[2] = gate_source;
260
261         retval = comedi_do_insn(device, &insn);
262         if(retval < 0)
263         {
264                 fprintf(stderr, "%s: error:\n", __FUNCTION__);
265                 comedi_perror("comedi_do_insn");
266                 return retval;
267         }
268         return 0;
269 }