Optional config-based-setup for the main Piezo class.
authorW. Trevor King <wking@drexel.edu>
Thu, 15 Mar 2012 16:25:50 +0000 (12:25 -0400)
committerW. Trevor King <wking@drexel.edu>
Thu, 15 Mar 2012 16:25:50 +0000 (12:25 -0400)
This breaks compatibility with earlier versions, because we've added a
`config` option to `Piezo.__init__`.  The addition is a good thing,
though, because it makes `Piezo` more consistent with the `PiezoAxis`,
and `{I,O}Channel` classes.

pypiezo/afm.py
pypiezo/base.py
pypiezo/test.py

index 490f6bc6568c70849d4da9e3abdf296030da0f81..09f897677bc18426575c5885ac36adf7f928b57a 100644 (file)
@@ -43,25 +43,22 @@ class AFMPiezo (_base.Piezo):
 
     >>> from pprint import pprint
     >>> from pycomedi.device import Device
-    >>> from pycomedi.subdevice import StreamingSubdevice
-    >>> from pycomedi.channel import AnalogChannel
-    >>> from pycomedi.constant import AREF, SUBDEVICE_TYPE, UNIT
+    >>> from pycomedi.constant import AREF
     >>> from . import config
     >>> from . import surface
 
     >>> d = Device('/dev/comedi0')
     >>> d.open()
 
-    >>> s_in = d.find_subdevice_by_type(SUBDEVICE_TYPE.ai,
-    ...     factory=StreamingSubdevice)
-    >>> s_out = d.find_subdevice_by_type(SUBDEVICE_TYPE.ao,
-    ...     factory=StreamingSubdevice)
-
-    >>> axis_channel = s_out.channel(
-    ...     0, factory=AnalogChannel, aref=AREF.ground)
-    >>> input_channel = s_in.channel(0, factory=AnalogChannel, aref=AREF.diff)
-    >>> for chan in [axis_channel, input_channel]:
-    ...     chan.range = chan.find_range(unit=UNIT.volt, min=-10, max=10)
+    >>> piezo_config = config.PiezoConfig()
+    >>> piezo_config['name'] = 'Molly'
+    >>> piezo_config['axes'] = [config.AxisConfig()]
+    >>> piezo_config['axes'][0]['channel'] = config.OutputChannelConfig()
+    >>> piezo_config['axes'][0]['channel']['analog-reference'] = AREF.ground
+    >>> piezo_config['axes'][0]['channel']['name'] = 'z'
+    >>> piezo_config['inputs'] = [config.InputChannelConfig()]
+    >>> piezo_config['inputs'][0]['analog-reference'] = AREF.diff
+    >>> piezo_config['inputs'][0]['name'] = 'deflection'
 
     We set the minimum voltage for the `z` axis to -9 (a volt above
     the minimum possible voltage) to help with testing
@@ -70,21 +67,14 @@ class AFMPiezo (_base.Piezo):
     first few surface approaching steps, which could lead to an
     `EdgeKink` error instead of a `FlatFit` error.
 
-    >>> axis_config = config.AxisConfig()
-    >>> axis_config.update(
+    >>> piezo_config['axes'][0].update(
     ...     {'gain':20, 'sensitivity':8e-9, 'minimum':-9})
-    >>> axis_config['channel'] = config.OutputChannelConfig()
-    >>> axis_config['channel']['name'] = 'z'
-    >>> input_config = config.InputChannelConfig()
-    >>> input_config['name'] = 'deflection'
-
-    >>> a = _base.PiezoAxis(config=axis_config, axis_channel=axis_channel)
-    >>> a.setup_config()
 
-    >>> c = _base.InputChannel(config=input_config, channel=input_channel)
-    >>> c.setup_config()
+    >>> a = _base.PiezoAxis(config=piezo_config['axes'][0], devices=[d])
+    >>> c = _base.InputChannel(config=piezo_config['inputs'][0], devices=[d])
 
-    >>> p = AFMPiezo(axes=[a], inputs=[c], name='Molly')
+    >>> p = AFMPiezo(config=piezo_config, axes=[a], inputs=[c])
+    >>> p.setup_config()
 
     >>> deflection = p.read_deflection()
     >>> deflection  # doctest: +SKIP
index c21dd3f0d8d09d5aca8b1ee5e20b718cc568d2ba..c3d5ec17941417ae20fbceb4efca347e4b7964f5 100644 (file)
@@ -560,35 +560,28 @@ class Piezo (object):
     >>> d = Device('/dev/comedi0')
     >>> d.open()
 
-    >>> s_in = d.find_subdevice_by_type(SUBDEVICE_TYPE.ai,
-    ...     factory=StreamingSubdevice)
-    >>> s_out = d.find_subdevice_by_type(SUBDEVICE_TYPE.ao,
-    ...     factory=StreamingSubdevice)
-
-    >>> axis_channel = s_out.channel(
-    ...     0, factory=AnalogChannel, aref=AREF.ground)
-    >>> monitor_channel = s_in.channel(
-    ...     0, factory=AnalogChannel, aref=AREF.diff)
-    >>> input_channel = s_in.channel(1, factory=AnalogChannel, aref=AREF.diff)
-    >>> for chan in [axis_channel, monitor_channel, input_channel]:
-    ...     chan.range = chan.find_range(unit=UNIT.volt, min=-10, max=10)
-
     >>> axis_config = _config.AxisConfig()
-    >>> axis_config.update({'gain':20, 'sensitivity':8e-9})
+    >>> axis_config['gain'] = 20.0
+    >>> axis_config['sensitivity'] = 8e-9
     >>> axis_config['channel'] = _config.OutputChannelConfig()
+    >>> axis_config['channel']['analog-reference'] = AREF.ground
     >>> axis_config['channel']['name'] = 'z'
     >>> axis_config['monitor'] = _config.InputChannelConfig()
-    >>> input_config = _config.InputChannelConfig()
-    >>> input_config['name'] = 'some-input'
-
-    >>> a = PiezoAxis(config=axis_config, axis_channel=axis_channel,
-    ...     monitor_channel=monitor_channel)
+    >>> axis_config['monitor']['analog-reference'] = AREF.diff
+    >>> a = PiezoAxis(config=axis_config, devices=[d])
     >>> a.setup_config()
 
-    >>> c = InputChannel(config=input_config, channel=input_channel)
+    >>> input_config = _config.InputChannelConfig()
+    >>> input_config['analog-reference'] = AREF.diff
+    >>> input_config['name'] = 'some-input'
+    >>> c = InputChannel(config=input_config, devices=[d])
     >>> c.setup_config()
 
-    >>> p = Piezo(axes=[a], inputs=[c], name='Charlie')
+    >>> config = _config.PiezoConfig()
+    >>> config['name'] = 'Charlie'
+
+    >>> p = Piezo(config=config, axes=[a], inputs=[c])
+    >>> p.setup_config()
     >>> inputs = p.read_inputs()
     >>> pprint(inputs)  # doctest: +SKIP
     {'some-input': 34494L, 'z-monitor': 32669L}
@@ -638,17 +631,48 @@ class Piezo (object):
      'z': array([    0,  3276,  ..., 32760], dtype=uint16),
      'z-monitor': array([ 3102,  6384,  ..., 32647], dtype=uint16)}
 
