Browse Source

Use pd-buddy-python instead of pySerial directly

The program structure is no better yet, but it uses the slick new
library I made.
Clara Hobbs 7 years ago
parent
commit
3d2fec1a77
2 changed files with 49 additions and 66 deletions
  1. 1
    1
      README.md
  2. 48
    65
      pd-buddy-gtk.py

+ 1
- 1
README.md View File

10
 * GTK+ 3.22
10
 * GTK+ 3.22
11
 * Python 3.6
11
 * Python 3.6
12
 * python-gobject 3.22
12
 * python-gobject 3.22
13
-* pyserial 3.2
13
+* pd-buddy-python 0.1
14
 
14
 
15
 ## Usage
15
 ## Usage
16
 
16
 

+ 48
- 65
pd-buddy-gtk.py View File

2
 
2
 
3
 import sys
3
 import sys
4
 
4
 
5
-import serial
6
-import serial.tools.list_ports
5
+import pdbuddy
7
 import gi
6
 import gi
8
 gi.require_version('Gtk', '3.0')
7
 gi.require_version('Gtk', '3.0')
9
 from gi.repository import Gtk, Gio, GObject, GLib
8
 from gi.repository import Gtk, Gio, GObject, GLib
10
 
9
 
11
 
10
 
12
-def pdb_send_message(sp, message, window=None):
13
-    """Send a message over the serial port and return the response"""
14
-    try:
15
-        # Open the serial port
16
-        sp = serial.Serial(sp.device, baudrate=115200, timeout=0.01)
11
+def comms_error_dialog(parent, e):
12
+    dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
13
+            Gtk.ButtonsType.CLOSE, "Error communicating with device")
14
+    dialog.format_secondary_text(e.strerror)
15
+    dialog.run()
17
 
16
 
18
-        sp.write(bytes(message, 'utf-8') + b'\r\n')
19
-        sp.flush()
20
-        answer = sp.readlines()
21
-
22
-        sp.close()
23
-
24
-        # Remove the echoed command and prompt
25
-        answer = answer[1:-1]
26
-        return answer
27
-    except OSError as e:
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()
33
-
34
-            dialog.destroy()
35
-        raise
17
+    dialog.destroy()
36
 
18
 
37
 
19
 
38
 class ListRowModel(GObject.GObject):
20
 class ListRowModel(GObject.GObject):
46
 
28
 
47
     def update_items(self):
29
     def update_items(self):
48
         # Get a list of serial ports
30
         # Get a list of serial ports
49
-        serports = list(serial.tools.list_ports.grep("1209:0001"))
31
+        serports = list(pdbuddy.Sink.get_devices())
50
 
32
 
51
         # Mark ports to remove or add
33
         # Mark ports to remove or add
52
         remove_list = []
34
         remove_list = []
151
     def on_identify_clicked(self, button):
133
     def on_identify_clicked(self, button):
152
         window = self.get_toplevel()
134
         window = self.get_toplevel()
153
         try:
135
         try:
154
-            pdb_send_message(self.model.serport, 'identify', window)
155
-        except:
136
+            with pdbuddy.Sink(self.model.serport) as pdbs:
137
+                pdbs.identify()
138
+        except OSError as e:
139
+            comms_error_dialog(window, e)
156
             return
140
             return
157
 
141
 
158
 
142
 
191
 
175
 
192
         window = self.builder.get_object("pdb-window")
176
         window = self.builder.get_object("pdb-window")
193
         try:
177
         try:
194
-            pdb_send_message(self.serial_port, 'load', window)
195
-            tmpcfg = pdb_send_message(self.serial_port, 'get_tmpcfg', window)
196
-        except:
178
+            with pdbuddy.Sink(self.serial_port) as pdbs:
179
+                pdbs.load()
180
+                tmpcfg = pdbs.get_tmpcfg()
181
+        except OSError as e:
182
+            comms_error_dialog(window, e)
197
             return
183
             return
198
 
184
 
185
+        # Set giveback button state
186
+        giveback.set_active(bool(tmpcfg.flags & pdbuddy.SinkFlags.GIVEBACK))
187
+
199
         # Get voltage and current from device and load them into the GUI
188
         # Get voltage and current from device and load them into the GUI
