Skip to content

Commit 65762f6

Browse files
committed
Add basic keypad support
1 parent c8ad6ca commit 65762f6

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

micropython/usbd/hidkeypad.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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"))

micropython/usbd/keycodes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
_KEYPAD_KEYS = [
2+
"<NumLock>", "/", "*", "-", "+", "<Enter>", "1", "2", "3", "4", "5", "6",
3+
"7", "8", "9", "0", "."
4+
]
5+
6+
KEYPAD_KEYCODES_TO_KEYS = {k + 0x53: v for k, v in enumerate(_KEYPAD_KEYS)}
7+
KEYPAD_KEYS_TO_KEYCODES = {v: k for k, v in KEYPAD_KEYCODES_TO_KEYS.items()}

0 commit comments

Comments
 (0)