localstatedir should be /var, thus we need append a lib/ here
[comedilib.git] / lib / sv.c
1 /*
2     lib/sv.c
3     functions for slowly varying inputs
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 <stdio.h>
25 #include <math.h>
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/ioctl.h>
32 #include <errno.h>
33 #include <string.h>
34
35 #include "libinternal.h"
36
37
38
39 int sv_measure_l(comedi_sv_t *it,double *data);
40 int sv_measure_s(comedi_sv_t *it,double *data);
41
42 EXPORT_ALIAS_DEFAULT(_comedi_sv_init,comedi_sv_init,0.7.18);
43 int _comedi_sv_init(comedi_sv_t *it,comedi_t *dev,unsigned int subd,unsigned int chan)
44 {
45         if(!valid_chan(dev,subd,chan))return -1;
46         if(!it)return -1;
47
48         memset(it,0,sizeof(*it));
49
50         it->dev=dev;
51         it->subdevice=subd;
52         it->chan=chan;
53         it->n=100;
54
55         return comedi_sv_update(it);
56 }
57
58 EXPORT_ALIAS_DEFAULT(_comedi_sv_update,comedi_sv_update,0.7.18);
59 int _comedi_sv_update(comedi_sv_t *it)
60 {
61         if(!it)return -1;
62         if(!valid_chan(it->dev,it->subdevice,it->chan))return -1;
63
64         it->maxdata=comedi_get_maxdata(it->dev,it->subdevice,it->chan);
65
66         /* check current range policy */
67         /* check n */
68
69         return 0;
70 }
71
72 EXPORT_ALIAS_DEFAULT(_comedi_sv_measure,comedi_sv_measure,0.7.18);
73 int _comedi_sv_measure(comedi_sv_t *it,double *data)
74 {
75         if(it->dev->subdevices[it->subdevice].subd_flags & SDF_LSAMPL){
76                 return sv_measure_l(it,data);
77         }else{
78                 return sv_measure_s(it,data);
79         }
80 }
81
82 int sv_measure_l(comedi_sv_t *it,double *data)
83 {
84         comedi_trig t;
85         int ret=0;
86         lsampl_t *val;
87         unsigned int chan;
88         comedi_range *rng;
89         double sum;
90         int i;
91         int n;
92
93         val=malloc(sizeof(*val)*it->n);
94
95         chan=CR_PACK(it->chan,it->range,it->aref);
96         
97         t.subdev=it->subdevice;
98         t.mode=0;
99         t.flags=TRIG_DITHER;
100         t.n_chan=1;
101         t.chanlist=&chan;
102         t.trigsrc=0;
103         t.trigvar=0;
104         t.trigvar1=0;
105
106         rng=comedi_get_range(it->dev,it->subdevice,it->chan,it->range);
107
108         for(n=0;n<it->n;){
109                 t.data=(void *)(val+n);
110                 t.n=it->n-n;
111                 i = comedi_ioctl(it->dev->fd, COMEDI_TRIG, (unsigned long)&t);
112                 if(i<=0){
113                         ret=i;
114                         goto out;
115                 }
116                 n+=i;
117         }
118
119         sum=0;
120         for(i=0;i<it->n;i++){
121                 sum+=comedi_to_phys(val[i],rng,it->maxdata);
122         }
123         *data=sum/it->n;
124
125 out:
126         free(val);
127
128         return ret;
129 }
130
131 /* yes, these functions are _almost_ exactly the same... */
132
133 int sv_measure_s(comedi_sv_t *it,double *data)
134 {
135         comedi_trig t;
136         int ret=0;
137         sampl_t *val;
138         unsigned int chan;
139         comedi_range *rng;
140         double sum;
141         int i;
142         int n;
143
144         val=malloc(sizeof(*val)*it->n);
145
146         chan=CR_PACK(it->chan,it->range,it->aref);
147         
148         t.subdev=it->subdevice;
149         t.mode=0;
150         t.flags=TRIG_DITHER;
151         t.n_chan=1;
152         t.chanlist=&chan;
153         t.data=val;
154         t.n=it->n;
155         t.trigsrc=0;
156         t.trigvar=0;
157         t.trigvar1=0;
158
159         rng=comedi_get_range(it->dev,it->subdevice,it->chan,it->range);
160
161         for(n=0;n<it->n;){
162                 t.data=val+n;
163                 t.n=it->n-n;
164                 i = comedi_ioctl(it->dev->fd, COMEDI_TRIG, (unsigned long)&t);
165                 if(i<=0){
166                         ret=i;
167                         goto out;
168                 }
169                 n+=i;
170         }
171
172         sum=0;
173         for(i=0;i<it->n;i++){
174                 sum+=comedi_to_phys(val[i],rng,it->maxdata);
175         }
176         *data=sum/it->n;
177
178 out:
179         free(val);
180
181         return ret;
182 }
183
184
185
186
187
188
189