|
| 1 | +# Implement a keypad |
| 2 | + |
| 3 | +from .hid import HIDInterface |
| 4 | +from .keycodes import KEYPAD_KEYS_TO_KEYCODES |
| 5 | +from micropython import const |
| 6 | +_INTERFACE_PROTOCOL_KEYBOARD = const(0x01) |
| 7 | + |
| 8 | +# Basic 3-button mouse HID Report Descriptor. |
| 9 | +# This is cribbed from Appendix E.10 of the HID v1.11 document. |
| 10 | +_KEYPAD_REPORT_DESC = bytes( |
| 11 | + [ |
| 12 | + 0x05, 0x01, # Usage Page (Generic Desktop) |
| 13 | + 0x09, 0x07, # Usage (Keypad) |
| 14 | + 0xA1, 0x01, # Collection (Application) |
| 15 | + 0x05, 0x07, # Usage Page (Keypad) |
| 16 | + 0x19, 0x00, # Usage Minimum (00), |
| 17 | + 0x29, 0xff, # Usage Maximum (ff), |
| 18 | + 0x15, 0x00, # Logical Minimum (0), |
| 19 | + 0x25, 0xff, # Logical Maximum (ff), |
| 20 | + 0x95, 0x03, # Report Count (3), |
| 21 | + 0x75, 0x08, # Report Size (8), |
| 22 | + 0x81, 0x00, # Input (Data, Array, Absolute) |
| 23 | + 0xC0, # End Collection |
| 24 | + ] |
| 25 | +) |
| 26 | + |
| 27 | + |
| 28 | +class KeypadInterface(HIDInterface): |
| 29 | + # Very basic synchronous USB keypad HID interface |
| 30 | + |
| 31 | + def __init__(self): |
| 32 | + super().__init__( |
| 33 | + _KEYPAD_REPORT_DESC, |
| 34 | + protocol=_INTERFACE_PROTOCOL_KEYBOARD, |
| 35 | + interface_str="MicroPython Keypad!", |
| 36 | + ) |
| 37 | + |
| 38 | + def send_report(self, key): |
| 39 | + super().send_report(KEYPAD_KEYS_TO_KEYCODES[key].to_bytes(1, "big")) |
0 commit comments