Skip to content

Commit bad77d0

Browse files
authored
Merge pull request #56 from dhalbert/update-hid-for-bonding
Disconnect when restarting HID example; provide default HID descriptor
2 parents 40dc09e + dfea281 commit bad77d0

File tree

2 files changed

+114
-113
lines changed

2 files changed

+114
-113
lines changed

adafruit_ble/services/standard/hid.py

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,111 @@
5757
_APPEARANCE_HID_JOYSTICK = const(963)
5858
_APPEARANCE_HID_GAMEPAD = const(964)
5959

60+
#pylint: disable=line-too-long
61+
DEFAULT_HID_DESCRIPTOR = (
62+
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
63+
b'\x09\x06' # Usage (Keyboard)
64+
b'\xA1\x01' # Collection (Application)
65+
b'\x85\x01' # Report ID (1)
66+
b'\x05\x07' # Usage Page (Kbrd/Keypad)
67+
b'\x19\xE0' # Usage Minimum (\xE0)
68+
b'\x29\xE7' # Usage Maximum (\xE7)
69+
b'\x15\x00' # Logical Minimum (0)
70+
b'\x25\x01' # Logical Maximum (1)
71+
b'\x75\x01' # Report Size (1)
72+
b'\x95\x08' # Report Count (8)
73+
b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
74+
b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
75+
b'\x19\x00' # Usage Minimum (\x00)
76+
b'\x29\x65' # Usage Maximum (\x65)
77+
b'\x15\x00' # Logical Minimum (0)
78+
b'\x25\x65' # Logical Maximum (101)
79+
b'\x75\x08' # Report Size (8)
80+
b'\x95\x06' # Report Count (6)
81+
b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
82+
b'\x05\x08' # Usage Page (LEDs)
83+
b'\x19\x01' # Usage Minimum (Num Lock)
84+
b'\x29\x05' # Usage Maximum (Kana)
85+
b'\x15\x00' # Logical Minimum (0)
86+
b'\x25\x01' # Logical Maximum (1)
87+
b'\x75\x01' # Report Size (1)
88+
b'\x95\x05' # Report Count (5)
89+
b'\x91\x02' # Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
90+
b'\x95\x03' # Report Count (3)
91+
b'\x91\x01' # Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
92+
b'\xC0' # End Collection
93+
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
94+
b'\x09\x02' # Usage (Mouse)
95+
b'\xA1\x01' # Collection (Application)
96+
b'\x09\x01' # Usage (Pointer)
97+
b'\xA1\x00' # Collection (Physical)
98+
b'\x85\x02' # Report ID (2)
99+
b'\x05\x09' # Usage Page (Button)
100+
b'\x19\x01' # Usage Minimum (\x01)
101+
b'\x29\x05' # Usage Maximum (\x05)
102+
b'\x15\x00' # Logical Minimum (0)
103+
b'\x25\x01' # Logical Maximum (1)
104+
b'\x95\x05' # Report Count (5)
105+
b'\x75\x01' # Report Size (1)
106+
b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
107+
b'\x95\x01' # Report Count (1)
108+
b'\x75\x03' # Report Size (3)
109+
b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
110+
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
111+
b'\x09\x30' # Usage (X)
112+
b'\x09\x31' # Usage (Y)
113+
b'\x15\x81' # Logical Minimum (-127)
114+
b'\x25\x7F' # Logical Maximum (127)
115+
b'\x75\x08' # Report Size (8)
116+
b'\x95\x02' # Report Count (2)
117+
b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
118+
b'\x09\x38' # Usage (Wheel)
119+
b'\x15\x81' # Logical Minimum (-127)
120+
b'\x25\x7F' # Logical Maximum (127)
121+
b'\x75\x08' # Report Size (8)
122+
b'\x95\x01' # Report Count (1)
123+
b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
124+
b'\xC0' # End Collection
125+
b'\xC0' # End Collection
126+
b'\x05\x0C' # Usage Page (Consumer)
127+
b'\x09\x01' # Usage (Consumer Control)
128+
b'\xA1\x01' # Collection (Application)
129+
b'\x85\x03' # Report ID (3)
130+
b'\x75\x10' # Report Size (16)
131+
b'\x95\x01' # Report Count (1)
132+
b'\x15\x01' # Logical Minimum (1)
133+
b'\x26\x8C\x02' # Logical Maximum (652)
134+
b'\x19\x01' # Usage Minimum (Consumer Control)
135+
b'\x2A\x8C\x02' # Usage Maximum (AC Send)
136+
b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
137+
b'\xC0' # End Collection
138+
# b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
139+
# b'\x09\x05' # Usage (Game Pad)
140+
# b'\xA1\x01' # Collection (Application)
141+
# b'\x85\x05' # Report ID (5)
142+
# b'\x05\x09' # Usage Page (Button)
143+
# b'\x19\x01' # Usage Minimum (\x01)
144+
# b'\x29\x10' # Usage Maximum (\x10)
145+
# b'\x15\x00' # Logical Minimum (0)
146+
# b'\x25\x01' # Logical Maximum (1)
147+
# b'\x75\x01' # Report Size (1)
148+
# b'\x95\x10' # Report Count (16)
149+
# b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
150+
# b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
151+
# b'\x15\x81' # Logical Minimum (-127)
152+
# b'\x25\x7F' # Logical Maximum (127)
153+
# b'\x09\x30' # Usage (X)
154+
# b'\x09\x31' # Usage (Y)
155+
# b'\x09\x32' # Usage (Z)
156+
# b'\x09\x35' # Usage (Rz)
157+
# b'\x75\x08' # Report Size (8)
158+
# b'\x95\x04' # Report Count (4)
159+
# b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
160+
# b'\xC0' # End Collection
161+
)
162+
"""Default HID descriptor: provides mouse, keyboard, and consumer control devices."""
163+
#pylint: enable=line-too-long
164+
60165