+    Opening from the config alone:
+
+    >>> p = Piezo(config=config, devices=[d])
+    >>> for axis in p.axes:
+    ...     print(axis.axis_channel)
+    ...     print(axis.monitor_channel)
+    ... # doctest: +ELLIPSIS
+    <pycomedi.channel.AnalogChannel object at 0x...>
+    <pycomedi.channel.AnalogChannel object at 0x...>
+    >>> for input in p.inputs:
+    ...     print(input.channel)
+    ... # doctest: +ELLIPSIS
+    <pycomedi.channel.AnalogChannel object at 0x...>
+
     >>> d.close()
     """
-    def __init__(self, axes, inputs, name=None):
+    def __init__(self, config, axes=None, inputs=None, devices=None):
+        self.config=config
         self.axes = axes
         self.inputs = inputs
-        self.config = _config.PiezoConfig()
-        self.name = name
-        self.config['name'] = name
-        self.config['axes'] = [x.config for x in axes]
-        self.config['inputs'] = [x.config for x in inputs]
         self.last_output = {}
+        self.load_from_config(devices=devices)
+
+    def load_from_config(self, devices):
+        if not self.axes:
+            self.axes = []
+            for config in self.config['axes']:
+                self.axes.append(PiezoAxis(config=config, devices=devices))
+            self.last_output.clear()
+        if not self.inputs:
+            self.inputs = []
+            for config in self.config['inputs']:
+                self.inputs.append(
+                    InputChannel(config=config, devices=devices))
+        self.name = self.config['name']
+
+    def setup_config(self):
+        "Initialize the axis and input configs."
+        for x in self.axes + self.inputs:
+            x.setup_config()
+        self.config['axes'] = [x.config for x in self.axes]
+        self.config['inputs'] = [x.config for x in self.inputs]
 
     def axis_by_name(self, name):
         "Get an axis by its name."
index 4350dd6e799f64ae871546c9b5966972f0ec1c82..d2f24283863986f0e3a1e663a4fc624208db636d 100644 (file)
@@ -31,7 +31,7 @@ def get_piezo_config(storage=None):
     """Return a default PiezoConfig instance.
 
     >>> p = get_piezo_config()
-    >>> print p.dump()
+    >>> print p.dump()  # doctest: +REPORT_UDIFF
     name: test piezo
     axes:
       0:
@@ -45,7 +45,7 @@ def get_piezo_config(storage=None):
           subdevice: -1
           channel: 0
           maxdata: 100
-          range: 1
+          range: 0
           analog-reference: ground
           conversion-coefficients: 0, 1
           conversion-origin: 0
@@ -63,7 +63,7 @@ def get_piezo_config(storage=None):
           subdevice: -1
           channel: 1
           maxdata: 100
-          range: 1
+          range: 0
           analog-reference: ground
           conversion-coefficients: 0, 1
           conversion-origin: 0
@@ -77,7 +77,7 @@ def get_piezo_config(storage=None):
         subdevice: -1
         channel: 0
         maxdata: 100
-        range: 1
+        range: 0
         analog-reference: ground
         conversion-coefficients: 0, 1
         conversion-origin: 0
@@ -89,7 +89,7 @@ def get_piezo_config(storage=None):
         subdevice: -1
         channel: 1
         maxdata: 100
-        range: 1
+        range: 0
         analog-reference: ground
         conversion-coefficients: 0, 1
         conversion-origin: 0