Removed usbdux_firmware.lst because it's just the listing of the assembler which...
[comedilib.git] / demo / ledclock.c
1 /*
2  * LED Clock demo
3  * Part of Comedilib
4  *
5  * Copyright (c) 2001 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  * Requirements:  
14  *    - A board with a digital output subdevice and a subdevice that
15  *      can trigger on an external digital line.  A parallel port
16  *      satisfies these requirements.
17  *    - A Fantazein LED Clock modified so that the individual LEDs
18  *      can be controlled directly by the digital I/O lines.
19  *
20  * The Fantazein clock has 8 LEDs arranged in a row on a wand that
21  * sweeps back and forth at about 15 Hz.  Unmodified, the firmware
22  * of the clock lights the LEDs at the appropriate time to print
23  * words and the time of day.  Since the wand moves quickly, it is
24  * barely visible, so it looks like the image floats in the air.
25  * Stuart Hughes modified a clock so that the LEDs could be controlled
26  * directly by the parallel port of a computer, and wrote the
27  * appropriate software using RTAI to create a stable image.  This
28  * is an attempt to port the demo to Comedi.
29  *
30  * It needs much work.
31  */
32
33 #define _GNU_SOURCE
34
35 #include <stdio.h>
36 #include <comedilib.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <getopt.h>
42 #include <ctype.h>
43 #include <signal.h>
44 #include <string.h>
45 #include <sys/time.h>
46 #include "examples.h"
47
48
49 comedi_t *device;
50
51 int count;
52
53 int out_subd;
54
55 #define BUFSZ 1024
56 sampl_t buf[BUFSZ];
57
58 unsigned int chanlist[16];
59
60
61 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd);
62 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
63 void do_toggle(void);
64
65
66 void config_output(void)
67 {
68         int i;
69
70         for(i=0;i<8;i++){
71                 comedi_dio_config(device,out_subd,i,COMEDI_OUTPUT);
72         }
73 }
74
75 void do_toggle(void)
76 {
77 #if 1
78         comedi_insnlist il;
79         comedi_insn insn[3];
80         lsampl_t data[6];
81         int mask = 0xff;
82
83         count++;
84
85         il.n_insns = 3;
86         il.insns = insn;
87
88         memset(insn,0,3*sizeof(comedi_insn));
89
90         insn[0].insn = INSN_BITS;
91         insn[0].n = 2;
92         insn[0].data = data+0;
93         insn[0].subdev = out_subd;
94
95         data[0] = mask;
96         //data[1] = count;
97         data[1] = 0xfc;
98
99         insn[1].insn = INSN_WAIT;
100         insn[1].n = 1;
101         insn[1].data = data+2;
102
103         data[2] = 100000-1;
104
105         insn[2].insn = INSN_BITS;
106         insn[2].n = 2;
107         insn[2].data = data+4;
108         insn[2].subdev = out_subd;
109
110         data[4] = mask;
111         //data[5] = count;
112         data[5] = 0xff;
113
114         comedi_do_insnlist(device,&il);
115 #else
116         unsigned int data;
117         unsigned int mask = 0xff;
118
119         count++;
120         data = count;
121
122         comedi_dio_bitfield(device,out_subd,mask,&data);
123 #endif
124 }
125
126 int main(int argc, char *argv[])
127 {
128         char *fn = NULL;
129         int ret;
130         comedi_cmd cmd;
131
132         fn = "/dev/comedi1";
133
134         device = comedi_open(fn);
135         if(!device){
136                 perror(fn);
137                 exit(1);
138         }
139
140         subdevice = 3;
141         out_subd = 0;
142
143         config_output();
144
145         ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
146         if(ret<0)perror("fcntl");
147
148 #if 0
149         {
150         struct sched_param p;
151
152         memset(&p,0,sizeof(p));
153         p.sched_priority = 1;
154         ret = sched_setscheduler(0,SCHED_FIFO,&p);
155         if(ret<0)perror("sched_setscheduler");
156         }
157 #endif
158
159         prepare_cmd(device,&cmd);
160
161         do_cmd(device,&cmd);
162
163         return 0;
164 }
165
166 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
167 {
168         int total=0;
169         int ret;
170         int go;
171         fd_set rdset;
172         struct timeval timeout;
173
174         ret=comedi_command_test(dev,cmd);
175
176         printf("test ret=%d\n",ret);
177         if(ret<0){
178                 printf("errno=%d\n",errno);
179                 comedi_perror("comedi_command_test");
180                 return;
181         }
182
183         dump_cmd(stdout,cmd);
184
185         ret=comedi_command_test(dev,cmd);
186
187         printf("test ret=%d\n",ret);
188         if(ret<0){
189                 printf("errno=%d\n",errno);
190                 comedi_perror("comedi_command_test");
191                 return;
192         }
193
194         dump_cmd(stdout,cmd);
195
196         ret=comedi_command(dev,cmd);
197
198         printf("ret=%d\n",ret);
199         if(ret<0){
200                 printf("errno=%d\n",errno);
201                 comedi_perror("comedi_command");
202                 return;
203         }
204
205         go=1;
206         while(go){
207                 FD_ZERO(&rdset);
208                 FD_SET(comedi_fileno(dev),&rdset);
209                 timeout.tv_sec = 0;
210                 timeout.tv_usec = 50000;
211                 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
212                 if(ret<0){
213                         perror("select");
214                 }else if(ret==0){
215                         /* timeout */
216                 }else if(FD_ISSET(comedi_fileno(dev),&rdset)){
217                         ret=read(comedi_fileno(dev),buf,BUFSZ);
218                         if(ret<0){
219                                 if(errno==EAGAIN){
220                                         go = 0;
221                                         perror("read");
222                                 }
223                         }else if(ret==0){
224                                 go = 0;
225                         }else{
226                                 //int i;
227         
228                                 total+=ret;
229                                 //printf("read %d %d\n",ret,total);
230                                 //printf("count = %d\n",count);
231                                 do_toggle();
232 #if 0
233                                 for(i=0;i<ret;i+=sizeof(sampl_t)){
234                                         do_toggle();
235                                 }
236 #endif
237                         }
238                 }
239         }
240 }
241
242 /*
243  * This part of the demo measures channels 1, 2, 3, 4 at a rate of
244  * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
245  * of scans measured is 10.  This is analogous to the old mode2
246  * acquisition.
247  */
248 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd)
249 {
250         memset(cmd,0,sizeof(*cmd));
251
252         /* the subdevice that the command is sent to */
253         cmd->subdev =           subdevice;
254
255         /* flags */
256         cmd->flags =            TRIG_WAKE_EOS;
257
258         cmd->start_src =        TRIG_NOW;
259         cmd->start_arg =        0;
260
261         cmd->scan_begin_src =   TRIG_EXT;
262         cmd->scan_begin_arg =   0;
263
264 #if 0
265         cmd->convert_src =      TRIG_TIMER;
266         cmd->convert_arg =      1;
267 #else
268         cmd->convert_src =      TRIG_ANY;
269         cmd->convert_arg =      0;
270 #endif
271
272         cmd->scan_end_src =     TRIG_COUNT;
273         cmd->scan_end_arg =     1;
274
275         cmd->stop_src =         TRIG_NONE;
276         cmd->stop_arg =         0;
277
278         cmd->chanlist =         chanlist;
279         cmd->chanlist_len =     1;
280
281         chanlist[0]=CR_PACK(0,0,0);
282 }
283