localstatedir should be /var, thus we need append a lib/ here
[comedilib.git] / lib / range.c
index c0baf51e9177b05a66d896b6a972b78fae043988..cc6115d3d8304da8506f1818722a802761557ea9 100644 (file)
@@ -1,24 +1,24 @@
 /*
     lib/range.c
-    comedi library routines for voltage ranges
+    functions to manipulate physical unit conversion
 
-    COMEDI - Linux Control and Measurement Device Interface
-    Copyright (C) 1997-8 David A. Schleef <ds@stm.lbl.gov>
+    COMEDILIB - Linux Control and Measurement Device Interface Library
+    Copyright (C) 1997-2001 David A. Schleef <ds@schleef.org>
 
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
+    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 program is distributed in the hope that it will be useful,
+    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 General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+    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>
 #include <unistd.h>
 #include <sys/ioctl.h>
 #include <errno.h>
-#include <comedi.h>
 #include <string.h>
 
-#include <libinternal.h>
+#include "libinternal.h"
 
 
 /* sometimes we can't find a definition of NAN */
@@ -51,7 +50,8 @@
 
 static enum comedi_oor_behavior comedi_oor_is_nan = COMEDI_OOR_NAN;
 
-enum comedi_oor_behavior comedi_set_global_oor_behavior(
+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;
@@ -62,7 +62,8 @@ enum comedi_oor_behavior comedi_set_global_oor_behavior(
 }
 
 
-double comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata)
+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;
 
@@ -80,7 +81,8 @@ double comedi_to_phys(lsampl_t data,comedi_range *rng,lsampl_t maxdata)
        return x;
 }
 
-lsampl_t comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata)
+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;
 
@@ -94,7 +96,8 @@ lsampl_t comedi_from_phys(double data,comedi_range *rng,lsampl_t maxdata)
        return (lsampl_t)(floor(s+0.5));
 }
 
-int comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan,unsigned int unit,double min,double max)
+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;
@@ -119,7 +122,8 @@ int comedi_find_range(comedi_t *it,unsigned int subd,unsigned int chan,unsigned
        return best;
 }
 
-int comedi_get_n_ranges(comedi_t *it,unsigned int subd,unsigned int chan)
+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;
 
@@ -129,8 +133,75 @@ int comedi_get_n_ranges(comedi_t *it,unsigned int subd,unsigned int chan)
        return RANGE_LENGTH(range_type);
 }
 
-int comedi_range_is_chan_specific(comedi_t *it,unsigned int subd)
+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;
+}
+