No Description
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.

usb_hid.c 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Extended from TinyUSB example code.
  3. *
  4. * Copyright (c) 2022 - 2023 Thomas Buck (thomas@xythobuz.de)
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2019 Ha Thach (tinyusb.org)
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. *
  28. */
  29. #include "bsp/board.h"
  30. #include "tusb.h"
  31. #include "config.h"
  32. #include "controls.h"
  33. #include "usb_descriptors.h"
  34. #include "usb_hid.h"
  35. static void send_hid_report(uint8_t report_id, uint32_t btn) {
  36. // skip if hid is not ready yet
  37. if ( !tud_hid_ready() ) return;
  38. switch(report_id) {
  39. case REPORT_ID_KEYBOARD:
  40. {
  41. // use to avoid send multiple consecutive zero report for keyboard
  42. static bool has_keyboard_key = false;
  43. if ( btn ) {
  44. uint8_t keycode[6] = { 0 };
  45. keycode[0] = HID_KEY_A;
  46. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode);
  47. has_keyboard_key = true;
  48. } else {
  49. // send empty key report if previously has key pressed
  50. if (has_keyboard_key) tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
  51. has_keyboard_key = false;
  52. }
  53. }
  54. break;
  55. case REPORT_ID_MOUSE:
  56. {
  57. struct mouse_state mouse = controls_mouse_read();
  58. uint8_t buttons = 0x00;
  59. if (mouse.button[MOUSE_LEFT]) {
  60. buttons |= MOUSE_BUTTON_LEFT;
  61. }
  62. if (mouse.button[MOUSE_RIGHT]) {
  63. buttons |= MOUSE_BUTTON_RIGHT;
  64. }
  65. if (mouse.button[MOUSE_MIDDLE]) {
  66. buttons |= MOUSE_BUTTON_MIDDLE;
  67. }
  68. if (mouse.button[MOUSE_BACK]) {
  69. buttons |= MOUSE_BUTTON_BACKWARD;
  70. }
  71. if (mouse.changed) {
  72. tud_hid_mouse_report(REPORT_ID_MOUSE, buttons,
  73. mouse.delta_x * (INVERT_MOUSE_X_AXIS ? -1 : 1),
  74. mouse.delta_y * (INVERT_MOUSE_Y_AXIS ? -1 : 1),
  75. mouse.scroll_y * (INVERT_SCROLL_Y_AXIS ? -1 : 1),
  76. mouse.scroll_x * (INVERT_SCROLL_X_AXIS ? -1 : 1));
  77. }
  78. }
  79. break;
  80. case REPORT_ID_CONSUMER_CONTROL:
  81. {
  82. // use to avoid send multiple consecutive zero report
  83. //static bool has_consumer_key = false;
  84. if (btn) {
  85. // volume down
  86. //uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
  87. //tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
  88. //has_consumer_key = true;
  89. } else {
  90. // send empty key report (release key) if previously has key pressed
  91. //uint16_t empty_key = 0;
  92. //if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
  93. //has_consumer_key = false;
  94. }
  95. }
  96. break;
  97. case REPORT_ID_GAMEPAD:
  98. {
  99. // use to avoid send multiple consecutive zero report for keyboard
  100. //static bool has_gamepad_key = false;
  101. //hid_gamepad_report_t report = {
  102. // .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
  103. // .hat = 0, .buttons = 0
  104. //};
  105. if (btn) {
  106. //report.hat = GAMEPAD_HAT_UP;
  107. //report.buttons = GAMEPAD_BUTTON_A;
  108. //tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  109. //has_gamepad_key = true;
  110. } else {
  111. //report.hat = GAMEPAD_HAT_CENTERED;
  112. //report.buttons = 0;
  113. //if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  114. //has_gamepad_key = false;
  115. }
  116. }
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
  123. // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
  124. void hid_task(void) {
  125. // Poll every 10ms
  126. const uint32_t interval_ms = 10;
  127. static uint32_t start_ms = 0;
  128. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  129. start_ms += interval_ms;
  130. uint32_t const btn = 0;
  131. // Remote wakeup
  132. if ( tud_suspended() && btn ) {
  133. // Wake up host if we are in suspend mode
  134. // and REMOTE_WAKEUP feature is enabled by host
  135. tud_remote_wakeup();
  136. } else {
  137. // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
  138. //send_hid_report(REPORT_ID_KEYBOARD, btn);
  139. send_hid_report(REPORT_ID_MOUSE, btn);
  140. }
  141. }
  142. // Invoked when sent REPORT successfully to host
  143. // Application can use this to send the next report
  144. // Note: For composite reports, report[0] is report ID
  145. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len) {
  146. (void) instance;
  147. (void) len;
  148. uint8_t next_report_id = report[0] + 1;
  149. if (next_report_id < REPORT_ID_COUNT) {
  150. send_hid_report(next_report_id, board_button_read());
  151. }
  152. }
  153. // Invoked when received GET_REPORT control request
  154. // Application must fill buffer report's content and return its length.
  155. // Return zero will cause the stack to STALL request
  156. uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id,
  157. hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
  158. // TODO not Implemented
  159. (void) instance;
  160. (void) report_id;
  161. (void) report_type;
  162. (void) buffer;
  163. (void) reqlen;
  164. return 0;
  165. }
  166. // Invoked when received SET_REPORT control request or
  167. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  168. void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id,
  169. hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
  170. (void) instance;
  171. if (report_type == HID_REPORT_TYPE_OUTPUT) {
  172. // Set keyboard LED e.g Capslock, Numlock etc...
  173. if (report_id == REPORT_ID_KEYBOARD) {
  174. // bufsize should be (at least) 1
  175. if ( bufsize < 1 ) return;
  176. uint8_t const kbd_leds = buffer[0];
  177. if (kbd_leds & KEYBOARD_LED_CAPSLOCK) {
  178. // Capslock On: disable blink, turn led on
  179. //blink_interval_ms = 0;
  180. board_led_write(true);
  181. } else {
  182. // Caplocks Off: back to normal blink
  183. board_led_write(false);
  184. //blink_interval_ms = BLINK_MOUNTED;
  185. }
  186. }
  187. }
  188. }