Rearranging code: code for USB importing libraries directly in class instead of method, that's less structured but easier.

rebase020124
Ben 12 months ago
parent d13f9ebf9f
commit 67738e87da
  1. 40
      led-badge-11x44.py

@ -57,16 +57,6 @@ import time
from array import array from array import array
from datetime import datetime from datetime import datetime
# There are 2 possibilities for accessing the device: using hidapi oer usb-core. Here we just try to import both
# and decide later which one to use, see below in LedNameBadge._init_class().
try:
import pyhidapi
except:
pass
try:
import usb.core
except:
pass
__version = "0.12" __version = "0.12"
@ -409,24 +399,17 @@ class LedNameBadge:
) )
_have_pyhidapi = False _have_pyhidapi = False
@staticmethod
def init_class():
try: try:
if sys.version_info[0] < 3: if sys.version_info[0] < 3:
raise Exception("prefer usb.core with python-2.x because of https://github.com/jnweiger/led-badge-ls32/issues/9") raise Exception("Prefer usb.core with python-2.x because of https://github.com/jnweiger/led-badge-ls32/issues/9")
if pyhidapi: import pyhidapi
pyhidapi.hid_init() pyhidapi.hid_init()
LedNameBadge._have_pyhidapi = True _have_pyhidapi = True
print("Pyhidapi detected") print("Pyhidapi detected")
else: except Exception:
raise Exception()
except Exception as e:
try: try:
if usb.core: import usb.core
print("Pyusb detected") print("Pyusb detected")
else:
raise Exception()
except: except:
print("ERROR: Need the pyhidapi or usb.core module.") print("ERROR: Need the pyhidapi or usb.core module.")
if sys.platform == "darwin": if sys.platform == "darwin":
@ -505,20 +488,20 @@ or
sys.exit(1) sys.exit(1)
if LedNameBadge._have_pyhidapi: if LedNameBadge._have_pyhidapi:
dev_info = pyhidapi.hid_enumerate(0x0416, 0x5020) dev_info = LedNameBadge.pyhidapi.hid_enumerate(0x0416, 0x5020)
# dev = pyhidapi.hid_open(0x0416, 0x5020) # dev = pyhidapi.hid_open(0x0416, 0x5020)
if dev_info: if dev_info:
dev = pyhidapi.hid_open_path(dev_info[0].path) dev = LedNameBadge.pyhidapi.hid_open_path(dev_info[0].path)
print("using [%s %s] int=%d page=%s via pyHIDAPI" % ( print("using [%s %s] int=%d page=%s via pyHIDAPI" % (
dev_info[0].manufacturer_string, dev_info[0].product_string, dev_info[0].interface_number, dev_info[0].usage_page)) dev_info[0].manufacturer_string, dev_info[0].product_string, dev_info[0].interface_number, dev_info[0].usage_page))
else: else:
print("No led tag with vendorID 0x0416 and productID 0x5020 found.") print("No led tag with vendorID 0x0416 and productID 0x5020 found.")
print("Connect the led tag and run this tool as root.") print("Connect the led tag and run this tool as root.")
sys.exit(1) sys.exit(1)
pyhidapi.hid_write(dev, buf) LedNameBadge.pyhidapi.hid_write(dev, buf)
pyhidapi.hid_close(dev) LedNameBadge.pyhidapi.hid_close(dev)
else: else:
dev = usb.core.find(idVendor=0x0416, idProduct=0x5020) dev = LedNameBadge.usb.core.find(idVendor=0x0416, idProduct=0x5020)
if dev is None: if dev is None:
print("No led tag with vendorID 0x0416 and productID 0x5020 found.") print("No led tag with vendorID 0x0416 and productID 0x5020 found.")
print("Connect the led tag and run this tool as root.") print("Connect the led tag and run this tool as root.")
@ -536,9 +519,6 @@ or
dev.write(1, buf[i * 64:i * 64 + 64]) dev.write(1, buf[i * 64:i * 64 + 64])
LedNameBadge.init_class()
def split_to_ints(list_str): def split_to_ints(list_str):
return tuple([int(x) for x in re.split(r'[\s,]+', list_str)]) return tuple([int(x) for x in re.split(r'[\s,]+', list_str)])

Loading…
Cancel
Save