You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

345 lines
9.7 KiB

Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Use a single endpoint for HID reports (#3951) * Unify multiple HID interfaces into one This reduces the number of USB endpoints required, which frees them up for other things. NKRO and EXTRAKEY always use the shared endpoint. By default, MOUSEKEY also uses it. This means it won't work as a Boot Procotol mouse in some BIOSes, etc. If you really think your keyboard needs to work as a mouse in your BIOS, set MOUSE_SHARED_EP = no in your rules.mk. By default, the core keyboard does not use the shared endpoint, as not all BIOSes are standards compliant and that's one place you don't want to find out your keyboard doesn't work.. If you are really confident, you can set KEYBOARD_SHARED_EP = yes to use the shared endpoint here too. * unify endpoints: ChibiOS protocol implementation * fixup: missing #ifdef EXTRAKEY_ENABLEs broke build on AVR with EXTRAKEY disabled * endpoints: restore error when too many endpoints required * lufa: wait up to 10ms to send keyboard input This avoids packets being dropped when two reports are sent in quick succession (eg. releasing a dual role key). * endpoints: fix compile on ARM_ATSAM * endpoint: ARM_ATSAM fixes No longer use wrong or unexpected endpoint IDs * endpoints: accommodate VUSB protocol V-USB has its own, understandably simple ideas about the report formats. It already blasts the mouse and extrakeys through one endpoint with report IDs. We just stay out of its way. * endpoints: document new endpoint configuration options * endpoints: respect keyboard_report->mods in NKRO The caller(s) of host_keyboard_send expect to be able to just drop modifiers in the mods field and not worry about whether NKRO is in use. This is a good thing. So we just shift it over if needs be. * endpoints: report.c: update for new keyboard_report format
5 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
Hid joystick interface (#4226) * add support for hid gamepad interface add documentation for HID joystick Add joystick_task to read analog axes values even when no key is pressed or release. update doc Update docs/feature_joystick.md Manage pin setup and read to maintain matrix scan after analog read * Incorporates patches and changes to HID reporting There are some patches provided by @a-chol incorporated on this commit, and also some changes I made to the HID Report structure. The most interesting is the one dealing with number of buttons: Linux doesn't seem to care, but Windows requires the HID structure to be byte aligned (that's in the spec). So if one declares 8/16/32... buttons they should not have any issues, but this is what happens when you have 9 buttons: ``` bits |0|1|2|3|4|5|6|7| |*|*|*|*|*|*|*|*| axis 0 (report size 8) |*|*|*|*|*|*|*|*| ... |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| |*|*|*|*|*|*|*|*| axis 6 |*|*|*|*|*|*|*|*| first 8 buttons (report size 1) |*| | | | | | | | last of 9 buttons, not aligned ``` So for that I added a conditonal that will add a number of reports with size 1 to make sure it aligns to the next multiple of 8. Those reports send dummy inputs that don't do anything aside from aligning the data. Tested on Linux, Windows 10 and Street Fighter (where the joystick is recognized as direct-input) * Add save and restore of each pin used in reading joystick (AVR). Allow output pin to be JS_VIRTUAL_AXIS if the axis is connected to Vcc instead of an output pin from the MCU. Fix joystick report id Fix broken v-usb hid joystick interface. Make it more resilient to unusual settings (none multiple of eight button count, 0 buttons or 0 axes) Correct adc reading for multiple axes. Piecewise range conversion for uncentered raw value range. Input, output and ground pin configuration per axis. Documentation fixes * Fix port addressing for joystick analog read * The other required set of changes As per the PR, the changes still holding it up. Add onekey for testing. Fix ARM builds. Fix device descriptor when either axes or buttons is zero. Add compile-time check for at least one axis or button. Move definition to try to fix conflict. PR review comments. qmk cformat * avoid float functions to compute range mapping for axis adc reading * Remove V-USB support for now. Updated docs accordingly. * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/usb_descriptor.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add support for joystick adc reading for stm32 MCUs. Fix joystick hid report sending for chibios * Fix HID joystick report sending for ChibiOS. Add one analog axis to the onekey:joystick keymap. Fix pin state save and restore during joystick analog read for STM32 MCUs. * Update tmk_core/protocol/chibios/usb_main.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Update tmk_core/protocol/lufa/lufa.c Co-Authored-By: Ryan <fauxpark@gmail.com> * Add missing mcuconf.h and halconf.h to onekey:joystick keymap. Add suggested fixes from PR. * Switch saveState and restoreState signature to use pin_t type. onekey:joystick : add a second axis, virtual and programmatically animated. * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Update docs/feature_joystick.md Co-Authored-By: Ryan <fauxpark@gmail.com> * Add PR corrections * Remove halconf.h and mcuconf.h from onekey keymaps * Change ADC_PIN to A0 Co-authored-by: achol <allecooll@hotmail.com> Co-authored-by: José Júnior <jose.junior@gmail.com> Co-authored-by: a-chol <achol@notamail.com> Co-authored-by: Nick Brassel <nick@tzarc.org> Co-authored-by: Ryan <fauxpark@gmail.com>
3 years ago
  1. /*
  2. Copyright 2011,2012 Jun Wako <wakojun@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #pragma once
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #include "keycode.h"
  18. #include "util.h"
  19. #ifdef JOYSTICK_ENABLE
  20. # include "joystick.h"
  21. #endif
  22. // clang-format off
  23. /* HID report IDs */
  24. enum hid_report_ids {
  25. REPORT_ID_ALL = 0,
  26. REPORT_ID_KEYBOARD = 1,
  27. REPORT_ID_MOUSE,
  28. REPORT_ID_SYSTEM,
  29. REPORT_ID_CONSUMER,
  30. REPORT_ID_PROGRAMMABLE_BUTTON,
  31. REPORT_ID_NKRO,
  32. REPORT_ID_JOYSTICK,
  33. REPORT_ID_DIGITIZER,
  34. REPORT_ID_COUNT = REPORT_ID_DIGITIZER
  35. };
  36. #define IS_VALID_REPORT_ID(id) ((id) >= REPORT_ID_ALL && (id) <= REPORT_ID_COUNT)
  37. /* Mouse buttons */
  38. #define MOUSE_BTN_MASK(n) (1 << (n))
  39. enum mouse_buttons {
  40. MOUSE_BTN1 = MOUSE_BTN_MASK(0),
  41. MOUSE_BTN2 = MOUSE_BTN_MASK(1),
  42. MOUSE_BTN3 = MOUSE_BTN_MASK(2),
  43. MOUSE_BTN4 = MOUSE_BTN_MASK(3),
  44. MOUSE_BTN5 = MOUSE_BTN_MASK(4),
  45. MOUSE_BTN6 = MOUSE_BTN_MASK(5),
  46. MOUSE_BTN7 = MOUSE_BTN_MASK(6),
  47. MOUSE_BTN8 = MOUSE_BTN_MASK(7)
  48. };
  49. /* Consumer Page (0x0C)
  50. *
  51. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=75
  52. */
  53. enum consumer_usages {
  54. // 15.5 Display Controls
  55. SNAPSHOT = 0x065,
  56. BRIGHTNESS_UP = 0x06F, // https://www.usb.org/sites/default/files/hutrr41_0.pdf
  57. BRIGHTNESS_DOWN = 0x070,
  58. // 15.7 Transport Controls
  59. TRANSPORT_RECORD = 0x0B2,
  60. TRANSPORT_FAST_FORWARD = 0x0B3,
  61. TRANSPORT_REWIND = 0x0B4,
  62. TRANSPORT_NEXT_TRACK = 0x0B5,
  63. TRANSPORT_PREV_TRACK = 0x0B6,
  64. TRANSPORT_STOP = 0x0B7,
  65. TRANSPORT_EJECT = 0x0B8,
  66. TRANSPORT_RANDOM_PLAY = 0x0B9,
  67. TRANSPORT_STOP_EJECT = 0x0CC,
  68. TRANSPORT_PLAY_PAUSE = 0x0CD,
  69. // 15.9.1 Audio Controls - Volume
  70. AUDIO_MUTE = 0x0E2,
  71. AUDIO_VOL_UP = 0x0E9,
  72. AUDIO_VOL_DOWN = 0x0EA,
  73. // 15.15 Application Launch Buttons
  74. AL_CC_CONFIG = 0x183,
  75. AL_EMAIL = 0x18A,
  76. AL_CALCULATOR = 0x192,
  77. AL_LOCAL_BROWSER = 0x194,
  78. AL_LOCK = 0x19E,
  79. AL_CONTROL_PANEL = 0x19F,
  80. AL_ASSISTANT = 0x1CB,
  81. AL_KEYBOARD_LAYOUT = 0x1AE,
  82. // 15.16 Generic GUI Application Controls
  83. AC_NEW = 0x201,
  84. AC_OPEN = 0x202,
  85. AC_CLOSE = 0x203,
  86. AC_EXIT = 0x204,
  87. AC_MAXIMIZE = 0x205,
  88. AC_MINIMIZE = 0x206,
  89. AC_SAVE = 0x207,
  90. AC_PRINT = 0x208,
  91. AC_PROPERTIES = 0x209,
  92. AC_UNDO = 0x21A,
  93. AC_COPY = 0x21B,
  94. AC_CUT = 0x21C,
  95. AC_PASTE = 0x21D,
  96. AC_SELECT_ALL = 0x21E,
  97. AC_FIND = 0x21F,
  98. AC_SEARCH = 0x221,
  99. AC_HOME = 0x223,
  100. AC_BACK = 0x224,
  101. AC_FORWARD = 0x225,
  102. AC_STOP = 0x226,
  103. AC_REFRESH = 0x227,
  104. AC_BOOKMARKS = 0x22A,
  105. AC_NEXT_KEYBOARD_LAYOUT_SELECT = 0x29D,
  106. AC_DESKTOP_SHOW_ALL_WINDOWS = 0x29F,
  107. AC_SOFT_KEY_LEFT = 0x2A0
  108. };
  109. /* Generic Desktop Page (0x01)
  110. *
  111. * See https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf#page=26
  112. */
  113. enum desktop_usages {
  114. // 4.5.1 System Controls - Power Controls
  115. SYSTEM_POWER_DOWN = 0x81,
  116. SYSTEM_SLEEP = 0x82,
  117. SYSTEM_WAKE_UP = 0x83,
  118. SYSTEM_RESTART = 0x8F,
  119. // 4.10 System Display Controls
  120. SYSTEM_DISPLAY_TOGGLE_INT_EXT = 0xB5
  121. };
  122. // clang-format on
  123. #define NKRO_REPORT_BITS 30
  124. #ifdef KEYBOARD_SHARED_EP
  125. # define KEYBOARD_REPORT_SIZE 9
  126. #else
  127. # define KEYBOARD_REPORT_SIZE 8
  128. #endif
  129. #define KEYBOARD_REPORT_KEYS 6
  130. #ifdef __cplusplus
  131. extern "C" {
  132. #endif
  133. /*
  134. * keyboard report is 8-byte array retains state of 8 modifiers and 6 keys.
  135. *
  136. * byte |0 |1 |2 |3 |4 |5 |6 |7
  137. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  138. * desc |mods |reserved|keys[0] |keys[1] |keys[2] |keys[3] |keys[4] |keys[5]
  139. *
  140. * It is exended to 16 bytes to retain 120keys+8mods when NKRO mode.
  141. *
  142. * byte |0 |1 |2 |3 |4 |5 |6 |7 ... |15
  143. * -----+--------+--------+--------+--------+--------+--------+--------+-------- +--------
  144. * desc |mods |bits[0] |bits[1] |bits[2] |bits[3] |bits[4] |bits[5] |bits[6] ... |bit[14]
  145. *
  146. * mods retains state of 8 modifiers.
  147. *
  148. * bit |0 |1 |2 |3 |4 |5 |6 |7
  149. * -----+--------+--------+--------+--------+--------+--------+--------+--------
  150. * desc |Lcontrol|Lshift |Lalt |Lgui |Rcontrol|Rshift |Ralt |Rgui
  151. *
  152. */
  153. typedef struct {
  154. #ifdef KEYBOARD_SHARED_EP
  155. uint8_t report_id;
  156. #endif
  157. uint8_t mods;
  158. uint8_t reserved;
  159. uint8_t keys[KEYBOARD_REPORT_KEYS];
  160. } PACKED report_keyboard_t;
  161. typedef struct {
  162. uint8_t report_id;
  163. uint8_t mods;
  164. uint8_t bits[NKRO_REPORT_BITS];
  165. } PACKED report_nkro_t;
  166. typedef struct {
  167. uint8_t report_id;
  168. uint16_t usage;
  169. } PACKED report_extra_t;
  170. typedef struct {
  171. uint8_t report_id;
  172. uint32_t usage;
  173. } PACKED report_programmable_button_t;
  174. #ifdef MOUSE_EXTENDED_REPORT
  175. typedef int16_t mouse_xy_report_t;
  176. #else
  177. typedef int8_t mouse_xy_report_t;
  178. #endif
  179. typedef struct {
  180. #ifdef MOUSE_SHARED_EP
  181. uint8_t report_id;
  182. #endif
  183. uint8_t buttons;
  184. #ifdef MOUSE_EXTENDED_REPORT
  185. int8_t boot_x;
  186. int8_t boot_y;
  187. #endif
  188. mouse_xy_report_t x;
  189. mouse_xy_report_t y;
  190. int8_t v;
  191. int8_t h;
  192. } PACKED report_mouse_t;
  193. typedef struct {
  194. #ifdef DIGITIZER_SHARED_EP
  195. uint8_t report_id;
  196. #endif
  197. bool in_range : 1;
  198. bool tip : 1;
  199. bool barrel : 1;
  200. uint8_t reserved : 5;
  201. uint16_t x;
  202. uint16_t y;
  203. } PACKED report_digitizer_t;
  204. #if JOYSTICK_AXIS_RESOLUTION > 8
  205. typedef int16_t joystick_axis_t;
  206. #else
  207. typedef int8_t joystick_axis_t;
  208. #endif
  209. typedef struct {
  210. #ifdef JOYSTICK_SHARED_EP
  211. uint8_t report_id;
  212. #endif
  213. #if JOYSTICK_AXIS_COUNT > 0
  214. joystick_axis_t axes[JOYSTICK_AXIS_COUNT];
  215. #endif
  216. #if JOYSTICK_BUTTON_COUNT > 0
  217. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  218. #endif
  219. } PACKED report_joystick_t;
  220. /* keycode to system usage */
  221. static inline uint16_t KEYCODE2SYSTEM(uint8_t key) {
  222. switch (key) {
  223. case KC_SYSTEM_POWER:
  224. return SYSTEM_POWER_DOWN;
  225. case KC_SYSTEM_SLEEP:
  226. return SYSTEM_SLEEP;
  227. case KC_SYSTEM_WAKE:
  228. return SYSTEM_WAKE_UP;
  229. default:
  230. return 0;
  231. }
  232. }
  233. /* keycode to consumer usage */
  234. static inline uint16_t KEYCODE2CONSUMER(uint8_t key) {
  235. switch (key) {
  236. case KC_AUDIO_MUTE:
  237. return AUDIO_MUTE;
  238. case KC_AUDIO_VOL_UP:
  239. return AUDIO_VOL_UP;
  240. case KC_AUDIO_VOL_DOWN:
  241. return AUDIO_VOL_DOWN;
  242. case KC_MEDIA_NEXT_TRACK:
  243. return TRANSPORT_NEXT_TRACK;
  244. case KC_MEDIA_PREV_TRACK:
  245. return TRANSPORT_PREV_TRACK;
  246. case KC_MEDIA_FAST_FORWARD:
  247. return TRANSPORT_FAST_FORWARD;
  248. case KC_MEDIA_REWIND:
  249. return TRANSPORT_REWIND;
  250. case KC_MEDIA_STOP:
  251. return TRANSPORT_STOP;
  252. case KC_MEDIA_EJECT:
  253. return TRANSPORT_STOP_EJECT;
  254. case KC_MEDIA_PLAY_PAUSE:
  255. return TRANSPORT_PLAY_PAUSE;
  256. case KC_MEDIA_SELECT:
  257. return AL_CC_CONFIG;
  258. case KC_MAIL:
  259. return AL_EMAIL;
  260. case KC_CALCULATOR:
  261. return AL_CALCULATOR;
  262. case KC_MY_COMPUTER:
  263. return AL_LOCAL_BROWSER;
  264. case KC_CONTROL_PANEL:
  265. return AL_CONTROL_PANEL;
  266. case KC_ASSISTANT:
  267. return AL_ASSISTANT;
  268. case KC_WWW_SEARCH:
  269. return AC_SEARCH;
  270. case KC_WWW_HOME:
  271. return AC_HOME;
  272. case KC_WWW_BACK:
  273. return AC_BACK;
  274. case KC_WWW_FORWARD:
  275. return AC_FORWARD;
  276. case KC_WWW_STOP:
  277. return AC_STOP;
  278. case KC_WWW_REFRESH:
  279. return AC_REFRESH;
  280. case KC_BRIGHTNESS_UP:
  281. return BRIGHTNESS_UP;
  282. case KC_BRIGHTNESS_DOWN:
  283. return BRIGHTNESS_DOWN;
  284. case KC_WWW_FAVORITES:
  285. return AC_BOOKMARKS;
  286. case KC_MISSION_CONTROL:
  287. return AC_DESKTOP_SHOW_ALL_WINDOWS;
  288. case KC_LAUNCHPAD:
  289. return AC_SOFT_KEY_LEFT;
  290. default:
  291. return 0;
  292. }
  293. }
  294. uint8_t has_anykey(void);
  295. uint8_t get_first_key(void);
  296. bool is_key_pressed(uint8_t key);
  297. void add_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  298. void del_key_byte(report_keyboard_t* keyboard_report, uint8_t code);
  299. #ifdef NKRO_ENABLE
  300. void add_key_bit(report_nkro_t* nkro_report, uint8_t code);
  301. void del_key_bit(report_nkro_t* nkro_report, uint8_t code);
  302. #endif
  303. void add_key_to_report(uint8_t key);
  304. void del_key_from_report(uint8_t key);
  305. void clear_keys_from_report(void);
  306. #ifdef MOUSE_ENABLE
  307. bool has_mouse_report_changed(report_mouse_t* new_report, report_mouse_t* old_report);
  308. #endif
  309. #ifdef __cplusplus
  310. }
  311. #endif