HID: Support Consumer (media) keys
What a short title for such a massive diff.
This (heavily squashed) commit adds support for Consumer keys such as
volume keys, media play/pause/stop, etc. by exposing four HID devices
over a single USB lane (as opposed to just exposing a keyboard). This
heavily refactors how HIDHelper works due to the new reporting
structure.
Many of the media keys were changed (mostly Keycodes.Media section), but
many (especially anything regarding Application keys) haven't been
touched yet - thus many keycodes may still be wrong. Probably worth
updating those soon, but I didn't get around to it yet. The definitive
list I refered to was
http://www.freebsddiary.org/APC/usb_hid_usages.php, which is basically
copy-pasta from the official USB HID spec at
https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
(warning: massive PDF, not light reading).
The only known regression this introduces is that instead of 6KRO as the
USB spec usually supports, we can now only have 5KRO (maybe even 4KRO),
for reasons I have yet to fully debug - this seems to be related to the
report having to include the device descriptor _and_ not supporting a
full 8 bytes as it used to. For now I'm willing to accept this, but it
definitely will be great to squash that bug.
This adds descriptor support for MOUSE and SYSCONTROL devices, as of yet
unimplemented.
2018-09-30 00:46:17 +02:00
|
|
|
from pyb import USB_HID, delay, hid_keyboard
|
2018-09-17 05:49:14 +02:00
|
|
|
|
2018-10-06 14:59:03 +02:00
|
|
|
from kmk.common.abstract.hid import AbstractHidHelper
|
|
|
|
from kmk.common.consts import HID_REPORT_STRUCTURE
|
HID: Support Consumer (media) keys
What a short title for such a massive diff.
This (heavily squashed) commit adds support for Consumer keys such as
volume keys, media play/pause/stop, etc. by exposing four HID devices
over a single USB lane (as opposed to just exposing a keyboard). This
heavily refactors how HIDHelper works due to the new reporting
structure.
Many of the media keys were changed (mostly Keycodes.Media section), but
many (especially anything regarding Application keys) haven't been
touched yet - thus many keycodes may still be wrong. Probably worth
updating those soon, but I didn't get around to it yet. The definitive
list I refered to was
http://www.freebsddiary.org/APC/usb_hid_usages.php, which is basically
copy-pasta from the official USB HID spec at
https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf
(warning: massive PDF, not light reading).
The only known regression this introduces is that instead of 6KRO as the
USB spec usually supports, we can now only have 5KRO (maybe even 4KRO),
for reasons I have yet to fully debug - this seems to be related to the
report having to include the device descriptor _and_ not supporting a
full 8 bytes as it used to. For now I'm willing to accept this, but it
definitely will be great to squash that bug.
This adds descriptor support for MOUSE and SYSCONTROL devices, as of yet
unimplemented.
2018-09-30 00:46:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def generate_pyb_hid_descriptor():
|
|
|
|
existing_keyboard = list(hid_keyboard)
|
|
|
|
existing_keyboard[-1] = HID_REPORT_STRUCTURE
|
|
|
|
return tuple(existing_keyboard)
|
2018-09-17 05:49:14 +02:00
|
|
|
|
|
|
|
|
2018-10-06 14:59:03 +02:00
|
|
|
class HIDHelper(AbstractHidHelper):
|
|
|
|
# For some bizarre reason this can no longer be 8, it'll just fail to send
|
|
|
|
# anything. This is almost certainly a bug in the report descriptor sent
|
|
|
|
# over in the boot process. For now the sacrifice is that we only support
|
|
|
|
# 5KRO until I figure this out, rather than the 6KRO HID defines.
|
|
|
|
REPORT_BYTES = 7
|
2018-09-17 08:20:16 +02:00
|
|
|
|
2018-10-06 14:59:03 +02:00
|
|
|
def post_init(self):
|
2018-09-17 05:49:14 +02:00
|
|
|
self._hid = USB_HID()
|
2018-10-06 14:59:03 +02:00
|
|
|
self.hid_send = self._hid.send
|
2018-09-17 08:20:16 +02:00
|
|
|
|
2018-09-17 05:49:14 +02:00
|
|
|
def send(self):
|
|
|
|
self.logger.debug('Sending HID report: {}'.format(self._evt))
|
2018-10-06 14:59:03 +02:00
|
|
|
self.hid_send(self._evt)
|
2018-09-17 05:49:14 +02:00
|
|
|
|
2018-10-01 03:03:43 +02:00
|
|
|
# Without this delay, events get clobbered and you'll likely end up with
|
|
|
|
# a string like `heloooooooooooooooo` rather than `hello`. This number
|
|
|
|
# may be able to be shrunken down. It may also make sense to use
|
|
|
|
# time.sleep_us or time.sleep_ms or time.sleep (platform dependent)
|
|
|
|
# on non-Pyboards.
|
|
|
|
#
|
|
|
|
# It'd be real awesome if pyb.USB_HID.send/recv would support
|
|
|
|
# uselect.poll or uselect.select to more safely determine when
|
|
|
|
# it is safe to write to the host again...
|
2018-10-08 11:31:30 +02:00
|
|
|
delay(1)
|
2018-10-01 03:03:43 +02:00
|
|
|
|
2018-09-17 05:49:14 +02:00
|
|
|
return self
|