61166
# Boot keyboard and mouse not currently supported.
62167
_BOOT_KEYBOARD_INPUT_REPORT_UUID_NUM = const(0x2A22)
@@ -191,7 +296,7 @@ class HIDService(Service):
191296
max_value=1)
192297
"""Controls whether the device should be suspended (0) or not (1)."""
193298

194-
def __init__(self, hid_descriptor=None, service=None):
299+
def __init__(self, hid_descriptor=DEFAULT_HID_DESCRIPTOR, service=None):
195300
super().__init__(report_map=hid_descriptor)
196301
if service:
197302
# TODO: Add support for connecting to a remote hid server.

examples/ble_hid_periph.py

Lines changed: 8 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -14,125 +14,21 @@
1414
from adafruit_hid.keyboard import Keyboard
1515
from adafruit_hid.keyboard_layout_us import KeyboardLayoutUS
1616

17-
#pylint: disable=line-too-long
18-
HID_DESCRIPTOR = (
19-
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
20-
b'\x09\x06' # Usage (Keyboard)
21-
b'\xA1\x01' # Collection (Application)
22-
b'\x85\x01' # Report ID (1)
23-
b'\x05\x07' # Usage Page (Kbrd/Keypad)
24-
b'\x19\xE0' # Usage Minimum (\xE0)
25-
b'\x29\xE7' # Usage Maximum (\xE7)
26-
b'\x15\x00' # Logical Minimum (0)
27-
b'\x25\x01' # Logical Maximum (1)
28-
b'\x75\x01' # Report Size (1)
29-
b'\x95\x08' # Report Count (8)
30-
b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
31-
b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
32-
b'\x19\x00' # Usage Minimum (\x00)
33-
b'\x29\x65' # Usage Maximum (\x65)
34-
b'\x15\x00' # Logical Minimum (0)
35-
b'\x25\x65' # Logical Maximum (101)
36-
b'\x75\x08' # Report Size (8)
37-
b'\x95\x06' # Report Count (6)
38-
b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
39-
b'\x05\x08' # Usage Page (LEDs)
40-
b'\x19\x01' # Usage Minimum (Num Lock)
41-
b'\x29\x05' # Usage Maximum (Kana)
42-
b'\x15\x00' # Logical Minimum (0)
43-
b'\x25\x01' # Logical Maximum (1)
44-
b'\x75\x01' # Report Size (1)
45-
b'\x95\x05' # Report Count (5)
46-
b'\x91\x02' # Output (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
47-
b'\x95\x03' # Report Count (3)
48-
b'\x91\x01' # Output (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position,Non-volatile)
49-
b'\xC0' # End Collection
50-
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
51-
b'\x09\x02' # Usage (Mouse)
52-
b'\xA1\x01' # Collection (Application)
53-
b'\x09\x01' # Usage (Pointer)
54-
b'\xA1\x00' # Collection (Physical)
55-
b'\x85\x02' # Report ID (2)
56-
b'\x05\x09' # Usage Page (Button)
57-
b'\x19\x01' # Usage Minimum (\x01)
58-
b'\x29\x05' # Usage Maximum (\x05)
59-
b'\x15\x00' # Logical Minimum (0)
60-
b'\x25\x01' # Logical Maximum (1)
61-
b'\x95\x05' # Report Count (5)
62-
b'\x75\x01' # Report Size (1)
63-
b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
64-
b'\x95\x01' # Report Count (1)
65-
b'\x75\x03' # Report Size (3)
66-
b'\x81\x01' # Input (Const,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
67-
b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
68-
b'\x09\x30' # Usage (X)
69-
b'\x09\x31' # Usage (Y)
70-
b'\x15\x81' # Logical Minimum (-127)
71-
b'\x25\x7F' # Logical Maximum (127)
72-
b'\x75\x08' # Report Size (8)
73-
b'\x95\x02' # Report Count (2)
74-
b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
75-
b'\x09\x38' # Usage (Wheel)
76-
b'\x15\x81' # Logical Minimum (-127)
77-
b'\x25\x7F' # Logical Maximum (127)
78-
b'\x75\x08' # Report Size (8)
79-
b'\x95\x01' # Report Count (1)
80-
b'\x81\x06' # Input (Data,Var,Rel,No Wrap,Linear,Preferred State,No Null Position)
81-
b'\xC0' # End Collection
82-
b'\xC0' # End Collection
83-
b'\x05\x0C' # Usage Page (Consumer)
84-
b'\x09\x01' # Usage (Consumer Control)
85-
b'\xA1\x01' # Collection (Application)
86-
b'\x85\x03' # Report ID (3)
87-
b'\x75\x10' # Report Size (16)
88-
b'\x95\x01' # Report Count (1)
89-
b'\x15\x01' # Logical Minimum (1)
90-
b'\x26\x8C\x02' # Logical Maximum (652)
91-
b'\x19\x01' # Usage Minimum (Consumer Control)
92-
b'\x2A\x8C\x02' # Usage Maximum (AC Send)
93-
b'\x81\x00' # Input (Data,Array,Abs,No Wrap,Linear,Preferred State,No Null Position)
94-
b'\xC0' # End Collection
95-
# b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
96-
# b'\x09\x05' # Usage (Game Pad)
97-
# b'\xA1\x01' # Collection (Application)
98-
# b'\x85\x05' # Report ID (5)
99-
# b'\x05\x09' # Usage Page (Button)
100-
# b'\x19\x01' # Usage Minimum (\x01)
101-
# b'\x29\x10' # Usage Maximum (\x10)
102-
# b'\x15\x00' # Logical Minimum (0)
103-
# b'\x25\x01' # Logical Maximum (1)
104-
# b'\x75\x01' # Report Size (1)
105-
# b'\x95\x10' # Report Count (16)
106-
# b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
107-
# b'\x05\x01' # Usage Page (Generic Desktop Ctrls)
108-
# b'\x15\x81' # Logical Minimum (-127)
109-
# b'\x25\x7F' # Logical Maximum (127)
110-
# b'\x09\x30' # Usage (X)
111-
# b'\x09\x31' # Usage (Y)
112-
# b'\x09\x32' # Usage (Z)
113-
# b'\x09\x35' # Usage (Rz)
114-
# b'\x75\x08' # Report Size (8)
115-
# b'\x95\x04' # Report Count (4)
116-
# b'\x81\x02' # Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
117-
# b'\xC0' # End Collection
118-
)
119-
#pylint: enable=line-too-long
120-
121-
hid = HIDService(HID_DESCRIPTOR)
17+
# Use default HID descriptor
18+
hid = HIDService()
12219
device_info = DeviceInfoService(software_revision=adafruit_ble.__version__,
12320
manufacturer="Adafruit Industries")
12421
advertisement = ProvideServicesAdvertisement(hid)
12522
advertisement.appearance = 961
12623
scan_response = Advertisement()
127-
scan_response.complete_name = "CircuitPython HID"
12824

12925
ble = adafruit_ble.BLERadio()
130-
if not ble.connected:
131-
print("advertising")
132-
ble.start_advertising(advertisement, scan_response)
133-
else:
134-
print("already connected")
135-
print(ble.connections)
26+
if ble.connected:
27+
for c in ble.connections:
28+
c.disconnect()
29+
30+
print("advertising")
31+
ble.start_advertising(advertisement, scan_response)
13632

13733
k = Keyboard(hid.devices)
13834
kl = KeyboardLayoutUS(k)

0 commit comments

Comments
 (0)