Преглед изворни кода

Return to device list when device is unplugged

There was a bug where a device could end up being programmed with 0
current if the device was disconnected and reconnected while editing its
configuration.  I thought of several solutions for this, but concluded
that the right one would really be to go ahead with my original plan to
kick the user out of the configuration screen when the device was
unplugged.  As usual, the implementation could be better, but it works.
Clara Hobbs пре 7 година
родитељ
комит
5979920ce3
1 измењених фајлова са 42 додато и 21 уклоњено
  1. 42
    21
      pd-buddy-gtk.py

+ 42
- 21
pd-buddy-gtk.py Прегледај датотеку

@@ -9,7 +9,7 @@ gi.require_version('Gtk', '3.0')
9 9
 from gi.repository import Gtk, Gio, GObject, GLib
10 10
 
11 11
 
12
-def pdb_send_message(window, sp, message):
12
+def pdb_send_message(sp, message, window=None):
13 13
     """Send a message over the serial port and return the response"""
14 14
     try:
15 15
         # Open the serial port
@@ -25,12 +25,13 @@ def pdb_send_message(window, sp, message):
25 25
         answer = answer[1:-1]
26 26
         return answer
27 27
     except OSError as e:
28
-        dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
29
-                Gtk.ButtonsType.CLOSE, "Error communicating with device")
30
-        dialog.format_secondary_text(e.strerror)
31
-        dialog.run()
28
+        if window is not None:
29
+            dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
30
+                    Gtk.ButtonsType.CLOSE, "Error communicating with device")
31
+            dialog.format_secondary_text(e.strerror)
32
+            dialog.run()
32 33
 
33
-        dialog.destroy()
34
+            dialog.destroy()
34 35
         raise
35 36
 
36 37
 
@@ -107,10 +108,10 @@ class SelectList(Gtk.Box):
107 108
         self._builder.get_object("select-list").bind_model(model, func)
108 109
         self._model = model
109 110
 
110
-        self._reload()
111
-        GLib.timeout_add(1000, self._reload)
111
+        self.reload()
112
+        GLib.timeout_add(1000, self.reload)
112 113
 
113
-    def _reload(self):
114
+    def reload(self):
114 115
         self._model.update_items()
115 116
 
116 117
         # Set the visible child
@@ -149,7 +150,7 @@ class SelectListRow(Gtk.ListBoxRow):
149 150
     def on_identify_clicked(self, button):
150 151
         window = self.get_toplevel()
151 152
         try:
152
-            pdb_send_message(window, self.model.serport, 'identify')
153
+            pdb_send_message(self.model.serport, 'identify', window)
153 154
         except:
154 155
             return
155 156
 
@@ -161,18 +162,19 @@ class Handler:
161 162
         self.serial_port = None
162 163
         self.voltage = None
163 164
         self.current = None
165
+        self.selectlist = None
164 166
 
165 167
     def on_pdb_window_realize(self, *args):
166 168
         # Get the list
167 169
         sb = self.builder.get_object("select-box")
168
-        sl = SelectList()
169
-        sb.pack_start(sl, True, True, 0)
170
+        self.selectlist = SelectList()
171
+        sb.pack_start(self.selectlist, True, True, 0)
170 172
 
171 173
         liststore = SelectListStore()
172 174
 
173
-        sl.bind_model(liststore, SelectListRow)
175
+        self.selectlist.bind_model(liststore, SelectListRow)
174 176
 
175
-        sl.connect("row-activated", self.on_select_list_row_activated)
177
+        self.selectlist.connect("row-activated", self.on_select_list_row_activated)
176 178
 
177 179
     def on_pdb_window_delete_event(self, *args):
178 180
         Gtk.main_quit(*args)
@@ -186,8 +188,8 @@ class Handler:
186 188
 
187 189
         window = self.builder.get_object("pdb-window")
188 190
         try:
189
-            pdb_send_message(window, self.serial_port, 'load')
190
-            tmpcfg = pdb_send_message(window, self.serial_port, 'get_tmpcfg')
191
+            pdb_send_message(self.serial_port, 'load', window)
192
+            tmpcfg = pdb_send_message(self.serial_port, 'get_tmpcfg', window)
191 193
         except:
192 194
             return
193 195
 
@@ -220,6 +222,23 @@ class Handler:
220 222
         sink = self.builder.get_object("sink")
221 223
         st.set_visible_child(sink)
222 224
 
225
+        # Ping the Sink repeatedly
226
+        GLib.timeout_add(1000, self._ping)
227
+
228
+    def _ping(self):
229
+        """Ping the device we're configuring, showing to the list on failure"""
230
+        if self.serial_port is None:
231
+            self.selectlist.reload()
232
+            self.on_header_sink_back_clicked(None)
233
+            return False
234
+        try:
235
+            pdb_send_message(self.serial_port, '')
236
+            return True
237
+        except:
238
+            self.selectlist.reload()
239
+            self.on_header_sink_back_clicked(None)
240
+            return False
241
+
223 242
     def on_header_sink_back_clicked(self, data):
224 243
         self.serial_port = None
225 244
 
@@ -235,7 +254,7 @@ class Handler:
235 254
     def on_header_sink_save_clicked(self, button):
236 255
         window = self.builder.get_object("pdb-window")
237 256
         try:
238
-            pdb_send_message(window, self.serial_port, 'write')
257
+            pdb_send_message(self.serial_port, 'write', window)
239 258
 
240 259
             self._store_device_settings()
241 260
             self._set_save_button_visibility()
@@ -266,8 +285,9 @@ class Handler:
266 285
     def on_voltage_combobox_changed(self, combo):
267 286
         window = self.builder.get_object("pdb-window")
268 287
         try:
269
-            pdb_send_message(window, self.serial_port,
270
-                             'set_v {}'.format(int(combo.get_active_text())*1000))
288
+            pdb_send_message(self.serial_port,
289
+                             'set_v {}'.format(int(combo.get_active_text())*1000),
290
+                             window)
271 291
 
272 292
             self._set_save_button_visibility()
273 293
         except:
@@ -276,8 +296,9 @@ class Handler:
276 296
     def on_current_spinbutton_changed(self, spin):
277 297
         window = self.builder.get_object("pdb-window")
278 298
         try:
279
-            pdb_send_message(window, self.serial_port,
280
-                             'set_i {}'.format(int(spin.get_value()*1000)))
299
+            pdb_send_message(self.serial_port,
300
+                             'set_i {}'.format(int(spin.get_value()*1000)),
301
+                             window)
281 302
 
282 303
             self._set_save_button_visibility()
283 304
         except:

Loading…
Откажи
Сачувај