From e0cd2cf9d699f142cd107fca32f0afa207b60190 Mon Sep 17 00:00:00 2001
From: "Michael D. M. Dryden" <mdryden@chem.utoronto.ca>
Date: Fri, 7 Nov 2014 17:45:44 -0500
Subject: [PATCH] Implemented version checking and setting of correct gain
 options in UI (still need to change in experiment to get correct plots)

---
 dstat-interface/dstat-interface/dstat_comm.py | 36 ++++++++++++++++++-
 .../dstat-interface/interface/adc_pot.py      | 34 +++++++++++++++++-
 .../interface/dstatinterface.glade            | 17 ++++++++-
 dstat-interface/dstat-interface/main.py       | 20 +++++++++++
 4 files changed, 104 insertions(+), 3 deletions(-)

diff --git a/dstat-interface/dstat-interface/dstat_comm.py b/dstat-interface/dstat-interface/dstat_comm.py
index 0a17f89..8b45b2a 100644
--- a/dstat-interface/dstat-interface/dstat_comm.py
+++ b/dstat-interface/dstat-interface/dstat_comm.py
@@ -36,6 +36,40 @@ def call_it(instance, name, args=(), kwargs=None):
         kwargs = {}
     return getattr(instance, name)(*args, **kwargs)
 
+def version_check(ser_port):
+    """Tries to contact DStat and get version. Returns a tuple of
+    (major, minor). If no response, returns empty tuple.
+        
+    Arguments:
+    ser_port -- address of serial port to use
+    """
+    ser = delayedSerial(ser_port, 1024000, timeout=1)
+    ser.write("ck")
+        
+    ser.flushInput()
+    ser.write('!')
+            
+    while not ser.read().startswith("C"):
+        pass
+    ser.write('V')
+    for line in ser:
+        if line.startswith('V'):
+            input = line.lstrip('V')
+        elif line.startswith("#"):
+            print line
+        elif line.lstrip().startswith("no"):
+            print line
+            ser.flushInput()
+            break
+            
+    parted = input.rstrip().split('.')
+    print parted
+    
+    ser.close()
+    
+    return (int(parted[0]), int(parted[1]))
+    
+    
 
 class delayedSerial(serial.Serial): 
     """Extends Serial.write so that characters are output individually
@@ -77,7 +111,7 @@ class Experiment(object):
         self.databytes = 8
 
         self.data_extra = []  # must be defined even when not needed
-        self.__gaintable = [1e2, 3e2, 3e3, 3e4, 3e5, 3e6, 3e7, 5e8]
+        self.__gaintable = [1e2, 3e2, 3e3, 3e4, 3e5, 3e6, 3e7, 5e8] #todo version settings
         self.gain = self.__gaintable[int(self.parameters['gain'])]
 
         self.commands = ["A", "G"]
diff --git a/dstat-interface/dstat-interface/interface/adc_pot.py b/dstat-interface/dstat-interface/interface/adc_pot.py
index 715b07a..e388898 100644
--- a/dstat-interface/dstat-interface/interface/adc_pot.py
+++ b/dstat-interface/dstat-interface/interface/adc_pot.py
@@ -1,4 +1,5 @@
 #!/usr/bin/env python
+# -*- coding: utf-8 -*-
 #     DStat Interface - An interface for the open hardware DStat potentiostat
 #     Copyright (C) 2014  Michael D. M. Dryden - 
 #     Wheeler Microfluidics Laboratory <http://microfluidics.utoronto.ca>
@@ -19,6 +20,24 @@
 
 import gtk
 
+v1_1_gain = [(0, "100 Ω (15 mA FS)", 0),
+             (1, "300 Ω (5 mA FS)", 1),
+             (2, "3 kΩ (500 µA FS)", 2),
+             (3, "30 kΩ (50 µA FS)", 3),
+             (4, "300 kΩ (5 µA FS)", 4),
+             (5, "3 MΩ (500 nA FS)", 5),
+             (6, "30 MΩ (50 nA FS)", 6),
+             (7, "500 MΩ (3 nA FS)", 7)]
+
+v1_2_gain = [(0, "Bypass", 0),
+             (1, "100 Ω (15 mA FS)", 1),
+             (2, "3 kΩ (500 µA FS)", 2),
+             (3, "30 kΩ (50 µA FS)", 3),
+             (4, "300 kΩ (5 µA FS)", 4),
+             (5, "3 MΩ (500 nA FS)", 5),
+             (6, "30 MΩ (50 nA FS)", 6),
+             (7, "100 MΩ (15 nA FS)", 7)]
+
 class adc_pot:
     def __init__(self):
         self.builder = gtk.Builder()
@@ -40,6 +59,19 @@ class adc_pot:
         self.srate_combobox.set_active(7)
         
         self.gain_combobox = self.builder.get_object('gain_combobox')
+        self.gain_liststore = self.builder.get_object('gain_liststore')
         self.gain_combobox.pack_start(self.cell, True)
         self.gain_combobox.add_attribute(self.cell, 'text', 1)
-        self.gain_combobox.set_active(2)
\ No newline at end of file
+        self.gain_combobox.set_active(2)
+        
+    def set_version(self, version):
+        """ Sets menus for DStat version. """
+        self.gain_liststore.clear()
+        if version[0] == 1:
+            if version[1] == 1:
+                for i in v1_1_gain:
+                    self.gain_liststore.append(i)
+            elif version[1] >= 2:
+                for i in v1_2_gain:
+                    self.gain_liststore.append(i)
+                
\ No newline at end of file
diff --git a/dstat-interface/dstat-interface/interface/dstatinterface.glade b/dstat-interface/dstat-interface/interface/dstatinterface.glade
index ee27bdf..644e430 100644
--- a/dstat-interface/dstat-interface/interface/dstatinterface.glade
+++ b/dstat-interface/dstat-interface/interface/dstatinterface.glade
@@ -1210,6 +1210,21 @@ Thanks to Christian Fobel for help with Dropbot Plugin</property>
                 <property name="position">2</property>
               </packing>
             </child>
+            <child>
+              <object class="GtkButton" id="serial_version">
+                <property name="label">gtk-connect</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">True</property>
+                <property name="use_stock">True</property>
+                <signal name="clicked" handler="on_serial_version_clicked" swapped="no"/>
+              </object>
+              <packing>
+                <property name="expand">False</property>
+                <property name="fill">False</property>
+                <property name="position">3</property>
+              </packing>
+            </child>
             <child>
               <object class="GtkStatusbar" id="statusbar">
                 <property name="visible">True</property>
@@ -1219,7 +1234,7 @@ Thanks to Christian Fobel for help with Dropbot Plugin</property>
               <packing>
                 <property name="expand">True</property>
                 <property name="fill">True</property>
-                <property name="position">3</property>
+                <property name="position">4</property>
               </packing>
             </child>
           </object>
diff --git a/dstat-interface/dstat-interface/main.py b/dstat-interface/dstat-interface/main.py
index 4343169..574cf39 100644
--- a/dstat-interface/dstat-interface/main.py
+++ b/dstat-interface/dstat-interface/main.py
@@ -167,6 +167,26 @@ class Main(object):
         
         for i in self.serial_devices.ports:
             self.serial_liststore.append([i])
+            
+    def on_serial_version_clicked(self, data=None):
+        """Retrieve DStat version."""
+        self.version = comm.version_check(self.serial_liststore.get_value(
+                                     self.serial_combobox.get_active_iter(), 0))
+        
+        print self.version
+        
+        self.statusbar.remove_all(self.error_context_id)
+        
+        if not len(self.version) == 2:
+            self.statusbar.push(self.error_context_id, "Communication Error")
+            return
+        
+        else:
+            self.adc_pot.set_version(self.version)
+            self.statusbar.push(self.error_context_id,
+                                "".join(["DStat version: ", str(self.version[0]), ".",
+                                str(self.version[1])])
+                               )
 
     def on_pot_start_clicked(self, data=None):
         """Run currently visible experiment."""
-- 
GitLab