insert usleep() in loop
[comedilib.git] / demo / receiver.c
1 /*
2  * Digital I/O example
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  * Requirements:  A board with a digital I/O subdevice.  Not just
13  *    a 'digital input' or 'digital output' subdevice, but one in
14  *    which the channels can be configured between input and output.
15  */
16
17 #define _GNU_SOURCE
18
19 #include <stdio.h>
20 #include <comedilib.h>
21 #include <fcntl.h>
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <ctype.h>
27 #include <signal.h>
28 #include <string.h>
29 #include <sys/time.h>
30 #include "examples.h"
31
32 int pin_clk = 2;
33 int pin_data = 10;
34
35 comedi_t *device;
36
37 #define BUFSZ 1024
38 sampl_t buf[BUFSZ];
39
40 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd);
41 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
42
43 int main(int argc, char *argv[])
44 {
45         char *fn = NULL;
46         comedi_t *dev;
47         comedi_cmd cmd;
48         int ret;
49
50         parse_options(argc,argv);
51
52         //fn = "/dev/comedi1";
53         fn = "/dev/comedi0";
54
55         dev = comedi_open(fn);
56         if(!dev){
57                 perror(fn);
58                 exit(1);
59         }
60         device = dev;
61
62         subdevice = 0;
63
64         if(channel)pin_data=channel;
65
66         ret = fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
67         if(ret<0)perror("fcntl");
68
69         prepare_cmd(dev,&cmd);
70
71         do_cmd(dev,&cmd);
72
73         return 0;
74 }
75
76 static int c=0;
77 static unsigned int bits =0;
78
79 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
80 {
81         unsigned int *chanlist;
82         int n_chans;
83         int total=0;
84         int ret;
85         int go;
86         struct timeval timeout;
87         fd_set rdset;
88
89         chanlist = cmd->chanlist;
90         n_chans = cmd->chanlist_len;
91
92         ret=comedi_command_test(dev,cmd);
93
94         //printf("test ret=%d\n",ret);
95         if(ret<0){
96                 printf("errno=%d\n",errno);
97                 comedi_perror("comedi_command_test");
98                 return;
99         }
100
101         dump_cmd(stdout,cmd);
102
103         cmd->chanlist =         chanlist;
104         cmd->chanlist_len =     n_chans;
105
106         ret=comedi_command_test(dev,cmd);
107
108         printf("test ret=%d\n",ret);
109         if(ret<0){
110                 printf("errno=%d\n",errno);
111                 comedi_perror("comedi_command_test");
112                 return;
113         }
114
115         dump_cmd(stdout,cmd);
116
117         cmd->chanlist =         chanlist;
118         cmd->chanlist_len =     n_chans;
119
120         ret=comedi_command(dev,cmd);
121
122         printf("ret=%d\n",ret);
123         if(ret<0){
124                 printf("errno=%d\n",errno);
125                 comedi_perror("comedi_command");
126                 return;
127         }
128
129         go=1;
130         while(go){
131                 FD_ZERO(&rdset);
132                 FD_SET(comedi_fileno(dev),&rdset);
133                 timeout.tv_sec=0;
134                 timeout.tv_usec=50000;
135                 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
136                 if(ret<0){
137                         perror("select");
138                 }else if(ret==0){
139                         if(c){
140                                 fprintf(stderr,"\n");
141                                 c=0;
142                                 bits=0;
143                         }
144                 }else if(FD_ISSET(comedi_fileno(dev),&rdset)){
145                         ret=read(comedi_fileno(dev),buf,BUFSZ);
146                         if(ret<0){
147                                 if(errno==EAGAIN){
148                                         go = 0;
149                                         perror("read");
150                                 }
151                         }else if(ret==0){
152                                 go = 0;
153                         }else{
154                                 int i;
155         
156                                 total+=ret;
157                                 for(i=0;i<ret/sizeof(sampl_t);i++){
158                                         fprintf(stderr,"%d",buf[i]>0xa000);
159                                         c++;
160                                         if(c>=32){
161                                                 fprintf(stderr,"\n");
162                                                 c=0;
163                                         }
164 #if 0
165                                         //printf("%d %d\n",buf[i],buf[i]>0xa000);
166                                         //printf("%d",buf[i]>0xa000);
167                                         bits<<=1;
168                                         bits|=(buf[i]>0xa000);
169                                         c++;
170                                         if(c>=33){
171 #if 0
172                                                 struct timeval now;
173
174                                                 gettimeofday(&now,NULL);
175                                                 printf(" %08x %ld.%06ld\n",bits,now.tv_sec,now.tv_usec);
176                                                 c=0;
177                                                 bits=0;
178 #else
179                                                 printf(" %08x\n",bits);
180                                                 c=0;
181                                                 bits=0;
182 #endif
183                                         }
184                                         if(!bits)c=0;
185 #endif
186                                 }
187                                 fflush(stdout);
188                                 fflush(stderr);
189                         }
190                 }
191         }
192 }
193
194 unsigned int chanlist[16];
195 /*
196  * This part of the demo measures channels 1, 2, 3, 4 at a rate of
197  * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
198  * of scans measured is 10.  This is analogous to the old mode2
199  * acquisition.
200  */
201 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd)
202 {
203         memset(cmd,0,sizeof(comedi_cmd));
204
205         /* the subdevice that the command is sent to */
206         cmd->subdev =           subdevice;
207
208         /* flags */
209         cmd->flags =            TRIG_WAKE_EOS;
210
211         cmd->start_src =        TRIG_NOW;
212         cmd->start_arg =        0;
213
214         cmd->scan_begin_src =   TRIG_EXT;
215         cmd->scan_begin_arg =   CR_EDGE | CR_INVERT | pin_clk;
216
217 #if 1
218         cmd->convert_src =      TRIG_TIMER;
219         cmd->convert_arg =      1;
220 #else
221         cmd->convert_src =      TRIG_ANY;
222         cmd->convert_arg =      0;
223 #endif
224
225         cmd->scan_end_src =     TRIG_COUNT;
226         cmd->scan_end_arg =     1;
227
228         cmd->stop_src =         TRIG_NONE;
229         cmd->stop_arg =         0;
230
231         cmd->chanlist =         chanlist;
232         cmd->chanlist_len =     1;
233
234         chanlist[0]=CR_PACK(pin_data,0,0);
235 }
236