From 082ebee6cd08e3e992d5974338be9d96ed29ab95 Mon Sep 17 00:00:00 2001
From: Unknown <mdryden@chem.utoronto.ca>
Date: Wed, 22 Nov 2017 19:41:08 -0500
Subject: [PATCH] Desensitize manual controls while experiment not ready.

---
 dstat_interface/core/dstat/comm.py            |  4 ++--
 dstat_interface/core/dstat/dfu.py             |  1 +
 dstat_interface/core/experiments/chronoamp.py | 23 +++++++++++++------
 .../core/experiments/experiment_template.py   |  1 +
 dstat_interface/core/interface/exp_int.py     | 20 +++++++++++++++-
 dstat_interface/core/interface/exp_window.py  |  4 +++-
 dstat_interface/main.py                       |  2 +-
 7 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/dstat_interface/core/dstat/comm.py b/dstat_interface/core/dstat/comm.py
index d78ea58..66903c8 100755
--- a/dstat_interface/core/dstat/comm.py
+++ b/dstat_interface/core/dstat/comm.py
@@ -192,7 +192,7 @@ class SerialConnection(GObject.Object):
         
     def get_proc(self, block=False):
         self.assert_connected()
-            
+
         if block is True:
             return self.proc_pipe_p.recv()
         else:
@@ -211,7 +211,7 @@ class SerialConnection(GObject.Object):
                 return self.ctrl_pipe_p.recv()
             else:
                 return None
-
+                
     def get_data(self, block=False):
         self.assert_connected()
         
diff --git a/dstat_interface/core/dstat/dfu.py b/dstat_interface/core/dstat/dfu.py
index 3b56288..101b9da 100755
--- a/dstat_interface/core/dstat/dfu.py
+++ b/dstat_interface/core/dstat/dfu.py
@@ -278,6 +278,7 @@ def test_firmware_version(current=None):
         logger.error('Unexpected git error. Git exited {}'.format(test))
         return False, None
 
+
 def dfu_program(path='./dstat-firmware.hex'):
     """Tries to program DStat over USB with DFU with hex file at path."""
     try:
diff --git a/dstat_interface/core/experiments/chronoamp.py b/dstat_interface/core/experiments/chronoamp.py
index 56417cd..bce5ecb 100644
--- a/dstat_interface/core/experiments/chronoamp.py
+++ b/dstat_interface/core/experiments/chronoamp.py
@@ -72,6 +72,7 @@ class Chronoamp(Experiment):
         except IndexError:
             pass
 
+
 class ManExp(Chronoamp):
     id = 'man'
     """Manual experiment"""
@@ -132,8 +133,8 @@ class ManExp(Chronoamp):
 
                 for line in self.serial:
                     check_ctrl()
-
-                    if line.startswith('B'):
+                    line_in = line.lstrip()
+                    if line_in.startswith('B'):
                         data = self.data_handler(
                             (scan, voltage, self.serial.read(size=self.databytes)))
                         data = self.data_postprocessing(data)
@@ -144,19 +145,22 @@ class ManExp(Chronoamp):
                         except AttributeError:  # Datapoint counting is optional
                             pass
 
-                    elif line.lstrip().startswith('S'):
+                    elif line_in.startswith('S'):
                         scan += 1
 
-                    elif line.lstrip().startswith('G'):
+                    elif line_in.startswith('G'):
                         self.gain = self.parameters["gain_table"][int(line.split(" ")[1])]
 
-                    elif line.lstrip().startswith('V'):
+                    elif line_in.startswith('V'):
                         voltage = int(line.split(" ")[1])
 
-                    elif line.lstrip().startswith("#"):
+                    elif line_in.startswith("#"):
                         dstat_logger.info(line.lstrip().rstrip())
 
-                    elif line.lstrip().startswith("@DONE"):
+                    elif line_in.startswith("R"):
+                        self.ctrl_pipe.send("R")
+
+                    elif line_in.startswith("@DONE"):
                         dstat_logger.debug(line.lstrip().rstrip())
                         time.sleep(.3)
                         return True
@@ -164,6 +168,10 @@ class ManExp(Chronoamp):
         except serial.SerialException:
             return False
 
