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.3KB

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