keyboardio / KeyboardioHID

A HID library for Arduino
MIT License
34 stars 17 forks source link

Support a fallback to boot report protocol #20

Closed algernon closed 6 years ago

algernon commented 6 years ago

Based on earlier work by @obra, this first refactors BootKeyboard a little, preparing it to be used as a fallback, then adds a bit of logic to Keyboard that dispatches everything to BootKeyboard if we are in boot mode. By default, we rely on the host to send a SET_PROTOCOL request to put us into boot mode, while defaulting to report mode. The default can be changed by setting BootKeyboard.default_protocol to HID_BOOT_PROTOCOL. If a manual override is required, BootKeyboard.setProtocol can do that.

Boot mode is optional, and disabled by default. Another PR against Kaleidoscope will add a small macro that makes it easier to enable it.

While this works on FreeBSD (which does not support NKRO), I have not tested it anywhere else.

Fixes #10.

algernon commented 6 years ago

It looks like the cost of this is non-trivial, even if BootKeyboard itself is optimized out: 538 bytes of PROGMEM and 2 bytes of RAM with my own sketch, and size-map does not have BootKeyboard in it anywhere.

Looking at what is adding such a significant amount...

algernon commented 6 years ago

Ow. Keyboard_::press went from 78 bytes to 176. That if branch seems to be killing us.

algernon commented 6 years ago

I think I have a way to make the boot stuff optional, and have zero run-time cost when boot support is not compiled in (and less overhead when it is). I can even make the default being on.

If all goes well, to disable it, all one will have to do is #define KEYBOARDIOHID_BOOT_REPORT 0 in the user sketch, before including anything else.

algernon commented 6 years ago

If I do a bit of template magic, I was able to cut down the extra PROGMEM size to 124 bytes with fallback disabled. That is still a lot. And the code is incredibly ugly.

I have another idea, which involves introducing a KeyboardAPI, and having Keyboard be an implementation of that, with CombinedKeyboard being a weak default, and NKROKeyboard or BootKeyboard being other options. The user can then just override the symbol in their sketch, and live happily ever after.

I have high hopes for this last idea...

algernon commented 6 years ago

I'm closing this in favour of using the kaleidoscope::hid facade to implement a similar thing. The still useful parts of the PR will be re-submitted separately.