From a432514db49454b22aabc43bfd5d319b8adcb3c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miha=20Frange=C5=BE?= Date: Mon, 30 Dec 2024 09:18:13 +0100 Subject: [PATCH] USB Serial support for the open-source firmware (#9) * USB Serial support for the open-source firmware --- lednamebadge.py | 67 +++++++++++++++++++++++- tests/test_lednamebadge_select_method.py | 1 + 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lednamebadge.py b/lednamebadge.py index d8a700c..757b30d 100755 --- a/lednamebadge.py +++ b/lednamebadge.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): diff --git a/tests/test_lednamebadge_select_method.py b/tests/test_lednamebadge_select_method.py index 98de979..100f785 100644 --- a/tests/test_lednamebadge_select_method.py +++ b/tests/test_lednamebadge_select_method.py @@ -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)