HID
busylight_core.hid
¶
USB Human Interface Device (HID) Porcelain.
Why This File Exists.
The 'apmorton/pyhidapi' package and 'trezor/cython-hidapi' packages both occupy the namespace hid when installed. Regardless of install order, apmorton/pyhidapi wins and overwrites cython-hidapi in the directory /lib/python3/site-packages/hid. The two packages provide similar support for performing IO to USB human interface devices with small but significant differences. Since I cannot control which package is installed first, I've provided a wrapper around the two packages that allows either to be used without changing the consumer code.
I originally picked the trezor/cython-hidapi package for HID support because it does not require the user to install additional non-Python software. The pyhidapi package is a ctypes interface to the hidapi shared object which the user must install in a seperate operation.
Both cython-hidapi and pyhidapi modules provide a function enumerate
which returns a list of dictionaries, each dictionary describing
discovered USB devices.
The Device class in this module wallpapers over the differences between cython-hidapi:hid.device and pyhidapi:hid.Device, favoring the open() semantics of cython-hidapi:hid.device since it requires fewer changes to the consumer in this package.
Hopefully at some point in the future the two projects can come to an agreement and resolve the namespace collision. Then this file can go away and the project can consume hid directly.
Classes¶
busylight_core.hid.Device
¶
A wrapper around the hidapi Device class.
Initialize the Device wrapper.
Source code in src/busylight_core/hid.py
Attributes¶
Functions¶
busylight_core.hid.Device.open
¶
Open the first device matching vendor_id, product_id or serial number.
:param vendor_id: 16-bit USB vendor ID of the device :param product_id: 16-bit USB product ID of the device :param serial_number: Optional serial number of the device
Raises: - HardwareAlreadyOpenError
Source code in src/busylight_core/hid.py
busylight_core.hid.Device.open_path
¶
Open the device specified by path.
If the path is given as a string, it is encoded to bytes before opening the device.
:param path: Filesystem path to the device
Raises: - HardwareAlreadyOpenError - OSError
Source code in src/busylight_core/hid.py
busylight_core.hid.Device.close
¶
Close this device.
Raises: - HardwareNotOpenError - OSError
busylight_core.hid.Device.read
¶
Read nbytes from the device, returns a list of ints.
If timeout_ms is None, the read operation will block until the requested number of bytes are read or an error occurs.
:param nbytes: Number of bytes to read :param timeout_ms: Optional timeout in milliseconds for the read operation
Raises: - HardwareNotOpenError - OSError
Source code in src/busylight_core/hid.py
busylight_core.hid.Device.write
¶
Write bytes in buf to the device.
:param buf: Bytes to write to the device Raises: - HardwareNotOpenError - OSError
Source code in src/busylight_core/hid.py
busylight_core.hid.Device.get_feature_report
¶
Read a nbytes feature report from the device.
:param report: Feature report ID to read :param nbytes: Number of bytes to read from the feature report
Raises: - HardwareNotOpenError - OSError
Source code in src/busylight_core/hid.py
busylight_core.hid.Device.send_feature_report
¶
Write bytes in buf using device feature report.
:param buf: Bytes to send as a feature report
Raises: - HardwareNotOpenError - OSError