Teensy 4.1 core updated for C++20
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

691 lines
18KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2016 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. #if F_CPU >= 20000000
  38. // which modifier keys are currently pressed
  39. // 1=left ctrl, 2=left shift, 4=left alt, 8=left gui
  40. // 16=right ctrl, 32=right shift, 64=right alt, 128=right gui
  41. uint8_t keyboard_modifier_keys=0;
  42. // which media keys are currently pressed
  43. uint8_t keyboard_media_keys=0;
  44. // which keys are currently pressed, up to 6 keys may be down at once
  45. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  46. #ifdef KEYMEDIA_INTERFACE
  47. uint16_t keymedia_consumer_keys[4];
  48. uint8_t keymedia_system_keys[3];
  49. #endif
  50. // protocol setting from the host. We use exactly the same report
  51. // either way, so this variable only stores the setting since we
  52. // are required to be able to report which setting is in use.
  53. uint8_t keyboard_protocol=1;
  54. // the idle configuration, how often we send the report to the
  55. // host (ms * 4) even when it hasn't changed
  56. uint8_t keyboard_idle_config=125;
  57. // count until idle timeout
  58. uint8_t keyboard_idle_count=0;
  59. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  60. volatile uint8_t keyboard_leds=0;
  61. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
  62. static void write_key(KEYCODE_TYPE keycode);
  63. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  64. static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  65. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
  66. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
  67. #ifdef DEADKEYS_MASK
  68. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  69. #endif
  70. #ifdef KEYMEDIA_INTERFACE
  71. static void usb_keymedia_press_consumer_key(uint16_t key);
  72. static void usb_keymedia_release_consumer_key(uint16_t key);
  73. static void usb_keymedia_press_system_key(uint8_t key);
  74. static void usb_keymedia_release_system_key(uint8_t key);
  75. static int usb_keymedia_send(void);
  76. #endif
  77. // Step #1, decode UTF8 to Unicode code points
  78. //
  79. void usb_keyboard_write(uint8_t c)
  80. {
  81. static int utf8_state=0;
  82. static uint16_t unicode_wchar=0;
  83. if (c < 0x80) {
  84. // single byte encoded, 0x00 to 0x7F
  85. utf8_state = 0;
  86. usb_keyboard_write_unicode(c);
  87. } else if (c < 0xC0) {
  88. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  89. c &= 0x3F;
  90. if (utf8_state == 1) {
  91. utf8_state = 0;
  92. usb_keyboard_write_unicode(unicode_wchar | c);
  93. } else if (utf8_state == 2) {
  94. unicode_wchar |= ((uint16_t)c << 6);
  95. utf8_state = 1;
  96. }
  97. } else if (c < 0xE0) {
  98. // begin 2 byte sequence, 0xC2 to 0xDF
  99. // or illegal 2 byte sequence, 0xC0 to 0xC1
  100. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  101. utf8_state = 1;
  102. } else if (c < 0xF0) {
  103. // begin 3 byte sequence, 0xE0 to 0xEF
  104. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  105. utf8_state = 2;
  106. } else {
  107. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  108. // or illegal, 0xF5 to 0xFF
  109. utf8_state = 255;
  110. }
  111. }
  112. // Step #2: translate Unicode code point to keystroke sequence
  113. //
  114. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
  115. {
  116. // Unicode code points beyond U+FFFF are not supported
  117. // technically this input should probably be called UCS-2
  118. if (cpoint < 32) {
  119. if (cpoint == 10) return KEY_ENTER & KEYCODE_MASK;
  120. if (cpoint == 11) return KEY_TAB & KEYCODE_MASK;
  121. return 0;
  122. }
  123. if (cpoint < 128) {
  124. return keycodes_ascii[cpoint - 0x20];
  125. }
  126. #ifdef ISO_8859_1_A0
  127. if (cpoint >= 0xA0 && cpoint < 0x100) {
  128. return keycodes_iso_8859_1[cpoint - 0xA0];
  129. }
  130. #endif
  131. //#ifdef UNICODE_20AC
  132. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  133. //#endif
  134. #ifdef KEYCODE_EXTRA00
  135. if (cpoint == UNICODE_EXTRA00) return (KEYCODE_EXTRA00) & 0x3FFF;
  136. #endif
  137. #ifdef KEYCODE_EXTRA01
  138. if (cpoint == UNICODE_EXTRA01) return (KEYCODE_EXTRA01) & 0x3FFF;
  139. #endif
  140. #ifdef KEYCODE_EXTRA02
  141. if (cpoint == UNICODE_EXTRA02) return (KEYCODE_EXTRA02) & 0x3FFF;
  142. #endif
  143. #ifdef KEYCODE_EXTRA03
  144. if (cpoint == UNICODE_EXTRA03) return (KEYCODE_EXTRA03) & 0x3FFF;
  145. #endif
  146. #ifdef KEYCODE_EXTRA04
  147. if (cpoint == UNICODE_EXTRA04) return (KEYCODE_EXTRA04) & 0x3FFF;
  148. #endif
  149. #ifdef KEYCODE_EXTRA05
  150. if (cpoint == UNICODE_EXTRA05) return (KEYCODE_EXTRA05) & 0x3FFF;
  151. #endif
  152. #ifdef KEYCODE_EXTRA06
  153. if (cpoint == UNICODE_EXTRA06) return (KEYCODE_EXTRA06) & 0x3FFF;
  154. #endif
  155. #ifdef KEYCODE_EXTRA07
  156. if (cpoint == UNICODE_EXTRA07) return (KEYCODE_EXTRA07) & 0x3FFF;
  157. #endif
  158. #ifdef KEYCODE_EXTRA08
  159. if (cpoint == UNICODE_EXTRA08) return (KEYCODE_EXTRA08) & 0x3FFF;
  160. #endif
  161. #ifdef KEYCODE_EXTRA09
  162. if (cpoint == UNICODE_EXTRA09) return (KEYCODE_EXTRA09) & 0x3FFF;
  163. #endif
  164. #ifdef KEYCODE_EXTRA0A
  165. if (cpoint == UNICODE_EXTRA0A) return (KEYCODE_EXTRA0A) & 0x3FFF;
  166. #endif
  167. return 0;
  168. }
  169. // Step #3: execute keystroke sequence
  170. //
  171. #ifdef DEADKEYS_MASK
  172. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
  173. {
  174. keycode &= DEADKEYS_MASK;
  175. if (keycode == 0) return 0;
  176. #ifdef ACUTE_ACCENT_BITS
  177. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  178. #endif
  179. #ifdef CEDILLA_BITS
  180. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  181. #endif
  182. #ifdef CIRCUMFLEX_BITS
  183. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  184. #endif
  185. #ifdef DIAERESIS_BITS
  186. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  187. #endif
  188. #ifdef GRAVE_ACCENT_BITS
  189. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  190. #endif
  191. #ifdef TILDE_BITS
  192. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  193. #endif
  194. #ifdef RING_ABOVE_BITS
  195. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  196. #endif
  197. #ifdef DEGREE_SIGN_BITS
  198. if (keycode == DEGREE_SIGN_BITS) return DEADKEY_DEGREE_SIGN;
  199. #endif
  200. #ifdef CARON_BITS
  201. if (keycode == CARON_BITS) return DEADKEY_CARON;
  202. #endif
  203. #ifdef BREVE_BITS
  204. if (keycode == BREVE_BITS) return DEADKEY_BREVE;
  205. #endif
  206. #ifdef OGONEK_BITS
  207. if (keycode == OGONEK_BITS) return DEADKEY_OGONEK;
  208. #endif
  209. #ifdef DOT_ABOVE_BITS
  210. if (keycode == DOT_ABOVE_BITS) return DEADKEY_DOT_ABOVE;
  211. #endif
  212. #ifdef DOUBLE_ACUTE_BITS
  213. if (keycode == DOUBLE_ACUTE_BITS) return DEADKEY_DOUBLE_ACUTE;
  214. #endif
  215. return 0;
  216. }
  217. #endif
  218. void usb_keyboard_write_unicode(uint16_t cpoint)
  219. {
  220. KEYCODE_TYPE keycode;
  221. keycode = unicode_to_keycode(cpoint);
  222. if (keycode) {
  223. #ifdef DEADKEYS_MASK
  224. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  225. if (deadkeycode) write_key(deadkeycode);
  226. #endif
  227. write_key(keycode);
  228. }
  229. }
  230. // Step #4: do each keystroke
  231. //
  232. static void write_key(KEYCODE_TYPE keycode)
  233. {
  234. /*
  235. uint8_t key, modifier=0;
  236. #ifdef SHIFT_MASK
  237. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  238. #endif
  239. #ifdef ALTGR_MASK
  240. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  241. #endif
  242. #ifdef RCTRL_MASK
  243. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  244. #endif
  245. key = keycode & 0x3F;
  246. #ifdef KEY_NON_US_100
  247. if (key == KEY_NON_US_100) key = 100;
  248. #endif
  249. usb_keyboard_press(key, modifier);
  250. */
  251. usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
  252. }
  253. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode)
  254. {
  255. uint8_t modifier=0;
  256. #ifdef SHIFT_MASK
  257. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  258. #endif
  259. #ifdef ALTGR_MASK
  260. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  261. #endif
  262. #ifdef RCTRL_MASK
  263. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  264. #endif
  265. return modifier;
  266. }
  267. static uint8_t keycode_to_key(KEYCODE_TYPE keycode)
  268. {
  269. uint8_t key = keycode & 0x3F;
  270. #ifdef KEY_NON_US_100
  271. if (key == KEY_NON_US_100) key = 100;
  272. #endif
  273. return key;
  274. }
  275. // Input can be:
  276. // 32 - 127 ASCII direct (U+0020 to U+007F) <-- uses layout
  277. // 128 - 0xC1FF Unicode direct (U+0080 to U+C1FF) <-- uses layout
  278. // 0xC200 - 0xDFFF Unicode UTF8 packed (U+0080 to U+07FF) <-- uses layout
  279. // 0xE000 - 0xE0FF Modifier key (bitmap, 8 keys, shift/ctrl/alt/gui)
  280. // 0xE200 - 0xE2FF System key (HID usage code, within usage page 1)
  281. // 0xE400 - 0xE7FF Media/Consumer key (HID usage code, within usage page 12)
  282. // 0xF000 - 0xFFFF Normal key (HID usage code, within usage page 7)
  283. void usb_keyboard_press_keycode(uint16_t n)
  284. {
  285. uint8_t key, mod, msb, modrestore=0;
  286. KEYCODE_TYPE keycode;
  287. #ifdef DEADKEYS_MASK
  288. KEYCODE_TYPE deadkeycode;
  289. #endif
  290. msb = n >> 8;
  291. if (msb >= 0xC2) {
  292. if (msb <= 0xDF) {
  293. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  294. } else if (msb == 0xF0) {
  295. usb_keyboard_press_key(n, 0);
  296. return;
  297. } else if (msb == 0xE0) {
  298. usb_keyboard_press_key(0, n);
  299. return;
  300. #ifdef KEYMEDIA_INTERFACE
  301. } else if (msb == 0xE2) {
  302. usb_keymedia_press_system_key(n);
  303. return;
  304. } else if (msb >= 0xE4 && msb <= 0xE7) {
  305. usb_keymedia_press_consumer_key(n & 0x3FF);
  306. return;
  307. #endif
  308. } else {
  309. return;
  310. }
  311. }
  312. keycode = unicode_to_keycode(n);
  313. if (!keycode) return;
  314. #ifdef DEADKEYS_MASK
  315. deadkeycode = deadkey_to_keycode(keycode);
  316. if (deadkeycode) {
  317. modrestore = keyboard_modifier_keys;
  318. if (modrestore) {
  319. keyboard_modifier_keys = 0;
  320. usb_keyboard_send();
  321. }
  322. // TODO: test if operating systems recognize
  323. // deadkey sequences when other keys are held
  324. mod = keycode_to_modifier(deadkeycode);
  325. key = keycode_to_key(deadkeycode);
  326. usb_keyboard_press_key(key, mod);
  327. usb_keyboard_release_key(key, mod);
  328. }
  329. #endif
  330. mod = keycode_to_modifier(keycode);
  331. key = keycode_to_key(keycode);
  332. usb_keyboard_press_key(key, mod | modrestore);
  333. }
  334. void usb_keyboard_release_keycode(uint16_t n)
  335. {
  336. uint8_t key, mod, msb;
  337. msb = n >> 8;
  338. if (msb >= 0xC2) {
  339. if (msb <= 0xDF) {
  340. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  341. } else if (msb == 0xF0) {
  342. usb_keyboard_release_key(n, 0);
  343. return;
  344. } else if (msb == 0xE0) {
  345. usb_keyboard_release_key(0, n);
  346. return;
  347. #ifdef KEYMEDIA_INTERFACE
  348. } else if (msb == 0xE2) {
  349. usb_keymedia_release_system_key(n);
  350. return;
  351. } else if (msb >= 0xE4 && msb <= 0xE7) {
  352. usb_keymedia_release_consumer_key(n & 0x3FF);
  353. return;
  354. #endif
  355. } else {
  356. return;
  357. }
  358. }
  359. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  360. if (!keycode) return;
  361. mod = keycode_to_modifier(keycode);
  362. key = keycode_to_key(keycode);
  363. usb_keyboard_release_key(key, mod);
  364. }
  365. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  366. {
  367. int i, send_required = 0;
  368. if (modifier) {
  369. if ((keyboard_modifier_keys & modifier) != modifier) {
  370. keyboard_modifier_keys |= modifier;
  371. send_required = 1;
  372. }
  373. }
  374. if (key) {
  375. for (i=0; i < 6; i++) {
  376. if (keyboard_keys[i] == key) goto end;
  377. }
  378. for (i=0; i < 6; i++) {
  379. if (keyboard_keys[i] == 0) {
  380. keyboard_keys[i] = key;
  381. send_required = 1;
  382. goto end;
  383. }
  384. }
  385. }
  386. end:
  387. if (send_required) usb_keyboard_send();
  388. }
  389. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  390. {
  391. int i, send_required = 0;
  392. if (modifier) {
  393. if ((keyboard_modifier_keys & modifier) != 0) {
  394. keyboard_modifier_keys &= ~modifier;
  395. send_required = 1;
  396. }
  397. }
  398. if (key) {
  399. for (i=0; i < 6; i++) {
  400. if (keyboard_keys[i] == key) {
  401. keyboard_keys[i] = 0;
  402. send_required = 1;
  403. }
  404. }
  405. }
  406. if (send_required) usb_keyboard_send();
  407. }
  408. void usb_keyboard_release_all(void)
  409. {
  410. uint8_t i, anybits;
  411. anybits = keyboard_modifier_keys;
  412. keyboard_modifier_keys = 0;
  413. anybits |= keyboard_media_keys;
  414. keyboard_media_keys = 0;
  415. for (i=0; i < 6; i++) {
  416. anybits |= keyboard_keys[i];
  417. keyboard_keys[i] = 0;
  418. }
  419. if (anybits) usb_keyboard_send();
  420. #ifdef KEYMEDIA_INTERFACE
  421. anybits = 0;
  422. for (i=0; i < 4; i++) {
  423. if (keymedia_consumer_keys[i] != 0) anybits = 1;
  424. keymedia_consumer_keys[i] = 0;
  425. }
  426. for (i=0; i < 3; i++) {
  427. if (keymedia_system_keys[i] != 0) anybits = 1;
  428. keymedia_system_keys[i] = 0;
  429. }
  430. if (anybits) usb_keymedia_send();
  431. #endif
  432. }
  433. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  434. {
  435. int r;
  436. keyboard_modifier_keys = modifier;
  437. keyboard_keys[0] = key;
  438. keyboard_keys[1] = 0;
  439. keyboard_keys[2] = 0;
  440. keyboard_keys[3] = 0;
  441. keyboard_keys[4] = 0;
  442. keyboard_keys[5] = 0;
  443. r = usb_keyboard_send();
  444. if (r) return r;
  445. keyboard_modifier_keys = 0;
  446. keyboard_keys[0] = 0;
  447. return usb_keyboard_send();
  448. }
  449. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  450. #define TX_PACKET_LIMIT 4
  451. static uint8_t transmit_previous_timeout=0;
  452. // When the PC isn't listening, how long do we wait before discarding data?
  453. #define TX_TIMEOUT_MSEC 50
  454. #if F_CPU == 192000000
  455. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
  456. #elif F_CPU == 180000000
  457. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
  458. #elif F_CPU == 168000000
  459. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
  460. #elif F_CPU == 144000000
  461. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
  462. #elif F_CPU == 120000000
  463. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
  464. #elif F_CPU == 96000000
  465. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
  466. #elif F_CPU == 72000000
  467. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
  468. #elif F_CPU == 48000000
  469. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
  470. #elif F_CPU == 24000000
  471. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
  472. #endif
  473. // send the contents of keyboard_keys and keyboard_modifier_keys
  474. int usb_keyboard_send(void)
  475. {
  476. #if 0
  477. serial_print("Send:");
  478. serial_phex(keyboard_modifier_keys);
  479. serial_phex(keyboard_keys[0]);
  480. serial_phex(keyboard_keys[1]);
  481. serial_phex(keyboard_keys[2]);
  482. serial_phex(keyboard_keys[3]);
  483. serial_phex(keyboard_keys[4]);
  484. serial_phex(keyboard_keys[5]);
  485. serial_print("\n");
  486. #endif
  487. #if 1
  488. uint32_t wait_count=0;
  489. usb_packet_t *tx_packet;
  490. while (1) {
  491. if (!usb_configuration) {
  492. return -1;
  493. }
  494. if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
  495. tx_packet = usb_malloc();
  496. if (tx_packet) break;
  497. }
  498. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  499. transmit_previous_timeout = 1;
  500. return -1;
  501. }
  502. yield();
  503. }
  504. *(tx_packet->buf) = keyboard_modifier_keys;
  505. *(tx_packet->buf + 1) = keyboard_media_keys;
  506. memcpy(tx_packet->buf + 2, keyboard_keys, 6);
  507. tx_packet->len = 8;
  508. usb_tx(KEYBOARD_ENDPOINT, tx_packet);
  509. #endif
  510. return 0;
  511. }
  512. #ifdef KEYMEDIA_INTERFACE
  513. static void usb_keymedia_press_consumer_key(uint16_t key)
  514. {
  515. int i;
  516. if (key == 0) return;
  517. for (i=0; i < 4; i++) {
  518. if (keymedia_consumer_keys[i] == key) return;
  519. }
  520. for (i=0; i < 4; i++) {
  521. if (keymedia_consumer_keys[i] == 0) {
  522. keymedia_consumer_keys[i] = key;
  523. usb_keymedia_send();
  524. return;
  525. }
  526. }
  527. }
  528. static void usb_keymedia_release_consumer_key(uint16_t key)
  529. {
  530. int i;
  531. if (key == 0) return;
  532. for (i=0; i < 4; i++) {
  533. if (keymedia_consumer_keys[i] == key) {
  534. keymedia_consumer_keys[i] = 0;
  535. usb_keymedia_send();
  536. return;
  537. }
  538. }
  539. }
  540. static void usb_keymedia_press_system_key(uint8_t key)
  541. {
  542. int i;
  543. if (key == 0) return;
  544. for (i=0; i < 3; i++) {
  545. if (keymedia_system_keys[i] == key) return;
  546. }
  547. for (i=0; i < 3; i++) {
  548. if (keymedia_system_keys[i] == 0) {
  549. keymedia_consumer_keys[i] = key;
  550. usb_keymedia_send();
  551. return;
  552. }
  553. }
  554. }
  555. static void usb_keymedia_release_system_key(uint8_t key)
  556. {
  557. int i;
  558. if (key == 0) return;
  559. for (i=0; i < 3; i++) {
  560. if (keymedia_system_keys[i] == key) {
  561. keymedia_system_keys[i] = 0;
  562. usb_keymedia_send();
  563. return;
  564. }
  565. }
  566. }
  567. void usb_keymedia_release_all(void)
  568. {
  569. uint8_t i, anybits;
  570. anybits = 0;
  571. for (i=0; i < 4; i++) {
  572. if (keymedia_consumer_keys[i] != 0) anybits = 1;
  573. keymedia_consumer_keys[i] = 0;
  574. }
  575. for (i=0; i < 3; i++) {
  576. if (keymedia_system_keys[i] != 0) anybits = 1;
  577. keymedia_system_keys[i] = 0;
  578. }
  579. if (anybits) usb_keymedia_send();
  580. }
  581. // send the contents of keyboard_keys and keyboard_modifier_keys
  582. static int usb_keymedia_send(void)
  583. {
  584. uint32_t wait_count=0;
  585. usb_packet_t *tx_packet;
  586. const uint16_t *consumer;
  587. while (1) {
  588. if (!usb_configuration) {
  589. return -1;
  590. }
  591. if (usb_tx_packet_count(KEYMEDIA_ENDPOINT) < TX_PACKET_LIMIT) {
  592. tx_packet = usb_malloc();
  593. if (tx_packet) break;
  594. }
  595. if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
  596. transmit_previous_timeout = 1;
  597. return -1;
  598. }
  599. yield();
  600. }
  601. // 44444444 44333333 33332222 22222211 11111111
  602. // 98765432 10987654 32109876 54321098 76543210
  603. consumer = keymedia_consumer_keys;
  604. *(tx_packet->buf + 0) = consumer[0];
  605. *(tx_packet->buf + 1) = (consumer[1] << 2) | ((consumer[0] >> 8) & 0x03);
  606. *(tx_packet->buf + 2) = (consumer[2] << 4) | ((consumer[1] >> 6) & 0x0F);
  607. *(tx_packet->buf + 3) = (consumer[3] << 6) | ((consumer[2] >> 4) & 0x3F);
  608. *(tx_packet->buf + 4) = consumer[3] >> 2;
  609. *(tx_packet->buf + 5) = keymedia_system_keys[0];
  610. *(tx_packet->buf + 6) = keymedia_system_keys[1];
  611. *(tx_packet->buf + 7) = keymedia_system_keys[2];
  612. tx_packet->len = 8;
  613. usb_tx(KEYMEDIA_ENDPOINT, tx_packet);
  614. return 0;
  615. }
  616. #endif // KEYMEDIA_INTERFACE
  617. #endif // F_CPU
  618. #endif // KEYBOARD_INTERFACE