Teensy 4.1 core updated for C++20
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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 10 години
преди 11 години
преди 10 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2013 PJRC.COM, LLC.
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining
  6. * a copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * 1. The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * 2. If the Software is incorporated into a build system that allows
  17. * selection among a list of target devices, then similar target
  18. * devices manufactured by PJRC.COM must be included in the list of
  19. * target devices and selectable in the same manner.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  25. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  26. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  27. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  28. * SOFTWARE.
  29. */
  30. #include "usb_dev.h"
  31. #include "usb_keyboard.h"
  32. #include "core_pins.h" // for yield()
  33. #include "keylayouts.h"
  34. //#include "HardwareSerial.h"
  35. #include <string.h> // for memcpy()
  36. #ifdef KEYBOARD_INTERFACE // defined by usb_dev.h -> usb_desc.h
  37. // which modifier keys are currently pressed
  38. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  39. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  40. uint8_t keyboard_modifier_keys=0;
  41. // which media keys are currently pressed
  42. uint8_t keyboard_media_keys=0;
  43. // which keys are currently pressed, up to 6 keys may be down at once
  44. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  45. // protocol setting from the host. We use exactly the same report
  46. // either way, so this variable only stores the setting since we
  47. // are required to be able to report which setting is in use.
  48. uint8_t keyboard_protocol=1;
  49. // the idle configuration, how often we send the report to the
  50. // host (ms * 4) even when it hasn't changed
  51. uint8_t keyboard_idle_config=125;
  52. // count until idle timeout
  53. uint8_t keyboard_idle_count=0;
  54. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  55. volatile uint8_t keyboard_leds=0;
  56. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
  57. static void write_key(KEYCODE_TYPE keycode);
  58. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  59. static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  60. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
  61. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
  62. #ifdef DEADKEYS_MASK
  63. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  64. #endif
  65. // Step #1, decode UTF8 to Unicode code points
  66. //
  67. void usb_keyboard_write(uint8_t c)
  68. {
  69. static int utf8_state=0;
  70. static uint16_t unicode_wchar=0;
  71. if (c < 0x80) {
  72. // single byte encoded, 0x00 to 0x7F
  73. utf8_state = 0;
  74. usb_keyboard_write_unicode(c);
  75. } else if (c < 0xC0) {
  76. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  77. c &= 0x3F;
  78. if (utf8_state == 1) {
  79. utf8_state = 0;
  80. usb_keyboard_write_unicode(unicode_wchar | c);
  81. } else if (utf8_state == 2) {
  82. unicode_wchar |= ((uint16_t)c << 6);
  83. utf8_state = 1;
  84. }
  85. } else if (c < 0xE0) {
  86. // begin 2 byte sequence, 0xC2 to 0xDF
  87. // or illegal 2 byte sequence, 0xC0 to 0xC1
  88. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  89. utf8_state = 1;
  90. } else if (c < 0xF0) {
  91. // begin 3 byte sequence, 0xE0 to 0xEF
  92. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  93. utf8_state = 2;
  94. } else {
  95. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  96. // or illegal, 0xF5 to 0xFF
  97. utf8_state = 255;
  98. }
  99. }
  100. // Step #2: translate Unicode code point to keystroke sequence
  101. //
  102. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
  103. {
  104. // Unicode code points beyond U+FFFF are not supported
  105. // technically this input should probably be called UCS-2
  106. if (cpoint < 32) {
  107. if (cpoint == 10) return KEY_ENTER & 0x3FFF;
  108. if (cpoint == 11) return KEY_TAB & 0x3FFF;
  109. return 0;
  110. }
  111. if (cpoint < 128) {
  112. return keycodes_ascii[cpoint - 0x20];
  113. }
  114. #ifdef ISO_8859_1_A0
  115. if (cpoint >= 0xA0 && cpoint < 0x100) {
  116. return keycodes_iso_8859_1[cpoint - 0xA0];
  117. }
  118. #endif
  119. //#ifdef UNICODE_20AC
  120. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  121. //#endif
  122. #ifdef KEYCODE_EXTRA00
  123. if (cpoint == UNICODE_EXTRA00) return (KEYCODE_EXTRA00) & 0x3FFF;
  124. #endif
  125. #ifdef KEYCODE_EXTRA01
  126. if (cpoint == UNICODE_EXTRA01) return (KEYCODE_EXTRA01) & 0x3FFF;
  127. #endif
  128. #ifdef KEYCODE_EXTRA02
  129. if (cpoint == UNICODE_EXTRA02) return (KEYCODE_EXTRA02) & 0x3FFF;
  130. #endif
  131. #ifdef KEYCODE_EXTRA03
  132. if (cpoint == UNICODE_EXTRA03) return (KEYCODE_EXTRA03) & 0x3FFF;
  133. #endif
  134. #ifdef KEYCODE_EXTRA04
  135. if (cpoint == UNICODE_EXTRA04) return (KEYCODE_EXTRA04) & 0x3FFF;
  136. #endif
  137. #ifdef KEYCODE_EXTRA05
  138. if (cpoint == UNICODE_EXTRA05) return (KEYCODE_EXTRA05) & 0x3FFF;
  139. #endif
  140. #ifdef KEYCODE_EXTRA06
  141. if (cpoint == UNICODE_EXTRA06) return (KEYCODE_EXTRA06) & 0x3FFF;
  142. #endif
  143. #ifdef KEYCODE_EXTRA07
  144. if (cpoint == UNICODE_EXTRA07) return (KEYCODE_EXTRA07) & 0x3FFF;
  145. #endif
  146. #ifdef KEYCODE_EXTRA08
  147. if (cpoint == UNICODE_EXTRA08) return (KEYCODE_EXTRA08) & 0x3FFF;
  148. #endif
  149. #ifdef KEYCODE_EXTRA09
  150. if (cpoint == UNICODE_EXTRA09) return (KEYCODE_EXTRA09) & 0x3FFF;
  151. #endif
  152. #ifdef KEYCODE_EXTRA0A
  153. if (cpoint == UNICODE_EXTRA0A) return (KEYCODE_EXTRA0A) & 0x3FFF;
  154. #endif
  155. return 0;
  156. }
  157. // Step #3: execute keystroke sequence
  158. //
  159. #ifdef DEADKEYS_MASK
  160. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
  161. {
  162. keycode &= DEADKEYS_MASK;
  163. if (keycode == 0) return 0;
  164. #ifdef ACUTE_ACCENT_BITS
  165. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  166. #endif
  167. #ifdef CEDILLA_BITS
  168. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  169. #endif
  170. #ifdef CIRCUMFLEX_BITS
  171. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  172. #endif
  173. #ifdef DIAERESIS_BITS
  174. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  175. #endif
  176. #ifdef GRAVE_ACCENT_BITS
  177. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  178. #endif
  179. #ifdef TILDE_BITS
  180. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  181. #endif
  182. #ifdef RING_ABOVE_BITS
  183. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  184. #endif
  185. #ifdef DEGREE_SIGN_BITS
  186. if (keycode == DEGREE_SIGN_BITS) return DEADKEY_DEGREE_SIGN;
  187. #endif
  188. #ifdef CARON_BITS
  189. if (keycode == CARON_BITS) return DEADKEY_CARON;
  190. #endif
  191. #ifdef BREVE_BITS
  192. if (keycode == BREVE_BITS) return DEADKEY_BREVE;
  193. #endif
  194. #ifdef OGONEK_BITS
  195. if (keycode == OGONEK_BITS) return DEADKEY_OGONEK;
  196. #endif
  197. #ifdef DOT_ABOVE_BITS
  198. if (keycode == DOT_ABOVE_BITS) return DEADKEY_DOT_ABOVE;
  199. #endif
  200. #ifdef DOUBLE_ACUTE_BITS
  201. if (keycode == DOUBLE_ACUTE_BITS) return DEADKEY_DOUBLE_ACUTE;
  202. #endif
  203. return 0;
  204. }
  205. #endif
  206. void usb_keyboard_write_unicode(uint16_t cpoint)
  207. {
  208. KEYCODE_TYPE keycode;
  209. keycode = unicode_to_keycode(cpoint);
  210. if (keycode) {
  211. #ifdef DEADKEYS_MASK
  212. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  213. if (deadkeycode) write_key(deadkeycode);
  214. #endif
  215. write_key(keycode);
  216. }
  217. }
  218. // Step #4: do each keystroke
  219. //
  220. static void write_key(KEYCODE_TYPE keycode)
  221. {
  222. /*
  223. uint8_t key, modifier=0;
  224. #ifdef SHIFT_MASK
  225. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  226. #endif
  227. #ifdef ALTGR_MASK
  228. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  229. #endif
  230. #ifdef RCTRL_MASK
  231. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  232. #endif
  233. key = keycode & 0x3F;
  234. #ifdef KEY_NON_US_100
  235. if (key == KEY_NON_US_100) key = 100;
  236. #endif
  237. usb_keyboard_press(key, modifier);
  238. */
  239. usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
  240. }
  241. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode)
  242. {
  243. uint8_t modifier=0;
  244. #ifdef SHIFT_MASK
  245. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  246. #endif
  247. #ifdef ALTGR_MASK
  248. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  249. #endif
  250. #ifdef RCTRL_MASK
  251. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  252. #endif
  253. return modifier;
  254. }
  255. static uint8_t keycode_to_key(KEYCODE_TYPE keycode)
  256. {
  257. uint8_t key = keycode & 0x3F;
  258. #ifdef KEY_NON_US_100
  259. if (key == KEY_NON_US_100) key = 100;
  260. #endif
  261. return key;
  262. }
  263. void usb_keyboard_press_keycode(uint16_t n)
  264. {
  265. uint8_t key, mod, msb, modrestore=0;
  266. KEYCODE_TYPE keycode;
  267. #ifdef DEADKEYS_MASK
  268. KEYCODE_TYPE deadkeycode;
  269. #endif
  270. msb = n >> 8;
  271. if (msb >= 0xC2 && msb <= 0xDF) {
  272. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  273. } else
  274. if (msb == 0x80) {
  275. usb_keyboard_press_key(0, n);
  276. return;
  277. } else
  278. if (msb == 0x40) {
  279. usb_keyboard_press_key(n, 0);
  280. return;
  281. }
  282. keycode = unicode_to_keycode(n);
  283. if (!keycode) return;
  284. #ifdef DEADKEYS_MASK
  285. deadkeycode = deadkey_to_keycode(keycode);
  286. if (deadkeycode) {
  287. modrestore = keyboard_modifier_keys;
  288. if (modrestore) {
  289. keyboard_modifier_keys = 0;
  290. usb_keyboard_send();
  291. }
  292. // TODO: test if operating systems recognize
  293. // deadkey sequences when other keys are held
  294. mod = keycode_to_modifier(deadkeycode);
  295. key = keycode_to_key(deadkeycode);
  296. usb_keyboard_press_key(key, mod);
  297. usb_keyboard_release_key(key, mod);
  298. }
  299. #endif
  300. mod = keycode_to_modifier(keycode);
  301. key = keycode_to_key(keycode);
  302. usb_keyboard_press_key(key, mod | modrestore);
  303. }
  304. void usb_keyboard_release_keycode(uint16_t n)
  305. {
  306. uint8_t key, mod, msb;
  307. msb = n >> 8;
  308. if (msb >= 0xC2 && msb <= 0xDF) {
  309. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  310. } else
  311. if (msb == 0x80) {
  312. usb_keyboard_release_key(0, n);
  313. return;
  314. } else
  315. if (msb == 0x40) {
  316. usb_keyboard_release_key(n, 0);
  317. return;
  318. }
  319. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  320. if (!keycode) return;
  321. mod = keycode_to_modifier(keycode);
  322. key = keycode_to_key(keycode);
  323. usb_keyboard_release_key(key, mod);
  324. }
  325. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  326. {
  327. int i, send_required = 0;
  328. if (modifier) {
  329. if ((keyboard_modifier_keys & modifier) != modifier) {
  330. keyboard_modifier_keys |= modifier;
  331. send_required = 1;
  332. }
  333. }
  334. if (key) {
  335. for (i=0; i < 6; i++) {
  336. if (keyboard_keys[i] == key) goto end;
  337. }
  338. for (i=0; i < 6; i++) {
  339. if (keyboard_keys[i] == 0) {
  340. keyboard_keys[i] = key;
  341. send_required = 1;
  342. goto end;
  343. }
  344. }
  345. }
  346. end:
  347. if (send_required) usb_keyboard_send();
  348. }
  349. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  350. {
  351. int i, send_required = 0;
  352. if (modifier) {
  353. if ((keyboard_modifier_keys & modifier) != 0) {
  354. keyboard_modifier_keys &= ~modifier;
  355. send_required = 1;
  356. }
  357. }
  358. if (key) {
  359. for (i=0; i < 6; i++) {
  360. if (keyboard_keys[i] == key) {
  361. keyboard_keys[i] = 0;
  362. send_required = 1;
  363. }
  364. }
  365. }
  366. if (send_required) usb_keyboard_send();
  367. }
  368. void usb_keyboard_release_all(void)
  369. {
  370. uint8_t i, anybits;
  371. anybits = keyboard_modifier_keys;
  372. keyboard_modifier_keys = 0;
  373. anybits |= keyboard_media_keys;
  374. keyboard_media_keys = 0;
  375. for (i=0; i < 6; i++) {
  376. anybits |= keyboard_keys[i];
  377. keyboard_keys[i] = 0;
  378. }
  379. if (anybits) usb_keyboard_send();
  380. }
  381. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  382. {
  383. int r;
  384. keyboard_modifier_keys = modifier;
  385. keyboard_keys[0] = key;
  386. keyboard_keys[1] = 0;
  387. keyboard_keys[2] = 0;
  388. keyboard_keys[3] = 0;
  389. keyboard_keys[4] = 0;
  390. keyboard_keys[5] = 0;
  391. r = usb_keyboard_send();
  392. if (r) return r;
  393. keyboard_modifier_keys = 0;
  394. keyboard_keys[0] = 0;
  395. return usb_keyboard_send();
  396. }
  397. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  398. #define TX_PACKET_LIMIT 4
  399. static uint8_t transmit_previous_timeout=0;
  400. // When the PC isn't listening, how long do we wait before discarding data?
  401. #define TX_TIMEOUT_MSEC 50
  402. #if F_CPU == 168000000
  403. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  404. #elif F_CPU == 144000000
  405. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  406. #elif F_CPU == 120000000
  407. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  408. #elif F_CPU == 96000000
  409. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  410. #elif F_CPU == 72000000
  411. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  412. #elif F_CPU == 48000000
  413. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  414. #elif F_CPU == 24000000
  415. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  416. #endif
  417. // send the contents of keyboard_keys and keyboard_modifier_keys
  418. int usb_keyboard_send(void)
  419. {
  420. #if 0
  421. serial_print("Send:");
  422. serial_phex(keyboard_modifier_keys);
  423. serial_phex(keyboard_keys[0]);
  424. serial_phex(keyboard_keys[1]);
  425. serial_phex(keyboard_keys[2]);
  426. serial_phex(keyboard_keys[3]);
  427. serial_phex(keyboard_keys[4]);
  428. serial_phex(keyboard_keys[5]);
  429. serial_print("\n");
  430. #endif
  431. #if 1
  432. uint32_t wait_count=0;
  433. usb_packet_t *tx_packet;
  434. while (1) {
  435. if (!usb_configuration) {
  436. return -1;
  437. }
  438. if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
  439. tx_packet = usb_malloc();
  440. if (tx_packet) break;
  441. }
  442. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  443. transmit_previous_timeout = 1;
  444. return -1;
  445. }
  446. yield();
  447. }
  448. *(tx_packet->buf) = keyboard_modifier_keys;
  449. *(tx_packet->buf + 1) = keyboard_media_keys;
  450. memcpy(tx_packet->buf + 2, keyboard_keys, 6);
  451. tx_packet->len = 8;
  452. usb_tx(KEYBOARD_ENDPOINT, tx_packet);
  453. #endif
  454. return 0;
  455. }
  456. #endif // KEYBOARD_INTERFACE