comedi_board_info: improve display of external ranges.
[comedilib.git] / testing / cmd_3.c
1
2 #include <stdio.h>
3 #include <comedilib.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/ioctl.h>
7 #include <errno.h>
8 #include <getopt.h>
9 #include <ctype.h>
10 #include <math.h>
11 #include <sys/time.h>
12 #include <string.h>
13 #include "comedi_test.h"
14
15 static int do_continuous(int multiplier);
16
17 #define BUFSZ 10000
18
19 int test_cmd_continuous(void)
20 {
21         int mult;
22
23         if(!(comedi_get_subdevice_flags(device,subdevice)&SDF_CMD)){
24                 printf("not applicable\n");
25                 return 0;
26         }
27
28         /* as if doing _one_ infinite loop wasn't slow enough,
29          * we loop through with higher and higher multipliers,
30          * in case the test fails because of latency problems */
31
32         for(mult=1;mult<1024;mult*=2){
33                 do_continuous(mult);
34         }
35
36         return 0;
37 }
38
39 static int do_continuous(int multiplier)
40 {
41         comedi_cmd cmd;
42         char buf[BUFSZ];
43         unsigned int chanlist[1];
44         int go;
45         int total=0;
46         int ret;
47         int chunks=0;
48         unsigned long total_secs = 0;
49         struct timeval tv,start_tv;
50
51         if(comedi_get_cmd_generic_timed(device,subdevice, &cmd, 1, 1)<0){
52                 printf("  not supported\n");
53                 return 0;
54         }
55
56         if(realtime)cmd.flags |= TRIG_RT;
57
58         cmd.chanlist = chanlist;
59         cmd.scan_end_arg = 1;
60         cmd.stop_src = TRIG_NONE;
61         cmd.stop_arg = 0;
62         cmd.chanlist_len = 1;
63         chanlist[0] = CR_PACK(0,0,0);
64
65         // slow down a bit
66         cmd.scan_begin_arg *= multiplier;
67         printf("multiplier=%d, scan_begin_arg=%d\n",
68                 multiplier,
69                 cmd.scan_begin_arg);
70
71         ret=comedi_command(device,&cmd);
72         if(ret<0){
73                 perror("comedi_command");
74         }else{
75                 printf("ret==%d\n",ret);
76         }
77
78         gettimeofday(&start_tv,NULL);
79
80         go=1;
81         while(go){
82                 ret = read(comedi_fileno(device),buf,BUFSZ);
83                 if(ret<0){
84                         if(errno==EAGAIN){
85                                 usleep(10000);
86                         }else{
87                                 go = 0;
88                                 perror("read");
89                         }
90                 }else if(ret==0){
91                         go = 0;
92                 }else{
93                         total += ret;
94                         chunks++;
95
96                         gettimeofday(&tv,NULL);
97                         tv.tv_sec-=start_tv.tv_sec;
98                         tv.tv_usec-=start_tv.tv_usec;
99                         if(tv.tv_usec<0){
100                                 tv.tv_usec+=1000000;
101                                 tv.tv_sec--;
102                         }
103                         if(tv.tv_sec>total_secs){
104                                 double t;
105
106                                 t=tv.tv_sec+1e-6*tv.tv_usec;
107                                 printf("%0.3f %d (%g) %d (%g)\n",
108                                         t,
109                                         chunks,chunks/t,
110                                         total,total/t);
111                                 total_secs++;
112                         }
113                 }
114         }
115         {
116                 double t;
117
118                 t=tv.tv_sec+1e-6*tv.tv_usec;
119                 printf("end: %0.3f %d (%g) %d (%g)\n",
120                         t,
121                         chunks,chunks/t,
122                         total,total/t);
123         }
124
125         return 0;
126 }
127