-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathhid.py
374 lines (286 loc) · 10.4 KB
/
hid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import supervisor
import usb_hid
from micropython import const
from struct import pack, pack_into
from kmk.keys import (
Axis,
ConsumerKey,
KeyboardKey,
ModifierKey,
MouseKey,
SixAxis,
SpacemouseKey,
)
from kmk.scheduler import cancel_task, create_task
from kmk.utils import Debug, clamp
try:
from adafruit_ble import BLERadio
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
from adafruit_ble.services.standard.hid import HIDService
from storage import getmount
_BLE_APPEARANCE_HID_KEYBOARD = const(961)
except ImportError:
# BLE not supported on this platform
pass
debug = Debug(__name__)
class HIDModes:
NOOP = 0 # currently unused; for testing?
USB = 1
BLE = 2
_USAGE_PAGE_CONSUMER = const(0x0C)
_USAGE_PAGE_KEYBOARD = const(0x01)
_USAGE_PAGE_MOUSE = const(0x01)
_USAGE_PAGE_SIXAXIS = const(0x01)
_USAGE_PAGE_SYSCONTROL = const(0x01)
_USAGE_CONSUMER = const(0x01)
_USAGE_KEYBOARD = const(0x06)
_USAGE_MOUSE = const(0x02)
_USAGE_SIXAXIS = const(0x08)
_USAGE_SYSCONTROL = const(0x80)
_REPORT_SIZE_CONSUMER = const(2)
_REPORT_SIZE_KEYBOARD = const(8)
_REPORT_SIZE_KEYBOARD_NKRO = const(16)
_REPORT_SIZE_MOUSE = const(4)
_REPORT_SIZE_MOUSE_HSCROLL = const(5)
_REPORT_SIZE_SIXAXIS = const(12)
_REPORT_SIZE_SIXAXIS_BUTTON = const(2)
_REPORT_SIZE_SYSCONTROL = const(8)
def find_device(devices, usage_page, usage):
for device in devices:
if (
device.usage_page == usage_page
and device.usage == usage
and hasattr(device, 'send_report')
):
return device
class Report:
def __init__(self, size):
self.buffer = bytearray(size)
self.pending = False
def clear(self):
for k, v in enumerate(self.buffer):
if v:
self.buffer[k] = 0x00
self.pending = True
def get_action_map(self):
return {}
class KeyboardReport(Report):
def __init__(self, size=_REPORT_SIZE_KEYBOARD):
self.buffer = bytearray(size)
self.prev_buffer = bytearray(size)
@property
def pending(self):
return self.buffer != self.prev_buffer
@pending.setter
def pending(self, v):
if v is False:
self.prev_buffer[:] = self.buffer[:]
def clear(self):
for idx in range(len(self.buffer)):
self.buffer[idx] = 0x00
def add_key(self, key):
# Find the first empty slot in the key report, and fill it; drop key if
# report is full.
idx = self.buffer.find(b'\x00', 2)
if 0 < idx < _REPORT_SIZE_KEYBOARD:
self.buffer[idx] = key.code
def remove_key(self, key):
idx = self.buffer.find(pack('B', key.code), 2)
if 0 < idx:
self.buffer[idx] = 0x00
def add_modifier(self, modifier):
self.buffer[0] |= modifier.code
def remove_modifier(self, modifier):
self.buffer[0] &= ~modifier.code
def get_action_map(self):
return {KeyboardKey: self.add_key, ModifierKey: self.add_modifier}
class NKROKeyboardReport(KeyboardReport):
def __init__(self):
super().__init__(_REPORT_SIZE_KEYBOARD_NKRO)
def add_key(self, key):
self.buffer[(key.code >> 3) + 1] |= 1 << (key.code & 0x07)
def remove_key(self, key):
self.buffer[(key.code >> 3) + 1] &= ~(1 << (key.code & 0x07))
class ConsumerControlReport(Report):
def __init__(self):
super().__init__(_REPORT_SIZE_CONSUMER)
def add_cc(self, cc):
pack_into('<H', self.buffer, 0, cc.code)
self.pending = True
def remove_cc(self):
if self.buffer != b'\x00\x00':
self.buffer = b'\x00\x00'
self.pending = True
def get_action_map(self):
return {ConsumerKey: self.add_cc}
class PointingDeviceReport(Report):
def __init__(self, size=_REPORT_SIZE_MOUSE):
super().__init__(size)
def add_button(self, key):
self.buffer[0] |= key.code
self.pending = True
def remove_button(self, key):
self.buffer[0] &= ~key.code
self.pending = True
def move_axis(self, axis):
delta = clamp(axis.delta, -127, 127)
axis.delta -= delta
try:
self.buffer[axis.code + 1] = 0xFF & delta
self.pending = True
except IndexError:
if debug.enabled:
debug(axis, ' not supported')
def get_action_map(self):
return {Axis: self.move_axis, MouseKey: self.add_button}
class HSPointingDeviceReport(PointingDeviceReport):
def __init__(self):
super().__init__(_REPORT_SIZE_MOUSE_HSCROLL)
class SixAxisDeviceReport(Report):
def __init__(self, size=_REPORT_SIZE_SIXAXIS):
super().__init__(size)
def move_six_axis(self, axis):
delta = clamp(axis.delta, -500, 500)
axis.delta -= delta
index = 2 * axis.code
try:
self.buffer[index] = 0xFF & delta
self.buffer[index + 1] = 0xFF & (delta >> 8)
self.pending = True
except IndexError:
if debug.enabled:
debug(axis, ' not supported')
def get_action_map(self):
return {SixAxis: self.move_six_axis}
class SixAxisDeviceButtonReport(Report):
def __init__(self, size=_REPORT_SIZE_SIXAXIS_BUTTON):
super().__init__(size)
def add_six_axis_button(self, key):
self.buffer[0] |= key.code
self.pending = True
def remove_six_axis_button(self, key):
self.buffer[0] &= ~key.code
self.pending = True
def get_action_map(self):
return {SpacemouseKey: self.add_six_axis_button}
class AbstractHID:
def __init__(self):
self.report_map = {}
self.device_map = {}
self._setup_task = create_task(self.setup, period_ms=100)
def __repr__(self):
return self.__class__.__name__
def create_report(self, keys):
for report in self.device_map.keys():
report.clear()
for key in keys:
if action := self.report_map.get(type(key)):
action(key)
def send(self):
for report in self.device_map.keys():
if report.pending:
if hasattr(report, 'move_six_axis'):
self.device_map[report].send_report(report.buffer, 1)
elif hasattr(report, 'add_six_axis_button'):
self.device_map[report].send_report(report.buffer, 3)
else:
self.device_map[report].send_report(report.buffer)
report.pending = False
def setup(self):
if not self.connected:
return
try:
self.setup_keyboard_hid()
self.setup_consumer_control()
self.setup_mouse_hid()
self.setup_sixaxis_hid()
cancel_task(self._setup_task)
self._setup_task = None
if debug.enabled:
self.show_debug()
except OSError as e:
if debug.enabled:
debug(type(e), ':', e)
def setup_keyboard_hid(self):
if device := find_device(self.devices, _USAGE_PAGE_KEYBOARD, _USAGE_KEYBOARD):
# bodgy NKRO autodetect
try:
report = KeyboardReport()
device.send_report(report.buffer)
except ValueError:
report = NKROKeyboardReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
def setup_consumer_control(self):
if device := find_device(self.devices, _USAGE_PAGE_CONSUMER, _USAGE_CONSUMER):
report = ConsumerControlReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
def setup_mouse_hid(self):
if device := find_device(self.devices, _USAGE_PAGE_MOUSE, _USAGE_MOUSE):
# bodgy pointing device panning autodetect
try:
report = PointingDeviceReport()
device.send_report(report.buffer)
except ValueError:
report = HSPointingDeviceReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
def setup_sixaxis_hid(self):
if device := find_device(self.devices, _USAGE_PAGE_SIXAXIS, _USAGE_SIXAXIS):
report = SixAxisDeviceReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
report = SixAxisDeviceButtonReport()
self.report_map.update(report.get_action_map())
self.device_map[report] = device
def show_debug(self):
for report in self.device_map.keys():
debug('use ', report.__class__.__name__)
class USBHID(AbstractHID):
@property
def connected(self):
return supervisor.runtime.usb_connected
@property
def devices(self):
return usb_hid.devices
class BLEHID(AbstractHID):
def __init__(self, ble_name=None):
super().__init__()
self.ble = BLERadio()
self.ble.name = ble_name if ble_name else getmount('/').label
self.ble_connected = False
self.hid = HIDService()
self.hid.protocol_mode = 0 # Boot protocol
create_task(self.ble_monitor, period_ms=1000)
@property
def connected(self):
return self.ble.connected
@property
def devices(self):
return self.hid.devices
def ble_monitor(self):
if self.ble_connected != self.connected:
self.ble_connected = self.connected
if self._connected:
if debug.enabled:
debug('BLE connected')
else:
# Security-wise this is not right. While you're away someone turns
# on your keyboard and they can pair with it nice and clean and then
# listen to keystrokes.
# On the other hand we don't have LESC so it's like shouting your
# keystrokes in the air
self.start_advertising()
if debug.enabled:
debug('BLE disconnected')
def clear_bonds(self):
import _bleio
_bleio.adapter.erase_bonding()
def start_advertising(self):
if not self.ble.advertising:
advertisement = ProvideServicesAdvertisement(self.hid)
advertisement.appearance = _BLE_APPEARANCE_HID_KEYBOARD
self.ble.start_advertising(advertisement)
def stop_advertising(self):
self.ble.stop_advertising()