main.c 5.4 KB

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