No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

690 líneas
18KB

  1. /* Teensyduino Core Library
  2. * http://www.pjrc.com/teensy/
  3. * Copyright (c) 2017 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 keys are currently pressed, up to 6 keys may be down at once
  43. uint8_t keyboard_keys[6]={0,0,0,0,0,0};
  44. #ifdef KEYMEDIA_INTERFACE
  45. uint16_t keymedia_consumer_keys[4];
  46. uint8_t keymedia_system_keys[3];
  47. #endif
  48. // protocol setting from the host. We use exactly the same report
  49. // either way, so this variable only stores the setting since we
  50. // are required to be able to report which setting is in use.
  51. uint8_t keyboard_protocol=1;
  52. // the idle configuration, how often we send the report to the
  53. // host (ms * 4) even when it hasn't changed
  54. uint8_t keyboard_idle_config=125;
  55. // count until idle timeout
  56. uint8_t keyboard_idle_count=0;
  57. // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
  58. volatile uint8_t keyboard_leds=0;
  59. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
  60. static void write_key(KEYCODE_TYPE keycode);
  61. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
  62. static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
  63. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
  64. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
  65. #ifdef DEADKEYS_MASK
  66. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
  67. #endif
  68. #ifdef KEYMEDIA_INTERFACE
  69. static void usb_keymedia_press_consumer_key(uint16_t key);
  70. static void usb_keymedia_release_consumer_key(uint16_t key);
  71. static void usb_keymedia_press_system_key(uint8_t key);
  72. static void usb_keymedia_release_system_key(uint8_t key);
  73. static int usb_keymedia_send(void);
  74. #endif
  75. // Step #1, decode UTF8 to Unicode code points
  76. //
  77. void usb_keyboard_write(uint8_t c)
  78. {
  79. static int utf8_state=0;
  80. static uint16_t unicode_wchar=0;
  81. if (c < 0x80) {
  82. // single byte encoded, 0x00 to 0x7F
  83. utf8_state = 0;
  84. usb_keyboard_write_unicode(c);
  85. } else if (c < 0xC0) {
  86. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  87. c &= 0x3F;
  88. if (utf8_state == 1) {
  89. utf8_state = 0;
  90. usb_keyboard_write_unicode(unicode_wchar | c);
  91. } else if (utf8_state == 2) {
  92. unicode_wchar |= ((uint16_t)c << 6);
  93. utf8_state = 1;
  94. }
  95. } else if (c < 0xE0) {
  96. // begin 2 byte sequence, 0xC2 to 0xDF
  97. // or illegal 2 byte sequence, 0xC0 to 0xC1
  98. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  99. utf8_state = 1;
  100. } else if (c < 0xF0) {
  101. // begin 3 byte sequence, 0xE0 to 0xEF
  102. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  103. utf8_state = 2;
  104. } else {
  105. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  106. // or illegal, 0xF5 to 0xFF
  107. utf8_state = 255;
  108. }
  109. }
  110. // Step #2: translate Unicode code point to keystroke sequence
  111. //
  112. static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
  113. {
  114. // Unicode code points beyond U+FFFF are not supported
  115. // technically this input should probably be called UCS-2
  116. if (cpoint < 32) {
  117. if (cpoint == 10) return KEY_ENTER & KEYCODE_MASK;
  118. if (cpoint == 11) return KEY_TAB & KEYCODE_MASK;
  119. return 0;
  120. }
  121. if (cpoint < 128) {
  122. return keycodes_ascii[cpoint - 0x20];
  123. }
  124. #ifdef ISO_8859_1_A0
  125. if (cpoint >= 0xA0 && cpoint < 0x100) {
  126. return keycodes_iso_8859_1[cpoint - 0xA0];
  127. }
  128. #endif
  129. //#ifdef UNICODE_20AC
  130. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  131. //#endif
  132. #ifdef KEYCODE_EXTRA00
  133. if (cpoint == UNICODE_EXTRA00) return (KEYCODE_EXTRA00) & 0x3FFF;
  134. #endif
  135. #ifdef KEYCODE_EXTRA01
  136. if (cpoint == UNICODE_EXTRA01) return (KEYCODE_EXTRA01) & 0x3FFF;
  137. #endif
  138. #ifdef KEYCODE_EXTRA02
  139. if (cpoint == UNICODE_EXTRA02) return (KEYCODE_EXTRA02) & 0x3FFF;
  140. #endif
  141. #ifdef KEYCODE_EXTRA03
  142. if (cpoint == UNICODE_EXTRA03) return (KEYCODE_EXTRA03) & 0x3FFF;
  143. #endif
  144. #ifdef KEYCODE_EXTRA04
  145. if (cpoint == UNICODE_EXTRA04) return (KEYCODE_EXTRA04) & 0x3FFF;
  146. #endif
  147. #ifdef KEYCODE_EXTRA05
  148. if (cpoint == UNICODE_EXTRA05) return (KEYCODE_EXTRA05) & 0x3FFF;
  149. #endif
  150. #ifdef KEYCODE_EXTRA06
  151. if (cpoint == UNICODE_EXTRA06) return (KEYCODE_EXTRA06) & 0x3FFF;
  152. #endif
  153. #ifdef KEYCODE_EXTRA07
  154. if (cpoint == UNICODE_EXTRA07) return (KEYCODE_EXTRA07) & 0x3FFF;
  155. #endif
  156. #ifdef KEYCODE_EXTRA08
  157. if (cpoint == UNICODE_EXTRA08) return (KEYCODE_EXTRA08) & 0x3FFF;
  158. #endif
  159. #ifdef KEYCODE_EXTRA09
  160. if (cpoint == UNICODE_EXTRA09) return (KEYCODE_EXTRA09) & 0x3FFF;
  161. #endif
  162. #ifdef KEYCODE_EXTRA0A
  163. if (cpoint == UNICODE_EXTRA0A) return (KEYCODE_EXTRA0A) & 0x3FFF;
  164. #endif
  165. return 0;
  166. }
  167. // Step #3: execute keystroke sequence
  168. //
  169. #ifdef DEADKEYS_MASK
  170. static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
  171. {
  172. keycode &= DEADKEYS_MASK;
  173. if (keycode == 0) return 0;
  174. #ifdef ACUTE_ACCENT_BITS
  175. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  176. #endif
  177. #ifdef CEDILLA_BITS
  178. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  179. #endif
  180. #ifdef CIRCUMFLEX_BITS
  181. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  182. #endif
  183. #ifdef DIAERESIS_BITS
  184. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  185. #endif
  186. #ifdef GRAVE_ACCENT_BITS
  187. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  188. #endif
  189. #ifdef TILDE_BITS
  190. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  191. #endif
  192. #ifdef RING_ABOVE_BITS
  193. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  194. #endif
  195. #ifdef DEGREE_SIGN_BITS
  196. if (keycode == DEGREE_SIGN_BITS) return DEADKEY_DEGREE_SIGN;
  197. #endif
  198. #ifdef CARON_BITS
  199. if (keycode == CARON_BITS) return DEADKEY_CARON;
  200. #endif
  201. #ifdef BREVE_BITS
  202. if (keycode == BREVE_BITS) return DEADKEY_BREVE;
  203. #endif
  204. #ifdef OGONEK_BITS
  205. if (keycode == OGONEK_BITS) return DEADKEY_OGONEK;
  206. #endif
  207. #ifdef DOT_ABOVE_BITS
  208. if (keycode == DOT_ABOVE_BITS) return DEADKEY_DOT_ABOVE;
  209. #endif
  210. #ifdef DOUBLE_ACUTE_BITS
  211. if (keycode == DOUBLE_ACUTE_BITS) return DEADKEY_DOUBLE_ACUTE;
  212. #endif
  213. return 0;
  214. }
  215. #endif
  216. void usb_keyboard_write_unicode(uint16_t cpoint)
  217. {
  218. KEYCODE_TYPE keycode;
  219. keycode = unicode_to_keycode(cpoint);
  220. if (keycode) {
  221. #ifdef DEADKEYS_MASK
  222. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  223. if (deadkeycode) write_key(deadkeycode);
  224. #endif
  225. write_key(keycode);
  226. }
  227. }
  228. // Step #4: do each keystroke
  229. //
  230. static void write_key(KEYCODE_TYPE keycode)
  231. {
  232. /*
  233. uint8_t key, modifier=0;
  234. #ifdef SHIFT_MASK
  235. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  236. #endif
  237. #ifdef ALTGR_MASK
  238. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  239. #endif
  240. #ifdef RCTRL_MASK
  241. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  242. #endif
  243. key = keycode & 0x3F;
  244. #ifdef KEY_NON_US_100
  245. if (key == KEY_NON_US_100) key = 100;
  246. #endif
  247. usb_keyboard_press(key, modifier);
  248. */
  249. usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
  250. }
  251. static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode)
  252. {
  253. uint8_t modifier=0;
  254. #ifdef SHIFT_MASK
  255. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  256. #endif
  257. #ifdef ALTGR_MASK
  258. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  259. #endif
  260. #ifdef RCTRL_MASK
  261. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  262. #endif
  263. return modifier;
  264. }
  265. static uint8_t keycode_to_key(KEYCODE_TYPE keycode)
  266. {
  267. uint8_t key = keycode & 0x3F;
  268. #ifdef KEY_NON_US_100
  269. if (key == KEY_NON_US_100) key = 100;
  270. #endif
  271. return key;
  272. }
  273. // Input can be:
  274. // 32 - 127 ASCII direct (U+0020 to U+007F) <-- uses layout
  275. // 128 - 0xC1FF Unicode direct (U+0080 to U+C1FF) <-- uses layout
  276. // 0xC200 - 0xDFFF Unicode UTF8 packed (U+0080 to U+07FF) <-- uses layout
  277. // 0xE000 - 0xE0FF Modifier key (bitmap, 8 keys, shift/ctrl/alt/gui)
  278. // 0xE200 - 0xE2FF System key (HID usage code, within usage page 1)
  279. // 0xE400 - 0xE7FF Media/Consumer key (HID usage code, within usage page 12)
  280. // 0xF000 - 0xFFFF Normal key (HID usage code, within usage page 7)
  281. void usb_keyboard_press_keycode(uint16_t n)
  282. {
  283. uint8_t key, mod, msb, modrestore=0;
  284. KEYCODE_TYPE keycode;
  285. #ifdef DEADKEYS_MASK
  286. KEYCODE_TYPE deadkeycode;
  287. #endif
  288. msb = n >> 8;
  289. if (msb >= 0xC2) {
  290. if (msb <= 0xDF) {
  291. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  292. } else if (msb == 0xF0) {
  293. usb_keyboard_press_key(n, 0);
  294. return;
  295. } else if (msb == 0xE0) {
  296. usb_keyboard_press_key(0, n);
  297. return;
  298. #ifdef KEYMEDIA_INTERFACE
  299. } else if (msb == 0xE2) {
  300. usb_keymedia_press_system_key(n);
  301. return;
  302. } else if (msb >= 0xE4 && msb <= 0xE7) {
  303. usb_keymedia_press_consumer_key(n & 0x3FF);
  304. return;
  305. #endif
  306. } else {
  307. return;
  308. }
  309. }
  310. keycode = unicode_to_keycode(n);
  311. if (!keycode) return;
  312. #ifdef DEADKEYS_MASK
  313. deadkeycode = deadkey_to_keycode(keycode);
  314. if (deadkeycode) {
  315. modrestore = keyboard_modifier_keys;
  316. if (modrestore) {
  317. keyboard_modifier_keys = 0;
  318. usb_keyboard_send();
  319. }
  320. // TODO: test if operating systems recognize
  321. // deadkey sequences when other keys are held
  322. mod = keycode_to_modifier(deadkeycode);
  323. key = keycode_to_key(deadkeycode);
  324. usb_keyboard_press_key(key, mod);
  325. usb_keyboard_release_key(key, mod);
  326. }
  327. #endif
  328. mod = keycode_to_modifier(keycode);
  329. key = keycode_to_key(keycode);
  330. usb_keyboard_press_key(key, mod | modrestore);
  331. }
  332. void usb_keyboard_release_keycode(uint16_t n)
  333. {
  334. uint8_t key, mod, msb;
  335. msb = n >> 8;
  336. if (msb >= 0xC2) {
  337. if (msb <= 0xDF) {
  338. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  339. } else if (msb == 0xF0) {
  340. usb_keyboard_release_key(n, 0);
  341. return;
  342. } else if (msb == 0xE0) {
  343. usb_keyboard_release_key(0, n);
  344. return;
  345. #ifdef KEYMEDIA_INTERFACE
  346. } else if (msb == 0xE2) {
  347. usb_keymedia_release_system_key(n);
  348. return;
  349. } else if (msb >= 0xE4 && msb <= 0xE7) {
  350. usb_keymedia_release_consumer_key(n & 0x3FF);
  351. return;
  352. #endif
  353. } else {
  354. return;
  355. }
  356. }
  357. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  358. if (!keycode) return;
  359. mod = keycode_to_modifier(keycode);
  360. key = keycode_to_key(keycode);
  361. usb_keyboard_release_key(key, mod);
  362. }
  363. static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
  364. {
  365. int i, send_required = 0;
  366. if (modifier) {
  367. if ((keyboard_modifier_keys & modifier) != modifier) {
  368. keyboard_modifier_keys |= modifier;
  369. send_required = 1;
  370. }
  371. }
  372. if (key) {
  373. for (i=0; i < 6; i++) {
  374. if (keyboard_keys[i] == key) goto end;
  375. }
  376. for (i=0; i < 6; i++) {
  377. if (keyboard_keys[i] == 0) {
  378. keyboard_keys[i] = key;
  379. send_required = 1;
  380. goto end;
  381. }
  382. }
  383. }
  384. end:
  385. if (send_required) usb_keyboard_send();
  386. }
  387. static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
  388. {
  389. int i, send_required = 0;
  390. if (modifier) {
  391. if ((keyboard_modifier_keys & modifier) != 0) {
  392. keyboard_modifier_keys &= ~modifier;
  393. send_required = 1;
  394. }
  395. }
  396. if (key) {
  397. for (i=0; i < 6; i++) {
  398. if (keyboard_keys[i] == key) {
  399. keyboard_keys[i] = 0;
  400. send_required = 1;
  401. }
  402. }
  403. }
  404. if (send_required) usb_keyboard_send();
  405. }
  406. void usb_keyboard_release_all(void)
  407. {
  408. uint8_t i, anybits;
  409. anybits = keyboard_modifier_keys;
  410. keyboard_modifier_keys = 0;
  411. for (i=0; i < 6; i++) {
  412. anybits |= keyboard_keys[i];
  413. keyboard_keys[i] = 0;
  414. }
  415. if (anybits) usb_keyboard_send();
  416. #ifdef KEYMEDIA_INTERFACE
  417. anybits = 0;
  418. for (i=0; i < 4; i++) {
  419. if (keymedia_consumer_keys[i] != 0) anybits = 1;
  420. keymedia_consumer_keys[i] = 0;
  421. }
  422. for (i=0; i < 3; i++) {
  423. if (keymedia_system_keys[i] != 0) anybits = 1;
  424. keymedia_system_keys[i] = 0;
  425. }
  426. if (anybits) usb_keymedia_send();
  427. #endif
  428. }
  429. int usb_keyboard_press(uint8_t key, uint8_t modifier)
  430. {
  431. int r;
  432. keyboard_modifier_keys = modifier;
  433. keyboard_keys[0] = key;
  434. keyboard_keys[1] = 0;
  435. keyboard_keys[2] = 0;
  436. keyboard_keys[3] = 0;
  437. keyboard_keys[4] = 0;
  438. keyboard_keys[5] = 0;
  439. r = usb_keyboard_send();
  440. if (r) return r;
  441. keyboard_modifier_keys = 0;
  442. keyboard_keys[0] = 0;
  443. return usb_keyboard_send();
  444. }
  445. // Maximum number of transmit packets to queue so we don't starve other endpoints for memory
  446. #define TX_PACKET_LIMIT 4
  447. static uint8_t transmit_previous_timeout=0;
  448. // When the PC isn't listening, how long do we wait before discarding data?
  449. #define TX_TIMEOUT_MSEC 50
  450. #if F_CPU == 240000000
  451. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
  452. #elif F_CPU == 216000000
  453. #define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
  454. #elif 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) = 0;
  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_system_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