hidapi.enumerate() returns an iterable

This commit is contained in:
Daniel Pavel 2012-11-02 22:09:04 +02:00
parent c6d1ab4526
commit 8fc078bdaf
4 changed files with 8 additions and 29 deletions

View File

@ -82,8 +82,8 @@ del namedtuple
def _makeDeviceInfo(native_device_info):
return DeviceInfo(
path=native_device_info.path.decode('ascii'),
vendor_id=hex(native_device_info.vendor_id)[2:],
product_id=hex(native_device_info.product_id)[2:],
vendor_id=hex(native_device_info.vendor_id)[2:].zfill(4),
product_id=hex(native_device_info.product_id)[2:].zfill(4),
serial=native_device_info.serial if native_device_info.serial else None,
release=hex(native_device_info.release)[2:],
manufacturer=native_device_info.manufacturer,
@ -187,22 +187,19 @@ def enumerate(vendor_id=None, product_id=None, interface_number=None):
List all the HID devices attached to the system, optionally filtering by
vendor_id, product_id, and/or interface_number.
:returns: a list of matching ``DeviceInfo`` tuples.
:returns: an iterable of matching ``DeviceInfo`` tuples.
"""
results = []
devices = _native.hid_enumerate(vendor_id, product_id)
d = devices
while d:
if interface_number is None or interface_number == d.contents.interface:
results.append(_makeDeviceInfo(d.contents))
yield _makeDeviceInfo(d.contents)
d = d.contents.next_device
if devices:
_native.hid_free_enumeration(devices)
return results
def open(vendor_id, product_id, serial=None):
"""Open a HID device by its Vendor ID, Product ID and optional serial number.
@ -378,15 +375,3 @@ def get_indexed_string(device_handle, index):
:param index: the index of the string to get.
"""
return _read_wchar(_native.hid_get_indexed_string, device_handle, index)
def last_error(device_handle):
"""Get a string describing the last error which occurred.
Note: currently not working in either underlying native implementation.
:param device_handle: a device handle returned by open() or open_path().
:returns: a string containing the last error which occurred, or None.
"""
error = _native.hid_error(device_handle)
return error.value

View File

@ -96,7 +96,7 @@ class Receiver(object):
reply = _base.request(self.handle, 0xFF, b'\x83\xB5', b'\x02')
if reply and reply[0:1] == b'\x02':
fw_version = _hex(reply[1:5])
fw_version = '%s.%s.%s' % (fw_version[0:2], fw_version[2:4], fw_version[4:8])
fw_version = '%s.%s.B%s' % (fw_version[0:2], fw_version[2:4], fw_version[4:8])
firmware.append(_FirmwareInfo(0, FIRMWARE_KIND[0], '', fw_version, None))
reply = _base.request(self.handle, 0xFF, b'\x81\xF1', b'\x04')

View File

@ -110,13 +110,7 @@ def try_open(path):
# 'device 0 unreachable' is the expected reply from a valid receiver handle
_log.info("[%s] success: handle %X", path, receiver_handle)
return receiver_handle
# any other replies are ignored, and will assume this is the wrong Linux device
if reply == b'\x01\x00\x00\x00\x00\x00\x00\x00':
# no idea what this is, but it comes up occasionally
_log.debug("[%s] %X mistery reply [%s]", path, receiver_handle, _hex(reply))
else:
_log.debug("[%s] %X unknown reply [%s]", path, receiver_handle, _hex(reply))
_log.debug("[%s] %X ignored reply %s", path, receiver_handle, _hex(reply))
else:
_log.debug("[%s] %X no reply", path, receiver_handle)

View File

@ -27,8 +27,8 @@ class Test_UR_Base(unittest.TestCase):
def test_10_list_receiver_devices(self):
rawdevices = base.list_receiver_devices()
self.assertIsNotNone(rawdevices, "list_receiver_devices returned None")
self.assertIsInstance(rawdevices, list, "list_receiver_devices should have returned a list")
Test_UR_Base.ur_available = len(rawdevices) > 0
# self.assertIsInstance(rawdevices, Iterable, "list_receiver_devices should have returned an iterable")
Test_UR_Base.ur_available = len(list(rawdevices)) > 0
def test_20_try_open(self):
if not self.ur_available: