Cleaned up mixing of spaces/tabs for indentation probably done by emacs.
[comedilib.git] / demo / poll.c
1 /*
2  * poll.c - Example of using comedi_poll() with Comedi
3  * Part of Comedilib
4  *
5  * Copyright (c) 1999,2000 David A. Schleef <ds@schleef.org>
6  *
7  * This file may be freely modified, distributed, and combined with
8  * other software, as long as proper attribution is given in the
9  * source code.
10  */
11
12 /*
13  * An example for using comedi_poll() in asynchronous input,
14  * so you can ask the driver to pull samples that may be waiting
15  * on the board into the buffer (so they can be read).
16  */
17
18 #include <stdio.h>
19 #include <comedilib.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <ctype.h>
26 #include <string.h>
27 #include <sys/time.h>
28 #include "examples.h"
29
30 #define N_SCANS         10
31 #define N_CHANS         16
32
33 #define BUFSZ 1000
34 sampl_t buf[BUFSZ];
35
36 const int n_chans = 1;
37 const int n_scans = 10;
38
39 unsigned int chanlist[4];
40
41 comedi_t *device;
42
43 void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice);
44 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
45
46 int main(int argc, char *argv[])
47 {
48         comedi_cmd cmd;
49         int i;
50         struct parsed_options options;
51
52         init_parsed_options(&options);
53         parse_options(&options, argc, argv);
54
55         device = comedi_open(options.filename);
56         if(!device){
57                 perror(options.filename);
58                 exit(1);
59         }
60
61         fcntl(comedi_fileno(device), F_SETFL, O_NONBLOCK);
62
63         for(i = 0; i < n_chans; i++){
64                 chanlist[i] = CR_PACK(options.channel + i, options.range, options.aref);
65         }
66
67         prepare_cmd(device, &cmd, options.subdevice);
68
69         do_cmd(device, &cmd);
70
71         return 0;
72 }
73
74 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
75 {
76         int total=0;
77         int ret;
78         int go;
79         fd_set rdset;
80         struct timeval timeout;
81
82         ret=comedi_command_test(dev,cmd);
83
84         printf("test ret=%d\n",ret);
85         if(ret<0){
86                 printf("errno=%d\n",errno);
87                 comedi_perror("comedi_command_test");
88                 return;
89         }
90
91         dump_cmd(stdout,cmd);
92
93         ret=comedi_command_test(dev,cmd);
94
95         printf("test ret=%d\n",ret);
96         if(ret<0){
97                 printf("errno=%d\n",errno);
98                 comedi_perror("comedi_command_test");
99                 return;
100         }
101
102         dump_cmd(stdout,cmd);
103
104         ret=comedi_command(dev,cmd);
105
106         printf("ret=%d\n",ret);
107         if(ret<0){
108                 printf("errno=%d\n",errno);
109                 comedi_perror("comedi_command");
110                 return;
111         }
112
113         go=1;
114         while(go){
115                 FD_ZERO(&rdset);
116                 FD_SET(comedi_fileno(device),&rdset);
117                 timeout.tv_sec = 0;
118                 timeout.tv_usec = 50000;
119                 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
120                 //printf("select returned %d\n",ret);
121                 if(ret<0){
122                         perror("select");
123                 }else if(ret==0){
124                         /* hit timeout */
125                         printf("timeout, polling...\n");
126                         ret = comedi_poll(device,0);
127                         printf("poll returned %d\n",ret);
128                 }else if(FD_ISSET(comedi_fileno(device),&rdset)){
129                         /* comedi file descriptor became ready */
130                         //printf("comedi file descriptor ready\n");
131                         ret=read(comedi_fileno(dev),buf,sizeof(buf));
132                         printf("read returned %d\n",ret);
133                         if(ret<0){
134                                 if(errno==EAGAIN){
135                                         go = 0;
136                                         perror("read");
137                                 }
138                         }else if(ret==0){
139                                 go = 0;
140                         }else{
141                                 int i;
142                                 total+=ret;
143                                 //printf("read %d %d\n",ret,total);
144                                 for(i=0;i<ret/sizeof(sampl_t);i++){
145                                         printf("%d\n",buf[i]);
146                                 }
147                         }
148                 }else{
149                         /* unknown file descriptor became ready */
150                         printf("unknown file descriptor ready\n");
151                 }
152         }
153 }
154
155 /*
156  * This part of the demo measures channels 1, 2, 3, 4 at a rate of
157  * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
158  * of scans measured is 10.  This is analogous to the old mode2
159  * acquisition.
160  */
161 void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice)
162 {
163         memset(cmd,0,sizeof(*cmd));
164
165         /* the subdevice that the command is sent to */
166         cmd->subdev =   subdevice;
167
168         /* flags */
169         cmd->flags =    0;
170
171         /* each event requires a trigger, which is specified
172            by a source and an argument.  For example, to specify
173            an external digital line 3 as a source, you would use
174            src=TRIG_EXT and arg=3. */
175
176         /* In this case, we specify using TRIG_NOW to start
177          * acquisition immediately when the command is issued.
178          * The argument of TRIG_NOW is "number of nsec after
179          * NOW", but no driver supports it yet.  Also, no driver
180          * currently supports using a start_src other than
181          * TRIG_NOW.  */
182         cmd->start_src =                TRIG_NOW;
183         cmd->start_arg =                0;
184
185         /* The timing of the beginning of each scan is controlled
186          * by scan_begin.  TRIG_TIMER specifies that scan_start
187          * events occur periodically at a rate of scan_begin_arg
188          * nanoseconds between scans. */
189         cmd->scan_begin_src =   TRIG_TIMER;
190         cmd->scan_begin_arg =   msec_to_nsec(100);
191
192         /* The timing between each sample in a scan is controlled
193          * by convert.  Like above, TRIG_TIMER specifies that
194          * convert events occur periodically at a rate of convert_arg
195          * nanoseconds between scans. */
196         cmd->convert_src =      TRIG_TIMER;
197         cmd->convert_arg =      msec_to_nsec(1);
198
199         /* The end of each scan is almost always specified using
200          * TRIG_COUNT, with the argument being the same as the
201          * number of channels in the chanlist.  You could probably
202          * find a device that allows something else, but it would
203          * be strange. */
204         cmd->scan_end_src =     TRIG_COUNT;
205         cmd->scan_end_arg =     n_chans;        /* number of channels */
206
207         /* The end of acquisition is controlled by stop_src and
208          * stop_arg.  The src will typically be TRIG_COUNT or
209          * TRIG_NONE.  Specifying TRIG_COUNT will stop acquisition
210          * after stop_arg number of scans, or TRIG_NONE will
211          * cause acquisition to continue until stopped using
212          * comedi_cancel(). */
213         cmd->stop_src =         TRIG_COUNT;
214         cmd->stop_arg =         n_scans;
215
216         /* the channel list determined which channels are sampled.
217            In general, chanlist_len is the same as scan_end_arg.  Most
218            boards require this.  */
219         cmd->chanlist =         chanlist;
220         cmd->chanlist_len =     n_chans;
221 }
222