Add `-*- coding: utf-8 -*-` comments where Éric Piel is in authors.
[pycomedi.git] / pycomedi / channel.pyx
index c7d6cc178cdceb1a16819b8cd6dcc3971c395393..45f5c231f1711444dc6651b92c45b838b91e479d 100644 (file)
@@ -1,4 +1,6 @@
+# -*- coding: utf-8 -*-
 # Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
+#                         Éric Piel <piel@delmic.com>
 #
 # This file is part of pycomedi.
 #
@@ -23,6 +25,7 @@ import numpy as _numpy
 cimport _comedi_h
 cimport _comedilib_h
 from calibration cimport CalibratedConverter as _CalibratedConverter
+from calibration cimport Calibration as _Calibration
 from range cimport Range as _Range
 from subdevice cimport Subdevice as _Subdevice
 
@@ -227,6 +230,15 @@ cdef class AnalogChannel (Channel):
     >>> c.aref
     <_NamedInt ground>
 
+    You can calibrate the channel with the default channel calibration.
+
+    >>> c.apply_calibration()
+
+    Or you can use a parsed (and possibly altered) calibration.
+
+    >>> calibration = d.parse_calibration()
+    >>> c.apply_calibration(calibration=calibration)
+
     >>> data = c.data_read()
     >>> data  # doctest: +SKIP
     32670L
@@ -423,10 +435,10 @@ cdef class AnalogChannel (Channel):
             if calibration is None:
                 calibration = self.subdevice.device.parse_calibration()
             to_physical = self.get_softcal_converter(
-                   _constant.CONVERSION_DIRECTION.to_physical,
+                _constant.CONVERSION_DIRECTION.to_physical,
                 calibration)
             from_physical = self.get_softcal_converter(
-                   _constant.CONVERSION_DIRECTION.from_physical,
+                _constant.CONVERSION_DIRECTION.from_physical,
                 calibration)
         else:
             to_physical = self.get_hardcal_converter(
@@ -440,3 +452,47 @@ cdef class AnalogChannel (Channel):
 
     def get_converter(self, calibration=None):
         return self._get_converter(calibration)
+
+    cdef _apply_calibration(self, char *path):
+        if path is NULL:
+            p = self.subdevice.device.get_default_calibration_path()
+            # automatically get a char * refernce into the Python string p
+            path = p
+        ret = _comedilib_h.comedi_apply_calibration(
+            self.subdevice.device.device,
+            self.subdevice.index, self.index,
+            _constant.bitwise_value(self.range),
+            _constant.bitwise_value(self.aref),
+            path)
+        if ret < 0:
+            _error.raise_error(
+                function_name='comedi_apply_calibration', ret=ret)
+        return ret
+
+    cdef _apply_parsed_calibration(self, _Calibration calibration):
+        ret = _comedilib_h.comedi_apply_parsed_calibration(
+            self.subdevice.device.device,
+            self.subdevice.index, self.index,
+            _constant.bitwise_value(self.range),
+            _constant.bitwise_value(self.aref),
+            calibration.calibration)
+        if ret < 0:
+            _error.raise_error(
+                function_name='comedi_apply_parsed_calibration', ret=ret)
+        return ret
+
+    def apply_calibration(self, calibration=None, path=None):
+        """Apply a calibration to this channel configuration
+
+        `calibration` may None or a `Calibration` instance.  If it is
+        a `Calibration` instance, that instance is used for the
+        calibration.  Otherwise we look at `path`.  If `path` is None,
+        we use the default device calibration, otherwise we try and
+        use the calibration file located at `path`.
+        """
+        if calibration is not None:
+            self._apply_parsed_calibration(calibration)
+        elif path is not None:
+            self._apply_calibration(path)
+        else:
+            self._apply_calibration(NULL)