more features
authorDavid Schleef <ds@schleef.org>
Wed, 9 Aug 2000 21:08:17 +0000 (21:08 +0000)
committerDavid Schleef <ds@schleef.org>
Wed, 9 Aug 2000 21:08:17 +0000 (21:08 +0000)
demo/cmd.c

index 7cb85e12d576e554b8460b831fc07146de0221bc..d007269b24e0e6b1a99eecd61763ad6809689a5e 100644 (file)
@@ -21,11 +21,13 @@ int range = 0;
 int aref = AREF_GROUND;
 double freq = 1000;
 
-#define BUFSZ 100
+#define BUFSZ 1000
 char buf[BUFSZ];
 
 
-static void do_cmd(comedi_t *dev);
+static void do_cmd_1(comedi_t *dev);
+static void do_cmd_2(comedi_t *dev);
+static void do_cmd(comedi_t *dev,comedi_cmd *cmd);
 void dump_cmd(comedi_cmd *cmd);
 
 int main(int argc, char *argv[])
@@ -37,16 +39,88 @@ int main(int argc, char *argv[])
 
        dev = comedi_open(fn);
 
-       do_cmd(dev);
+       //fcntl(comedi_fileno(dev),F_SETFL,O_NONBLOCK);
+
+       do_cmd_2(dev);
 
        return 0;
 }
 
-static void do_cmd(comedi_t *dev)
+static void do_cmd(comedi_t *dev,comedi_cmd *cmd)
+{
+       unsigned int *chanlist;
+       int n_chans;
+       int total=0;
+       int ret;
+       int go;
+
+       chanlist = cmd->chanlist;
+       n_chans = cmd->chanlist_len;
+
+       ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,cmd);
+
+       printf("test ret=%d\n",ret);
+       if(ret<0){
+               printf("errno=%d\n",errno);
+               perror("ioctl");
+               return;
+       }
+
+       dump_cmd(cmd);
+
+       cmd->chanlist =         chanlist;
+       cmd->chanlist_len =     n_chans;
+
+       ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,cmd);
+
+       printf("test ret=%d\n",ret);
+       if(ret<0){
+               printf("errno=%d\n",errno);
+               comedi_perror("ioctl");
+               return;
+       }
+
+       dump_cmd(cmd);
+
+       cmd->chanlist =         chanlist;
+       cmd->chanlist_len =     n_chans;
+
+       ret=ioctl(comedi_fileno(dev),COMEDI_CMD,cmd);
+
+       printf("ret=%d\n",ret);
+       if(ret<0){
+               printf("errno=%d\n",errno);
+               comedi_perror("ioctl");
+               return;
+       }
+
+       go=1;
+       while(go){
+               ret=read(comedi_fileno(dev),buf,BUFSZ);
+               if(ret<0){
+                       if(errno==EAGAIN){
+                               usleep(10000);
+                       }else{
+                               go = 0;
+                       }
+               }else{
+                       total+=ret;
+               }
+               printf("read %d %d\n",ret,total);
+       }
+       perror("ack");
+       printf("errno=%d\n",errno);
+}
+
+static void do_cmd_1(comedi_t *dev)
 {
        comedi_cmd cmd;
        unsigned int chanlist[4];
+       int total=0;
        int ret;
+       int go;
+
+       memset(&cmd,0,sizeof(cmd));
 
        /* the subdevice that the command is sent to */
        cmd.subdev =    subdevice;
@@ -62,17 +136,22 @@ static void do_cmd(comedi_t *dev)
        cmd.start_src =         TRIG_NOW;
        cmd.start_arg =         0;
 
-       cmd.scan_begin_src =    TRIG_FOLLOW;
-       cmd.scan_begin_arg =    1;      /* in ns */
+       cmd.scan_begin_src =    TRIG_TIMER;
+       cmd.scan_begin_arg =    1000000;        /* in ns */
 
        cmd.convert_src =       TRIG_TIMER;
-       cmd.convert_arg =       1;              /* in ns */
+       cmd.convert_arg =       100000;         /* in ns */
 
        cmd.scan_end_src =      TRIG_COUNT;
        cmd.scan_end_arg =      4;              /* number of channels */
 
+#if 1
        cmd.stop_src =          TRIG_COUNT;
-       cmd.stop_arg =          1000;
+       cmd.stop_arg =          100;
+#else
+       cmd.stop_src =          TRIG_NONE;
+       cmd.stop_arg =          0;
+#endif
 
        /* the channel list determined which channels are sampled.
           In general, chanlist_len is the same as scan_end_arg.  Most
@@ -85,100 +164,109 @@ static void do_cmd(comedi_t *dev)
        chanlist[2]=CR_PACK(2,range,aref);
        chanlist[3]=CR_PACK(3,range,aref);
 
-       /* data and data_len are not used for user-space
-          programs */
-       cmd.data = NULL;
-       cmd.data_len = 0;
+       do_cmd(dev,&cmd);
+}
 
