Переглянути джерело

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 роки тому
джерело
коміт
3d2fec1a77
2 змінених файлів з 49 додано та 66 видалено
  1. 1
    1
      README.md
  2. 48
    65
      pd-buddy-gtk.py

+ 1
- 1
README.md Переглянути файл

@@ -10,7 +10,7 @@ but older versions may work.
10 10
 * GTK+ 3.22
11 11
 * Python 3.6
12 12
 * python-gobject 3.22
13
-* pyserial 3.2
13
+* pd-buddy-python 0.1
14 14
 
15 15
 ## Usage
16 16
 

+ 48
- 65
pd-buddy-gtk.py Переглянути файл

@@ -2,37 +2,19 @@
2 2
 
3 3
 import sys
4 4
 
5
-import serial
6
-import serial.tools.list_ports
5
+import pdbuddy
7 6
 import gi
8 7
 gi.require_version('Gtk', '3.0')
9 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 20
 class ListRowModel(GObject.GObject):
@@ -46,7 +28,7 @@ class SelectListStore(Gio.ListStore):
46 28
 
47 29
     def update_items(self):
48 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 33
         # Mark ports to remove or add
52 34
         remove_list = []
@@ -151,8 +133,10 @@ class SelectListRow(Gtk.ListBoxRow):
151 133
     def on_identify_clicked(self, button):
152 134
         window = self.get_toplevel()
153 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 140
             return
157 141
 
158 142
 
@@ -191,33 +175,27 @@ class Handler:
191 175
 
192 176
         window = self.builder.get_object("pdb-window")
193 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 183
             return
198 184
 
185
+        # Set giveback button state
186
+        giveback.set_active(bool(tmpcfg.flags & pdbuddy.SinkFlags.GIVEBACK))
187
+
199 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 200
         self._store_device_settings()
223 201
         self._set_save_button_visibility()
@@ -243,7 +221,8 @@ class Handler:
243 221
             self.on_header_sink_back_clicked(None)
244 222
             return False
245 223
         try:
246
-            pdb_send_message(self.serial_port, '')
224
+            with pdbuddy.Sink(self.serial_port) as pdbs:
225
+                pdbs.send_command("")
247 226
             return True
248 227
         except:
249 228
             self.selectlist.reload()
@@ -265,11 +244,13 @@ class Handler:
265 244
     def on_header_sink_save_clicked(self, button):
266 245
         window = self.builder.get_object("pdb-window")
267 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 250
             self._store_device_settings()
271 251
             self._set_save_button_visibility()
272
-        except:
252
+        except OSError as e:
253
+            comms_error_dialog(window, e)
273 254
             self.on_header_sink_back_clicked(None)
274 255
 
275 256
     def _store_device_settings(self):
@@ -300,32 +281,34 @@ class Handler:
300 281
     def on_voltage_combobox_changed(self, combo):
301 282
         window = self.builder.get_object("pdb-window")
302 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 287
             self._set_save_button_visibility()
308
-        except:
288
+        except OSError as e:
289
+            comms_error_dialog(window, e)
309 290
             self.on_header_sink_back_clicked(None)
310 291
 
311 292
     def on_current_spinbutton_changed(self, spin):
312 293
         window = self.builder.get_object("pdb-window")
313 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 298
             self._set_save_button_visibility()
319
-        except:
299
+        except OSError as e:
300
+            comms_error_dialog(window, e)
320 301
             self.on_header_sink_back_clicked(None)
321 302
 
322 303
     def on_giveback_toggle_toggled(self, toggle):
323 304
         window = self.builder.get_object("pdb-window")
324 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 309
             self._set_save_button_visibility()
328
-        except:
310
+        except OSError as e:
311
+            comms_error_dialog(window, e)
329 312
             self.on_header_sink_back_clicked(None)
330 313
 
331 314
 

Завантаження…
Відмінити
Зберегти