hid.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* Motenpoche
  2. *
  3. * (c) 2023 Daniele Lacamera <root@danielinux.net>
  4. *
  5. *
  6. * Motenpoche is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Motenpoche is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  19. *
  20. */
  21. #include <stdio.h>
  22. #include <stdint.h>
  23. #include "pico/stdlib.h"
  24. #include "pico/multicore.h"
  25. #include "hardware/gpio.h"
  26. #include "hardware/adc.h"
  27. #include "hardware/i2c.h"
  28. #include "hardware/spi.h"
  29. #include "cryptoengine.h"
  30. #include "bsp/board.h"
  31. #include "tusb.h"
  32. #include "usb_descriptors.h"
  33. #include "ui.h"
  34. static bool has_keyboard_key = false;
  35. /* USB HID: capslock */
  36. static int capslock_on = 0;
  37. /* USB HID: tx buffer */
  38. #define TX_BUFFER_SIZE 256
  39. static char tx_buffer_str[TX_BUFFER_SIZE];
  40. static volatile int tx_buffer_wr=0, tx_buffer_rd=0;
  41. /* ASCII to HID conversion */
  42. static const uint8_t const conv_table[128][2] = { HID_ASCII_TO_KEYCODE };
  43. // Invoked when device is mounted
  44. void tud_mount_cb(void)
  45. {
  46. }
  47. // Invoked when device is unmounted
  48. void tud_umount_cb(void)
  49. {
  50. }
  51. // Invoked when usb bus is suspended
  52. // remote_wakeup_en : if host allow us to perform remote wakeup
  53. // Within 7ms, device must draw an average of current less than 2.5 mA from bus
  54. void tud_suspend_cb(bool remote_wakeup_en)
  55. {
  56. (void) remote_wakeup_en;
  57. }
  58. // Invoked when usb bus is resumed
  59. void tud_resume_cb(void)
  60. {
  61. }
  62. /* USB: tud complete cb */
  63. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len)
  64. {
  65. (void) instance;
  66. (void) len;
  67. (void) report;
  68. if (has_keyboard_key) {
  69. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
  70. has_keyboard_key = false;
  71. } else if (tx_buffer_rd > tx_buffer_wr) {
  72. uint8_t modifier = 0;
  73. uint8_t keycode[6] = {0};
  74. char c = tx_buffer_str[tx_buffer_wr];
  75. if (c == '\t')
  76. keycode[0] = HID_KEY_TAB;
  77. else {
  78. modifier = conv_table[c][0];
  79. keycode[0] = conv_table[c][1];
  80. }
  81. if (capslock_on) {
  82. if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
  83. modifier = !modifier;
  84. }
  85. }
  86. if (modifier)
  87. modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
  88. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, modifier, keycode);
  89. has_keyboard_key = true;
  90. tx_buffer_wr++;
  91. }
  92. }
  93. /* USB HID: get report */
  94. uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
  95. {
  96. // TODO not Implemented
  97. (void) instance;
  98. (void) report_id;
  99. (void) report_type;
  100. (void) buffer;
  101. (void) reqlen;
  102. return 0;
  103. }
  104. /* Invoked when SET_REPORT control request or
  105. * data on OUT endpoint ( Report ID = 0, Type = 0 ) are received
  106. */
  107. void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
  108. {
  109. (void) instance;
  110. if (report_type == HID_REPORT_TYPE_OUTPUT)
  111. {
  112. // Set keyboard LED e.g Capslock, Numlock etc...
  113. if (report_id == REPORT_ID_KEYBOARD)
  114. {
  115. // bufsize should be (at least) 1
  116. if ( bufsize < 1 ) return;
  117. uint8_t const kbd_leds = buffer[0];
  118. if (kbd_leds & KEYBOARD_LED_CAPSLOCK)
  119. {
  120. capslock_on = 1;
  121. }else
  122. {
  123. capslock_on = 0;
  124. }
  125. }
  126. }
  127. }
  128. //--------------------------------------------------------------------+
  129. // USB HID
  130. //--------------------------------------------------------------------+
  131. static void send_hid_report(uint8_t report_id, uint32_t unused)
  132. {
  133. (void)unused;
  134. if ( !tud_hid_ready() )
  135. return;
  136. switch(report_id)
  137. {
  138. case REPORT_ID_KEYBOARD:
  139. {
  140. // use to avoid send multiple consecutive zero report for keyboard
  141. uint8_t modifier = 0;
  142. if (tx_buffer_rd > tx_buffer_wr) {
  143. uint8_t keycode[6] = { 0 };
  144. char c = tx_buffer_str[tx_buffer_wr];
  145. if (c == '\t')
  146. keycode[0] = HID_KEY_TAB;
  147. else {
  148. modifier = conv_table[c][0];
  149. keycode[0] = conv_table[c][1];
  150. }
  151. if (capslock_on) {
  152. if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))) {
  153. modifier = !modifier;
  154. }
  155. }
  156. if (modifier)
  157. modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
  158. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, modifier, keycode);
  159. has_keyboard_key = true;
  160. tx_buffer_wr++;
  161. } else {
  162. if (has_keyboard_key)
  163. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, NULL);
  164. has_keyboard_key = false;
  165. }
  166. }
  167. break;
  168. default: break;
  169. }
  170. }
  171. void hid_keys_string_send(const char *str)
  172. {
  173. int len = strlen(str);
  174. int i;
  175. if (tx_buffer_rd == tx_buffer_wr) {
  176. tx_buffer_rd = 0;
  177. ForceZero(tx_buffer_str, TX_BUFFER_SIZE);
  178. tx_buffer_wr = 0;
  179. }
  180. for(i = 0; i < len; i++) {
  181. if (tx_buffer_rd >= TX_BUFFER_SIZE)
  182. return;
  183. tx_buffer_str[tx_buffer_rd++] = str[i];
  184. }
  185. gpio_put(17, 0);
  186. }
  187. // Every 10ms, we will sent 1 report for each HID profile (keyboard, mouse etc ..)
  188. // tud_hid_report_complete_cb() is used to send the next report after previous one is complete
  189. void hid_task(void)
  190. {
  191. // Poll every 10ms
  192. const uint32_t interval_ms = 10;
  193. static uint32_t start_ms = 0;
  194. if (!tud_inited())
  195. return;
  196. if ( board_millis() - start_ms < interval_ms) return; // not enough time
  197. start_ms += interval_ms;
  198. // Remote wakeup
  199. if ( tud_suspended() && (tx_buffer_rd > tx_buffer_wr))
  200. {
  201. // Wake up host if we are in suspend mode
  202. // and REMOTE_WAKEUP feature is enabled by host
  203. tud_remote_wakeup();
  204. }else
  205. {
  206. // Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
  207. send_hid_report(REPORT_ID_KEYBOARD, 0);
  208. gpio_put(17, 1);
  209. }
  210. }