RTXI 1.3
comedilib/demo/ledclock.c
Go to the documentation of this file.
00001 /*
00002  * LED Clock demo
00003  * Part of Comedilib
00004  *
00005  * Copyright (c) 2001 David A. Schleef <ds@schleef.org>
00006  *
00007  * This file may be freely modified, distributed, and combined with
00008  * other software, as long as proper attribution is given in the
00009  * source code.
00010  */
00011 
00012 /*
00013  * Requirements:
00014  *    - A board with a digital output subdevice and a subdevice that
00015  *      can trigger on an external digital line.  A parallel port
00016  *      satisfies these requirements.
00017  *    - A Fantazein LED Clock modified so that the individual LEDs
00018  *      can be controlled directly by the digital I/O lines.
00019  *
00020  * The Fantazein clock has 8 LEDs arranged in a row on a wand that
00021  * sweeps back and forth at about 15 Hz.  Unmodified, the firmware
00022  * of the clock lights the LEDs at the appropriate time to print
00023  * words and the time of day.  Since the wand moves quickly, it is
00024  * barely visible, so it looks like the image floats in the air.
00025  * Stuart Hughes modified a clock so that the LEDs could be controlled
00026  * directly by the parallel port of a computer, and wrote the
00027  * appropriate software using RTAI to create a stable image.  This
00028  * is an attempt to port the demo to Comedi.
00029  *
00030  * It needs much work.
00031  */
00032 
00033 #define _GNU_SOURCE
00034 
00035 #include <stdio.h>
00036 #include <comedilib.h>
00037 #include <fcntl.h>
00038 #include <unistd.h>
00039 #include <stdlib.h>
00040 #include <errno.h>
00041 #include <getopt.h>
00042 #include <ctype.h>
00043 #include <signal.h>
00044 #include <string.h>
00045 #include <sys/time.h>
00046 #include "examples.h"
00047 
00048 
00049 comedi_t *device;
00050 
00051 int count;
00052 
00053 int out_subd;
00054 
00055 #define BUFSZ 1024
00056 sampl_t buf[BUFSZ];
00057 
00058 unsigned int chanlist[16];
00059 
00060 
00061 void prepare_cmd(comedi_t *dev,comedi_cmd *cmd, int subdevice);
00062 void do_cmd(comedi_t *dev,comedi_cmd *cmd);
00063 void do_toggle(void);
00064 
00065 
00066 void config_output(void)
00067 {
00068         int i;
00069 
00070         for(i=0;i<8;i++){
00071                 comedi_dio_config(device,out_subd,i,COMEDI_OUTPUT);
00072         }
00073 }
00074 
00075 void do_toggle(void)
00076 {
00077 #if 1
00078         comedi_insnlist il;
00079         comedi_insn insn[3];
00080         lsampl_t data[6];
00081         int mask = 0xff;
00082 
00083         count++;
00084 
00085         il.n_insns = 3;
00086         il.insns = insn;
00087 
00088         memset(insn,0,3*sizeof(comedi_insn));
00089 
00090         insn[0].insn = INSN_BITS;
00091         insn[0].n = 2;
00092         insn[0].data = data+0;
00093         insn[0].subdev = out_subd;
00094 
00095         data[0] = mask;
00096         //data[1] = count;
00097         data[1] = 0xfc;
00098 
00099         insn[1].insn = INSN_WAIT;
00100         insn[1].n = 1;
00101         insn[1].data = data+2;
00102 
00103         data[2] = 100000-1;
00104 
00105         insn[2].insn = INSN_BITS;
00106         insn[2].n = 2;
00107         insn[2].data = data+4;
00108         insn[2].subdev = out_subd;
00109 
00110         data[4] = mask;
00111         //data[5] = count;
00112         data[5] = 0xff;
00113 
00114         comedi_do_insnlist(device,&il);
00115 #else
00116         unsigned int data;
00117         unsigned int mask = 0xff;
00118 
00119         count++;
00120         data = count;
00121 
00122         comedi_dio_bitfield(device,out_subd,mask,&data);
00123 #endif
00124 }
00125 
00126 int main(int argc, char *argv[])
00127 {
00128         int ret;
00129         comedi_cmd cmd;
00130         struct parsed_options options;
00131 
00132         init_parsed_options(&options);
00133         parse_options(&options, argc, argv);
00134 
00135         device = comedi_open(options.filename);
00136         if(!device){
00137                 perror(options.filename);
00138                 exit(1);
00139         }
00140 
00141         out_subd = 0;
00142 
00143         config_output();
00144 
00145         ret = fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK|O_ASYNC);
00146         if(ret<0)perror("fcntl");
00147 
00148 #if 0
00149         {
00150         struct sched_param p;
00151 
00152         memset(&p,0,sizeof(p));
00153         p.sched_priority = 1;
00154         ret = sched_setscheduler(0,SCHED_FIFO,&p);
00155         if(ret<0)perror("sched_setscheduler");
00156         }
00157 #endif
00158 
00159         prepare_cmd(device, &cmd, options.subdevice);
00160 
00161         do_cmd(device,&cmd);
00162 
00163         return 0;
00164 }
00165 
00166 void do_cmd(comedi_t *dev,comedi_cmd *cmd)
00167 {
00168         int total=0;
00169         int ret;
00170         int go;
00171         fd_set rdset;
00172         struct timeval timeout;
00173 
00174         ret=comedi_command_test(dev,cmd);
00175 
00176         printf("test ret=%d\n",ret);
00177         if(ret<0){
00178                 printf("errno=%d\n",errno);
00179                 comedi_perror("comedi_command_test");
00180                 return;
00181         }
00182 
00183         dump_cmd(stdout,cmd);
00184 
00185         ret=comedi_command_test(dev,cmd);
00186 
00187         printf("test ret=%d\n",ret);
00188         if(ret<0){
00189                 printf("errno=%d\n",errno);
00190                 comedi_perror("comedi_command_test");
00191                 return;
00192         }
00193 
00194         dump_cmd(stdout,cmd);
00195 
00196         ret=comedi_command(dev,cmd);
00197 
00198         printf("ret=%d\n",ret);
00199         if(ret<0){
00200                 printf("errno=%d\n",errno);
00201                 comedi_perror("comedi_command");
00202                 return;
00203         }
00204 
00205         go=1;
00206         while(go){
00207                 FD_ZERO(&rdset);
00208                 FD_SET(comedi_fileno(dev),&rdset);
00209                 timeout.tv_sec = 0;
00210                 timeout.tv_usec = 50000;
00211                 ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
00212                 if(ret<0){
00213                         perror("select");
00214                 }else if(ret==0){
00215                         /* timeout */
00216                 }else if(FD_ISSET(comedi_fileno(dev),&rdset)){
00217                         ret=read(comedi_fileno(dev),buf,BUFSZ);
00218                         if(ret<0){
00219                                 if(errno==EAGAIN){
00220                                         go = 0;
00221                                         perror("read");
00222                                 }
00223                         }else if(ret==0){
00224                                 go = 0;
00225                         }else{
00226                                 //int i;
00227 
00228                                 total+=ret;
00229                                 //printf("read %d %d\n",ret,total);
00230                                 //printf("count = %d\n",count);
00231                                 do_toggle();
00232 #if 0
00233                                 for(i=0;i<ret;i+=sizeof(sampl_t)){
00234                                         do_toggle();
00235                                 }
00236 #endif
00237                         }
00238                 }
00239         }
00240 }
00241 
00242 /*
00243  * This part of the demo measures channels 1, 2, 3, 4 at a rate of
00244  * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
00245  * of scans measured is 10.  This is analogous to the old mode2
00246  * acquisition.
00247  */
00248 void prepare_cmd(comedi_t *dev, comedi_cmd *cmd, int subdevice)
00249 {
00250         memset(cmd,0,sizeof(*cmd));
00251 
00252         /* the subdevice that the command is sent to */
00253         cmd->subdev = subdevice;
00254 
00255         /* flags */
00256         cmd->flags =            TRIG_WAKE_EOS;
00257 
00258         cmd->start_src =        TRIG_NOW;
00259         cmd->start_arg =        0;
00260 
00261         cmd->scan_begin_src =   TRIG_EXT;
00262         cmd->scan_begin_arg =   0;
00263 
00264 #if 0
00265         cmd->convert_src =      TRIG_TIMER;
00266         cmd->convert_arg =      1;
00267 #else
00268         cmd->convert_src =      TRIG_ANY;
00269         cmd->convert_arg =      0;
00270 #endif
00271 
00272         cmd->scan_end_src =     TRIG_COUNT;
00273         cmd->scan_end_arg =     1;
00274 
00275         cmd->stop_src =         TRIG_NONE;
00276         cmd->stop_arg =         0;
00277 
00278         cmd->chanlist =         chanlist;
00279         cmd->chanlist_len =     1;
00280 
00281         chanlist[0]=CR_PACK(0,0,0);
00282 }
00283 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines