USB Serial support for the open-source firmware (#9)

* USB Serial support for the open-source firmware
master
Miha Frangež 1 week ago committed by GitHub
parent 2c066e7f2a
commit a432514db4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 67
      lednamebadge.py
  2. 1
      tests/test_lednamebadge_select_method.py

@ -733,6 +733,71 @@ class WriteUsbHidApi(WriteMethod):
WriteUsbHidApi.pyhidapi.hid_write(self.dev, sendbuf)
class WriteSerial(WriteMethod):
"""Write to a device with the open-source firmware via the USB serial port using pyserial.
"""
_module_loaded = False
try:
import serial
_module_loaded = True
print("Module pyserial detected")
except:
pass
def __init__(self):
WriteMethod.__init__(self)
self.description = None
self.path = None
self.dev = None
def get_name(self):
return 'pyserial'
def get_description(self):
return 'Program a device with the open-source firmware, connected via USB, using the pyserial package.'
def _open(self, device_id):
import serial
self.description = self.devices[device_id][0]
self.path = self.devices[device_id][1]
self.dev = serial.Serial(self.path)
if self.dev:
print("Pyserial device initialized")
return self.dev is not None
def close(self):
if self.dev is not None:
self.dev.close()
self.description = None
self.path = None
self.dev = None
def _get_available_devices(self):
import serial.tools.list_ports
ports = serial.tools.list_ports.comports()
devices = {}
for port, desc, hwid in ports:
if desc == "n/a":
continue
devices[port] = (f"{desc} ({hwid})", port)
return devices
def is_ready(self):
return WriteSerial._module_loaded
def has_device(self):
return self.dev is not None
def _write(self, buf):
if not self.dev:
return
print("Write using [%s] via pyserial" % (self.description,))
self.dev.write(buf)
class LedNameBadge:
_protocol_header_template = (
0x77, 0x61, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40,
@ -945,7 +1010,7 @@ class LedNameBadge:
@staticmethod
def _get_auto_order_method_list():
return [WriteUsbHidApi(), WriteLibUsb()]
return [WriteUsbHidApi(), WriteLibUsb(), WriteSerial()]
@staticmethod
def _print_available_methods(methods):

@ -11,6 +11,7 @@ class Test(abstract_write_method_test.AbstractWriteMethodTest):
self.assertIn("'auto'", output)
self.assertIn("'hidapi'", output)
self.assertIn("'libusb'", output)
self.assertIn("'pyserial'", output)
method, output = self.call_find(True, True, True, 'hidapi', 'list')
self.assertIn("Known device ids with method 'hidapi' are:", output)

Loading…
Cancel
Save