3 functions to manipulate physical unit conversion
5 COMEDILIB - Linux Control and Measurement Device Interface Library
6 Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
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
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.
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
30 #include <sys/types.h>
34 #include <sys/ioctl.h>
38 #include "libinternal.h"
41 /* sometimes we can't find a definition of NAN */
45 (__extension__ ((union { unsigned char __c[8]; \
47 { { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f } }).__d)
51 static enum comedi_oor_behavior comedi_oor_is_nan = COMEDI_OOR_NAN;
53 EXPORT_ALIAS_DEFAULT(_comedi_set_global_oor_behavior,comedi_set_global_oor_behavior,0.7.18);
54 enum comedi_oor_behavior _comedi_set_global_oor_behavior(
55 enum comedi_oor_behavior behavior)
57 int old_behavior=comedi_oor_is_nan;
59 comedi_oor_is_nan=behavior;
65 EXPORT_ALIAS_DEFAULT(_comedi_to_phys,comedi_to_phys,0.7.18);
66 double _comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata)
71 if(!maxdata)return NAN;
73 if(comedi_oor_is_nan==COMEDI_OOR_NAN && (data==0 || data==maxdata))
78 x*=(rng->max-rng->min);
84 EXPORT_ALIAS_DEFAULT(_comedi_from_phys,comedi_from_phys,0.7.18);
85 lsampl_t _comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata)
92 s=(data-rng->min)/(rng->max-rng->min)*maxdata;
94 if(s>maxdata)return maxdata;
96 return (lsampl_t)(floor(s+0.5));
99 EXPORT_ALIAS_DEFAULT(_comedi_find_range,comedi_find_range,0.7.18);
100 int _comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan,unsigned int unit,double min,double max)
102 unsigned int range_type;
104 comedi_range *range_ptr,*best_ptr;
107 if(!valid_chan(it,subd,chan))return -1;
109 range_type=comedi_get_rangetype(it,subd,chan);
112 for(i=0;i<RANGE_LENGTH(range_type);i++){
113 range_ptr=comedi_get_range(it,subd,chan,i);
114 if(range_ptr->unit==unit && range_ptr->min<=min && range_ptr->max>=max){
115 if(best<0 || (range_ptr->max-range_ptr->min) <
116 (best_ptr->max-best_ptr->min)){
125 EXPORT_ALIAS_DEFAULT(_comedi_get_n_ranges,comedi_get_n_ranges,0.7.18);
126 int _comedi_get_n_ranges(comedi_t *it,unsigned int subd,unsigned int chan)
128 unsigned int range_type;
130 if(!valid_chan(it,subd,chan))return -1;
132 range_type=comedi_get_rangetype(it,subd,chan);
133 return RANGE_LENGTH(range_type);
136 EXPORT_ALIAS_DEFAULT(_comedi_range_is_chan_specific,comedi_range_is_chan_specific,0.7.18);
137 int _comedi_range_is_chan_specific(comedi_t *it,unsigned int subd)
139 if(!valid_subd(it,subd)) return -1;
140 return (it->subdevices[subd].subd_flags&SDF_RANGETYPE)?1:0;
143 EXPORT_ALIAS_DEFAULT(_comedi_sampl_to_phys,comedi_sampl_to_phys,0.7.18);
144 int _comedi_sampl_to_phys(double *dest, int dst_stride, sampl_t *src,
145 int src_stride, comedi_range *rng, lsampl_t maxdata, int n)
152 if(!maxdata)return -1;
154 mult = (rng->max-rng->min)/maxdata;
155 if(comedi_oor_is_nan==COMEDI_OOR_NAN){
157 if(*src==0 || *src==maxdata){
161 *dest = rng->min + mult*(*src);
163 dest = ((void *)dest) + dst_stride;
164 src = ((void *)src) + src_stride;
168 if(*src==0 || *src==maxdata){
171 *dest = rng->min + mult*(*src);
172 dest = ((void *)dest) + dst_stride;
173 src = ((void *)src) + src_stride;
180 EXPORT_ALIAS_DEFAULT(_comedi_sampl_from_phys,comedi_sampl_from_phys,0.7.18);
181 int _comedi_sampl_from_phys(sampl_t *dest,int dst_stride,double *src,
182 int src_stride, comedi_range *rng, lsampl_t maxdata, int n)
189 if(!maxdata)return -1;
191 mult = (maxdata+1)/(rng->max-rng->min);
193 *dest=mult*(*src-rng->min);
202 dest = ((void *)dest) + dst_stride;
203 src = ((void *)src) + src_stride;