Debian Codegraph @

demo/tut3.c

raw symbols SCIP
1 /*
2 * Example of using commands - asynchronous input
3 * Part of Comedilib
4 *
5 * Copyright (c) 1999,2000,2001 David A. Schleef <[email protected]>
6 * 2008 Bernd Porr <[email protected]>
7 *
8 * This file may be freely modified, distributed, and combined with
9 * other software, as long as proper attribution is given in the
10 * source code.
11 */
12
13 #include <stdio.h>
14 #include <comedilib.h>cxx . . $ `<file>/include/comedilib.h`/
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/time.h>
18 #include <errno.h>
19 #include <stdlib.h>
20 #include <string.h>
21
22 extern comedi_tcxx . . $ comedi_t# *devicecxx . . $ device.;
23
24 struct parsed_optionscxx . . $ parsed_options# {
25 char *filenamecxx . . $ parsed_options#filename.;
26 double valuecxx . . $ parsed_options#value.;
27 int subdevicecxx . . $ parsed_options#subdevice.;
28 int channelcxx . . $ parsed_options#channel.;
29 int arefcxx . . $ parsed_options#aref.;
30 int rangecxx . . $ parsed_options#range.;
31 int verbosecxx . . $ parsed_options#verbose.;
32 int n_chancxx . . $ parsed_options#n_chan.;
33 int n_scancxx . . $ parsed_options#n_scan.;
34 double freqcxx . . $ parsed_options#freq.;
35 };
36
37
38
39 #define BUFSZcxx . . $ `demo/tut3.c:39:9`! 10000
40 char bufcxx . . $ buf.[BUFSZcxx . . $ `demo/tut3.c:39:9`!];
41
42 #define N_CHANScxx . . $ `demo/tut3.c:42:9`! 256
43 static unsigned int chanlistcxx . . $ chanlist.[N_CHANScxx . . $ `demo/tut3.c:42:9`!];
44 static comedi_rangecxx . . $ comedi_range# * range_infocxx . . $ range_info.[N_CHANScxx . . $ `demo/tut3.c:42:9`!];
45 static lsampl_tcxx . . $ lsampl_t# maxdatacxx . . $ maxdata.[N_CHANScxx . . $ `demo/tut3.c:42:9`!];
46
47
48 int prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan,
49 int n_chan, unsigned period_nanosec,
50 comedi_cmdcxx . . $ comedi_cmd# *cmd);
51
52 void do_cmdcxx . . $ do_cmd(9c43a04e3b550225).(comedi_tcxx . . $ comedi_t# *dev,comedi_cmdcxx . . $ comedi_cmd# *cmd);
53
54 void print_datumcxx . . $ print_datum(3d89109e85ced1be).(lsampl_tcxx . . $ lsampl_t# raw, int channel_index);
55
56 char *cmdtest_messagescxx . . $ cmdtest_messages.[] = {
57 "success",
58 "invalid source",
59 "source conflict",
60 "invalid argument",
61 "argument conflict",
62 "invalid chanlist",
63 };
64
65 int maincxx . . $ main(afcd3168fc1188a4).(int argc, char *argv[])
66 {
67 comedi_tcxx . . $ comedi_t# *dev;
68 comedi_cmdcxx . . $ comedi_cmd# c,*cmd = &c;
69 int ret;
70 int total = 0;
71 int col;
72 int i;
73 int subdev_flags;
74 lsampl_tcxx . . $ lsampl_t# raw;
75
76 struct parsed_optionscxx . . $ parsed_options# options;
77
78 memsetcxx . . $ memset(d1de1e4fb215f1c6).(&options, 0, sizeof(options));
79 options.filenamecxx . . $ parsed_options#filename. = "/dev/comedi0";
80 options.subdevicecxx . . $ parsed_options#subdevice. = 0;
81 options.channelcxx . . $ parsed_options#channel. = 0;
82 options.rangecxx . . $ parsed_options#range. = 0;
83 options.arefcxx . . $ parsed_options#aref. = AREF_GROUNDcxx . . $ `include/comedi.h:96:9`!;
84 options.n_chancxx . . $ parsed_options#n_chan. = 2;
85 options.n_scancxx . . $ parsed_options#n_scan. = 10000;
86 options.freqcxx . . $ parsed_options#freq. = 1000.0;
87
88 /* open the device */
89 dev = comedi_opencxx . . $ comedi_open(d67864c6c007fe39).(options.filenamecxx . . $ parsed_options#filename.);
90 if (!dev) {
91 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).(options.filenamecxx . . $ parsed_options#filename.);
92 exitcxx . . $ exit(3ca292c338df9bb3).(1);
93 }
94
95 /* Print numbers for clipped inputs */
96 comedi_set_global_oor_behaviorcxx . . $ comedi_set_global_oor_behavior(711436fe91983c69).(COMEDI_OOR_NUMBERcxx . . $ COMEDI_OOR_NUMBER.);
97
98 /* Set up channel list */
99 for (i = 0; i < options.n_chancxx . . $ parsed_options#n_chan.; i++) {
100 chanlistcxx . . $ chanlist.[i] =
101 CR_PACKcxx . . $ `include/comedi.h:79:9`!(options.channel + i, options.range,
102 options.aref);
103 range_infocxx . . $ range_info.[i] =
104 comedi_get_rangecxx . . $ comedi_get_range(60f094977346e00a).(dev, options.subdevicecxx . . $ parsed_options#subdevice.,
105 options.channelcxx . . $ parsed_options#channel., options.rangecxx . . $ parsed_options#range.);
106 maxdatacxx . . $ maxdata.[i] =
107 comedi_get_maxdatacxx . . $ comedi_get_maxdata(9f5413f81e406f65).(dev, options.subdevicecxx . . $ parsed_options#subdevice.,
108 options.channelcxx . . $ parsed_options#channel.);
109 }
110
111 /* prepare_cmd_lib() uses a Comedilib routine to find a
112 * good command for the device. prepare_cmd() explicitly
113 * creates a command, which may not work for your device. */
114 prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(dev, options.subdevicecxx . . $ parsed_options#subdevice., options.n_scancxx . . $ parsed_options#n_scan.,
115 options.n_chancxx . . $ parsed_options#n_chan., 1e9 / options.freqcxx . . $ parsed_options#freq., cmd);
116
117 /* comedi_command_test() tests a command to see if the
118 * trigger sources and arguments are valid for the subdevice.
119 * If a trigger source is invalid, it will be logically ANDed
120 * with valid values (trigger sources are actually bitmasks),
121 * which may or may not result in a valid trigger source.
122 * If an argument is invalid, it will be adjusted to the
123 * nearest valid value. In this way, for many commands, you
124 * can test it multiple times until it passes. Typically,
125 * if you can't get a valid command in two tests, the original
126 * command wasn't specified very well. */
127 ret = comedi_command_testcxx . . $ comedi_command_test(43d1de8b01a26507).(dev, cmd);
128 if (ret < 0) {
129 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_command_test");
130 if(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == EIOcxx . . $ `<external>/8987895939074593727/errno-base.h:9:9`!){
131 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
132 "Ummm... this subdevice doesn't support commands\n");
133 }
134 exitcxx . . $ exit(3ca292c338df9bb3).(1);
135 }
136 ret = comedi_command_testcxx . . $ comedi_command_test(43d1de8b01a26507).(dev, cmd);
137 if (ret < 0) {
138 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_command_test");
139 exitcxx . . $ exit(3ca292c338df9bb3).(1);
140 }
141 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,"second test returned %d (%s)\n", ret,
142 cmdtest_messagescxx . . $ cmdtest_messages.[ret]);
143 if (ret != 0) {
144 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "Error preparing command\n");
145 exitcxx . . $ exit(3ca292c338df9bb3).(1);
146 }
147
148 /* comedi_set_read_subdevice() attempts to change the current
149 * 'read' subdevice to the specified subdevice if it is
150 * different. Changing the read or write subdevice might not be
151 * supported by the version of Comedi you are using. */
152 comedi_set_read_subdevicecxx . . $ comedi_set_read_subdevice(7ee088ff0d1b2810).(dev, cmd->subdevcxx . . $ comedi_cmd_struct#subdev.);
153 /* comedi_get_read_subdevice() gets the current 'read'
154 * subdevice. if any. This is the subdevice whose buffer the
155 * read() call will read from. Check that it is the one we want
156 * to use. */
157 ret = comedi_get_read_subdevicecxx . . $ comedi_get_read_subdevice(33bb234f098f8f92).(dev);
158 if (ret < 0 || ret != cmd->subdevcxx . . $ comedi_cmd_struct#subdev.) {
159 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
160 "failed to change 'read' subdevice from %d to %d\n",
161 ret, cmd->subdevcxx . . $ comedi_cmd_struct#subdev.);
162 exitcxx . . $ exit(3ca292c338df9bb3).(1);
163 }
164
165 /* start the command */
166 ret = comedi_commandcxx . . $ comedi_command(43d1de8b01a26507).(dev, cmd);
167 if (ret < 0) {
168 comedi_perrorcxx . . $ comedi_perror(85c52e162fed56f9).("comedi_command");
169 exitcxx . . $ exit(3ca292c338df9bb3).(1);
170 }
171 subdev_flags = comedi_get_subdevice_flagscxx . . $ comedi_get_subdevice_flags(7ee088ff0d1b2810).(dev, options.subdevicecxx . . $ parsed_options#subdevice.);
172 col = 0;
173 while (1) {
174 ret = readcxx . . $ read(7e34d0a36f355389).(comedi_filenocxx . . $ comedi_fileno(33bb234f098f8f92).(dev),bufcxx . . $ buf.,BUFSZcxx . . $ `demo/tut3.c:39:9`!);
175 if (ret < 0) {
176 /* some error occurred */
177 perrorcxx . . $ perror(85c52e162fed56f9).("read");
178 break;
179 } else if (ret == 0) {
180 /* reached stop condition */
181 break;
182 } else {
183 int bytes_per_sample;
184
185 total += ret;
186 if (options.verbosecxx . . $ parsed_options#verbose.) {
187 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, "read %d %d\n", ret,
188 total);
189 }
190 if (subdev_flags & SDF_LSAMPLcxx . . $ `include/comedi.h:222:9`!) {
191 bytes_per_sample = sizeof(lsampl_tcxx . . $ lsampl_t#);
192 } else {
193 bytes_per_sample = sizeof(sampl_tcxx . . $ sampl_t#);
194 }
195 for (i = 0; i < ret / bytes_per_sample; i++) {
196 if (subdev_flags & SDF_LSAMPLcxx . . $ `include/comedi.h:222:9`!) {
197 raw = ((lsampl_tcxx . . $ lsampl_t# *)bufcxx . . $ buf.)[i];
198 } else {
199 raw = ((sampl_tcxx . . $ sampl_t# *)bufcxx . . $ buf.)[i];
200 }
201 print_datumcxx . . $ print_datum(3d89109e85ced1be).(raw, col);
202 col++;
203 if (col == options.n_chancxx . . $ parsed_options#n_chan.) {
204 printfcxx . . $ printf(581fddd72a8db8fc).("\n");
205 col=0;
206 }
207 }
208 }
209 }
210
211 return 0;
212 }
213
214 /*
215 * This prepares a command in a pretty generic way. We ask the
216 * library to create a stock command that supports periodic
217 * sampling of data, then modify the parts we want.
218 */
219 int prepare_cmd_libcxx . . $ prepare_cmd_lib(dccf6e8bc23bbc03).(comedi_tcxx . . $ comedi_t# *dev, int subdevice, int n_scan,
220 int n_chan, unsigned scan_period_nanosec,
221 comedi_cmdcxx . . $ comedi_cmd# *cmd)
222 {
223 int ret;
224
225 memsetcxx . . $ memset(d1de1e4fb215f1c6).(cmd,0,sizeof(*cmd));
226
227 /* This comedilib function will get us a generic timed
228 * command for a particular board. If it returns -1,
229 * that's bad. */
230 ret = comedi_get_cmd_generic_timedcxx . . $ comedi_get_cmd_generic_timed(6f6d668026c3281f).(dev, subdevice, cmd, n_chan,
231 scan_period_nanosec);
232 if (ret < 0) {
233 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!,
234 "comedi_get_cmd_generic_timed failed\n");
235 return ret;
236 }
237
238 /* Modify parts of the command */
239 cmd->chanlistcxx . . $ comedi_cmd_struct#chanlist. = chanlistcxx . . $ chanlist.;
240 cmd->chanlist_lencxx . . $ comedi_cmd_struct#chanlist_len. = n_chan;
241 if (cmd->stop_srccxx . . $ comedi_cmd_struct#stop_src. == TRIG_COUNTcxx . . $ `include/comedi.h:186:9`!) {
242 cmd->stop_argcxx . . $ comedi_cmd_struct#stop_arg. = n_scan;
243 }
244
245 return 0;
246 }
247
248 void print_datumcxx . . $ print_datum(3d89109e85ced1be).(lsampl_tcxx . . $ lsampl_t# raw, int channel_index)
249 {
250 double physical_value;
251
252 physical_value = comedi_to_physcxx . . $ comedi_to_phys(28294eee26a5294e).(raw, range_infocxx . . $ range_info.[channel_index],
253 maxdatacxx . . $ maxdata.[channel_index]);
254 printfcxx . . $ printf(581fddd72a8db8fc).("%#8.6g ", physical_value);
255 }