+    def ctrl_loop(self, data):
+        if data == 'R':
+            self.emit('exp_ready')
+
     def data_handler(self, data_input):
         """Overrides Experiment method to not convert x axis to mV."""
         scan, voltage, data = data_input
@@ -181,6 +189,7 @@ class ManExp(Chronoamp):
         super(ManExp, self).experiment_done()
         for i in self.handler_ids:
             self.parameters['exp_window'].disconnect(i)
+        self.emit('exp_done')
 
 class ManBox(ChronoampBox):
     def redraw(self):
diff --git a/dstat_interface/core/experiments/experiment_template.py b/dstat_interface/core/experiments/experiment_template.py
index bd36ce5..9fab05d 100755
--- a/dstat_interface/core/experiments/experiment_template.py
+++ b/dstat_interface/core/experiments/experiment_template.py
@@ -69,6 +69,7 @@ class Experiment(GObject.Object):
         b'exp_ready': (GObject.SIGNAL_RUN_FIRST, None, ()),
         b'exp_done': (GObject.SIGNAL_RUN_FIRST, None, ())
     }
+
     def __init__(self, parameters):
         """Adds commands for gain and ADC."""
         super(Experiment, self).__init__()
diff --git a/dstat_interface/core/interface/exp_int.py b/dstat_interface/core/interface/exp_int.py
index 4a68475..6f26ece 100755
--- a/dstat_interface/core/interface/exp_int.py
+++ b/dstat_interface/core/interface/exp_int.py
@@ -214,7 +214,7 @@ class Manual(ExpInterface):
         self.window.set_vexpand(True)
 
         grid = Gtk.Grid(orientation=Gtk.Orientation.VERTICAL)
-        grid.set_column_homogeneous(False)
+        grid.set_column_homogeneous(True)
 
         entries = OrderedDict([
             ('voltage', 'Voltage (mV)'),
@@ -236,8 +236,26 @@ class Manual(ExpInterface):
         grid.attach(gain_button, 0, n, 2, 1)
         gain_button.connect('clicked', self.new_gain)
 
+        self.buttons = [voltage_button, gain_button]
+        for i in self.buttons:
+            i.set_sensitive(False)
+
         self.window.add(grid)
 
+    def exp_ready(self, widget):
+        for i in self.buttons:
+            i.set_sensitive(True)
+
+    def exp_done(self, widget):
+        for i in self.buttons:
+            i.set_sensitive(False)
+
+    def get_experiment(self, parameters):
+        exp = super(Manual, self).get_experiment(parameters)
+        exp.connect('exp_ready', self.exp_ready)
+        exp.connect('exp_done', self.exp_done)
+        return exp
+
     def new_voltage(self, widget):
         self.emit('new_voltage', int(self.entry['voltage'].get_text()))
 
diff --git a/dstat_interface/core/interface/exp_window.py b/dstat_interface/core/interface/exp_window.py
index 9f7f25a..9f977da 100755
--- a/dstat_interface/core/interface/exp_window.py
+++ b/dstat_interface/core/interface/exp_window.py
@@ -33,11 +33,13 @@ from . import exp_int
 
 logger = logging.getLogger(__name__)
 
+
 class Experiments(GObject.Object):
     __gsignals__ = {
         'run_utility': (GObject.SIGNAL_RUN_FIRST, None, ()),
         'done_utility': (GObject.SIGNAL_RUN_FIRST, None, ())
     }
+
     def __init__(self, builder):
         super(Experiments,self).__init__()
         self.builder = builder
@@ -96,7 +98,7 @@ class Experiments(GObject.Object):
                     exp.name)
                 )
         return exp.get_experiment(parameters)
-
+    
     def hide_exps(self):
         for key in self.containers:
             self.containers[key].hide()
diff --git a/dstat_interface/main.py b/dstat_interface/main.py
index ff49410..febd0ba 100755
--- a/dstat_interface/main.py
+++ b/dstat_interface/main.py
@@ -640,7 +640,7 @@ class Main(object):
                     self.current_exp.ctrl_loop(ctrl_buffer)
             except AttributeError:
                 pass
-            
+
             proc_buffer = dstat.state.ser.get_proc()
             if proc_buffer is not None:
                 if proc_buffer in ["DONE", "SERIAL_ERROR", "ABORT"]:
-- 
GitLab