GTK+ GUI for configuring PD Buddy Sink devices
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

pd-buddy-gtk.py 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/usr/bin/env python3
  2. import sys
  3. import serial
  4. import serial.tools.list_ports
  5. import gi
  6. gi.require_version('Gtk', '3.0')
  7. from gi.repository import Gtk, Gio, GObject, GLib
  8. def pdb_send_message(window, sp, message):
  9. """Send a message over the serial port and return the response"""
  10. try:
  11. # Open the serial port
  12. sp = serial.Serial(sp.device, baudrate=115200, timeout=0.01)
  13. sp.write(bytes(message, 'utf-8') + b'\r\n')
  14. sp.flush()
  15. answer = sp.readlines()
  16. sp.close()
  17. # Remove the echoed command and prompt
  18. answer = answer[1:-1]
  19. return answer
  20. except OSError as e:
  21. dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
  22. Gtk.ButtonsType.CLOSE, "Error communicating with device")
  23. dialog.format_secondary_text(e.strerror)
  24. dialog.run()
  25. dialog.destroy()
  26. raise
  27. class ListRowModel(GObject.GObject):
  28. def __init__(self, serport):
  29. GObject.GObject.__init__(self)
  30. self.serport = serport
  31. class SelectListStore(Gio.ListStore):
  32. def update_items(self):
  33. # Get a list of serial ports
  34. serports = list(serial.tools.list_ports.grep("1209:0001"))
  35. # Mark ports to remove or add
  36. remove_list = []
  37. list_len = self.get_n_items()
  38. for i in range(list_len):
  39. remove = True
  40. for j in range(len(serports)):
  41. if serports[j] is not None and self.get_item(i).serport == serports[j]:
  42. serports[j] = None
  43. remove = False
  44. if remove:
  45. remove_list.append(i)
  46. # Remove the missing ones
  47. for i in remove_list:
  48. self.remove(i)
  49. # Add any new ports
  50. for port in serports:
  51. if port is not None:
  52. self.append(ListRowModel(port))
  53. class SelectList(Gtk.Box):
  54. __gsignals__ = {
  55. 'row-activated': (GObject.SIGNAL_RUN_FIRST, None,
  56. (object,))
  57. }
  58. def __init__(self):
  59. Gtk.Box.__init__(self)
  60. self._model = None
  61. self._builder = Gtk.Builder()
  62. self._builder.add_from_file("data/select-stack.ui")
  63. self._builder.connect_signals(self)
  64. sl = self._builder.get_object("select-list")
  65. # Add separators to the list
  66. sl.set_header_func(self._update_header_func, None)
  67. self.pack_start(self._builder.get_object("select-stack"), True, True, 0)
  68. self.show_all()
  69. def _update_header_func(self, row, before, data):
  70. """Add a separator header to all rows but the first one"""
  71. if before is None:
  72. row.set_header(None)
  73. return
  74. current = row.get_header()
  75. if current is None:
  76. current = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)
  77. row.set_header(current)
  78. def bind_model(self, model, func):
  79. self._builder.get_object("select-list").bind_model(model, func)
  80. self._model = model
  81. self._reload()
  82. GLib.timeout_add(1000, self._reload)
  83. def _reload(self):
  84. self._model.update_items()
  85. # Set the visible child
  86. stack = self._builder.get_object("select-stack")
  87. if self._model.get_n_items():
  88. stack.set_visible_child(self._builder.get_object("select-frame"))
  89. else:
  90. stack.set_visible_child(self._builder.get_object("select-none"))
  91. return True
  92. def on_select_list_row_activated(self, box, row):
  93. self.emit("row-activated", row.model.serport)
  94. class SelectListRow(Gtk.ListBoxRow):
  95. def __init__(self, model):
  96. Gtk.EventBox.__init__(self)
  97. self.model = model
  98. self._builder = Gtk.Builder()
  99. self._builder.add_from_file("data/select-list-row.ui")
  100. self._builder.connect_signals(self)
  101. name = self._builder.get_object("name")
  102. name.set_text(self.model.serport.description)
  103. device = self._builder.get_object("device")
  104. device.set_text(self.model.serport.device)
  105. self.add(self._builder.get_object("grid"))
  106. self.show_all()
  107. def on_identify_clicked(self, button):
  108. window = self.get_toplevel()
  109. try:
  110. pdb_send_message(window, self.model.serport, 'identify')
  111. except:
  112. return
  113. class Handler:
  114. def __init__(self, builder):
  115. self.builder = builder
  116. self.serial_port = None
  117. self.voltage = None
  118. self.current = None
  119. def on_pdb_window_realize(self, *args):
  120. # Get the list
  121. sb = self.builder.get_object("select-box")
  122. sl = SelectList()
  123. sb.pack_start(sl, True, True, 0)
  124. liststore = SelectListStore()
  125. sl.bind_model(liststore, SelectListRow)
  126. sl.connect("row-activated", self.on_select_list_row_activated)
  127. def on_pdb_window_delete_event(self, *args):
  128. Gtk.main_quit(*args)
  129. def on_select_list_row_activated(self, selectlist, serport):
  130. # Get voltage and current widgets
  131. voltage = self.builder.get_object("voltage-combobox")
  132. current = self.builder.get_object("current-spinbutton")
  133. self.serial_port = serport
  134. window = self.builder.get_object("pdb-window")
  135. try:
  136. pdb_send_message(window, self.serial_port, 'load')
  137. tmpcfg = pdb_send_message(window, self.serial_port, 'get_tmpcfg')
  138. except:
  139. return
  140. # Get voltage and current from device and load them into the GUI
  141. for line in tmpcfg:
  142. if line.startswith(b'v:'):
  143. v = line.split()[1]
  144. if v == b'5.00':
  145. voltage.set_active_id('voltage-five')
  146. elif v == b'9.00':
  147. voltage.set_active_id('voltage-nine')
  148. elif v == b'15.00':
  149. voltage.set_active_id('voltage-fifteen')
  150. if v == b'20.00':
  151. voltage.set_active_id('voltage-twenty')
  152. elif line.startswith(b'i:'):
  153. i = float(line.split()[1])
  154. current.set_value(i)
  155. self._store_device_settings()
  156. self._set_save_button_visibility()
  157. # Show the Sink page
  158. hst = self.builder.get_object("header-stack")
  159. hsink = self.builder.get_object("header-sink")
  160. hsink.set_subtitle(serport.device)
  161. hst.set_visible_child(hsink)
  162. st = self.builder.get_object("stack")
  163. sink = self.builder.get_object("sink")
  164. st.set_visible_child(sink)
  165. def on_header_sink_back_clicked(self, data):
  166. self.serial_port = None
  167. # Show the Select page
  168. hst = self.builder.get_object("header-stack")
  169. hselect = self.builder.get_object("header-select")
  170. hst.set_visible_child(hselect)
  171. st = self.builder.get_object("stack")
  172. select = self.builder.get_object("select")
  173. st.set_visible_child(select)
  174. def on_header_sink_save_clicked(self, button):
  175. window = self.builder.get_object("pdb-window")
  176. try:
  177. pdb_send_message(window, self.serial_port, 'write')
  178. self._store_device_settings()
  179. self._set_save_button_visibility()
  180. except:
  181. self.on_header_sink_back_clicked(None)
  182. def _store_device_settings(self):
  183. """Store the settings that were loaded from the device"""
  184. # Get voltage and current widgets
  185. voltage = self.builder.get_object("voltage-combobox")
  186. current = self.builder.get_object("current-spinbutton")
  187. # Remember the loaded settings
  188. self.voltage = voltage.get_active_id()
  189. self.current = current.get_value()
  190. def _set_save_button_visibility(self):
  191. """Show the save button if there are new settings to save"""
  192. # Get relevant widgets
  193. voltage = self.builder.get_object("voltage-combobox")
  194. current = self.builder.get_object("current-spinbutton")
  195. rev = self.builder.get_object("header-sink-save-revealer")
  196. # Set visibility
  197. rev.set_reveal_child(voltage.get_active_id() != self.voltage
  198. or current.get_value() != self.current)
  199. def on_voltage_combobox_changed(self, combo):
  200. window = self.builder.get_object("pdb-window")
  201. try:
  202. pdb_send_message(window, self.serial_port,
  203. 'set_v {}'.format(int(combo.get_active_text())*1000))
  204. self._set_save_button_visibility()
  205. except:
  206. self.on_header_sink_back_clicked(None)
  207. def on_current_spinbutton_changed(self, spin):
  208. window = self.builder.get_object("pdb-window")
  209. try:
  210. pdb_send_message(window, self.serial_port,
  211. 'set_i {}'.format(int(spin.get_value()*1000)))
  212. self._set_save_button_visibility()
  213. except:
  214. self.on_header_sink_back_clicked(None)
  215. class Application(Gtk.Application):
  216. def __init__(self, *args, **kwargs):
  217. super().__init__(*args, application_id="com.clayhobbs.pd-buddy-gtk",
  218. **kwargs)
  219. self.window = None
  220. def do_startup(self):
  221. Gtk.Application.do_startup(self)
  222. self.builder = Gtk.Builder()
  223. self.builder.add_from_file("data/pd-buddy-gtk.ui")
  224. self.builder.connect_signals(Handler(self.builder))
  225. def do_activate(self):
  226. # We only allow a single window and raise any existing ones
  227. if not self.window:
  228. # Windows are associated with the application
  229. # when the last one is closed the application shuts down
  230. self.window = self.builder.get_object("pdb-window")
  231. self.add_window(self.window)
  232. self.window.set_wmclass("PD Buddy Configuration",
  233. "PD Buddy Configuration")
  234. self.window.present()
  235. if __name__ == "__main__":
  236. app = Application()
  237. app.run(sys.argv)