Only call figure.show if it exits.
[pypiezo.git] / pypiezo / base.py
index 98b5e062dd767e29317db16fa8dbdb41d51080f6..3220e21833a060eca3eb76d2a03e0dd695ed6ba8 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2011-2012 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2011-2012 W. Trevor King <wking@tremily.us>
 #
 # This file is part of pypiezo.
 #
@@ -386,7 +386,8 @@ class PiezoAxis (object):
 
     Opening from the config alone:
 
-    >>> p = PiezoAxis(config=config, devices=[d])
+    >>> p = PiezoAxis(config=config)
+    >>> p.load_from_config(devices=[d])
     >>> p.axis_channel  # doctest: +ELLIPSIS
     <pycomedi.channel.AnalogChannel object at 0x...>
     >>> p.monitor_channel  # doctest: +ELLIPSIS
@@ -394,12 +395,10 @@ class PiezoAxis (object):
 
     >>> d.close()
     """
-    def __init__(self, config, axis_channel=None, monitor_channel=None,
-                 devices=None):
+    def __init__(self, config, axis_channel=None, monitor_channel=None):
         self.config = config
         self.axis_channel = axis_channel
         self.monitor_channel = monitor_channel
-        self.load_from_config(devices=devices)
 
     def load_from_config(self, devices):
         c = self.config  # reduce verbosity
@@ -410,11 +409,13 @@ class PiezoAxis (object):
                  '({} and {})').format(
                     c['channel']['device'], c['monitor']['device']))
         if not self.axis_channel:
-            self.axis_channel = OutputChannel(
-                config=c['channel'], devices=devices).channel
+            output = OutputChannel(config=c['channel'])
+            output.load_from_config(devices=devices)
+            self.axis_channel = output.channel
         if c['monitor'] and not self.monitor_channel:
-            self.monitor_channel = InputChannel(
-                config=c['monitor'], devices=devices).channel
+            monitor = InputChannel(config=c['monitor'])
+            monitor.load_from_config(devices=devices)
+            self.monitor_channel = monitor.channel
         self.name = c['channel']['name']
 
     def setup_config(self):
@@ -471,16 +472,16 @@ class OutputChannel(object):
 
     Opening from the config alone:
 
-    >>> c = OutputChannel(config=channel_config, devices=[d])
+    >>> c = OutputChannel(config=channel_config)
+    >>> c.load_from_config(devices=[d])
     >>> c.channel  # doctest: +ELLIPSIS
     <pycomedi.channel.AnalogChannel object at 0x...>
 
     >>> d.close()
     """
-    def __init__(self, config, channel=None, devices=None):
+    def __init__(self, config, channel=None):
         self.config = config
         self.channel = channel
-        self.load_from_config(devices=devices)
 
     def load_from_config(self, devices):
         _load_channel_from_config(
@@ -529,16 +530,16 @@ class InputChannel(object):
 
     Opening from the config alone:
 
-    >>> c = InputChannel(config=channel_config, devices=[d])
+    >>> c = InputChannel(config=channel_config)
+    >>> c.load_from_config(devices=[d])
     >>> c.channel  # doctest: +ELLIPSIS
     <pycomedi.channel.AnalogChannel object at 0x...>
 
     >>> d.close()
     """
-    def __init__(self, config, channel=None, devices=None):
+    def __init__(self, config, channel=None):
         self.config = config
         self.channel = channel
-        self.load_from_config(devices=devices)
 
     def load_from_config(self, devices):
         _load_channel_from_config(
@@ -568,13 +569,15 @@ class Piezo (object):
     >>> axis_config['channel']['name'] = 'z'
     >>> axis_config['monitor'] = _config.InputChannelConfig()
     >>> axis_config['monitor']['analog-reference'] = AREF.diff
-    >>> a = PiezoAxis(config=axis_config, devices=[d])
+    >>> a = PiezoAxis(config=axis_config)
+    >>> a.load_from_config(devices=[d])
     >>> a.setup_config()
 
     >>> input_config = _config.InputChannelConfig()
     >>> input_config['analog-reference'] = AREF.diff
     >>> input_config['name'] = 'some-input'
-    >>> c = InputChannel(config=input_config, devices=[d])
+    >>> c = InputChannel(config=input_config)
+    >>> c.load_from_config(devices=[d])
     >>> c.setup_config()
 
     >>> config = _config.PiezoConfig()
@@ -633,7 +636,8 @@ class Piezo (object):
 
     Opening from the config alone:
 
-    >>> p = Piezo(config=config, devices=[d])
+    >>> p = Piezo(config=config)
+    >>> p.load_from_config(devices=[d])
     >>> for axis in p.axes:
     ...     print(axis.axis_channel)
     ...     print(axis.monitor_channel)
@@ -647,24 +651,26 @@ class Piezo (object):
 
     >>> d.close()
     """
-    def __init__(self, config, axes=None, inputs=None, devices=None):
+    def __init__(self, config, axes=None, inputs=None):
         self.config=config
         self.axes = axes
         self.inputs = 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))
+                axis = PiezoAxis(config=config)
+                axis.load_from_config(devices=devices)
+                self.axes.append(axis)
             self.last_output.clear()
         if not self.inputs:
             self.inputs = []
             for config in self.config['inputs']:
-                self.inputs.append(
-                    InputChannel(config=config, devices=devices))
+                input = InputChannel(config=config)
+                input.load_from_config(devices=devices)
+                self.inputs.append(input)
         self.name = self.config['name']
 
     def setup_config(self):
@@ -893,7 +899,11 @@ class Piezo (object):
                             (input_data, input_names)]:
                 for i,name in enumerate(names):
                     axes.plot(d[:,i], label=name)
-            figure.show()
+            figure.canvas.draw()
+            if hasattr(figure, 'show'):
+                figure.show()
+            if not _matplotlib.is_interactive():
+                _matplotlib_pyplot.show()
         return input_data
 
     def named_ramp(self, data, frequency, output_names, input_names=()):
@@ -906,3 +916,16 @@ class Piezo (object):
         for i,name in enumerate(input_names):
             ret[name] = input_data[:,i]
         return ret
+
+    def zero(self, axis_names=None, **kwargs):
+        zeros = []
+        if axis_names is None:
+            axis_names = [axis.name for axis in self.axes]
+        for axis_name in axis_names:
+            axis = self.axis_by_name(axis_name)
+            config = self.config.select_config(
+                'axes', axis_name, get_attribute=get_axis_name)['channel']
+            zero = convert_volts_to_bits(config, 0)
+            zeros.append(zero)
+            self.jump(axis_name, zero)
+        return zeros