|
| 1 | +# MicroPython USB HID custom Keypad example |
| 2 | +# |
| 3 | +# This example demonstrates creating a custom HID device with its own |
| 4 | +# HID descriptor, in this case for a USB number keypad. |
| 5 | +# |
| 6 | +# For higher level examples that require less code to use, see mouse_example.py |
| 7 | +# and keyboard_example.py |
| 8 | +# |
| 9 | +# This example uses the usb-device-hid package for the HIDInterface class. |
| 10 | +# This can be installed with: |
| 11 | +# |
| 12 | +# mpremote mip install usb-device-hid |
| 13 | +# |
| 14 | +# MIT license; Copyright (c) 2023 Dave Wickham, 2023-2024 Angus Gratton |
| 15 | +from micropython import const |
| 16 | +import time |
| 17 | +import usb.device |
| 18 | +from usb.device.hid import HIDInterface |
| 19 | + |
| 20 | +_INTERFACE_PROTOCOL_KEYBOARD = const(0x01) |
| 21 | + |
| 22 | + |
| 23 | +def keypad_example(): |
| 24 | + k = KeypadInterface() |
| 25 | + |
| 26 | + usb.device.get().init(k, builtin_driver=True) |
| 27 | + |
| 28 | + while not k.is_open(): |
| 29 | + time.sleep_ms(100) |
| 30 | + |
| 31 | + while True: |
| 32 | + time.sleep(2) |
| 33 | + print("Press NumLock...") |
| 34 | + k.send_key("<NumLock>") |
| 35 | + time.sleep_ms(100) |
| 36 | + k.send_key() |
| 37 | + time.sleep(1) |
| 38 | + # continue |
| 39 | + print("Press ...") |
| 40 | + for _ in range(3): |
| 41 | + time.sleep(0.1) |
| 42 | + k.send_key(".") |
| 43 | + time.sleep(0.1) |
| 44 | + k.send_key() |
| 45 | + print("Starting again...") |
| 46 | + |
| 47 | + |
| 48 | +class KeypadInterface(HIDInterface): |
| 49 | + # Very basic synchronous USB keypad HID interface |
| 50 | + |
| 51 | + def __init__(self): |
| 52 | + super().__init__( |
| 53 | + _KEYPAD_REPORT_DESC, |
| 54 | + set_report_buf=bytearray(1), |
| 55 | + protocol=_INTERFACE_PROTOCOL_KEYBOARD, |
| 56 | + interface_str="MicroPython Keypad", |
| 57 | + ) |
| 58 | + self.numlock = False |
| 59 | + |
| 60 | + def on_set_report(self, report_data, _report_id, _report_type): |
| 61 | + report = report_data[0] |
| 62 | + b = bool(report & 1) |
| 63 | + if b != self.numlock: |
| 64 | + print("Numlock: ", b) |
| 65 | + self.numlock = b |
| 66 | + |
| 67 | + def send_key(self, key=None): |
| 68 | + if key is None: |
| 69 | + self.send_report(b"\x00") |
| 70 | + else: |
| 71 | + self.send_report(_key_to_id(key).to_bytes(1, "big")) |
| 72 | + |
| 73 | + |
| 74 | +# See HID Usages and Descriptions 1.4, section 10 Keyboard/Keypad Page (0x07) |
| 75 | +# |
| 76 | +# This keypad example has a contiguous series of keys (KEYPAD_KEY_IDS) starting |
| 77 | +# from the NumLock/Clear keypad key (0x53), but you can send any Key IDs from |
| 78 | +# the table in the HID Usages specification. |
| 79 | +_KEYPAD_KEY_OFFS = const(0x53) |
| 80 | + |
| 81 | +_KEYPAD_KEY_IDS = [ |
| 82 | + "<NumLock>", |
| 83 | + "/", |
| 84 | + "*", |
| 85 | + "-", |
| 86 | + "+", |
| 87 | + "<Enter>", |
| 88 | + "1", |
| 89 | + "2", |
| 90 | + "3", |
| 91 | + "4", |
| 92 | + "5", |
| 93 | + "6", |
| 94 | + "7", |
| 95 | + "8", |
| 96 | + "9", |
| 97 | + "0", |
| 98 | + ".", |
| 99 | +] |
| 100 | + |
| 101 | + |
| 102 | +def _key_to_id(key): |
| 103 | + # This is a little slower than making a dict for lookup, but uses |
| 104 | + # less memory and O(n) can be fast enough when n is small. |
| 105 | + return _KEYPAD_KEY_IDS.index(key) + _KEYPAD_KEY_OFFS |
| 106 | + |
| 107 | + |
| 108 | +# HID Report descriptor for a numeric keypad |
| 109 | +# |
| 110 | +# fmt: off |
| 111 | +_KEYPAD_REPORT_DESC = ( |
| 112 | + b'\x05\x01' # Usage Page (Generic Desktop) |
| 113 | + b'\x09\x07' # Usage (Keypad) |
| 114 | + b'\xA1\x01' # Collection (Application) |
| 115 | + b'\x05\x07' # Usage Page (Keypad) |
| 116 | + b'\x19\x00' # Usage Minimum (0) |
| 117 | + b'\x29\xFF' # Usage Maximum (ff) |
| 118 | + b'\x15\x00' # Logical Minimum (0) |
| 119 | + b'\x25\xFF' # Logical Maximum (ff) |
| 120 | + b'\x95\x01' # Report Count (1), |
| 121 | + b'\x75\x08' # Report Size (8), |
| 122 | + b'\x81\x00' # Input (Data, Array, Absolute) |
| 123 | + b'\x05\x08' # Usage page (LEDs) |
| 124 | + b'\x19\x01' # Usage Minimum (1) |
| 125 | + b'\x29\x01' # Usage Maximum (1), |
| 126 | + b'\x95\x01' # Report Count (1), |
| 127 | + b'\x75\x01' # Report Size (1), |
| 128 | + b'\x91\x02' # Output (Data, Variable, Absolute) |
| 129 | + b'\x95\x01' # Report Count (1), |
| 130 | + b'\x75\x07' # Report Size (7), |
| 131 | + b'\x91\x01' # Output (Constant) - padding bits |
| 132 | + b'\xC0' # End Collection |
| 133 | +) |
| 134 | +# fmt: on |
| 135 | + |
| 136 | + |
| 137 | +keypad_example() |
0 commit comments