Browse Source

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 years ago
parent
commit
5979920ce3
1 changed files with 42 additions and 21 deletions
  1. 42
    21
      pd-buddy-gtk.py

+ 42
- 21
pd-buddy-gtk.py View File

9
 from gi.repository import Gtk, Gio, GObject, GLib
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
     """Send a message over the serial port and return the response"""
13
     """Send a message over the serial port and return the response"""
14
     try:
14
     try:
15
         # Open the serial port
15
         # Open the serial port
25
         answer = answer[1:-1]
25
         answer = answer[1:-1]
26
         return answer
26
         return answer
27
     except OSError as e:
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
         raise
35
         raise
35
 
36
 
36
 
37
 
107
         self._builder.get_object("select-list").bind_model(model, func)
108
         self._builder.get_object("select-list").bind_model(model, func)
108
         self._model = model
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
         self._model.update_items()
115
         self._model.update_items()
115
 
116
 
116
         # Set the visible child
117
         # Set the visible child
149
     def on_identify_clicked(self, button):
150
     def on_identify_clicked(self, button):
150
         window = self.get_toplevel()
151
         window = self.get_toplevel()
151
         try:
152
         try:
152
-            pdb_send_message(window, self.model.serport, 'identify')
153
+            pdb_send_message(self.model.serport, 'identify', window)
153
         except:
154
         except:
154
             return
155
             return
155
 
156
 
161
         self.serial_port = None
162
         self.serial_port = None
162
         self.voltage = None
163
         self.voltage = None
163
         self.current = None
164
         self.current = None
165
+        self.selectlist = None
164
 
166
 
165
     def on_pdb_window_realize(self, *args):
167
     def on_pdb_window_realize(self, *args):
166
         # Get the list
168
         # Get the list
167
         sb = self.builder.get_object("select-box")
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
         liststore = SelectListStore()
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
     def on_pdb_window_delete_event(self, *args):
179
     def on_pdb_window_delete_event(self, *args):
178
         Gtk.main_quit(*args)
180
         Gtk.main_quit(*args)
186
 
188
 
187
         window = self.builder.get_object("pdb-window")
189
         window = self.builder.get_object("pdb-window")
188
         try:
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
         except:
193
         except:
192
             return
194
             return
193
 
195
 
220
         sink = self.builder.get_object("sink")
222
         sink = self.builder.get_object("sink")
221
         st.set_visible_child(sink)
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
     def on_header_sink_back_clicked(self, data):
242
     def on_header_sink_back_clicked(self, data):
224
         self.serial_port = None
243
         self.serial_port = None
225
 
244
 
235
     def on_header_sink_save_clicked(self, button):
254
     def on_header_sink_save_clicked(self, button):
236
         window = self.builder.get_object("pdb-window")
255
         window = self.builder.get_object("pdb-window")
237
         try:
256
         try:
238
-            pdb_send_message(window, self.serial_port, 'write')
257
+            pdb_send_message(self.serial_port, 'write', window)
239
 
258
 
240
             self._store_device_settings()
259
             self._store_device_settings()
241
             self._set_save_button_visibility()
260
             self._set_save_button_visibility()
266
     def on_voltage_combobox_changed(self, combo):
285
     def on_voltage_combobox_changed(self, combo):
267
         window = self.builder.get_object("pdb-window")
286
         window = self.builder.get_object("pdb-window")
268
         try:
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
             self._set_save_button_visibility()
292
             self._set_save_button_visibility()
273
         except:
293
         except:
276
     def on_current_spinbutton_changed(self, spin):
296
     def on_current_spinbutton_changed(self, spin):
277
         window = self.builder.get_object("pdb-window")
297
         window = self.builder.get_object("pdb-window")
278
         try:
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
             self._set_save_button_visibility()
303
             self._set_save_button_visibility()
283
         except:
304
         except:

Loading…
Cancel
Save