-       ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,&cmd);
+static void do_cmd_2(comedi_t *dev)
+{
+       comedi_cmd cmd;
+       unsigned int chanlist[4];
+       int total=0;
+       int ret;
+       int go;
 
-       printf("test ret=%d\n",ret);
-       if(ret<0){
-               printf("errno=%d\n",errno);
-               comedi_perror("ioctl");
-               return;
-       }
+       memset(&cmd,0,sizeof(cmd));
 
-       dump_cmd(&cmd);
+       /* the subdevice that the command is sent to */
+       cmd.subdev =    subdevice;
 
-       cmd.chanlist =          chanlist;
-       cmd.chanlist_len =      4;
+       /* flags */
+       cmd.flags =     0;
+
+       /* each event requires a trigger, which is specified
+          by a source and an argument.  For example, to specify
+          an external digital line 3 as a source, you would use
+          src=TRIG_EXT and arg=3. */
 
-       ret=ioctl(comedi_fileno(dev),COMEDI_CMDTEST,&cmd);
+       cmd.start_src =         TRIG_NOW;
+       cmd.start_arg =         0;
 
-       printf("test ret=%d\n",ret);
-       if(ret<0){
-               printf("errno=%d\n",errno);
-               comedi_perror("ioctl");
-               return;
-       }
+       cmd.scan_begin_src =    TRIG_TIMER;
+       cmd.scan_begin_arg =    1;      /* in ns */
+
+       cmd.convert_src =       TRIG_TIMER;
+       cmd.convert_arg =       1;              /* in ns */
+
+       cmd.scan_end_src =      TRIG_COUNT;
+       cmd.scan_end_arg =      4;              /* number of channels */
 
-       dump_cmd(&cmd);
+#if 1
+       cmd.stop_src =          TRIG_COUNT;
+       cmd.stop_arg =          100;
+#else
+       cmd.stop_src =          TRIG_NONE;
+       cmd.stop_arg =          0;
+#endif
 
+       /* the channel list determined which channels are sampled.
+          In general, chanlist_len is the same as scan_end_arg.  Most
+          boards require this.  */
        cmd.chanlist =          chanlist;
        cmd.chanlist_len =      4;
 
-       ret=ioctl(comedi_fileno(dev),COMEDI_CMD,&cmd);
-
-       printf("ret=%d\n",ret);
-       if(ret<0){
-               printf("errno=%d\n",errno);
-               comedi_perror("ioctl");
-               return;
-       }
+       chanlist[0]=CR_PACK(0,range,aref);
+       chanlist[1]=CR_PACK(1,range,aref);
+       chanlist[2]=CR_PACK(2,range,aref);
+       chanlist[3]=CR_PACK(3,range,aref);
 
-       do{
-               ret=read(comedi_fileno(dev),buf,BUFSZ);
-               printf("read %d\n",ret);
-       }while(ret>0);
-       printf("errno=%d\n",errno);
+       do_cmd(dev,&cmd);
 }
 
-char *cmd_src(int src)
+char *cmd_src(int src,char *buf)
 {
-       switch(src){
-       case TRIG_NONE:
-               return "none";
-       case TRIG_NOW:
-               return "now";
-       case TRIG_FOLLOW:
-               return "follow";
-       case TRIG_TIME:
-               return "time";
-       case TRIG_TIMER:
-               return "timer";
-       case TRIG_COUNT:
-               return "count";
-       case TRIG_EXT:
-               return "ext";
-       case TRIG_INT:
-               return "int";
-       default:
-               return "unknown";
+       buf[0]=0;
+
+       if(src&TRIG_NONE)strcat(buf,"none|");
+       if(src&TRIG_NOW)strcat(buf,"now|");
+       if(src&TRIG_FOLLOW)strcat(buf, "follow|");
+       if(src&TRIG_TIME)strcat(buf, "time|");
+       if(src&TRIG_TIMER)strcat(buf, "timer|");
+       if(src&TRIG_COUNT)strcat(buf, "count|");
+       if(src&TRIG_EXT)strcat(buf, "ext|");
+       if(src&TRIG_INT)strcat(buf, "int|");
+
+       if(strlen(buf)==0){
+               sprintf(buf,"unknown(0x%02x)",src);
+       }else{
+               buf[strlen(buf)-1]=0;
        }
+
+       return buf;
 }
 
 
 void dump_cmd(comedi_cmd *cmd)
 {
+       char buf[100];
+
        printf("start: %s %d\n",
-               cmd_src(cmd->start_src),
+               cmd_src(cmd->start_src,buf),
                cmd->start_arg);
 
        printf("scan_begin: %s %d\n",
-               cmd_src(cmd->scan_begin_src),
+               cmd_src(cmd->scan_begin_src,buf),
                cmd->scan_begin_arg);
 
        printf("convert: %s %d\n",
-               cmd_src(cmd->convert_src),
+               cmd_src(cmd->convert_src,buf),
                cmd->convert_arg);
 
        printf("scan_end: %s %d\n",
-               cmd_src(cmd->scan_end_src),
+               cmd_src(cmd->scan_end_src,buf),
                cmd->scan_end_arg);
 
        printf("stop: %s %d\n",
-               cmd_src(cmd->stop_src),
+               cmd_src(cmd->stop_src,buf),
                cmd->stop_arg);
 }