Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

usb_hid.c 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "usb_descriptors.h"
  28. #include "usb_hid.h"
  29. // TODO
  30. extern int16_t delta_x, delta_y;
  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. // no button, right + down, no scroll, no pan
  54. tud_hid_mouse_report(REPORT_ID_MOUSE, 0x00, delta_x, delta_y, 0, 0);
  55. // TODO
  56. delta_x = 0;
  57. delta_y = 0;
  58. }
  59. break;
  60. case REPORT_ID_CONSUMER_CONTROL:
  61. {
  62. // use to avoid send multiple consecutive zero report
  63. static bool has_consumer_key = false;
  64. if (btn) {
  65. // volume down
  66. uint16_t volume_down = HID_USAGE_CONSUMER_VOLUME_DECREMENT;
  67. tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &volume_down, 2);
  68. has_consumer_key = true;
  69. } else {
  70. // send empty key report (release key) if previously has key pressed
  71. uint16_t empty_key = 0;
  72. if (has_consumer_key) tud_hid_report(REPORT_ID_CONSUMER_CONTROL, &empty_key, 2);
  73. has_consumer_key = false;
  74. }
  75. }
  76. break;
  77. case REPORT_ID_GAMEPAD:
  78. {
  79. // use to avoid send multiple consecutive zero report for keyboard
  80. static bool has_gamepad_key = false;
  81. hid_gamepad_report_t report = {
  82. .x = 0, .y = 0, .z = 0, .rz = 0, .rx = 0, .ry = 0,
  83. .hat = 0, .buttons = 0
  84. };
  85. if (btn) {
  86. report.hat = GAMEPAD_HAT_UP;
  87. report.buttons = GAMEPAD_BUTTON_A;
  88. tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  89. has_gamepad_key = true;
  90. } else {
  91. report.hat = GAMEPAD_HAT_CENTERED;
  92. report.buttons = 0;
  93. if (has_gamepad_key) tud_hid_report(REPORT_ID_GAMEPAD, &report, sizeof(report));
  94. has_gamepad_key = false;
  95. }
  96. }
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
  103. // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
  104. void hid_task(void) {
  105. // Poll every 10ms
  106. const uint32_t interval_ms = 10;
  107. static uint32_t start_ms = 0;
  108. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  109. start_ms += interval_ms;
  110. uint32_t const btn = board_button_read();
  111. // Remote wakeup
  112. if ( tud_suspended() && btn ) {
  113. // Wake up host if we are in suspend mode
  114. // and REMOTE_WAKEUP feature is enabled by host
  115. tud_remote_wakeup();
  116. } else {
  117. // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
  118. //send_hid_report(REPORT_ID_KEYBOARD, btn);
  119. send_hid_report(REPORT_ID_MOUSE, btn);
  120. }
  121. }
  122. // Invoked when sent REPORT successfully to host
  123. // Application can use this to send the next report
  124. // Note: For composite reports, report[0] is report ID
  125. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len) {
  126. (void) instance;
  127. (void) len;
  128. uint8_t next_report_id = report[0] + 1;
  129. if (next_report_id < REPORT_ID_COUNT) {
  130. send_hid_report(next_report_id, board_button_read());
  131. }
  132. }
  133. // Invoked when received GET_REPORT control request
  134. // Application must fill buffer report's content and return its length.
  135. // Return zero will cause the stack to STALL request
  136. uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id,
  137. hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
  138. // TODO not Implemented
  139. (void) instance;
  140. (void) report_id;
  141. (void) report_type;
  142. (void) buffer;
  143. (void) reqlen;
  144. return 0;
  145. }
  146. // Invoked when received SET_REPORT control request or
  147. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  148. void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id,
  149. hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
  150. (void) instance;
  151. if (report_type == HID_REPORT_TYPE_OUTPUT) {
  152. // Set keyboard LED e.g Capslock, Numlock etc...
  153. if (report_id == REPORT_ID_KEYBOARD) {
  154. // bufsize should be (at least) 1
  155. if ( bufsize < 1 ) return;
  156. uint8_t const kbd_leds = buffer[0];
  157. if (kbd_leds & KEYBOARD_LED_CAPSLOCK) {
  158. // Capslock On: disable blink, turn led on
  159. //blink_interval_ms = 0;
  160. board_led_write(true);
  161. } else {
  162. // Caplocks Off: back to normal blink
  163. board_led_write(false);
  164. //blink_interval_ms = BLINK_MOUNTED;
  165. }
  166. }
  167. }
  168. }