You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.3 KiB

  1. # Raw HID :id=raw-hid
  2. The Raw HID feature allows for bidirectional communication between QMK and the host computer over an HID interface. This has many potential use cases, such as switching keymaps on the fly or sending useful metrics like CPU/RAM usage.
  3. In order to communicate with the keyboard using this feature, you will need to write a program that runs on the host. As such, some basic programming skills are required - more if you intend to implement complex behaviour.
  4. ## Usage :id=usage
  5. Add the following to your `rules.mk`:
  6. ```make
  7. RAW_ENABLE = yes
  8. ```
  9. ## Basic Configuration :id=basic-configuration
  10. By default, the HID Usage Page and Usage ID for the Raw HID interface are `0xFF60` and `0x61`. However, they can be changed if necessary by adding the following to your `config.h`:
  11. |Define |Default |Description |
  12. |----------------|--------|---------------------------------------|
  13. |`RAW_USAGE_PAGE`|`0xFF60`|The usage page of the Raw HID interface|
  14. |`RAW_USAGE_ID` |`0x61` |The usage ID of the Raw HID interface |
  15. ## Sending Data to the Keyboard :id=sending-data-to-the-keyboard
  16. To send data to the keyboard, you must first find a library for communicating with HID devices in the programming language of your choice. Here are some examples:
  17. * **Node.js:** [node-hid](https://github.com/node-hid/node-hid)
  18. * **C/C++:** [hidapi](https://github.com/libusb/hidapi)
  19. * **Java:** [purejavahidapi](https://github.com/nyholku/purejavahidapi) and [hid4java](https://github.com/gary-rowe/hid4java)
  20. * **Python:** [pyhidapi](https://pypi.org/project/hid/) and [pywinusb](https://pypi.org/project/pywinusb)
  21. Please refer to these libraries' own documentation for instructions on usage. Remember to close the device once you are finished with it!
  22. Next, you will need to know the USB Vendor and Product IDs of the device. These can easily be found by looking at your keyboard's `info.json`, under the `usb` object (alternatively, you can also use Device Manager on Windows, System Information on macOS, or `lsusb` on Linux). For example, the Vendor ID for the Planck Rev 6 is `0x03A8`, and the Product ID is `0xA4F9`.
  23. It's also a good idea to narrow down the list of potential HID devices the library may give you by filtering on the usage page and usage ID, to avoid accidentally opening the interface on the same device for the keyboard, or mouse, or media keys, etc.
  24. Once you are able to open the HID device and send reports to it, it's time to handle them on the keyboard side. Implement the following function in your `keymap.c` and start coding:
  25. ```c
  26. void raw_hid_receive(uint8_t *data, uint8_t length) {
  27. // Your code goes here
  28. // `data` is a pointer to the buffer containing the received HID report
  29. // `length` is the length of the report - always `RAW_EPSIZE`
  30. }
  31. ```
  32. !> Because the HID specification does not support variable length reports, all reports in both directions must be exactly `RAW_EPSIZE` (currently 32) bytes long, regardless of actual payload length. However, variable length payloads can potentially be implemented on top of this by creating your own data structure that may span multiple reports.
  33. ## Receiving Data from the Keyboard :id=receiving-data-from-the-keyboard
  34. If you need the keyboard to send data back to the host, simply call the `raw_hid_send()` function. It requires two arguments - a pointer to a 32-byte buffer containing the data you wish to send, and the length (which should always be `RAW_EPSIZE`).
  35. The received report can then be handled in whichever way your HID library provides.
  36. ## Simple Example :id=simple-example
  37. The following example reads the first byte of the received report from the host, and if it is an ASCII "A", responds with "B". `memset()` is used to fill the response buffer (which could still contain the previous response) with null bytes.
  38. ```c
  39. void raw_hid_receive(uint8_t *data, uint8_t length) {
  40. uint8_t response[length];
  41. memset(response, 0, length);
  42. response[0] = 'B';
  43. if(data[0] == 'A') {
  44. raw_hid_send(response, length);
  45. }
  46. }
  47. ```
  48. On the host side (here we are using Python and the `pyhidapi` library), the HID device is opened by enumerating the interfaces on the USB device, then filtering on the usage page and usage ID. Then, a report containing a single ASCII "A" (hex `0x41`) is constructed and sent.
  49. For demonstration purposes, the manufacturer and product strings of the device, along with the request and response, are also printed.
  50. ```python
  51. import sys
  52. import hid
  53. vendor_id = 0x4335
  54. product_id = 0x0002
  55. usage_page = 0xFF60
  56. usage = 0x61
  57. report_length = 32
  58. def get_raw_hid_interface():
  59. device_interfaces = hid.enumerate(vendor_id, product_id)
  60. raw_hid_interfaces = [i for i in device_interfaces if i['usage_page'] == usage_page and i['usage'] == usage]
  61. if len(raw_hid_interfaces) == 0:
  62. return None
  63. interface = hid.Device(path=raw_hid_interfaces[0]['path'])
  64. print(f"Manufacturer: {interface.manufacturer}")
  65. print(f"Product: {interface.product}")
  66. return interface
  67. def send_raw_report(data):
  68. interface = get_raw_hid_interface()
  69. if interface is None:
  70. print("No device found")
  71. sys.exit(1)
  72. request_data = [0x00] * (report_length + 1) # First byte is Report ID
  73. request_data[1:len(data) + 1] = data
  74. request_report = bytes(request_data)
  75. print("Request:")
  76. print(request_report)
  77. try:
  78. interface.write(request_report)
  79. response_report = interface.read(report_length, timeout=1000)
  80. print("Response:")
  81. print(response_report)
  82. finally:
  83. interface.close()
  84. if __name__ == '__main__':
  85. send_raw_report([
  86. 0x41
  87. ])
  88. ```
  89. ## API :id=api
  90. ### `void raw_hid_receive(uint8_t *data, uint8_t length)` :id=api-raw-hid-receive
  91. Callback, invoked when a raw HID report has been received from the host.
  92. #### Arguments :id=api-raw-hid-receive-arguments
  93. - `uint8_t *data`
  94. A pointer to the received data. Always 32 bytes in length.
  95. - `uint8_t length`
  96. The length of the buffer. Always 32.
  97. ---
  98. ### `void raw_hid_send(uint8_t *data, uint8_t length)` :id=api-raw-hid-send
  99. Send an HID report.
  100. #### Arguments :id=api-raw-hid-send-arguments
  101. - `uint8_t *data`
  102. A pointer to the data to send. Must always be 32 bytes in length.
  103. - `uint8_t length`
  104. The length of the buffer. Must always be 32.