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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 SelectListRowModel(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(SelectListRowModel(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 PDOListRowModel(GObject.GObject):
  104. def __init__(self, pdo):
  105. GObject.GObject.__init__(self)
  106. self.pdo = pdo
  107. class PDOListStore(Gio.ListStore):
  108. def update_items(self, pdo_list):
  109. # Clear the list
  110. self.remove_all()
  111. # Add everything from the new list
  112. for pdo in pdo_list:
  113. self.append(PDOListRowModel(pdo))
  114. class PDOListRow(Gtk.ListBoxRow):
  115. oc_tooltips = [
  116. "I<sub>Peak</sub> = I<sub>OC</sub> (default)",
  117. """Overload Capabilities:
  118. 1. I<sub>Peak</sub> = 150% I<sub>OC</sub> for 1 ms @ 5% duty cycle (I<sub>Low</sub> = 97% I<sub>OC</sub> for 19 ms)
  119. 2. I<sub>Peak</sub> = 125% I<sub>OC</sub> for 2 ms @ 10% duty cycle (I<sub>Low</sub> = 97% I<sub>OC</sub> for 18 ms)
  120. 3. I<sub>Peak</sub> = 110% I<sub>OC</sub> for 10 ms @ 50% duty cycle (I<sub>Low</sub> = 90% I<sub>OC</sub> for 10 ms)""",
  121. """Overload Capabilities:
  122. 1. I<sub>Peak</sub> = 200% I<sub>OC</sub> for 1 ms @ 5% duty cycle (I<sub>Low</sub> = 95% I<sub>OC</sub> for 19 ms)
  123. 2. I<sub>Peak</sub> = 150% I<sub>OC</sub> for 2 ms @ 10% duty cycle (I<sub>Low</sub> = 94% I<sub>OC</sub> for 18 ms)
  124. 3. I<sub>Peak</sub> = 125% I<sub>OC</sub> for 10 ms @ 50% duty cycle (I<sub>Low</sub> = 75% I<sub>OC</sub> for 10 ms)""",
  125. """Overload Capabilities:
  126. 1. I<sub>Peak</sub> = 200% I<sub>OC</sub> for 1 ms @ 5% duty cycle (I<sub>Low</sub> = 95% I<sub>OC</sub> for 19 ms)
  127. 2. I<sub>Peak</sub> = 175% I<sub>OC</sub> for 2 ms @ 10% duty cycle (I<sub>Low</sub> = 92% I<sub>OC</sub> for 18 ms)
  128. 3. I<sub>Peak</sub> = 150% I<sub>OC</sub> for 10 ms @ 50% duty cycle (I<sub>Low</sub> = 50% I<sub>OC</sub> for 10 ms)"""
  129. ]
  130. def __init__(self, model):
  131. Gtk.ListBoxRow.__init__(self)
  132. self.model = model
  133. self.set_activatable(False)
  134. self.set_selectable(False)
  135. self.set_can_focus(False)
  136. # Make the widgets and populate them with info from the model
  137. # Main box
  138. box = Gtk.Box(Gtk.Orientation.HORIZONTAL, 12)
  139. box.set_homogeneous(True)
  140. box.set_margin_left(12)
  141. box.set_margin_right(12)
  142. box.set_margin_top(6)
  143. box.set_margin_bottom(6)
  144. # Type label
  145. if model.pdo.pdo_type == "fixed":
  146. type_text = "Fixed"
  147. elif model.pdo.pdo_type == "pps":
  148. type_text = "Programmable"
  149. elif model.pdo.pdo_type == "unknown":
  150. type_text = "Unknown"
  151. elif model.pdo.pdo_type == "typec_virtual":
  152. type_text = "Type-C Current"
  153. type_label = Gtk.Label(type_text)
  154. type_label.set_halign(Gtk.Align.START)
  155. box.pack_start(type_label, True, True, 0)
  156. # Voltage label
  157. if model.pdo.pdo_type == "fixed":
  158. voltage_label = Gtk.Label("{:g} V".format(model.pdo.v / 1000.0))
  159. voltage_label.set_halign(Gtk.Align.END)
  160. box.pack_start(voltage_label, True, True, 0)
  161. elif model.pdo.pdo_type == "pps":
  162. voltage_label = Gtk.Label("{:g}\u2013{:g} V".format(
  163. model.pdo.vmin / 1000.0, model.pdo.vmax / 1000.0))
  164. voltage_label.set_halign(Gtk.Align.END)
  165. box.pack_start(voltage_label, True, True, 0)
  166. # Right box
  167. right_box = Gtk.Box(Gtk.Orientation.HORIZONTAL, 6)
  168. right_box.set_halign(Gtk.Align.END)
  169. if model.pdo.pdo_type != "unknown":
  170. # Current label
  171. current_label = Gtk.Label("{:g} A".format(model.pdo.i / 1000.0))
  172. current_label.set_halign(Gtk.Align.END)
  173. right_box.pack_end(current_label, True, False, 0)
  174. # Over-current image(?)
  175. try:
  176. if model.pdo.peak_i > 0:
  177. oc_image = Gtk.Image.new_from_icon_name(
  178. "dialog-information-symbolic", Gtk.IconSize.BUTTON)
  179. oc_image.set_tooltip_markup(
  180. PDOListRow.oc_tooltips[model.pdo.peak_i])
  181. right_box.pack_end(oc_image, True, False, 0)
  182. except AttributeError:
  183. # If this isn't a fixed PDO, there's no peak_i attribute.
  184. # Not a problem, so just ignore the error.
  185. pass
  186. else:
  187. # PDO value
  188. text_label = Gtk.Label()
  189. text_label.set_markup("<tt>{}</tt>".format(model.pdo))
  190. right_box.pack_end(text_label, True, False, 0)
  191. box.pack_end(right_box, True, True, 0)
  192. self.add(box)
  193. self.show_all()
  194. class Handler:
  195. def __init__(self, builder):
  196. self.builder = builder
  197. self.serial_port = None
  198. self.voltage = None
  199. self.current = None
  200. self.giveback = None
  201. self.selectlist = None
  202. def on_pdb_window_realize(self, *args):
  203. # Get the list
  204. sb = self.builder.get_object("select-box")
  205. self.selectlist = SelectList()
  206. sb.pack_start(self.selectlist, True, True, 0)
  207. liststore = SelectListStore()
  208. self.selectlist.bind_model(liststore, SelectListRow)
  209. self.selectlist.connect("row-activated", self.on_select_list_row_activated)
  210. # Add separators to the configuration page lists
  211. sc_list = self.builder.get_object("sink-config-list")
  212. sc_list.set_header_func(list_box_update_header_func, None)
  213. pd_list = self.builder.get_object("power-delivery-list")
  214. pd_list.set_header_func(list_box_update_header_func, None)
  215. def on_pdb_window_delete_event(self, *args):
  216. Gtk.main_quit(*args)
  217. def on_select_list_row_activated(self, selectlist, serport):
  218. # Get relevant widgets
  219. voltage = self.builder.get_object("voltage-adjustment")
  220. current = self.builder.get_object("current-adjustment")
  221. current_dim = self.builder.get_object("current-dimension")
  222. giveback = self.builder.get_object("giveback-switch")
  223. pd_frame = self.builder.get_object("power-delivery-frame")
  224. output = self.builder.get_object("output-switch")
  225. cap_row = self.builder.get_object("source-cap-row")
  226. cap_warning = self.builder.get_object("source-cap-warning")
  227. cap_label = self.builder.get_object("short-source-cap-label")
  228. cap_arrow = self.builder.get_object("source-cap-arrow")
  229. self.serial_port = serport
  230. window = self.builder.get_object("pdb-window")
  231. try:
  232. with pdbuddy.Sink(self.serial_port) as pdbs:
  233. try:
  234. pdbs.load()
  235. except KeyError:
  236. # If there's no configuration, we don't want to fail. We
  237. # do want to display no configuration though
  238. self.cfg = pdbuddy.SinkConfig(
  239. status=pdbuddy.SinkStatus.VALID,
  240. flags=pdbuddy.SinkFlags.NONE, v=0, vmin=0, vmax=0,
  241. i=0, idim=pdbuddy.SinkDimension.CURRENT)
  242. else:
  243. self.cfg = pdbs.get_tmpcfg()
  244. except OSError as e:
  245. comms_error_dialog(window, e)
  246. return
  247. self._store_device_settings()
  248. self._set_save_button_visibility()
  249. # Set giveback switch state
  250. giveback.set_active(bool(self.cfg.flags & pdbuddy.SinkFlags.GIVEBACK))
  251. # Get voltage and current from device and load them into the GUI
  252. voltage.set_value(self.cfg.v/1000)
  253. if self.cfg.idim == pdbuddy.SinkDimension.CURRENT:
  254. current_dim.set_active_id("idim-current")
  255. elif self.cfg.idim == pdbuddy.SinkDimension.POWER:
  256. current_dim.set_active_id("idim-power")
  257. elif self.cfg.idim == pdbuddy.SinkDimension.RESISTANCE:
  258. current_dim.set_active_id("idim-resistance")
  259. current.set_value(self.cfg.i/1000)
  260. # Set PD frame visibility and output switch state
  261. try:
  262. with pdbuddy.Sink(self.serial_port) as pdbs:
  263. output.set_state(pdbs.output)
  264. except KeyError:
  265. pd_frame.set_visible(False)
  266. else:
  267. pd_frame.set_visible(True)
  268. # TODO: do these next things repeatedly
  269. # Get the Source_Capabilities
  270. with pdbuddy.Sink(self.serial_port) as pdbs:
  271. caps = pdbs.get_source_cap()
  272. # Update the warning icon
  273. cap_warning.set_visible(not pdbuddy.follows_power_rules(caps))
  274. # Update the text in the capability label
  275. if caps:
  276. cap_label.set_text('{:g} W'.format(pdbuddy.calculate_pdp(caps)))
  277. else:
  278. cap_label.set_text('None')
  279. # Make the row insensitive if there are no capabilities
  280. cap_row.set_activatable(caps)
  281. cap_arrow.set_visible(caps)
  282. # Show the Sink page
  283. hst = self.builder.get_object("header-stack")
  284. hsink = self.builder.get_object("header-sink")
  285. hsink.set_title('{} {} {}'.format(serport.manufacturer,
  286. serport.product,
  287. serport.serial_number))
  288. hsink.set_subtitle(serport.device)
  289. hst.set_visible_child(hsink)
  290. st = self.builder.get_object("stack")
  291. sink = self.builder.get_object("sink")
  292. st.set_visible_child(sink)
  293. # Ping the Sink repeatedly
  294. GLib.timeout_add(1000, self._ping)
  295. def _ping(self):
  296. """Ping the device we're configuring, showing to the list on failure"""
  297. if self.serial_port is None:
  298. self.selectlist.reload()
  299. self.on_header_sink_back_clicked(None)
  300. return False
  301. try:
  302. with pdbuddy.Sink(self.serial_port) as pdbs:
  303. pdbs.send_command("")
  304. return True
  305. except:
  306. self.selectlist.reload()
  307. self.on_header_sink_back_clicked(None)
  308. return False
  309. def on_header_sink_back_clicked(self, data):
  310. self.serial_port = None
  311. # Show the Select page
  312. hst = self.builder.get_object("header-stack")
  313. hselect = self.builder.get_object("header-select")
  314. hst.set_visible_child(hselect)
  315. st = self.builder.get_object("stack")
  316. select = self.builder.get_object("select")
  317. st.set_visible_child(select)
  318. def on_sink_save_clicked(self, button):
  319. window = self.builder.get_object("pdb-window")
  320. try:
  321. with pdbuddy.Sink(self.serial_port) as pdbs:
  322. pdbs.set_tmpcfg(self.cfg)
  323. pdbs.write()
  324. self._store_device_settings()
  325. self._set_save_button_visibility()
  326. except OSError as e:
  327. comms_error_dialog(window, e)
  328. self.on_header_sink_back_clicked(None)
  329. def _store_device_settings(self):
  330. """Store the settings that were loaded from the device"""
  331. self.cfg_clean = self.cfg
  332. def _set_save_button_visibility(self):
  333. """Show the save button if there are new settings to save"""
  334. # Get relevant widgets
  335. rev = self.builder.get_object("sink-save-revealer")
  336. # Set visibility
  337. rev.set_reveal_child(self.cfg != self.cfg_clean)
  338. def on_voltage_adjustment_value_changed(self, adj):
  339. self.cfg = self.cfg._replace(v=int(adj.get_value() * 1000))
  340. self._set_save_button_visibility()
  341. def on_current_dimension_changed(self, cb):
  342. item = cb.get_active_id()
  343. value = self.builder.get_object("current-adjustment")
  344. unit = self.builder.get_object("current-unit")
  345. if item == "idim-current":
  346. if self.cfg.idim == pdbuddy.SinkDimension.POWER:
  347. self.cfg = self.cfg._replace(i=self.cfg.i/self.cfg.v*1000.0)
  348. elif self.cfg.idim == pdbuddy.SinkDimension.RESISTANCE:
  349. self.cfg = self.cfg._replace(i=self.cfg.v/self.cfg.i*1000.0)
  350. value.configure(self.cfg.i / 1000.0, 0, 5, 0.1, 1, 0)
  351. idim = pdbuddy.SinkDimension.CURRENT
  352. unit.set_text("A")
  353. if item == "idim-power":
  354. if self.cfg.idim == pdbuddy.SinkDimension.CURRENT:
  355. self.cfg = self.cfg._replace(i=self.cfg.i*self.cfg.v/1000.0)
  356. elif self.cfg.idim == pdbuddy.SinkDimension.RESISTANCE:
  357. self.cfg = self.cfg._replace(
  358. i=self.cfg.v*self.cfg.v/self.cfg.i)
  359. idim = pdbuddy.SinkDimension.POWER
  360. value.configure(self.cfg.i / 1000.0, 0, 100, 1, 10, 0)
  361. unit.set_text("W")
  362. if item == "idim-resistance":
  363. if self.cfg.idim == pdbuddy.SinkDimension.CURRENT:
  364. self.cfg = self.cfg._replace(i=self.cfg.v/self.cfg.i*1000.0)
  365. elif self.cfg.idim == pdbuddy.SinkDimension.POWER:
  366. self.cfg = self.cfg._replace(
  367. i=self.cfg.v*self.cfg.v/self.cfg.i)
  368. idim = pdbuddy.SinkDimension.RESISTANCE
  369. value.configure(self.cfg.i / 1000.0, 0, 655.35, 1, 10, 0)
  370. unit.set_text("\u03a9")
  371. self.cfg = self.cfg._replace(idim=idim)
  372. self._set_save_button_visibility()
  373. def on_current_adjustment_value_changed(self, adj):
  374. self.cfg = self.cfg._replace(i=int(adj.get_value() * 1000))
  375. self._set_save_button_visibility()
  376. def on_giveback_switch_state_set(self, switch, state):
  377. if state:
  378. self.cfg = self.cfg._replace(flags=self.cfg.flags|pdbuddy.SinkFlags.GIVEBACK)
  379. else:
  380. self.cfg = self.cfg._replace(flags=self.cfg.flags&~pdbuddy.SinkFlags.GIVEBACK)
  381. self._set_save_button_visibility()
  382. def on_output_switch_state_set(self, switch, state):
  383. with pdbuddy.Sink(self.serial_port) as pdbs:
  384. pdbs.output = state
  385. def on_source_cap_row_activated(self, box, row):
  386. # Find which row was clicked
  387. sc_row = self.builder.get_object("source-cap-row")
  388. if row != sc_row:
  389. # If it's not the source-cap-row, leave
  390. return
  391. # Get the source capabilities
  392. with pdbuddy.Sink(self.serial_port) as pdbs:
  393. caps = pdbs.get_source_cap()
  394. if not caps:
  395. # If there are no capabilities, don't show a dialog
  396. return
  397. # Create the dialog
  398. window = self.builder.get_object("pdb-window")
  399. dialog_builder = Gtk.Builder.new_from_file("data/src-cap-dialog.ui")
  400. dialog = dialog_builder.get_object("src-cap-dialog")
  401. dialog.set_transient_for(window)
  402. dialog.get_content_area().set_border_width(0)
  403. # Populate PD Power
  404. d_power = dialog_builder.get_object("power-label")
  405. d_power.set_text("{:g} W".format(pdbuddy.calculate_pdp(caps)))
  406. # Warning icon
  407. cap_warning = dialog_builder.get_object("source-cap-warning")
  408. cap_warning.set_visible(not pdbuddy.follows_power_rules(caps))
  409. # Populate Information
  410. d_info_header = dialog_builder.get_object("info-header")
  411. d_info = dialog_builder.get_object("info-label")
  412. # Make the string to display
  413. info_str = ""
  414. try:
  415. if caps[0].dual_role_pwr:
  416. info_str += "Dual-Role Power\n"
  417. if caps[0].usb_suspend:
  418. info_str += "USB Suspend Supported\n"
  419. if caps[0].unconstrained_pwr:
  420. info_str += "Unconstrained Power\n"
  421. if caps[0].usb_comms:
  422. info_str += "USB Communications Capable\n"
  423. if caps[0].dual_role_data:
  424. info_str += "Dual-Role Data\n"
  425. info_str = info_str[:-1]
  426. except AttributeError:
  427. # If we have a typec_virtual PDO, there will be AttributeErrors
  428. # from the above. Not a problem, so just pass.
  429. pass
  430. # Set the text and label visibility
  431. d_info.set_text(info_str)
  432. d_info_header.set_visible(info_str)
  433. d_info.set_visible(info_str)
  434. # PDO list
  435. d_list = dialog_builder.get_object("src-cap-list")
  436. d_list.set_header_func(list_box_update_header_func, None)
  437. model = PDOListStore()
  438. d_list.bind_model(model, PDOListRow)
  439. model.update_items(caps)
  440. # Show the dialog
  441. dialog.run()
  442. dialog.destroy()
  443. class Application(Gtk.Application):
  444. def __init__(self, *args, **kwargs):
  445. super().__init__(*args, application_id="com.clayhobbs.pd-buddy-gtk",
  446. **kwargs)
  447. self.window = None
  448. def do_startup(self):
  449. Gtk.Application.do_startup(self)
  450. self.builder = Gtk.Builder.new_from_file("data/pd-buddy-gtk.ui")
  451. self.builder.connect_signals(Handler(self.builder))
  452. def do_activate(self):
  453. # We only allow a single window and raise any existing ones
  454. if not self.window:
  455. # Windows are associated with the application
  456. # when the last one is closed the application shuts down
  457. self.window = self.builder.get_object("pdb-window")
  458. self.add_window(self.window)
  459. self.window.set_wmclass("PD Buddy Configuration",
  460. "PD Buddy Configuration")
  461. self.window.present()
  462. def run():
  463. app = Application()
  464. app.run(sys.argv)
  465. if __name__ == "__main__":
  466. run()