main.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "bsp/board.h"
  29. #include "tusb.h"
  30. //--------------------------------------------------------------------+
  31. // MACRO CONSTANT TYPEDEF PROTYPES
  32. //--------------------------------------------------------------------+
  33. // Interface index depends on the order in configuration descriptor
  34. enum {
  35. ITF_KEYBOARD = 0,
  36. ITF_MOUSE = 1
  37. };
  38. /* Blink pattern
  39. * - 250 ms : device not mounted
  40. * - 1000 ms : device mounted
  41. * - 2500 ms : device is suspended
  42. */
  43. enum {
  44. BLINK_NOT_MOUNTED = 250,
  45. BLINK_MOUNTED = 1000,
  46. BLINK_SUSPENDED = 2500,
  47. };
  48. static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED;
  49. void led_blinking_task(void);
  50. void hid_task(void);
  51. /*------------- MAIN -------------*/
  52. int main(void)
  53. {
  54. board_init();
  55. // init device stack on configured roothub port
  56. tud_init(BOARD_TUD_RHPORT);
  57. while (1)
  58. {
  59. tud_task(); // tinyusb device task
  60. led_blinking_task();
  61. hid_task();
  62. }
  63. return 0;
  64. }
  65. //--------------------------------------------------------------------+
  66. // Device callbacks
  67. //--------------------------------------------------------------------+
  68. // Invoked when device is mounted
  69. void tud_mount_cb(void)
  70. {
  71. blink_interval_ms = BLINK_MOUNTED;
  72. }
  73. // Invoked when device is unmounted
  74. void tud_umount_cb(void)
  75. {
  76. blink_interval_ms = BLINK_NOT_MOUNTED;
  77. }
  78. // Invoked when usb bus is suspended
  79. // remote_wakeup_en : if host allow us to perform remote wakeup
  80. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  81. void tud_suspend_cb(bool remote_wakeup_en)
  82. {
  83. (void) remote_wakeup_en;
  84. blink_interval_ms = BLINK_SUSPENDED;
  85. }
  86. // Invoked when usb bus is resumed
  87. void tud_resume_cb(void)
  88. {
  89. blink_interval_ms = BLINK_MOUNTED;
  90. }
  91. //--------------------------------------------------------------------+
  92. // USB HID
  93. //--------------------------------------------------------------------+
  94. void hid_task(void)
  95. {
  96. // Poll every 10ms
  97. const uint32_t interval_ms = 10;
  98. static uint32_t start_ms = 0;
  99. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  100. start_ms += interval_ms;
  101. uint32_t const btn = board_button_read();
  102. // Remote wakeup
  103. if ( tud_suspended() && btn )
  104. {
  105. // Wake up host if we are in suspend mode
  106. // and REMOTE_WAKEUP feature is enabled by host
  107. tud_remote_wakeup();
  108. }
  109. /*------------- Keyboard -------------*/
  110. if ( tud_hid_n_ready(ITF_KEYBOARD) )
  111. {
  112. // use to avoid send multiple consecutive zero report for keyboard
  113. static bool has_key = false;
  114. if ( btn )
  115. {
  116. uint8_t keycode[6] = { 0 };
  117. keycode[0] = HID_KEY_A;
  118. tud_hid_n_keyboard_report(ITF_KEYBOARD, 0, 0, keycode);
  119. has_key = true;
  120. }else
  121. {
  122. // send empty key report if previously has key pressed
  123. if (has_key) tud_hid_n_keyboard_report(ITF_KEYBOARD, 0, 0, NULL);
  124. has_key = false;
  125. }
  126. }
  127. /*------------- Mouse -------------*/
  128. if ( tud_hid_n_ready(ITF_MOUSE) )
  129. {
  130. if ( btn )
  131. {
  132. int8_t const delta = 5;
  133. // no button, right + down, no scroll pan
  134. tud_hid_n_mouse_report(ITF_MOUSE, 0, 0x00, delta, delta, 0, 0);
  135. }
  136. }
  137. }
  138. // Invoked when received GET_REPORT control request
  139. // Application must fill buffer report's content and return its length.
  140. // Return zero will cause the stack to STALL request
  141. uint16_t tud_hid_get_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
  142. {
  143. // TODO not Implemented
  144. (void) itf;
  145. (void) report_id;
  146. (void) report_type;
  147. (void) buffer;
  148. (void) reqlen;
  149. return 0;
  150. }
  151. // Invoked when received SET_REPORT control request or
  152. // received data on OUT endpoint ( Report ID = 0, Type = 0 )
  153. void tud_hid_set_report_cb(uint8_t itf, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
  154. {
  155. // TODO set LED based on CAPLOCK, NUMLOCK etc...
  156. (void) itf;
  157. (void) report_id;
  158. (void) report_type;
  159. (void) buffer;
  160. (void) bufsize;
  161. }
  162. //--------------------------------------------------------------------+
  163. // BLINKING TASK
  164. //--------------------------------------------------------------------+
  165. void led_blinking_task(void)
  166. {
  167. static uint32_t start_ms = 0;
  168. static bool led_state = false;
  169. // Blink every interval ms
  170. if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time
  171. start_ms += blink_interval_ms;
  172. board_led_write(led_state);
  173. led_state = 1 - led_state; // toggle
  174. }