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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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
  8. def update_header_func(row, before, data):
  9. """Add a separator header to all rows but the first one"""
  10. if before is None:
  11. row.set_header(None)
  12. return
  13. current = row.get_header()
  14. if current is None:
  15. current = Gtk.Separator.new(Gtk.Orientation.HORIZONTAL)
  16. row.set_header(current)
  17. def pdb_send_message(sp, message):
  18. """Send a message over the serial port and return the response"""
  19. # Open the serial port
  20. # FIXME handle exceptions
  21. sp = serial.Serial(sp.device, baudrate=115200, timeout=0.01)
  22. sp.write(bytes(message, 'utf-8') + b'\r\n')
  23. sp.flush()
  24. answer = sp.readlines()
  25. sp.close()
  26. # Remove the echoed command and prompt
  27. answer = answer[1:-1]
  28. return answer
  29. class SelectListRow(Gtk.ListBoxRow):
  30. def __init__(self, serial_port):
  31. Gtk.EventBox.__init__(self)
  32. self.serial_port = serial_port
  33. self._builder = Gtk.Builder()
  34. self._builder.add_from_file("data/select-list-row.ui")
  35. self._builder.connect_signals(self)
  36. name = self._builder.get_object("name")
  37. name.set_text(serial_port.description)
  38. device = self._builder.get_object("device")
  39. device.set_text(serial_port.device)
  40. self.add(self._builder.get_object("grid"))
  41. self.show_all()
  42. def on_identify_clicked(self, button):
  43. pdb_send_message(self.serial_port, 'identify')
  44. class Handler:
  45. def __init__(self, builder):
  46. self.builder = builder
  47. def on_pdb_window_realize(self, *args):
  48. # Get the list
  49. sl = self.builder.get_object("select-list")
  50. ss = self.builder.get_object("select-stack")
  51. sf = self.builder.get_object("select-frame")
  52. # Search for the serial ports
  53. for serport in serial.tools.list_ports.grep("1209:0001"):
  54. # Show the list if we have a serial port
  55. ss.set_visible_child(sf)
  56. sl.insert(SelectListRow(serport), -1)
  57. # Add separators to the list
  58. sl.set_header_func(update_header_func, None)
  59. def on_pdb_window_delete_event(self, *args):
  60. Gtk.main_quit(*args)
  61. def on_select_list_row_activated(self, box, row):
  62. # Get voltage and current widgets
  63. voltage = self.builder.get_object("voltage-combobox")
  64. current = self.builder.get_object("current-spinbutton")
  65. self.serial_port = row.serial_port
  66. pdb_send_message(self.serial_port, 'load')
  67. tmpcfg = pdb_send_message(self.serial_port, 'get_tmpcfg')
  68. # Get information
  69. for line in tmpcfg:
  70. if line.startswith(b'v:'):
  71. v = line.split()[1]
  72. if v == b'5.00':
  73. voltage.set_active_id('voltage-five')
  74. elif v == b'9.00':
  75. voltage.set_active_id('voltage-nine')
  76. elif v == b'15.00':
  77. voltage.set_active_id('voltage-fifteen')
  78. if v == b'20.00':
  79. voltage.set_active_id('voltage-twenty')
  80. elif line.startswith(b'i:'):
  81. i = float(line.split()[1])
  82. current.set_value(i)
  83. # Hide the Save button
  84. rev = self.builder.get_object("header-sink-save-revealer")
  85. rev.set_reveal_child(False)
  86. # Show the Sink page
  87. hst = self.builder.get_object("header-stack")
  88. hsink = self.builder.get_object("header-sink")
  89. hsink.set_subtitle(row.serial_port.device)
  90. hst.set_visible_child(hsink)
  91. st = self.builder.get_object("stack")
  92. sink = self.builder.get_object("sink")
  93. st.set_visible_child(sink)
  94. def on_header_sink_back_clicked(self, data):
  95. # Show the Select page
  96. hst = self.builder.get_object("header-stack")
  97. hselect = self.builder.get_object("header-select")
  98. hst.set_visible_child(hselect)
  99. st = self.builder.get_object("stack")
  100. select = self.builder.get_object("select")
  101. st.set_visible_child(select)
  102. def on_header_sink_save_clicked(self, button):
  103. combo = self.builder.get_object("voltage-combobox")
  104. spin = self.builder.get_object("current-spinbutton")
  105. print("{} V".format(combo.get_active_text()))
  106. print("{} A".format(spin.get_value()))
  107. rev = self.builder.get_object("header-sink-save-revealer")
  108. rev.set_reveal_child(False)
  109. pdb_send_message(self.serial_port, 'write')
  110. def on_voltage_combobox_changed(self, combo):
  111. rev = self.builder.get_object("header-sink-save-revealer")
  112. rev.set_reveal_child(True)
  113. pdb_send_message(self.serial_port,
  114. 'set_v {}'.format(int(combo.get_active_text())*1000))
  115. def on_current_spinbutton_changed(self, spin):
  116. rev = self.builder.get_object("header-sink-save-revealer")
  117. rev.set_reveal_child(True)
  118. pdb_send_message(self.serial_port,
  119. 'set_i {}'.format(int(spin.get_value()*1000)))
  120. class Application(Gtk.Application):
  121. def __init__(self, *args, **kwargs):
  122. super().__init__(*args, application_id="com.clayhobbs.pd-buddy-gtk",
  123. **kwargs)
  124. self.window = None
  125. def do_startup(self):
  126. Gtk.Application.do_startup(self)
  127. self.builder = Gtk.Builder()
  128. self.builder.add_from_file("data/pd-buddy-gtk.ui")
  129. self.builder.connect_signals(Handler(self.builder))
  130. def do_activate(self):
  131. # We only allow a single window and raise any existing ones
  132. if not self.window:
  133. # Windows are associated with the application
  134. # when the last one is closed the application shuts down
  135. self.window = self.builder.get_object("pdb-window")
  136. self.add_window(self.window)
  137. self.window.set_wmclass("PD Buddy Configuration",
  138. "PD Buddy Configuration")
  139. self.window.present()
  140. if __name__ == "__main__":
  141. app = Application()
  142. app.run(sys.argv)