Skip to content

Commit 756d761

Browse files
committed
usbd: Update hid_keypad example module.
- Use SET_REPORT via control transfer. - Merge the keycodes module into hid_keypad. - Remove LEDs other than NumLock to make the report handler even simpler.
1 parent 3765d04 commit 756d761

File tree

3 files changed

+96
-117
lines changed

3 files changed

+96
-117
lines changed

micropython/usbd/hid_keypad.py

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
# MicroPython USB keypad module
2+
# MIT license; Copyright (c) 2023 Dave Wickham, Angus Gratton
3+
4+
from .hid import HIDInterface
5+
from micropython import const
6+
7+
_INTERFACE_PROTOCOL_KEYBOARD = const(0x01)
8+
9+
# See HID Usages and Descriptions 1.4, section 10 Keyboard/Keypad Page (0x07)
10+
#
11+
# This keypad example has a contiguous series of keys (KEYPAD_KEY_IDS) starting
12+
# from the NumLock/Clear keypad key (0x53), but you can send any Key IDs from
13+
# the table in the HID Usages specification.
14+
_KEYPAD_KEY_OFFS = const(0x53)
15+
16+
_KEYPAD_KEY_IDS = [
17+
"<NumLock>",
18+
"/",
19+
"*",
20+
"-",
21+
"+",
22+
"<Enter>",
23+
"1",
24+
"2",
25+
"3",
26+
"4",
27+
"5",
28+
"6",
29+
"7",
30+
"8",
31+
"9",
32+
"0",
33+
".",
34+
]
35+
36+
37+
def _key_to_id(key):
38+
# This is a little slower than making a dict for lookup, but uses
39+
# less memory and O(n) can be fast enough when n is small.
40+
return _KEYPAD_KEY_IDS.index(key) + _KEYPAD_KEY_OFFS
41+
42+
43+
# fmt: off
44+
_KEYPAD_REPORT_DESC = bytes(
45+
[
46+
0x05, 0x01, # Usage Page (Generic Desktop)
47+
0x09, 0x07, # Usage (Keypad)
48+
0xA1, 0x01, # Collection (Application)
49+
0x05, 0x07, # Usage Page (Keypad)
50+
0x19, 0x00, # Usage Minimum (0)
51+
0x29, 0xFF, # Usage Maximum (ff)
52+
0x15, 0x00, # Logical Minimum (0)
53+
0x25, 0xFF, # Logical Maximum (ff)
54+
0x95, 0x01, # Report Count (1),
55+
0x75, 0x08, # Report Size (8),
56+
0x81, 0x00, # Input (Data, Array, Absolute)
57+
0x05, 0x08, # Usage page (LEDs)
58+
0x19, 0x01, # Usage Minimum (1)
59+
0x29, 0x01, # Usage Maximum (1),
60+
0x95, 0x01, # Report Count (1),
61+
0x75, 0x01, # Report Size (1),
62+
0x91, 0x02, # Output (Data, Variable, Absolute)
63+
0x95, 0x01, # Report Count (1),
64+
0x75, 0x07, # Report Size (7),
65+
0x91, 0x01, # Output (Constant) - padding bits
66+
0xC0, # End Collection
67+
]
68+
)
69+
# fmt: on
70+
71+
72+
class KeypadInterface(HIDInterface):
73+
# Very basic synchronous USB keypad HID interface
74+
75+
def __init__(self):
76+
self.numlock = False
77+
self.set_report_initialised = False
78+
super().__init__(
79+
_KEYPAD_REPORT_DESC,
80+
set_report_buf=bytearray(1),
81+
protocol=_INTERFACE_PROTOCOL_KEYBOARD,
82+
interface_str="MicroPython Keypad",
83+
)
84+
85+
def handle_set_report(self, report_data, _report_id, _report_type):
86+
report = report_data[0]
87+
b = bool(report & 1)
88+
if b != self.numlock:
89+
print("Numlock: ", b)
90+
self.numlock = b
91+
92+
def send_key(self, key=None):
93+
if key is None:
94+
self.send_report(b"\x00")
95+
else:
96+
self.send_report(_key_to_id(key).to_bytes(1, "big"))

micropython/usbd/hidkeypad.py

-93
This file was deleted.

micropython/usbd/keycodes.py

-24
This file was deleted.

0 commit comments

Comments
 (0)