Skip to content

Instantly share code, notes, and snippets.

@mdkcore0
Last active December 6, 2019 19:11
Show Gist options
  • Save mdkcore0/1ad21b712591dc0416b1f29bca7caf4e to your computer and use it in GitHub Desktop.
Save mdkcore0/1ad21b712591dc0416b1f29bca7caf4e to your computer and use it in GitHub Desktop.
python hid test (linux/macos), should replace pyudev
# hid==1.0.4
# pyusb==1.0.2
# NOTE: should install hodpai (brew/package manager)
import hid
import binascii
def dec_to_hex(value):
return "{0:x}".format(value)
# interface = bInterfaceNumber, devices can have multiple interfaces
# choose wisely ;)
# some info can be gathered by:
# $ lsusb -v
# or device specific:
# $ lsusb -d vid:pid -v
supported_devices = {
'Tipro': {'vendor_id': '1222',
'devices': [
{'name': 'Handset Controller',
'product_id': 'facb', 'interface': 1}
]
}
}
devices_to_bind = {}
# IF we decide to use pyusb to get device info, we can use it to get
# bInterfaceNumber using a custom matcher by class (3), and also get
# wMaxPacketSize as a bonus
# also, MAYBE, we can register a callback to be notified about device
# add/remove (https://github.com/pyusb/pyusb/pull/160)
for dev in hid.enumerate():
manufacturer = dev.get('manufacturer_string')
product = dev.get('product_string')
if manufacturer in supported_devices:
for d in supported_devices[manufacturer]['devices']:
if product == d['name'] and \
dev.get('interface_number') == d['interface'] and \
dec_to_hex(dev.get('product_id')) == d['product_id']:
# print(d, dev)
# print(dec_to_hex(dev.get('usage')),
# dec_to_hex(dev.get('usage_page')))
devices_to_bind["%s %s" % (manufacturer, product)] = \
{'path': dev.get('path')}
print("devices to bind:", devices_to_bind)
# and open a device to read it's data:
# on linux we can open hidraw directly; check if we can do it on macos as well
d = hid.Device(path=devices_to_bind['Tipro Handset Controller']['path'])
# 8 = wMaxPacketSize
while True:
data = binascii.hexlify(d.read(8)).decode()
# macos keep reading "0000000000000000" while idle
# macos now: "0100000000000000"
if data:
print(data)
d.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment