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. 68
      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,34 +399,27 @@ class LedNameBadge:
) )
_have_pyhidapi = False _have_pyhidapi = False
try:
@staticmethod if sys.version_info[0] < 3:
def init_class(): raise Exception("Prefer usb.core with python-2.x because of https://github.com/jnweiger/led-badge-ls32/issues/9")
import pyhidapi
pyhidapi.hid_init()
_have_pyhidapi = True
print("Pyhidapi detected")
except Exception:
try: try:
if sys.version_info[0] < 3: import usb.core
raise Exception("prefer usb.core with python-2.x because of https://github.com/jnweiger/led-badge-ls32/issues/9") print("Pyusb detected")
if pyhidapi: except:
pyhidapi.hid_init() print("ERROR: Need the pyhidapi or usb.core module.")
LedNameBadge._have_pyhidapi = True if sys.platform == "darwin":
print("Pyhidapi detected") print("""Please try
else:
raise Exception()
except Exception as e:
try:
if usb.core:
print("Pyusb detected")
else:
raise Exception()
except:
print("ERROR: Need the pyhidapi or usb.core module.")
if sys.platform == "darwin":
print("""Please try
pip3 install pyhidapi pip3 install pyhidapi
pip install pyhidapi pip install pyhidapi
brew install hidapi brew install hidapi
""") """)
elif sys.platform == "linux": elif sys.platform == "linux":
print("""Please try print("""Please try
sudo pip3 install pyhidapi sudo pip3 install pyhidapi
sudo pip install pyhidapi sudo pip install pyhidapi
sudo apt-get install libhidapi-hidraw0 sudo apt-get install libhidapi-hidraw0
@ -444,9 +427,9 @@ class LedNameBadge:
or or
sudo apt-get install python3-usb sudo apt-get install python3-usb
""") """)
else: # windows? else: # windows?
print("""Please with Linux or MacOS or help us implement support for """ + sys.platform) print("""Please with Linux or MacOS or help us implement support for """ + sys.platform)
sys.exit(1) sys.exit(1)
@staticmethod @staticmethod
@ -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