1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
lib/range.c
functions to manipulate physical unit conversion
COMEDILIB - Linux Control and Measurement Device Interface Library
Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1
of the License.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
USA.
*/
#include <stdio.h>
#define __USE_GNU
#include <math.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <string.h>
#include "libinternal.h"
/* sometimes we can't find a definition of NAN */
#ifndef NAN
#define NAN \
(__extension__ ((union { unsigned char __c[8]; \
double __d; }) \
{ { 0, 0, 0, 0, 0, 0, 0xf8, 0x7f } }).__d)
#endif
static enum comedi_oor_behavior comedi_oor_is_nan = COMEDI_OOR_NAN;
EXPORT_ALIAS_DEFAULT(_comedi_set_global_oor_behavior,comedi_set_global_oor_behavior,0.7.18);
enum comedi_oor_behavior _comedi_set_global_oor_behavior(
enum comedi_oor_behavior behavior)
{
int old_behavior=comedi_oor_is_nan;
comedi_oor_is_nan=behavior;
return old_behavior;
}
EXPORT_ALIAS_DEFAULT(_comedi_to_phys,comedi_to_phys,0.7.18);
double _comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata)
{
double x;
if(!rng)return NAN;
if(!maxdata)return NAN;
if(comedi_oor_is_nan==COMEDI_OOR_NAN && (data==0 || data==maxdata))
return NAN;
x=data;
x/=maxdata;
x*=(rng->max-rng->min);
x+=rng->min;
return x;
}
EXPORT_ALIAS_DEFAULT(_comedi_from_phys,comedi_from_phys,0.7.18);
lsampl_t _comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata)
{
double s;
if(!rng)return 0;
if(!maxdata)return 0;
s=(data-rng->min)/(rng->max-rng->min)*maxdata;
if(s<0)return 0;
if(s>maxdata)return maxdata;
return (lsampl_t)(floor(s+0.5));
}
EXPORT_ALIAS_DEFAULT(_comedi_find_range,comedi_find_range,0.7.18);
int _comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan,unsigned int unit,double min,double max)
{
unsigned int range_type;
int best;
comedi_range *range_ptr,*best_ptr;
int i;
if(!valid_chan(it,subd,chan))return -1;
range_type=comedi_get_rangetype(it,subd,chan);
best=-1;
best_ptr=NULL;
for(i=0;i<RANGE_LENGTH(range_type);i++){
range_ptr=comedi_get_range(it,subd,chan,i);
if(range_ptr->min<=min && range_ptr->max>=max){
if(best<0 || (range_ptr->max-range_ptr->min) <
(best_ptr->max-best_ptr->min)){
best=i;
best_ptr=range_ptr;
}
}
}
return best;
}
EXPORT_ALIAS_DEFAULT(_comedi_get_n_ranges,comedi_get_n_ranges,0.7.18);
int _comedi_get_n_ranges(comedi_t *it,unsigned int subd,unsigned int chan)
{
unsigned int range_type;
if(!valid_chan(it,subd,chan))return -1;
range_type=comedi_get_rangetype(it,subd,chan);
return RANGE_LENGTH(range_type);
}
EXPORT_ALIAS_DEFAULT(_comedi_range_is_chan_specific,comedi_range_is_chan_specific,0.7.18);
int _comedi_range_is_chan_specific(comedi_t *it,unsigned int subd)
{
return (it->subdevices[subd].subd_flags&SDF_RANGETYPE)?1:0;
}
EXPORT_ALIAS_DEFAULT(_comedi_sampl_to_phys,comedi_sampl_to_phys,0.7.18);
int _comedi_sampl_to_phys(double *dest, int dst_stride, sampl_t *src,
int src_stride, comedi_range *rng, lsampl_t maxdata, int n)
{
int oor = 0;
int i;
double mult;
if(!rng)return -1;
if(!maxdata)return -1;
mult = (rng->max-rng->min)/maxdata;
if(comedi_oor_is_nan==COMEDI_OOR_NAN){
for(i=0;i<n;i++){
if(*src==0 || *src==maxdata){
oor++;
*dest=NAN;
}else{
*dest = rng->min + mult*(*src);
}
dest = ((void *)dest) + dst_stride;
src = ((void *)src) + src_stride;
}
}else{
for(i=0;i<n;i++){
if(*src==0 || *src==maxdata){
oor++;
}
*dest = rng->min + mult*(*src);
dest = ((void *)dest) + dst_stride;
src = ((void *)src) + src_stride;
}
}
return oor;
}
EXPORT_ALIAS_DEFAULT(_comedi_sampl_from_phys,comedi_sampl_from_phys,0.7.18);
int _comedi_sampl_from_phys(sampl_t *dest,int dst_stride,double *src,
int src_stride, comedi_range *rng, lsampl_t maxdata, int n)
{
int oor = 0;
double mult;
int i;
if(!rng)return -1;
if(!maxdata)return -1;
mult = (maxdata+1)/(rng->max-rng->min);
for(i=0;i<n;i++){
*dest=mult*(*src-rng->min);
if(*src<rng->min){
*dest=0;
oor++;
}
if(*src>rng->min){
*dest=maxdata;
oor++;
}
dest = ((void *)dest) + dst_stride;
src = ((void *)src) + src_stride;
}
return oor;
}
|