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 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #!/usr/bin/env python3
  2. import sys
  3. import pdbuddy
  4. import gi
  5. gi.require_version('Gtk', '3.0')
  6. from gi.repository import Gtk, Gio, GObject, GLib
  7. def comms_error_dialog(parent, e):
  8. dialog = Gtk.MessageDialog(window, 0, Gtk.MessageType.ERROR,
  9. Gtk.ButtonsType.CLOSE, "Error communicating with device")
  10. dialog.format_secondary_text(e.strerror)
  11. dialog.run()
  12. dialog.destroy()
  13. class ListRowModel(GObject.GObject):
  14. def __init__(self, serport):
  15. GObject.GObject.__init__(self)
  16. self.serport = serport
  17. class SelectListStore(Gio.ListStore):
  18. def update_items(self):
  19. # Get a list of serial ports
  20. serports = list(pdbuddy.Sink.get_devices())
  21. # Mark ports to remove or add
  22. remove_list = []
  23. list_len = self.get_n_items()
  24. for i in range(list_len):
  25. remove = True
  26. for j in range(len(serports)):
  27. if serports[j] is not None and self.get_item(i).serport == serports[j]:
  28. serports[j] = None
  29. remove = False
  30. if remove:
  31. remove_list.append(i)
  32. # Remove the missing ones
  33. for i in remove_list:
  34. self.remove(i)
  35. # Add any new ports
  36. for port in serports:
  37. if port is not None:
  38. self.append(ListRowModel(port))
  39. def list_box_update_header_func(row, before, data):
  40. """Add a separator header to all rows but the first one"""
  41. if before is None:
  42. row.set_header(None)
  43. return
  44. current = row.get_header()
  45. if current is None:
  46. current = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)
  47. row.set_header(current)
  48. class SelectList(Gtk.Box):
  49. __gsignals__ = {
  50. 'row-activated': (GObject.SIGNAL_RUN_FIRST, None,
  51. (object,))
  52. }
  53. def __init__(self):
  54. Gtk.Box.__init__(self)
  55. self._model = None
  56. self._builder = Gtk.Builder()
  57. self._builder.add_from_file("data/select-stack.ui")
  58. self._builder.connect_signals(self)
  59. sl = self._builder.get_object("select-list")
  60. # Add separators to the list
  61. sl.set_header_func(list_box_update_header_func, None)
  62. self.pack_start(self._builder.get_object("select-stack"), True, True, 0)
  63. self.show_all()
  64. def bind_model(self, model, func):
  65. self._builder.get_object("select-list").bind_model(model, func)
  66. self._model = model
  67. self.reload()
  68. GLib.timeout_add(1000, self.reload)
  69. def reload(self):
  70. self._model.update_items()
  71. # Set the visible child
  72. stack = self._builder.get_object("select-stack")
  73. if self._model.get_n_items():
  74. stack.set_visible_child(self._builder.get_object("select-frame"))
  75. else:
  76. stack.set_visible_child(self._builder.get_object("select-none"))
  77. return True
  78. def on_select_list_row_activated(self, box, row):
  79. self.emit("row-activated", row.model.serport)
  80. class SelectListRow(Gtk.ListBoxRow):
  81. def __init__(self, model):
  82. Gtk.EventBox.__init__(self)
  83. self.model = model
  84. self._builder = Gtk.Builder()
  85. self._builder.add_from_file("data/select-list-row.ui")
  86. self._builder.connect_signals(self)
  87. name = self._builder.get_object("name")
  88. name.set_text('{} {} {}'.format(self.model.serport.manufacturer,
  89. self.model.serport.product,
  90. self.model.serport.serial_number))
  91. device = self._builder.get_object("device")
  92. device.set_text(self.model.serport.device)
  93. self.add(self._builder.get_object("grid"))
  94. self.show_all()
  95. def on_identify_clicked(self, button):
  96. window = self.get_toplevel()
  97. try:
  98. with pdbuddy.Sink(self.model.serport) as pdbs:
  99. pdbs.identify()
  100. except OSError as e:
  101. comms_error_dialog(window, e)
  102. return
  103. class Handler:
  104. def __init__(self, builder):
  105. self.builder = builder
  106. self.serial_port = None
  107. self.voltage = None
  108. self.current = None
  109. self.giveback = None
  110. self.selectlist = None
  111. def on_pdb_window_realize(self, *args):
  112. # Get the list
  113. sb = self.builder.get_object("select-box")
  114. self.selectlist = SelectList()
  115. sb.pack_start(self.selectlist, True, True, 0)
  116. liststore = SelectListStore()
  117. self.selectlist.bind_model(liststore, SelectListRow)
  118. self.selectlist.connect("row-activated", self.on_select_list_row_activated)
  119. # Add separators to the configuration page lists
  120. sc_list = self.builder.get_object("sink-config-list")
  121. sc_list.set_header_func(list_box_update_header_func, None)
  122. pd_list = self.builder.get_object("power-delivery-list")
  123. pd_list.set_header_func(list_box_update_header_func, None)
  124. def on_pdb_window_delete_event(self, *args):
  125. Gtk.main_quit(*args)
  126. def on_select_list_row_activated(self, selectlist, serport):
  127. # Get relevant widgets
  128. voltage = self.builder.get_object("voltage-combobox")
  129. current = self.builder.get_object("current-spinbutton")
  130. giveback = self.builder.get_object("giveback-toggle")
  131. pd_frame = self.builder.get_object("power-delivery-frame")
  132. output = self.builder.get_object("output-switch")
  133. cap_label = self.builder.get_object("short-source-cap-label")
  134. self.serial_port = serport
  135. window = self.builder.get_object("pdb-window")
  136. try:
  137. with pdbuddy.Sink(self.serial_port) as pdbs:
  138. try:
  139. pdbs.load()
  140. except KeyError:
  141. # If there's no configuration, we don't want to fail. We
  142. # do want to display no configuration though
  143. self.cfg = pdbuddy.SinkConfig(
  144. status=pdbuddy.SinkStatus.VALID,
  145. flags=pdbuddy.SinkFlags.NONE, v=0, i=0)
  146. else:
  147. self.cfg = pdbs.get_tmpcfg()
  148. except OSError as e:
  149. comms_error_dialog(window, e)
  150. return
  151. self._store_device_settings()
  152. self._set_save_button_visibility()
  153. # Set giveback button state
  154. giveback.set_active(bool(self.cfg.flags & pdbuddy.SinkFlags.GIVEBACK))
  155. # Get voltage and current from device and load them into the GUI
  156. if self.cfg.v == 5000:
  157. voltage.set_active_id('voltage-five')
  158. elif self.cfg.v == 9000:
  159. voltage.set_active_id('voltage-nine')
  160. elif self.cfg.v == 12000:
  161. voltage.set_active_id('voltage-twelve')
  162. elif self.cfg.v == 15000:
  163. voltage.set_active_id('voltage-fifteen')
  164. if self.cfg.v == 20000:
  165. voltage.set_active_id('voltage-twenty')
  166. current.set_value(self.cfg.i/1000)
  167. # Set PD frame visibility and output switch state
  168. try:
  169. with pdbuddy.Sink(self.serial_port) as pdbs:
  170. output.set_state(pdbs.output)
  171. except KeyError:
  172. pd_frame.set_visible(False)
  173. else:
  174. pd_frame.set_visible(True)
  175. # Update the text in the capability label
  176. # TODO: do this repeatedly
  177. with pdbuddy.Sink(self.serial_port) as pdbs:
  178. caps = pdbs.get_source_cap()
  179. cap_label.set_text('{} PDOs'.format(len(caps)))
  180. # Show the Sink page
  181. hst = self.builder.get_object("header-stack")
  182. hsink = self.builder.get_object("header-sink")
  183. hsink.set_title('{} {} {}'.format(serport.manufacturer,
  184. serport.product,
  185. serport.serial_number))
  186. hsink.set_subtitle(serport.device)
  187. hst.set_visible_child(hsink)
  188. st = self.builder.get_object("stack")
  189. sink = self.builder.get_object("sink")
  190. st.set_visible_child(sink)
  191. # Ping the Sink repeatedly
  192. GLib.timeout_add(1000, self._ping)
  193. def _ping(self):
  194. """Ping the device we're configuring, showing to the list on failure"""
  195. if self.serial_port is None:
  196. self.selectlist.reload()
  197. self.on_header_sink_back_clicked(None)
  198. return False
  199. try:
  200. with pdbuddy.Sink(self.serial_port) as pdbs:
  201. pdbs.send_command("")
  202. return True
  203. except:
  204. self.selectlist.reload()
  205. self.on_header_sink_back_clicked(None)
  206. return False
  207. def on_header_sink_back_clicked(self, data):
  208. self.serial_port = None
  209. # Show the Select page
  210. hst = self.builder.get_object("header-stack")
  211. hselect = self.builder.get_object("header-select")
  212. hst.set_visible_child(hselect)
  213. st = self.builder.get_object("stack")
  214. select = self.builder.get_object("select")
  215. st.set_visible_child(select)
  216. def on_sink_save_clicked(self, button):
  217. window = self.builder.get_object("pdb-window")
  218. try:
  219. with pdbuddy.Sink(self.serial_port) as pdbs:
  220. pdbs.set_tmpcfg(self.cfg)
  221. pdbs.write()
  222. self._store_device_settings()
  223. self._set_save_button_visibility()
  224. except OSError as e:
  225. comms_error_dialog(window, e)
  226. self.on_header_sink_back_clicked(None)
  227. def _store_device_settings(self):
  228. """Store the settings that were loaded from the device"""
  229. self.cfg_clean = self.cfg
  230. def _set_save_button_visibility(self):
  231. """Show the save button if there are new settings to save"""
  232. # Get relevant widgets
  233. rev = self.builder.get_object("sink-save-revealer")
  234. # Set visibility
  235. rev.set_reveal_child(self.cfg != self.cfg_clean)
  236. def on_voltage_combobox_changed(self, combo):
  237. self.cfg = self.cfg._replace(v=int(combo.get_active_text()) * 1000)
  238. self._set_save_button_visibility()
  239. def on_current_spinbutton_changed(self, spin):
  240. self.cfg = self.cfg._replace(i=int(spin.get_value() * 1000))
  241. self._set_save_button_visibility()
  242. def on_giveback_toggle_toggled(self, toggle):
  243. if toggle.get_active():
  244. self.cfg = self.cfg._replace(flags=self.cfg.flags|pdbuddy.SinkFlags.GIVEBACK)
  245. else:
  246. self.cfg = self.cfg._replace(flags=self.cfg.flags&~pdbuddy.SinkFlags.GIVEBACK)
  247. self._set_save_button_visibility()
  248. def on_output_switch_state_set(self, switch, state):
  249. with pdbuddy.Sink(self.serial_port) as pdbs:
  250. pdbs.output = state
  251. def on_source_cap_row_activated(self, box, row):
  252. # Find which row was clicked
  253. sc_row = self.builder.get_object("source-cap-row")
  254. if row != sc_row:
  255. # If it's not the source-cap-row, leave
  256. return
  257. # Get the source capabilities
  258. with pdbuddy.Sink(self.serial_port) as pdbs:
  259. caps = pdbs.get_source_cap()
  260. s = ""
  261. for i, cap in enumerate(caps):
  262. s += "PDO {}: {}\n".format(i+1, cap)
  263. flags = Gtk.DialogFlags.DESTROY_WITH_PARENT;
  264. window = self.builder.get_object("pdb-window")
  265. dialog = Gtk.MessageDialog(window,
  266. flags,
  267. Gtk.MessageType.INFO,
  268. Gtk.ButtonsType.CLOSE,
  269. None)
  270. dialog.set_markup("<span font_family='monospace'>{}</span>".format(s[:-1]))
  271. dialog.run()
  272. dialog.destroy()
  273. class Application(Gtk.Application):
  274. def __init__(self, *args, **kwargs):
  275. super().__init__(*args, application_id="com.clayhobbs.pd-buddy-gtk",
  276. **kwargs)
  277. self.window = None
  278. def do_startup(self):
  279. Gtk.Application.do_startup(self)
  280. self.builder = Gtk.Builder()
  281. self.builder.add_from_file("data/pd-buddy-gtk.ui")
  282. self.builder.connect_signals(Handler(self.builder))
  283. def do_activate(self):
  284. # We only allow a single window and raise any existing ones
  285. if not self.window:
  286. # Windows are associated with the application
  287. # when the last one is closed the application shuts down
  288. self.window = self.builder.get_object("pdb-window")
  289. self.add_window(self.window)
  290. self.window.set_wmclass("PD Buddy Configuration",
  291. "PD Buddy Configuration")
  292. self.window.present()
  293. def run():
  294. app = Application()
  295. app.run(sys.argv)
  296. if __name__ == "__main__":
  297. run()