~ubuntu-branches/ubuntu/intrepid/comedilib/intrepid

« back to all changes in this revision

Viewing changes to demo/poll.c

  • Committer: Bazaar Package Importer
  • Author(s): David Schleef
  • Date: 2003-09-23 18:11:12 UTC
  • Revision ID: james.westby@ubuntu.com-20030923181112-sat05jyh702rb1at
Tags: upstream-0.7.21
ImportĀ upstreamĀ versionĀ 0.7.21

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * poll.c - Example of using comedi_poll() with Comedi
 
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
/*
 
13
 * An example for using comedi_poll() in asynchronous input,
 
14
 * so you can ask the driver to pull samples that may be waiting
 
15
 * on the board into the buffer (so they can be read).
 
16
 */
 
17
 
 
18
#include <stdio.h>
 
19
#include <comedilib.h>
 
20
#include <fcntl.h>
 
21
#include <unistd.h>
 
22
#include <stdlib.h>
 
23
#include <errno.h>
 
24
#include <getopt.h>
 
25
#include <ctype.h>
 
26
#include <string.h>
 
27
#include <sys/time.h>
 
28
#include "examples.h"
 
29
 
 
30
#define N_SCANS         10
 
31
#define N_CHANS         16
 
32
 
 
33
#define BUFSZ 1000
 
34
sampl_t buf[BUFSZ];
 
35
 
 
36
int n_chans = 1;
 
37
int n_scans = 10;
 
38
 
 
39
unsigned int chanlist[4];
 
40
 
 
41
comedi_t *device;
 
42
 
 
43
void prepare_cmd(comedi_t *dev,comedi_cmd *cmd);
 
44
void do_cmd(comedi_t *dev,comedi_cmd *cmd);
 
45
 
 
46
#define sec_to_nsec(x) ((x)*1000000000)
 
47
#define sec_to_usec(x) ((x)*1000000)
 
48
#define sec_to_msec(x) ((x)*1000)
 
49
#define msec_to_nsec(x) ((x)*1000000)
 
50
#define msec_to_usec(x) ((x)*1000)
 
51
#define usec_to_nsec(x) ((x)*1000)
 
52
 
 
53
int main(int argc, char *argv[])
 
54
{
 
55
        comedi_cmd cmd;
 
56
        int i;
 
57
 
 
58
        parse_options(argc,argv);
 
59
 
 
60
        device = comedi_open(filename);
 
61
        if(!device){
 
62
                perror(filename);
 
63
                exit(1);
 
64
        }
 
65
 
 
66
        fcntl(comedi_fileno(device),F_SETFL,O_NONBLOCK);
 
67
 
 
68
        for(i=0;i<n_chans;i++){
 
69
                chanlist[i]=CR_PACK(channel+i,range,aref);
 
70
        }
 
71
 
 
72
        prepare_cmd(device,&cmd);
 
73
 
 
74
        do_cmd(device,&cmd);
 
75
 
 
76
        return 0;
 
77
}
 
78
 
 
79
void do_cmd(comedi_t *dev,comedi_cmd *cmd)
 
80
{
 
81
        int total=0;
 
82
        int ret;
 
83
        int go;
 
84
        fd_set rdset;
 
85
        struct timeval timeout;
 
86
 
 
87
        ret=comedi_command_test(dev,cmd);
 
88
 
 
89
        printf("test ret=%d\n",ret);
 
90
        if(ret<0){
 
91
                printf("errno=%d\n",errno);
 
92
                comedi_perror("comedi_command_test");
 
93
                return;
 
94
        }
 
95
 
 
96
        dump_cmd(stdout,cmd);
 
97
 
 
98
        ret=comedi_command_test(dev,cmd);
 
99
 
 
100
        printf("test ret=%d\n",ret);
 
101
        if(ret<0){
 
102
                printf("errno=%d\n",errno);
 
103
                comedi_perror("comedi_command_test");
 
104
                return;
 
105
        }
 
106
 
 
107
        dump_cmd(stdout,cmd);
 
108
 
 
109
        ret=comedi_command(dev,cmd);
 
110
 
 
111
        printf("ret=%d\n",ret);
 
112
        if(ret<0){
 
113
                printf("errno=%d\n",errno);
 
114
                comedi_perror("comedi_command");
 
115
                return;
 
116
        }
 
117
 
 
118
        go=1;
 
119
        while(go){
 
120
                FD_ZERO(&rdset);
 
121
                FD_SET(comedi_fileno(device),&rdset);
 
122
                timeout.tv_sec = 0;
 
123
                timeout.tv_usec = 50000;
 
124
                ret = select(comedi_fileno(dev)+1,&rdset,NULL,NULL,&timeout);
 
125
                //printf("select returned %d\n",ret);
 
126
                if(ret<0){
 
127
                        perror("select");
 
128
                }else if(ret==0){
 
129
                        /* hit timeout */
 
130
                        printf("timeout, polling...\n");
 
131
                        ret = comedi_poll(device,0);
 
132
                        printf("poll returned %d\n",ret);
 
133
                }else if(FD_ISSET(comedi_fileno(device),&rdset)){
 
134
                        /* comedi file descriptor became ready */
 
135
                        //printf("comedi file descriptor ready\n");
 
136
                        ret=read(comedi_fileno(dev),buf,sizeof(buf));
 
137
                        printf("read returned %d\n",ret);
 
138
                        if(ret<0){
 
139
                                if(errno==EAGAIN){
 
140
                                        go = 0;
 
141
                                        perror("read");
 
142
                                }
 
143
                        }else if(ret==0){
 
144
                                go = 0;
 
145
                        }else{
 
146
                                int i;
 
147
                                total+=ret;
 
148
                                //printf("read %d %d\n",ret,total);
 
149
                                for(i=0;i<ret/sizeof(sampl_t);i++){
 
150
                                        printf("%d\n",buf[i]);
 
151
                                }
 
152
                        }
 
153
                }else{
 
154
                        /* unknown file descriptor became ready */
 
155
                        printf("unknown file descriptor ready\n");
 
156
                }
 
157
        }
 
158
}
 