200
-        for line in tmpcfg:
201
-            if line.startswith(b'flags:'):
202
-                line = line.split()[1:]
203
-                try:
204
-                    line.index(b'GiveBack')
205
-                    giveback.set_active(True)
206
-                except:
207
-                    giveback.set_active(False)
208
-            elif line.startswith(b'v:'):
209
-                v = line.split()[1]
210
-                if v == b'5.00':
211
-                    voltage.set_active_id('voltage-five')
212
-                elif v == b'9.00':
213
-                    voltage.set_active_id('voltage-nine')
214
-                elif v == b'15.00':
215
-                    voltage.set_active_id('voltage-fifteen')
216
-                if v == b'20.00':
217
-                    voltage.set_active_id('voltage-twenty')
218
-            elif line.startswith(b'i:'):
219
-                i = float(line.split()[1])
220
-                current.set_value(i)
189
+        if tmpcfg.v == 5000:
190
+            voltage.set_active_id('voltage-five')
191
+        elif tmpcfg.v == 9000:
192
+            voltage.set_active_id('voltage-nine')
193
+        elif tmpcfg.v == 15000:
194
+            voltage.set_active_id('voltage-fifteen')
195
+        if tmpcfg.v == 20000:
196
+            voltage.set_active_id('voltage-twenty')
197
+
198
+        current.set_value(tmpcfg.i/1000)
221
 
199
 
222
         self._store_device_settings()
200
         self._store_device_settings()
223
         self._set_save_button_visibility()
201
         self._set_save_button_visibility()
243
             self.on_header_sink_back_clicked(None)
221
             self.on_header_sink_back_clicked(None)
244
             return False
222
             return False
245
         try:
223
         try:
246
-            pdb_send_message(self.serial_port, '')
224
+            with pdbuddy.Sink(self.serial_port) as pdbs:
225
+                pdbs.send_command("")
247
             return True
226
             return True
248
         except:
227
         except:
249
             self.selectlist.reload()
228
             self.selectlist.reload()
265
     def on_header_sink_save_clicked(self, button):
244
     def on_header_sink_save_clicked(self, button):
266
         window = self.builder.get_object("pdb-window")
245
         window = self.builder.get_object("pdb-window")
267
         try:
246
         try:
268
-            pdb_send_message(self.serial_port, 'write', window)
247
+            with pdbuddy.Sink(self.serial_port) as pdbs:
248
+                pdbs.write()
269
 
249
 
270
             self._store_device_settings()
250
             self._store_device_settings()
271
             self._set_save_button_visibility()
251
             self._set_save_button_visibility()
272
-        except:
252
+        except OSError as e:
253
+            comms_error_dialog(window, e)
273
             self.on_header_sink_back_clicked(None)
254
             self.on_header_sink_back_clicked(None)
274
 
255
 
275
     def _store_device_settings(self):
256
     def _store_device_settings(self):
300
     def on_voltage_combobox_changed(self, combo):
281
     def on_voltage_combobox_changed(self, combo):
301
         window = self.builder.get_object("pdb-window")
282
         window = self.builder.get_object("pdb-window")
302
         try:
283
         try:
303
-            pdb_send_message(self.serial_port,
304
-                             'set_v {}'.format(int(combo.get_active_text())*1000),
305
-                             window)
284
+            with pdbuddy.Sink(self.serial_port) as pdbs:
285
+                pdbs.set_v(int(combo.get_active_text())*1000)
306
 
286
 
307
             self._set_save_button_visibility()
287
             self._set_save_button_visibility()
308
-        except:
288
+        except OSError as e:
289
+            comms_error_dialog(window, e)
309
             self.on_header_sink_back_clicked(None)
290
             self.on_header_sink_back_clicked(None)
310
 
291
 
311
     def on_current_spinbutton_changed(self, spin):
292
     def on_current_spinbutton_changed(self, spin):
312
         window = self.builder.get_object("pdb-window")
293
         window = self.builder.get_object("pdb-window")
313
         try:
294
         try:
314
-            pdb_send_message(self.serial_port,
315
-                             'set_i {}'.format(int(spin.get_value()*1000)),
316
-                             window)
295
+            with pdbuddy.Sink(self.serial_port) as pdbs:
296
+                pdbs.set_i(int(spin.get_value())*1000)
317
 
297
 
318
             self._set_save_button_visibility()
298
             self._set_save_button_visibility()
319
-        except:
299
+        except OSError as e:
300
+            comms_error_dialog(window, e)
320
             self.on_header_sink_back_clicked(None)
301
             self.on_header_sink_back_clicked(None)
321
 
302
 
322
     def on_giveback_toggle_toggled(self, toggle):
303
     def on_giveback_toggle_toggled(self, toggle):
323
         window = self.builder.get_object("pdb-window")
304
         window = self.builder.get_object("pdb-window")
324
         try:
305
         try:
325
-            pdb_send_message(self.serial_port, 'toggle_giveback', window)
306
+            with pdbuddy.Sink(self.serial_port) as pdbs:
307
+                pdbs.toggle_giveback()
326
 
308
 
327
             self._set_save_button_visibility()
309
             self._set_save_button_visibility()
328
-        except:
310
+        except OSError as e:
311
+            comms_error_dialog(window, e)
329
             self.on_header_sink_back_clicked(None)
312
             self.on_header_sink_back_clicked(None)
330
 
313
 
331
 
314
 

Loading…
Cancel
Save