fix ruby prefix to work for distcheck
[comedilib.git] / lib / timed.c
1 /*
2     lib/timed.c
3     description
4
5     COMEDILIB - Linux Control and Measurement Device Interface Library
6     Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
7
8     This library is free software; you can redistribute it and/or
9     modify it under the terms of the GNU Lesser General Public
10     License as published by the Free Software Foundation, version 2.1
11     of the License.
12
13     This library is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16     Lesser General Public License for more details.
17
18     You should have received a copy of the GNU Lesser General Public
19     License along with this library; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
21     USA.
22 */
23
24 #include "libinternal.h"
25
26 #include <stdio.h>
27 #include <math.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/ioctl.h>
34 #include <errno.h>
35 #include <string.h>
36
37
38 #define BUFSZ 100
39
40 EXPORT_ALIAS_DEFAULT(_comedi_timed_1chan,comedi_timed_1chan,0.7.18);
41 int _comedi_timed_1chan(comedi_t *dev,unsigned int subd,unsigned int chan,unsigned int range,
42         unsigned int aref,double freq,unsigned int n_samples,double *data)
43 {
44         comedi_trig t;
45         double act_freq;
46         sampl_t *buffer;
47         comedi_range *the_range;
48         unsigned int maxdata;
49         int i,n,m;
50         
51         if(!valid_chan(dev,subd,chan))return -1;
52         if(!data)return -1;
53         
54         memset(&t,0,sizeof(t));
55         
56         /* check range */
57
58         the_range=comedi_get_range(dev,subd,chan,range);
59         maxdata=comedi_get_maxdata(dev,subd,chan);
60
61         chan=CR_PACK(chan,range,aref);
62
63         t.subdev=subd;
64         t.mode=2;
65         t.n_chan=1;
66         t.chanlist=&chan;
67         t.n=n_samples;
68         comedi_get_timer(dev,subd,freq,&t.trigvar,&act_freq);
69         t.trigvar1=1;
70         
71         buffer=malloc(sizeof(sampl_t)*BUFSZ);
72         if(!buffer)return -1;
73
74         comedi_trigger(dev,&t);
75         n=0;
76         while(n<n_samples){
77                 m=n_samples-n;
78                 if(m>BUFSZ)m=BUFSZ;
79                 if((m=read(dev->fd,buffer,m*sizeof(sampl_t)))<0){
80                         /* ack! */
81                         return -1;
82                 }
83                 m/=sizeof(sampl_t);
84                 for(i=0;i<m;i++){
85                         data[n+i]=comedi_to_phys(buffer[i],the_range,maxdata);
86                 }
87                 n+=m;
88         }
89
90         free(buffer);
91
92         return 0;
93 }
94