159
 
 
160
/*
 
161
 * This part of the demo measures channels 1, 2, 3, 4 at a rate of
 
162
 * 10 khz, with the inter-sample time at 10 us (100 khz).  The number
 
163
 * of scans measured is 10.  This is analogous to the old mode2
 
164
 * acquisition.
 
165
 */
 
166
void prepare_cmd(comedi_t *dev,comedi_cmd *cmd)
 
167
{
 
168
        memset(cmd,0,sizeof(*cmd));
 
169
 
 
170
        /* the subdevice that the command is sent to */
 
171
        cmd->subdev =   subdevice;
 
172
 
 
173
        /* flags */
 
174
        cmd->flags =    0;
 
175
 
 
176
        /* each event requires a trigger, which is specified
 
177
           by a source and an argument.  For example, to specify
 
178
           an external digital line 3 as a source, you would use
 
179
           src=TRIG_EXT and arg=3. */
 
180
 
 
181
        /* In this case, we specify using TRIG_NOW to start
 
182
         * acquisition immediately when the command is issued.  
 
183
         * The argument of TRIG_NOW is "number of nsec after
 
184
         * NOW", but no driver supports it yet.  Also, no driver
 
185
         * currently supports using a start_src other than
 
186
         * TRIG_NOW.  */
 
187
        cmd->start_src =                TRIG_NOW;
 
188
        cmd->start_arg =                0;
 
189
 
 
190
        /* The timing of the beginning of each scan is controlled
 
191
         * by scan_begin.  TRIG_TIMER specifies that scan_start
 
192
         * events occur periodically at a rate of scan_begin_arg
 
193
         * nanoseconds between scans. */
 
194
        cmd->scan_begin_src =   TRIG_TIMER;
 
195
        cmd->scan_begin_arg =   msec_to_nsec(100);
 
196
 
 
197
        /* The timing between each sample in a scan is controlled
 
198
         * by convert.  Like above, TRIG_TIMER specifies that
 
199
         * convert events occur periodically at a rate of convert_arg
 
200
         * nanoseconds between scans. */
 
201
        cmd->convert_src =      TRIG_TIMER;
 
202
        cmd->convert_arg =      msec_to_nsec(1);
 
203
 
 
204
        /* The end of each scan is almost always specified using
 
205
         * TRIG_COUNT, with the argument being the same as the
 
206
         * number of channels in the chanlist.  You could probably
 
207
         * find a device that allows something else, but it would
 
208
         * be strange. */
 
209
        cmd->scan_end_src =     TRIG_COUNT;
 
210
        cmd->scan_end_arg =     n_chans;        /* number of channels */
 
211
 
 
212
        /* The end of acquisition is controlled by stop_src and
 
213
         * stop_arg.  The src will typically be TRIG_COUNT or
 
214
         * TRIG_NONE.  Specifying TRIG_COUNT will stop acquisition
 
215
         * after stop_arg number of scans, or TRIG_NONE will
 
216
         * cause acquisition to continue until stopped using
 
217
         * comedi_cancel(). */
 
218
        cmd->stop_src =         TRIG_COUNT;
 
219
        cmd->stop_arg =         n_scans;
 
220
 
 
221
        /* the channel list determined which channels are sampled.
 
222
           In general, chanlist_len is the same as scan_end_arg.  Most
 
223
           boards require this.  */
 
224
        cmd->chanlist =         chanlist;
 
225
        cmd->chanlist_len =     n_chans;
 
226
}
 
227