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.

922 lines
23KB

  1. /* USB API for Teensy USB Development Board
  2. * http://www.pjrc.com/teensy/teensyduino.html
  3. * Copyright (c) 2008 PJRC.COM, LLC
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. */
  23. #include <avr/io.h>
  24. #include <stdint.h>
  25. #include "usb_common.h"
  26. #include "usb_private.h"
  27. #include "usb_api.h"
  28. #include "wiring.h"
  29. // Public Methods //////////////////////////////////////////////////////////////
  30. void usb_serial_class::begin(long speed)
  31. {
  32. // make sure USB is initialized
  33. peek_buf = -1;
  34. usb_init();
  35. uint16_t begin_wait = (uint16_t)millis();
  36. while (1) {
  37. // wait for the host to finish enumeration
  38. if (usb_configuration) {
  39. delay(200); // a little time for host to load a driver
  40. return;
  41. }
  42. // or for suspend mode (powered without USB)
  43. if (usb_suspended) {
  44. uint16_t begin_suspend = (uint16_t)millis();
  45. while (usb_suspended) {
  46. // must remain suspended for a while, because
  47. // normal USB enumeration causes brief suspend
  48. // states, typically under 0.1 second
  49. if ((uint16_t)millis() - begin_suspend > 250) {
  50. return;
  51. }
  52. }
  53. }
  54. // ... or a timout (powered by a USB power adaptor that
  55. // wiggles the data lines to keep a USB device charging)
  56. if ((uint16_t)millis() - begin_wait > 2500) return;
  57. }
  58. }
  59. void usb_serial_class::end()
  60. {
  61. usb_shutdown();
  62. delay(25);
  63. }
  64. // number of bytes available in the receive buffer
  65. int usb_serial_class::available()
  66. {
  67. uint8_t n=0, i, intr_state;
  68. intr_state = SREG;
  69. cli();
  70. if (usb_configuration) {
  71. UENUM = CDC_RX_ENDPOINT;
  72. n = UEBCLX;
  73. if (!n) {
  74. i = UEINTX;
  75. if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
  76. }
  77. }
  78. SREG = intr_state;
  79. if (peek_buf >= 0 && n < 255) n++;
  80. return n;
  81. }
  82. int usb_serial_class::peek()
  83. {
  84. if (peek_buf < 0) peek_buf = read();
  85. return peek_buf;
  86. }
  87. // get the next character, or -1 if nothing received
  88. int usb_serial_class::read(void)
  89. {
  90. uint8_t c, intr_state;
  91. if (peek_buf >= 0) {
  92. c = peek_buf;
  93. peek_buf = -1;
  94. return c;
  95. }
  96. // interrupts are disabled so these functions can be
  97. // used from the main program or interrupt context,
  98. // even both in the same program!
  99. intr_state = SREG;
  100. cli();
  101. if (!usb_configuration) {
  102. SREG = intr_state;
  103. return -1;
  104. }
  105. UENUM = CDC_RX_ENDPOINT;
  106. retry:
  107. c = UEINTX;
  108. if (!(c & (1<<RWAL))) {
  109. // no data in buffer
  110. if (c & (1<<RXOUTI)) {
  111. UEINTX = 0x6B;
  112. goto retry;
  113. }
  114. SREG = intr_state;
  115. return -1;
  116. }
  117. // take one byte out of the buffer
  118. c = UEDATX;
  119. // if this drained the buffer, release it
  120. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
  121. SREG = intr_state;
  122. return c;
  123. }
  124. // discard any buffered input
  125. void usb_serial_class::flush()
  126. {
  127. uint8_t intr_state;
  128. if (usb_configuration) {
  129. intr_state = SREG;
  130. cli();
  131. UENUM = CDC_RX_ENDPOINT;
  132. while ((UEINTX & (1<<RWAL))) {
  133. UEINTX = 0x6B;
  134. }
  135. SREG = intr_state;
  136. }
  137. peek_buf = -1;
  138. }
  139. #if 0
  140. // transmit a character.
  141. void usb_serial_class::write(uint8_t c)
  142. {
  143. uint8_t timeout, intr_state;
  144. // if we're not online (enumerated and configured), error
  145. if (!usb_configuration) return;
  146. // interrupts are disabled so these functions can be
  147. // used from the main program or interrupt context,
  148. // even both in the same program!
  149. intr_state = SREG;
  150. cli();
  151. UENUM = CDC_TX_ENDPOINT;
  152. // if we gave up due to timeout before, don't wait again
  153. if (transmit_previous_timeout) {
  154. if (!(UEINTX & (1<<RWAL))) {
  155. SREG = intr_state;
  156. return;
  157. }
  158. transmit_previous_timeout = 0;
  159. }
  160. // wait for the FIFO to be ready to accept data
  161. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  162. while (1) {
  163. // are we ready to transmit?
  164. if (UEINTX & (1<<RWAL)) break;
  165. SREG = intr_state;
  166. // have we waited too long? This happens if the user
  167. // is not running an application that is listening
  168. if (UDFNUML == timeout) {
  169. transmit_previous_timeout = 1;
  170. return;
  171. }
  172. // has the USB gone offline?
  173. if (!usb_configuration) return;
  174. // get ready to try checking again
  175. intr_state = SREG;
  176. cli();
  177. UENUM = CDC_TX_ENDPOINT;
  178. }
  179. // actually write the byte into the FIFO
  180. UEDATX = c;
  181. // if this completed a packet, transmit it now!
  182. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  183. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  184. SREG = intr_state;
  185. }
  186. #endif
  187. // transmit a block of data
  188. #if ARDUINO >= 100
  189. size_t usb_serial_class::write(const uint8_t *buffer, uint16_t size)
  190. #else
  191. #define setWriteError()
  192. void usb_serial_class::write(const uint8_t *buffer, uint16_t size)
  193. #endif
  194. {
  195. uint8_t timeout, intr_state, write_size;
  196. #if ARDUINO >= 100
  197. size_t count=0;
  198. #endif
  199. // if we're not online (enumerated and configured), error
  200. if (!usb_configuration) {
  201. setWriteError();
  202. goto end;
  203. }
  204. // interrupts are disabled so these functions can be
  205. // used from the main program or interrupt context,
  206. // even both in the same program!
  207. intr_state = SREG;
  208. cli();
  209. UENUM = CDC_TX_ENDPOINT;
  210. // if we gave up due to timeout before, don't wait again
  211. if (transmit_previous_timeout) {
  212. if (!(UEINTX & (1<<RWAL))) {
  213. SREG = intr_state;
  214. setWriteError();
  215. goto end;
  216. }
  217. transmit_previous_timeout = 0;
  218. }
  219. // each iteration of this loop transmits a packet
  220. while (size) {
  221. // wait for the FIFO to be ready to accept data
  222. timeout = UDFNUML + TRANSMIT_TIMEOUT;
  223. while (1) {
  224. // are we ready to transmit?
  225. if (UEINTX & (1<<RWAL)) break;
  226. SREG = intr_state;
  227. // have we waited too long? This happens if the user
  228. // is not running an application that is listening
  229. if (UDFNUML == timeout) {
  230. transmit_previous_timeout = 1;
  231. setWriteError();
  232. goto end;
  233. }
  234. // has the USB gone offline?
  235. if (!usb_configuration) {
  236. setWriteError();
  237. goto end;
  238. }
  239. // get ready to try checking again
  240. intr_state = SREG;
  241. cli();
  242. UENUM = CDC_TX_ENDPOINT;
  243. }
  244. // compute how many bytes will fit into the next packet
  245. write_size = CDC_TX_SIZE - UEBCLX;
  246. if (write_size > size) write_size = size;
  247. size -= write_size;
  248. #if ARDUINO >= 100
  249. count += write_size;
  250. #endif
  251. #define ASM_COPY1(src, dest, tmp) "ld " tmp ", " src "\n\t" "st " dest ", " tmp "\n\t"
  252. #define ASM_COPY2(src, dest, tmp) ASM_COPY1(src, dest, tmp) ASM_COPY1(src, dest, tmp)
  253. #define ASM_COPY4(src, dest, tmp) ASM_COPY2(src, dest, tmp) ASM_COPY2(src, dest, tmp)
  254. #define ASM_COPY8(src, dest, tmp) ASM_COPY4(src, dest, tmp) ASM_COPY4(src, dest, tmp)
  255. #if 1
  256. // write the packet
  257. do {
  258. uint8_t tmp;
  259. asm volatile(
  260. "L%=begin:" "\n\t"
  261. "ldi r30, %4" "\n\t"
  262. "sub r30, %3" "\n\t"
  263. "cpi r30, %4" "\n\t"
  264. "brsh L%=err" "\n\t"
  265. "lsl r30" "\n\t"
  266. "clr r31" "\n\t"
  267. "subi r30, lo8(-(pm(L%=table)))" "\n\t"
  268. "sbci r31, hi8(-(pm(L%=table)))" "\n\t"
  269. "ijmp" "\n\t"
  270. "L%=err:" "\n\t"
  271. "rjmp L%=end" "\n\t"
  272. "L%=table:" "\n\t"
  273. #if (CDC_TX_SIZE == 64)
  274. ASM_COPY8("Y+", "X", "%1")
  275. ASM_COPY8("Y+", "X", "%1")
  276. ASM_COPY8("Y+", "X", "%1")
  277. ASM_COPY8("Y+", "X", "%1")
  278. #endif
  279. #if (CDC_TX_SIZE >= 32)
  280. ASM_COPY8("Y+", "X", "%1")
  281. ASM_COPY8("Y+", "X", "%1")
  282. #endif
  283. #if (CDC_TX_SIZE >= 16)
  284. ASM_COPY8("Y+", "X", "%1")
  285. #endif
  286. ASM_COPY8("Y+", "X", "%1")
  287. "L%=end:" "\n\t"
  288. : "+y" (buffer), "=r" (tmp)
  289. : "x" (&UEDATX), "r" (write_size), "M" (CDC_TX_SIZE)
  290. : "r30", "r31"
  291. );
  292. } while (0);
  293. #endif
  294. // if this completed a packet, transmit it now!
  295. if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
  296. transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
  297. }
  298. SREG = intr_state;
  299. end:
  300. #if ARDUINO >= 100
  301. return count;
  302. #else
  303. return;
  304. #endif
  305. }
  306. // transmit a string
  307. /*
  308. void usb_serial_class::write(const char *str)
  309. {
  310. uint16_t size=0;
  311. const char *p=str;
  312. while (*p++) size++;
  313. if (size) write((const uint8_t *)str, size);
  314. }
  315. */
  316. // These are Teensy-specific extensions to the Serial object
  317. // immediately transmit any buffered output.
  318. // This doesn't actually transmit the data - that is impossible!
  319. // USB devices only transmit when the host allows, so the best
  320. // we can do is release the FIFO buffer for when the host wants it
  321. void usb_serial_class::send_now(void)
  322. {
  323. uint8_t intr_state;
  324. intr_state = SREG;
  325. cli();
  326. if (usb_configuration && transmit_flush_timer) {
  327. UENUM = CDC_TX_ENDPOINT;
  328. UEINTX = 0x3A;
  329. transmit_flush_timer = 0;
  330. }
  331. SREG = intr_state;
  332. }
  333. uint32_t usb_serial_class::baud(void)
  334. {
  335. return *(uint32_t *)cdc_line_coding;
  336. }
  337. uint8_t usb_serial_class::stopbits(void)
  338. {
  339. return cdc_line_coding[4];
  340. }
  341. uint8_t usb_serial_class::paritytype(void)
  342. {
  343. return cdc_line_coding[5];
  344. }
  345. uint8_t usb_serial_class::numbits(void)
  346. {
  347. return cdc_line_coding[6];
  348. }
  349. uint8_t usb_serial_class::dtr(void)
  350. {
  351. return (cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0;
  352. }
  353. uint8_t usb_serial_class::rts(void)
  354. {
  355. return (cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0;
  356. }
  357. usb_serial_class::operator bool()
  358. {
  359. if (usb_configuration &&
  360. (cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS))) {
  361. return true;
  362. }
  363. return false;
  364. }
  365. // Step #1, decode UTF8 to Unicode code points
  366. //
  367. #if ARDUINO >= 100
  368. size_t usb_keyboard_class::write(uint8_t c)
  369. #else
  370. void usb_keyboard_class::write(uint8_t c)
  371. #endif
  372. {
  373. if (c < 0x80) {
  374. // single byte encoded, 0x00 to 0x7F
  375. utf8_state = 0;
  376. write_unicode(c);
  377. } else if (c < 0xC0) {
  378. // 2nd, 3rd or 4th byte, 0x80 to 0xBF
  379. c &= 0x3F;
  380. if (utf8_state == 1) {
  381. utf8_state = 0;
  382. write_unicode(unicode_wchar | c);
  383. } else if (utf8_state == 2) {
  384. unicode_wchar |= ((uint16_t)c << 6);
  385. utf8_state = 1;
  386. }
  387. } else if (c < 0xE0) {
  388. // begin 2 byte sequence, 0xC2 to 0xDF
  389. // or illegal 2 byte sequence, 0xC0 to 0xC1
  390. unicode_wchar = (uint16_t)(c & 0x1F) << 6;
  391. utf8_state = 1;
  392. } else if (c < 0xF0) {
  393. // begin 3 byte sequence, 0xE0 to 0xEF
  394. unicode_wchar = (uint16_t)(c & 0x0F) << 12;
  395. utf8_state = 2;
  396. } else {
  397. // begin 4 byte sequence (not supported), 0xF0 to 0xF4
  398. // or illegal, 0xF5 to 0xFF
  399. utf8_state = 255;
  400. }
  401. #if ARDUINO >= 100
  402. return 1;
  403. #endif
  404. }
  405. // Step #2: translate Unicode code point to keystroke sequence
  406. //
  407. KEYCODE_TYPE usb_keyboard_class::unicode_to_keycode(uint16_t cpoint)
  408. {
  409. // Unicode code points beyond U+FFFF are not supported
  410. // technically this input should probably be called UCS-2
  411. if (cpoint < 32) {
  412. if (cpoint == 10) return KEY_ENTER & 0x3FFF;
  413. return 0;
  414. }
  415. if (cpoint < 128) {
  416. if (sizeof(KEYCODE_TYPE) == 1) {
  417. return pgm_read_byte(keycodes_ascii + (cpoint - 0x20));
  418. } else if (sizeof(KEYCODE_TYPE) == 2) {
  419. return pgm_read_word(keycodes_ascii + (cpoint - 0x20));
  420. }
  421. return 0;
  422. }
  423. #ifdef ISO_8859_1_A0
  424. if (cpoint <= 0xA0) return 0;
  425. if (cpoint < 0x100) {
  426. if (sizeof(KEYCODE_TYPE) == 1) {
  427. return pgm_read_byte(keycodes_iso_8859_1 + (cpoint - 0xA0));
  428. } else if (sizeof(KEYCODE_TYPE) == 2) {
  429. return pgm_read_word(keycodes_iso_8859_1 + (cpoint - 0xA0));
  430. }
  431. return 0;
  432. }
  433. #endif
  434. //#ifdef UNICODE_20AC
  435. //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
  436. //#endif
  437. #ifdef KEYCODE_EXTRA00
  438. if (cpoint == UNICODE_EXTRA00) return KEYCODE_EXTRA00 & 0x3FFF;
  439. #endif
  440. #ifdef KEYCODE_EXTRA01
  441. if (cpoint == UNICODE_EXTRA01) return KEYCODE_EXTRA01 & 0x3FFF;
  442. #endif
  443. #ifdef KEYCODE_EXTRA02
  444. if (cpoint == UNICODE_EXTRA02) return KEYCODE_EXTRA02 & 0x3FFF;
  445. #endif
  446. #ifdef KEYCODE_EXTRA03
  447. if (cpoint == UNICODE_EXTRA03) return KEYCODE_EXTRA03 & 0x3FFF;
  448. #endif
  449. #ifdef KEYCODE_EXTRA04
  450. if (cpoint == UNICODE_EXTRA04) return KEYCODE_EXTRA04 & 0x3FFF;
  451. #endif
  452. #ifdef KEYCODE_EXTRA05
  453. if (cpoint == UNICODE_EXTRA05) return KEYCODE_EXTRA05 & 0x3FFF;
  454. #endif
  455. #ifdef KEYCODE_EXTRA06
  456. if (cpoint == UNICODE_EXTRA06) return KEYCODE_EXTRA06 & 0x3FFF;
  457. #endif
  458. #ifdef KEYCODE_EXTRA07
  459. if (cpoint == UNICODE_EXTRA07) return KEYCODE_EXTRA07 & 0x3FFF;
  460. #endif
  461. #ifdef KEYCODE_EXTRA08
  462. if (cpoint == UNICODE_EXTRA08) return KEYCODE_EXTRA08 & 0x3FFF;
  463. #endif
  464. #ifdef KEYCODE_EXTRA09
  465. if (cpoint == UNICODE_EXTRA09) return KEYCODE_EXTRA09 & 0x3FFF;
  466. #endif
  467. return 0;
  468. }
  469. // Step #3: execute keystroke sequence
  470. //
  471. void usb_keyboard_class::write_keycode(KEYCODE_TYPE keycode)
  472. {
  473. if (!keycode) return;
  474. #ifdef DEADKEYS_MASK
  475. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  476. if (deadkeycode) write_key(deadkeycode);
  477. #endif
  478. write_key(keycode);
  479. }
  480. KEYCODE_TYPE usb_keyboard_class::deadkey_to_keycode(KEYCODE_TYPE keycode)
  481. {
  482. #ifdef DEADKEYS_MASK
  483. keycode &= DEADKEYS_MASK;
  484. if (keycode == 0) return 0;
  485. #ifdef ACUTE_ACCENT_BITS
  486. if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
  487. #endif
  488. #ifdef CEDILLA_BITS
  489. if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
  490. #endif
  491. #ifdef CIRCUMFLEX_BITS
  492. if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
  493. #endif
  494. #ifdef DIAERESIS_BITS
  495. if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
  496. #endif
  497. #ifdef GRAVE_ACCENT_BITS
  498. if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
  499. #endif
  500. #ifdef TILDE_BITS
  501. if (keycode == TILDE_BITS) return DEADKEY_TILDE;
  502. #endif
  503. #ifdef RING_ABOVE_BITS
  504. if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
  505. #endif
  506. #endif // DEADKEYS_MASK
  507. return 0;
  508. }
  509. // Step #4: do each keystroke
  510. //
  511. void usb_keyboard_class::write_key(KEYCODE_TYPE keycode)
  512. {
  513. keyboard_report_data[0] = keycode_to_modifier(keycode);
  514. keyboard_report_data[1] = 0;
  515. keyboard_report_data[2] = keycode_to_key(keycode);
  516. keyboard_report_data[3] = 0;
  517. keyboard_report_data[4] = 0;
  518. keyboard_report_data[5] = 0;
  519. keyboard_report_data[6] = 0;
  520. keyboard_report_data[7] = 0;
  521. send_now();
  522. keyboard_report_data[0] = 0;
  523. keyboard_report_data[2] = 0;
  524. send_now();
  525. }
  526. uint8_t usb_keyboard_class::keycode_to_modifier(KEYCODE_TYPE keycode)
  527. {
  528. uint8_t modifier=0;
  529. #ifdef SHIFT_MASK
  530. if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
  531. #endif
  532. #ifdef ALTGR_MASK
  533. if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
  534. #endif
  535. #ifdef RCTRL_MASK
  536. if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
  537. #endif
  538. return modifier;
  539. }
  540. uint8_t usb_keyboard_class::keycode_to_key(KEYCODE_TYPE keycode)
  541. {
  542. uint8_t key = keycode & 0x3F;
  543. #ifdef KEY_NON_US_100
  544. if (key == KEY_NON_US_100) key = 100;
  545. #endif
  546. return key;
  547. }
  548. void usb_keyboard_class::set_modifier(uint8_t c)
  549. {
  550. keyboard_report_data[0] = c;
  551. }
  552. void usb_keyboard_class::set_key1(uint8_t c)
  553. {
  554. keyboard_report_data[2] = c;
  555. }
  556. void usb_keyboard_class::set_key2(uint8_t c)
  557. {
  558. keyboard_report_data[3] = c;
  559. }
  560. void usb_keyboard_class::set_key3(uint8_t c)
  561. {
  562. keyboard_report_data[4] = c;
  563. }
  564. void usb_keyboard_class::set_key4(uint8_t c)
  565. {
  566. keyboard_report_data[5] = c;
  567. }
  568. void usb_keyboard_class::set_key5(uint8_t c)
  569. {
  570. keyboard_report_data[6] = c;
  571. }
  572. void usb_keyboard_class::set_key6(uint8_t c)
  573. {
  574. keyboard_report_data[7] = c;
  575. }
  576. void usb_keyboard_class::set_media(uint8_t c)
  577. {
  578. keyboard_report_data[1] = c;
  579. }
  580. void usb_keyboard_class::send_now(void)
  581. {
  582. uint8_t intr_state, timeout;
  583. if (!usb_configuration) return;
  584. intr_state = SREG;
  585. cli();
  586. UENUM = KEYBOARD_ENDPOINT;
  587. timeout = UDFNUML + 50;
  588. while (1) {
  589. // are we ready to transmit?
  590. if (UEINTX & (1<<RWAL)) break;
  591. SREG = intr_state;
  592. // has the USB gone offline?
  593. if (!usb_configuration) return;
  594. // have we waited too long?
  595. if (UDFNUML == timeout) return;
  596. // get ready to try checking again
  597. intr_state = SREG;
  598. cli();
  599. UENUM = KEYBOARD_ENDPOINT;
  600. }
  601. UEDATX = keyboard_report_data[0];
  602. UEDATX = keyboard_report_data[1];
  603. UEDATX = keyboard_report_data[2];
  604. UEDATX = keyboard_report_data[3];
  605. UEDATX = keyboard_report_data[4];
  606. UEDATX = keyboard_report_data[5];
  607. UEDATX = keyboard_report_data[6];
  608. UEDATX = keyboard_report_data[7];
  609. UEINTX = 0x3A;
  610. keyboard_idle_count = 0;
  611. SREG = intr_state;
  612. }
  613. void usb_keyboard_class::press(uint16_t n)
  614. {
  615. uint8_t key, mod, msb, modrestore=0;
  616. msb = n >> 8;
  617. if (msb >= 0xC2 && msb <= 0xDF) {
  618. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  619. } else
  620. if (msb == 0x80) {
  621. presskey(0, n);
  622. return;
  623. } else
  624. if (msb == 0x40) {
  625. presskey(n, 0);
  626. return;
  627. }
  628. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  629. if (!keycode) return;
  630. #ifdef DEADKEYS_MASK
  631. KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
  632. if (deadkeycode) {
  633. modrestore = keyboard_report_data[0];
  634. if (modrestore) {
  635. keyboard_report_data[0] = 0;
  636. send_now();
  637. }
  638. // TODO: test if operating systems recognize
  639. // deadkey sequences when other keys are held
  640. mod = keycode_to_modifier(deadkeycode);
  641. key = keycode_to_key(deadkeycode);
  642. presskey(key, mod);
  643. releasekey(key, mod);
  644. }
  645. #endif
  646. mod = keycode_to_modifier(keycode);
  647. key = keycode_to_key(keycode);
  648. presskey(key, mod | modrestore);
  649. }
  650. void usb_keyboard_class::release(uint16_t n)
  651. {
  652. uint8_t key, mod, msb;
  653. msb = n >> 8;
  654. if (msb >= 0xC2 && msb <= 0xDF) {
  655. n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
  656. } else
  657. if (msb == 0x80) {
  658. releasekey(0, n);
  659. return;
  660. } else
  661. if (msb == 0x40) {
  662. releasekey(n, 0);
  663. return;
  664. }
  665. KEYCODE_TYPE keycode = unicode_to_keycode(n);
  666. if (!keycode) return;
  667. mod = keycode_to_modifier(keycode);
  668. key = keycode_to_key(keycode);
  669. releasekey(key, mod);
  670. }
  671. void usb_keyboard_class::presskey(uint8_t key, uint8_t modifier)
  672. {
  673. bool send_required = false;
  674. uint8_t i;
  675. if (modifier) {
  676. if ((keyboard_report_data[0] & modifier) != modifier) {
  677. keyboard_report_data[0] |= modifier;
  678. send_required = true;
  679. }
  680. }
  681. if (key) {
  682. for (i=2; i < 8; i++) {
  683. if (keyboard_report_data[i] == key) goto end;
  684. }
  685. for (i=2; i < 8; i++) {
  686. if (keyboard_report_data[i] == 0) {
  687. keyboard_report_data[i] = key;
  688. send_required = true;
  689. goto end;
  690. }
  691. }
  692. }
  693. end:
  694. if (send_required) send_now();
  695. }
  696. void usb_keyboard_class::releasekey(uint8_t key, uint8_t modifier)
  697. {
  698. bool send_required = false;
  699. uint8_t i;
  700. if (modifier) {
  701. if ((keyboard_report_data[0] & modifier) != 0) {
  702. keyboard_report_data[0] &= ~modifier;
  703. send_required = true;
  704. }
  705. }
  706. if (key) {
  707. for (i=2; i < 8; i++) {
  708. if (keyboard_report_data[i] == key) {
  709. keyboard_report_data[i] = 0;
  710. send_required = true;
  711. }
  712. }
  713. }
  714. if (send_required) send_now();
  715. }
  716. void usb_keyboard_class::releaseAll(void)
  717. {
  718. uint8_t i, anybits;
  719. anybits = keyboard_report_data[0];
  720. for (i=2; i < 8; i++) {
  721. anybits |= keyboard_report_data[i];
  722. keyboard_report_data[i] = 0;
  723. }
  724. if (!anybits) return;
  725. keyboard_report_data[0] = 0;
  726. send_now();
  727. }
  728. void usb_mouse_class::move(int8_t x, int8_t y, int8_t wheel)
  729. {
  730. uint8_t intr_state, timeout;
  731. if (!usb_configuration) return;
  732. if (x == -128) x = -127;
  733. if (y == -128) y = -127;
  734. if (wheel == -128) wheel = -127;
  735. intr_state = SREG;
  736. cli();
  737. UENUM = MOUSE_ENDPOINT;
  738. timeout = UDFNUML + 50;
  739. while (1) {
  740. // are we ready to transmit?
  741. if (UEINTX & (1<<RWAL)) break;
  742. SREG = intr_state;
  743. // has the USB gone offline?
  744. if (!usb_configuration) return;
  745. // have we waited too long?
  746. if (UDFNUML == timeout) return;
  747. // get ready to try checking again
  748. intr_state = SREG;
  749. cli();
  750. UENUM = MOUSE_ENDPOINT;
  751. }
  752. UEDATX = mouse_buttons;
  753. UEDATX = x;
  754. UEDATX = y;
  755. UEDATX = wheel;
  756. UEINTX = 0x3A;
  757. SREG = intr_state;
  758. }
  759. void usb_mouse_class::click(uint8_t b)
  760. {
  761. mouse_buttons = (b & 7);
  762. move(0, 0);
  763. mouse_buttons = 0;
  764. move(0, 0);
  765. }
  766. void usb_mouse_class::scroll(int8_t wheel)
  767. {
  768. move(0, 0, wheel);
  769. }
  770. void usb_mouse_class::set_buttons(uint8_t left, uint8_t middle, uint8_t right)
  771. {
  772. uint8_t mask=0;
  773. if (left) mask |= 1;
  774. if (middle) mask |= 4;
  775. if (right) mask |= 2;
  776. mouse_buttons = mask;
  777. move(0, 0);
  778. }
  779. void usb_mouse_class::press(uint8_t b)
  780. {
  781. uint8_t prev = mouse_buttons;
  782. mouse_buttons |= (b & 7);
  783. if (mouse_buttons != prev) move(0, 0);
  784. }
  785. void usb_mouse_class::release(uint8_t b)
  786. {
  787. uint8_t prev = mouse_buttons;
  788. mouse_buttons &= ~(b & 7);
  789. if (mouse_buttons != prev) move(0, 0);
  790. }
  791. bool usb_mouse_class::isPressed(uint8_t b)
  792. {
  793. return ((mouse_buttons & (b & 7)) != 0);
  794. }
  795. void usb_joystick_class::send_now(void)
  796. {
  797. uint8_t intr_state, timeout;
  798. if (!usb_configuration) return;
  799. intr_state = SREG;
  800. cli();
  801. UENUM = JOYSTICK_ENDPOINT;
  802. timeout = UDFNUML + 50;
  803. while (1) {
  804. // are we ready to transmit?
  805. if (UEINTX & (1<<RWAL)) break;
  806. SREG = intr_state;
  807. // has the USB gone offline?
  808. if (!usb_configuration) return;
  809. // have we waited too long?
  810. if (UDFNUML == timeout) return;
  811. // get ready to try checking again
  812. intr_state = SREG;
  813. cli();
  814. UENUM = JOYSTICK_ENDPOINT;
  815. }
  816. UEDATX = joystick_report_data[0];
  817. UEDATX = joystick_report_data[1];
  818. UEDATX = joystick_report_data[2];
  819. UEDATX = joystick_report_data[3];
  820. UEDATX = joystick_report_data[4];
  821. UEDATX = joystick_report_data[5];
  822. UEDATX = joystick_report_data[6];
  823. UEDATX = joystick_report_data[7];
  824. UEDATX = joystick_report_data[8];
  825. UEDATX = joystick_report_data[9];
  826. UEDATX = joystick_report_data[10];
  827. UEDATX = joystick_report_data[11];
  828. UEINTX = 0x3A;
  829. SREG = intr_state;
  830. }
  831. // Preinstantiate Objects //////////////////////////////////////////////////////
  832. usb_serial_class Serial = usb_serial_class();
  833. usb_keyboard_class Keyboard = usb_keyboard_class();
  834. usb_mouse_class Mouse = usb_mouse_class();
  835. usb_joystick_class Joystick = usb_joystick_class();