pull/12/merge
Sandro Bauer 8 months ago committed by GitHub
commit 8b7b7b2745
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 36
      lednamebadge.py

@ -292,7 +292,7 @@ class SimpleTextAndIcons:
0b10011001, # 0x99 0b10011001, # 0x99
0b01000010, # 0x42 0b01000010, # 0x42
0b00111100, # 0x3c 0b00111100, # 0x3c
0b00000000 # 0x00 0b00000000, # 0x00
)), 1, '\x1d'), )), 1, '\x1d'),
'happy2': (array('B', (0x00, 0x08, 0x14, 0x08, 0x01, 0x00, 0x00, 0x61, 0x30, 0x1c, 0x07, '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'), 0x00, 0x20, 0x50, 0x20, 0x00, 0x80, 0x80, 0x86, 0x0c, 0x38, 0xe0)), 2, '\x1c'),
@ -401,7 +401,7 @@ class SimpleTextAndIcons:
if im.height != 11: if im.height != 11:
sys.exit("%s: image height must be 11px. Seen %d" % (file, im.height)) sys.exit("%s: image height must be 11px. Seen %d" % (file, im.height))
buf = array('B') buf = array('B')
cols = int((im.width + 7) / 8) cols = (im.width + 7) // 8
for col in range(cols): for col in range(cols):
for row in range(11): # [0..10] for row in range(11): # [0..10]
byte_val = 0 byte_val = 0
@ -411,7 +411,7 @@ class SimpleTextAndIcons:
if x < im.width and row < im.height: if x < im.width and row < im.height:
pixel_color = im.getpixel((x, row)) pixel_color = im.getpixel((x, row))
if isinstance(pixel_color, tuple): if isinstance(pixel_color, tuple):
monochrome_color = sum(pixel_color[:3]) / len(pixel_color[:3]) monochrome_color = sum(pixel_color[:3]) // len(pixel_color[:3])
elif isinstance(pixel_color, int): elif isinstance(pixel_color, int):
monochrome_color = pixel_color monochrome_color = pixel_color
else: else:
@ -580,7 +580,8 @@ class WriteLibUsb(WriteMethod):
WriteMethod.__init__(self) WriteMethod.__init__(self)
self.description = None self.description = None
self.dev = None self.dev = None
self.endpoint = None self.endpoint_out = None
self.endpoint_in = None
def get_name(self): def get_name(self):
return 'libusb' return 'libusb'
@ -591,7 +592,8 @@ class WriteLibUsb(WriteMethod):
def _open(self, device_id): def _open(self, device_id):
self.description = self.devices[device_id][0] self.description = self.devices[device_id][0]
self.dev = self.devices[device_id][1] 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") print("Libusb device initialized")
return True return True
@ -601,7 +603,8 @@ class WriteLibUsb(WriteMethod):
WriteLibUsb.usb.util.dispose_resources(self.dev) WriteLibUsb.usb.util.dispose_resources(self.dev)
self.description = None self.description = None
self.dev = None self.dev = None
self.endpoint = None self.endpoint_out = None
self.endpoint_in = None
def _get_available_devices(self): def _get_available_devices(self):
devs = WriteLibUsb.usb.core.find(idVendor=0x0416, idProduct=0x5020, find_all=True) 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( eps = WriteLibUsb.usb.util.find_descriptor(
cfg, cfg,
find_all=True, 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: for ep in eps:
did = "%d:%d:%d" % (d.bus, d.address, ep.bEndpointAddress) if WriteLibUsb.usb.util.endpoint_direction(ep.bEndpointAddress) == WriteLibUsb.usb.util.ENDPOINT_OUT:
descr = ("%s - %s (bus=%d dev=%d endpoint=%d)" % ep_out = ep
(d.manufacturer, d.product, d.bus, d.address, ep.bEndpointAddress)) if WriteLibUsb.usb.util.endpoint_direction(ep.bEndpointAddress) == WriteLibUsb.usb.util.ENDPOINT_IN:
devices[did] = (descr, d, ep) 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 return devices
def is_ready(self): def is_ready(self):
@ -661,7 +672,8 @@ class WriteLibUsb(WriteMethod):
print("Write using %s via libusb" % (self.description,)) print("Write using %s via libusb" % (self.description,))
for i in range(int(len(buf) / 64)): for i in range(int(len(buf) / 64)):
time.sleep(0.1) 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): class WriteUsbHidApi(WriteMethod):

Loading…
Cancel
Save