Last active
March 26, 2023 20:48
-
-
Save fauxpark/03a3efcc7dbdfbfe57791ea267b13c55 to your computer and use it in GitHub Desktop.
rawhid in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
// This function is called when a packet is received on the raw HID interface. | |
// `length` will always be the size of the output (host to device) report in bytes - 32 in current QMK, but will eventually be raised to 64. | |
// Thus, if you wish to send variable length data, you should send the length along with the payload, splitting across multiple reports | |
// if needed, and handle the parsing yourself. | |
// | |
// In this simple example, we check that the first byte of the received data is the ASCII character 'A', | |
// in which case we respond with 'B' and toggle the backlight. | |
void raw_hid_receive(uint8_t *data, uint8_t length) { | |
uint8_t response[RAW_EPSIZE]; | |
memset(response, 0, RAW_EPSIZE); | |
response[0] = 'B'; | |
if(data[0] == 'A') { | |
raw_hid_send(response, RAW_EPSIZE); | |
backlight_toggle(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# NOTE: There are two HID libraries in Python: pyhidapi (https://pypi.org/project/hid/) and cython-hidapi (https://pypi.org/project/hidapi/). | |
# Both have similar, but not quite compatible, APIs. This example code uses pyhidapi. | |
import sys | |
import hid | |
vendor_id = 0x4335 | |
product_id = 0x0002 | |
usage_page = 0xFF60 | |
usage = 0x61 | |
def get_raw_hid_interface(): | |
device_interfaces = hid.enumerate(vendor_id, product_id) | |
raw_hid_interfaces = [i for i in device_interfaces if i['usage_page'] == usage_page and i['usage'] == usage] | |
if len(raw_hid_interfaces) == 0: | |
return None | |
interface = hid.Device(path=raw_hid_interfaces[0]['path']) | |
print("Manufacturer: %s" % interface.manufacturer) | |
print("Product: %s" % interface.product) | |
return interface | |
def send_raw_packet(data): | |
interface = get_raw_hid_interface() | |
if interface is None: | |
print("No device found") | |
sys.exit(1) | |
request_data = [0x00] * 33 # First byte is Report ID | |
request_data[1:len(data) + 1] = data | |
request_packet = bytes(request_data) | |
print("Request:") | |
print(request_packet) | |
try: | |
interface.write(request_packet) | |
response_packet = interface.read(32, timeout=1000) | |
print("Response:") | |
print(response_packet) | |
finally: | |
interface.close() | |
if __name__ == '__main__': | |
send_raw_packet([ | |
0x41 | |
]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment