From 60c3afda65ca33d26d084681df8265b7bfb1af97 Mon Sep 17 00:00:00 2001 From: sandr0 Date: Sun, 12 Jan 2025 16:36:01 +0100 Subject: [PATCH 1/3] fixed CH546 timeout issue --- lednamebadge.py | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/lednamebadge.py b/lednamebadge.py index 757b30d..1a740de 100755 --- a/lednamebadge.py +++ b/lednamebadge.py @@ -292,7 +292,7 @@ class SimpleTextAndIcons: 0b10011001, # 0x99 0b01000010, # 0x42 0b00111100, # 0x3c - 0b00000000 # 0x00 + 0b00000000, # 0x00 )), 1, '\x1d'), 'happy2': (array('B', (0x00, 0x08, 0x14, 0x08, 0x01, 0x00, 0x00, 0x61, 0x30, 0x1c, 0x07, 0x00, 0x20, 0x50, 0x20, 0x00, 0x80, 0x80, 0x86, 0x0c, 0x38, 0xe0)), 2, '\x1c'), @@ -580,7 +580,8 @@ class WriteLibUsb(WriteMethod): WriteMethod.__init__(self) self.description = None self.dev = None - self.endpoint = None + self.endpoint_out = None + self.endpoint_in = None def get_name(self): return 'libusb' @@ -591,7 +592,8 @@ class WriteLibUsb(WriteMethod): def _open(self, device_id): self.description = self.devices[device_id][0] self.dev = self.devices[device_id][1] - self.endpoint = self.devices[device_id][2] + self.endpoint_out = self.devices[device_id][2] + self.endpoint_in = self.devices[device_id][3] print("Libusb device initialized") return True @@ -601,7 +603,8 @@ class WriteLibUsb(WriteMethod): WriteLibUsb.usb.util.dispose_resources(self.dev) self.description = None self.dev = None - self.endpoint = None + self.endpoint_out = None + self.endpoint_in = None def _get_available_devices(self): devs = WriteLibUsb.usb.core.find(idVendor=0x0416, idProduct=0x5020, find_all=True) @@ -625,12 +628,20 @@ class WriteLibUsb(WriteMethod): eps = WriteLibUsb.usb.util.find_descriptor( cfg, find_all=True, - custom_match=lambda e: WriteLibUsb.usb.util.endpoint_direction(e.bEndpointAddress) == WriteLibUsb.usb.util.ENDPOINT_OUT) + ) + ep_in = None + ep_out = None for ep in eps: - did = "%d:%d:%d" % (d.bus, d.address, ep.bEndpointAddress) - descr = ("%s - %s (bus=%d dev=%d endpoint=%d)" % - (d.manufacturer, d.product, d.bus, d.address, ep.bEndpointAddress)) - devices[did] = (descr, d, ep) + if WriteLibUsb.usb.util.endpoint_direction(ep.bEndpointAddress) == WriteLibUsb.usb.util.ENDPOINT_OUT: + ep_out = ep + if WriteLibUsb.usb.util.endpoint_direction(ep.bEndpointAddress) == WriteLibUsb.usb.util.ENDPOINT_IN: + ep_in = ep + assert ep_in and ep_out + + did = "%d:%d:%d:%d" % (d.bus, d.address, ep_out.bEndpointAddress, ep_in.bEndpointAddress) + descr = ("%s - %s (bus=%d dev=%d endpoint_out=%d endpoint_in=%d)" % + (d.manufacturer, d.product, d.bus, d.address, ep_out.bEndpointAddress, ep_in.bEndpointAddress)) + devices[did] = (descr, d, ep_out, ep_in) return devices def is_ready(self): @@ -661,7 +672,8 @@ class WriteLibUsb(WriteMethod): print("Write using %s via libusb" % (self.description,)) for i in range(int(len(buf) / 64)): time.sleep(0.1) - self.endpoint.write(buf[i * 64:i * 64 + 64]) + self.endpoint_out.write(buf[i * 64:i * 64 + 64]) + self.endpoint_in.read(64) class WriteUsbHidApi(WriteMethod): @@ -779,7 +791,7 @@ class WriteSerial(WriteMethod): devices = {} for port, desc, hwid in ports: - if desc == "n/a": + if desc == "n/a": continue devices[port] = (f"{desc} ({hwid})", port) return devices @@ -1129,20 +1141,20 @@ def main(): parser.add_argument('message', metavar='MESSAGE', nargs='+', help="Up to 8 message texts with embedded builtin icons or loaded images within colons(:) -- See -l for a list of builtins.") parser.add_argument('--mode-help', action='version', help=argparse.SUPPRESS, version=""" - + -m 5 "Animation" - + Animation frames are 6 character (or 48px) wide. Upload an animation of N frames as one image N*48 pixels wide, 11 pixels high. Frames run from left to right and repeat endless. Speeds [1..8] result in ca. [1.2 1.3 2.0 2.4 2.8 4.5 7.5 15] fps. - + Example of a slowly beating heart: sudo %s -s1 -m5 " :heart2: :HEART2:" - + -m 9 "Smooth" -m 10 "Rotate" - + These modes are mentioned in the BMP Badge software. Text is shown static, or sometimes (longer texts?) not shown at all. One significant difference is: The text of the first message stays visible after From 692f5deb8cfae49d64fb16f919a594c4475b60d9 Mon Sep 17 00:00:00 2001 From: sandr0 Date: Sun, 12 Jan 2025 22:04:43 +0100 Subject: [PATCH 2/3] pacman + singular bits possible --- gfx/pacman.png | Bin 0 -> 746 bytes lednamebadge.py | 43 ++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 gfx/pacman.png diff --git a/gfx/pacman.png b/gfx/pacman.png new file mode 100644 index 0000000000000000000000000000000000000000..41c2bb8c6e3731f31d46b7bf4e269047c77ef4cf GIT binary patch literal 746 zcmV0003YX+uL$Nkc;* zaB^>EX>4Tx04R~2kg-a`P!xv0R%u1+q&SE}2G@>ea7;y0+l6 zE8C4e@AYwCDf>!OMou 127: + if monochrome_color == 255: bit_val = 1 << (7 - bit) byte_val += bit_val buf.append(byte_val) im.close() return buf, cols + @staticmethod + def bitmap_bits(rows: list[int], bits: int): + """ + Use with somethin like + [ + 0b11001100110011001100110011001100110011001100, + 0b11001100110011001100110011001100110011001100, + 0b00110011001100110011001100110011001100110011, + 0b00110011001100110011001100110011001100110011, + 0b11001100110011001100110011001100110011001100, + 0b11001100110011001100110011001100110011001100, + 0b00110011001100110011001100110011001100110011, + 0b00110011001100110011001100110011001100110011, + 0b11001100110011001100110011001100110011001100, + 0b11001100110011001100110011001100110011001100, + 0b00110011001100110011001100110011001100110011, + ] + + as an alternative to a PNG. + """ + buf = array('B') + assert len(rows) == 11 + + cols = (bits + 7) // 8 + for col in range(cols): + for row in range(11): # [0..10] + byte_val = 0 + for bit in range(8): # [0..7] + bit_val = 0 + x = 8 * col + bit + if x < bits: + pixel_color = ((rows[row] >> (bits - 1 - x)) & 1) << (7 - bit) + byte_val += pixel_color + buf.append(byte_val) + + return buf, cols + def bitmap(self, arg): """If arg is a valid and existing path name, we load it as an image. Otherwise, we take it as a string (with ":"-notation, see bitmap_text()). From 6bdfb02bd851bd2e64268a2efa054f0301b91bd5 Mon Sep 17 00:00:00 2001 From: sandr0 Date: Sun, 12 Jan 2025 22:08:00 +0100 Subject: [PATCH 3/3] reverted some changes which were intended for local use only --- gfx/pacman.png | Bin 746 -> 0 bytes lednamebadge.py | 39 +-------------------------------------- 2 files changed, 1 insertion(+), 38 deletions(-) delete mode 100644 gfx/pacman.png diff --git a/gfx/pacman.png b/gfx/pacman.png deleted file mode 100644 index 41c2bb8c6e3731f31d46b7bf4e269047c77ef4cf..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 746 zcmV0003YX+uL$Nkc;* zaB^>EX>4Tx04R~2kg-a`P!xv0R%u1+q&SE}2G@>ea7;y0+l6 zE8C4e@AYwCDf>!OMou 127: bit_val = 1 << (7 - bit) byte_val += bit_val buf.append(byte_val) im.close() return buf, cols - @staticmethod - def bitmap_bits(rows: list[int], bits: int): - """ - Use with somethin like - [ - 0b11001100110011001100110011001100110011001100, - 0b11001100110011001100110011001100110011001100, - 0b00110011001100110011001100110011001100110011, - 0b00110011001100110011001100110011001100110011, - 0b11001100110011001100110011001100110011001100, - 0b11001100110011001100110011001100110011001100, - 0b00110011001100110011001100110011001100110011, - 0b00110011001100110011001100110011001100110011, - 0b11001100110011001100110011001100110011001100, - 0b11001100110011001100110011001100110011001100, - 0b00110011001100110011001100110011001100110011, - ] - - as an alternative to a PNG. - """ - buf = array('B') - assert len(rows) == 11 - - cols = (bits + 7) // 8 - for col in range(cols): - for row in range(11): # [0..10] - byte_val = 0 - for bit in range(8): # [0..7] - bit_val = 0 - x = 8 * col + bit - if x < bits: - pixel_color = ((rows[row] >> (bits - 1 - x)) & 1) << (7 - bit) - byte_val += pixel_color - buf.append(byte_val) - - return buf, cols - def bitmap(self, arg): """If arg is a valid and existing path name, we load it as an image. Otherwise, we take it as a string (with ":"-notation, see bitmap_text()).