|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921 |
- /* USB API for Teensy USB Development Board
- * http://www.pjrc.com/teensy/teensyduino.html
- * Copyright (c) 2008 PJRC.COM, LLC
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- * THE SOFTWARE.
- */
-
- #include <avr/io.h>
- #include <stdint.h>
- #include "usb_common.h"
- #include "usb_private.h"
- #include "usb_api.h"
- #include "wiring.h"
-
- // Public Methods //////////////////////////////////////////////////////////////
-
- void usb_serial_class::begin(long speed)
- {
- // make sure USB is initialized
- peek_buf = -1;
- usb_init();
- uint16_t begin_wait = (uint16_t)millis();
- while (1) {
- // wait for the host to finish enumeration
- if (usb_configuration) {
- delay(200); // a little time for host to load a driver
- return;
- }
- // or for suspend mode (powered without USB)
- if (usb_suspended) {
- uint16_t begin_suspend = (uint16_t)millis();
- while (usb_suspended) {
- // must remain suspended for a while, because
- // normal USB enumeration causes brief suspend
- // states, typically under 0.1 second
- if ((uint16_t)millis() - begin_suspend > 250) {
- return;
- }
- }
- }
- // ... or a timout (powered by a USB power adaptor that
- // wiggles the data lines to keep a USB device charging)
- if ((uint16_t)millis() - begin_wait > 2500) return;
- }
- }
-
- void usb_serial_class::end()
- {
- usb_shutdown();
- delay(25);
- }
-
- // number of bytes available in the receive buffer
- int usb_serial_class::available()
- {
- uint8_t n=0, i, intr_state;
-
- intr_state = SREG;
- cli();
- if (usb_configuration) {
- UENUM = CDC_RX_ENDPOINT;
- n = UEBCLX;
- if (!n) {
- i = UEINTX;
- if (i & (1<<RXOUTI) && !(i & (1<<RWAL))) UEINTX = 0x6B;
- }
- }
- SREG = intr_state;
- if (peek_buf >= 0 && n < 255) n++;
- return n;
- }
-
- int usb_serial_class::peek()
- {
- if (peek_buf < 0) peek_buf = read();
- return peek_buf;
- }
-
- // get the next character, or -1 if nothing received
- int usb_serial_class::read(void)
- {
- uint8_t c, intr_state;
-
- if (peek_buf >= 0) {
- c = peek_buf;
- peek_buf = -1;
- return c;
- }
- // interrupts are disabled so these functions can be
- // used from the main program or interrupt context,
- // even both in the same program!
- intr_state = SREG;
- cli();
- if (!usb_configuration) {
- SREG = intr_state;
- return -1;
- }
- UENUM = CDC_RX_ENDPOINT;
- retry:
- c = UEINTX;
- if (!(c & (1<<RWAL))) {
- // no data in buffer
- if (c & (1<<RXOUTI)) {
- UEINTX = 0x6B;
- goto retry;
- }
- SREG = intr_state;
- return -1;
- }
- // take one byte out of the buffer
- c = UEDATX;
- // if this drained the buffer, release it
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x6B;
- SREG = intr_state;
- return c;
- }
-
- // discard any buffered input
- void usb_serial_class::flush()
- {
- uint8_t intr_state;
-
- if (usb_configuration) {
- intr_state = SREG;
- cli();
- UENUM = CDC_RX_ENDPOINT;
- while ((UEINTX & (1<<RWAL))) {
- UEINTX = 0x6B;
- }
- SREG = intr_state;
- }
- peek_buf = -1;
- }
- #if 0
- // transmit a character.
- void usb_serial_class::write(uint8_t c)
- {
- uint8_t timeout, intr_state;
-
- // if we're not online (enumerated and configured), error
- if (!usb_configuration) return;
- // interrupts are disabled so these functions can be
- // used from the main program or interrupt context,
- // even both in the same program!
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- // if we gave up due to timeout before, don't wait again
- if (transmit_previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- return;
- }
- transmit_previous_timeout = 0;
- }
- // wait for the FIFO to be ready to accept data
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
- // are we ready to transmit?
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- // have we waited too long? This happens if the user
- // is not running an application that is listening
- if (UDFNUML == timeout) {
- transmit_previous_timeout = 1;
- return;
- }
- // has the USB gone offline?
- if (!usb_configuration) return;
- // get ready to try checking again
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- }
- // actually write the byte into the FIFO
- UEDATX = c;
- // if this completed a packet, transmit it now!
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
- transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- SREG = intr_state;
- }
- #endif
-
-
- // transmit a block of data
- #if ARDUINO >= 100
- size_t usb_serial_class::write(const uint8_t *buffer, uint16_t size)
- #else
- #define setWriteError()
- void usb_serial_class::write(const uint8_t *buffer, uint16_t size)
- #endif
- {
- uint8_t timeout, intr_state, write_size;
- #if ARDUINO >= 100
- size_t count=0;
- #endif
-
- // if we're not online (enumerated and configured), error
- if (!usb_configuration) {
- setWriteError();
- goto end;
- }
- // interrupts are disabled so these functions can be
- // used from the main program or interrupt context,
- // even both in the same program!
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- // if we gave up due to timeout before, don't wait again
- if (transmit_previous_timeout) {
- if (!(UEINTX & (1<<RWAL))) {
- SREG = intr_state;
- setWriteError();
- goto end;
- }
- transmit_previous_timeout = 0;
- }
- // each iteration of this loop transmits a packet
- while (size) {
- // wait for the FIFO to be ready to accept data
- timeout = UDFNUML + TRANSMIT_TIMEOUT;
- while (1) {
- // are we ready to transmit?
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- // have we waited too long? This happens if the user
- // is not running an application that is listening
- if (UDFNUML == timeout) {
- transmit_previous_timeout = 1;
- setWriteError();
- goto end;
- }
- // has the USB gone offline?
- if (!usb_configuration) {
- setWriteError();
- goto end;
- }
- // get ready to try checking again
- intr_state = SREG;
- cli();
- UENUM = CDC_TX_ENDPOINT;
- }
-
- // compute how many bytes will fit into the next packet
- write_size = CDC_TX_SIZE - UEBCLX;
- if (write_size > size) write_size = size;
- size -= write_size;
- #if ARDUINO >= 100
- count += write_size;
- #endif
-
- #define ASM_COPY1(src, dest, tmp) "ld " tmp ", " src "\n\t" "st " dest ", " tmp "\n\t"
- #define ASM_COPY2(src, dest, tmp) ASM_COPY1(src, dest, tmp) ASM_COPY1(src, dest, tmp)
- #define ASM_COPY4(src, dest, tmp) ASM_COPY2(src, dest, tmp) ASM_COPY2(src, dest, tmp)
- #define ASM_COPY8(src, dest, tmp) ASM_COPY4(src, dest, tmp) ASM_COPY4(src, dest, tmp)
-
- #if 1
- // write the packet
- do {
- uint8_t tmp;
- asm volatile(
- "L%=begin:" "\n\t"
- "ldi r30, %4" "\n\t"
- "sub r30, %3" "\n\t"
- "cpi r30, %4" "\n\t"
- "brsh L%=err" "\n\t"
- "lsl r30" "\n\t"
- "clr r31" "\n\t"
- "subi r30, lo8(-(pm(L%=table)))" "\n\t"
- "sbci r31, hi8(-(pm(L%=table)))" "\n\t"
- "ijmp" "\n\t"
- "L%=err:" "\n\t"
- "rjmp L%=end" "\n\t"
- "L%=table:" "\n\t"
- #if (CDC_TX_SIZE == 64)
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- #endif
- #if (CDC_TX_SIZE >= 32)
- ASM_COPY8("Y+", "X", "%1")
- ASM_COPY8("Y+", "X", "%1")
- #endif
- #if (CDC_TX_SIZE >= 16)
- ASM_COPY8("Y+", "X", "%1")
- #endif
- ASM_COPY8("Y+", "X", "%1")
- "L%=end:" "\n\t"
- : "+y" (buffer), "=r" (tmp)
- : "x" (&UEDATX), "r" (write_size), "M" (CDC_TX_SIZE)
- : "r30", "r31"
- );
- } while (0);
- #endif
- // if this completed a packet, transmit it now!
- if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
- transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
- }
- SREG = intr_state;
- end:
- #if ARDUINO >= 100
- return count;
- #else
- return;
- #endif
- }
-
- // transmit a string
- /*
- void usb_serial_class::write(const char *str)
- {
- uint16_t size=0;
- const char *p=str;
-
- while (*p++) size++;
- if (size) write((const uint8_t *)str, size);
- }
- */
-
- // These are Teensy-specific extensions to the Serial object
-
- // immediately transmit any buffered output.
- // This doesn't actually transmit the data - that is impossible!
- // USB devices only transmit when the host allows, so the best
- // we can do is release the FIFO buffer for when the host wants it
- void usb_serial_class::send_now(void)
- {
- uint8_t intr_state;
-
- intr_state = SREG;
- cli();
- if (usb_configuration && transmit_flush_timer) {
- UENUM = CDC_TX_ENDPOINT;
- UEINTX = 0x3A;
- transmit_flush_timer = 0;
- }
- SREG = intr_state;
- }
-
- uint32_t usb_serial_class::baud(void)
- {
- return *(uint32_t *)cdc_line_coding;
- }
-
- uint8_t usb_serial_class::stopbits(void)
- {
- return cdc_line_coding[4];
- }
-
- uint8_t usb_serial_class::paritytype(void)
- {
- return cdc_line_coding[5];
- }
-
- uint8_t usb_serial_class::numbits(void)
- {
- return cdc_line_coding[6];
- }
-
- uint8_t usb_serial_class::dtr(void)
- {
- return (cdc_line_rtsdtr & USB_SERIAL_DTR) ? 1 : 0;
- }
-
- uint8_t usb_serial_class::rts(void)
- {
- return (cdc_line_rtsdtr & USB_SERIAL_RTS) ? 1 : 0;
- }
-
- usb_serial_class::operator bool()
- {
- if (usb_configuration &&
- (cdc_line_rtsdtr & (USB_SERIAL_DTR | USB_SERIAL_RTS))) {
- return true;
- }
- return false;
- }
-
-
-
-
- // Step #1, decode UTF8 to Unicode code points
- //
- #if ARDUINO >= 100
- size_t usb_keyboard_class::write(uint8_t c)
- #else
- void usb_keyboard_class::write(uint8_t c)
- #endif
- {
- if (c < 0x80) {
- // single byte encoded, 0x00 to 0x7F
- utf8_state = 0;
- write_unicode(c);
- } else if (c < 0xC0) {
- // 2nd, 3rd or 4th byte, 0x80 to 0xBF
- c &= 0x3F;
- if (utf8_state == 1) {
- utf8_state = 0;
- write_unicode(unicode_wchar | c);
- } else if (utf8_state == 2) {
- unicode_wchar |= ((uint16_t)c << 6);
- utf8_state = 1;
- }
- } else if (c < 0xE0) {
- // begin 2 byte sequence, 0xC2 to 0xDF
- // or illegal 2 byte sequence, 0xC0 to 0xC1
- unicode_wchar = (uint16_t)(c & 0x1F) << 6;
- utf8_state = 1;
- } else if (c < 0xF0) {
- // begin 3 byte sequence, 0xE0 to 0xEF
- unicode_wchar = (uint16_t)(c & 0x0F) << 12;
- utf8_state = 2;
- } else {
- // begin 4 byte sequence (not supported), 0xF0 to 0xF4
- // or illegal, 0xF5 to 0xFF
- utf8_state = 255;
- }
- #if ARDUINO >= 100
- return 1;
- #endif
- }
-
-
- // Step #2: translate Unicode code point to keystroke sequence
- //
- KEYCODE_TYPE usb_keyboard_class::unicode_to_keycode(uint16_t cpoint)
- {
- // Unicode code points beyond U+FFFF are not supported
- // technically this input should probably be called UCS-2
- if (cpoint < 32) {
- if (cpoint == 10) return KEY_ENTER & 0x3FFF;
- return 0;
- }
- if (cpoint < 128) {
- if (sizeof(KEYCODE_TYPE) == 1) {
- return pgm_read_byte(keycodes_ascii + (cpoint - 0x20));
- } else if (sizeof(KEYCODE_TYPE) == 2) {
- return pgm_read_word(keycodes_ascii + (cpoint - 0x20));
- }
- return 0;
- }
- #ifdef ISO_8859_1_A0
- if (cpoint <= 0xA0) return 0;
- if (cpoint < 0x100) {
- if (sizeof(KEYCODE_TYPE) == 1) {
- return pgm_read_byte(keycodes_iso_8859_1 + (cpoint - 0xA0));
- } else if (sizeof(KEYCODE_TYPE) == 2) {
- return pgm_read_word(keycodes_iso_8859_1 + (cpoint - 0xA0));
- }
- return 0;
- }
- #endif
- //#ifdef UNICODE_20AC
- //if (cpoint == 0x20AC) return UNICODE_20AC & 0x3FFF;
- //#endif
- #ifdef KEYCODE_EXTRA00
- if (cpoint == UNICODE_EXTRA00) return KEYCODE_EXTRA00 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA01
- if (cpoint == UNICODE_EXTRA01) return KEYCODE_EXTRA01 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA02
- if (cpoint == UNICODE_EXTRA02) return KEYCODE_EXTRA02 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA03
- if (cpoint == UNICODE_EXTRA03) return KEYCODE_EXTRA03 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA04
- if (cpoint == UNICODE_EXTRA04) return KEYCODE_EXTRA04 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA05
- if (cpoint == UNICODE_EXTRA05) return KEYCODE_EXTRA05 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA06
- if (cpoint == UNICODE_EXTRA06) return KEYCODE_EXTRA06 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA07
- if (cpoint == UNICODE_EXTRA07) return KEYCODE_EXTRA07 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA08
- if (cpoint == UNICODE_EXTRA08) return KEYCODE_EXTRA08 & 0x3FFF;
- #endif
- #ifdef KEYCODE_EXTRA09
- if (cpoint == UNICODE_EXTRA09) return KEYCODE_EXTRA09 & 0x3FFF;
- #endif
- return 0;
- }
-
- // Step #3: execute keystroke sequence
- //
- void usb_keyboard_class::write_keycode(KEYCODE_TYPE keycode)
- {
- if (!keycode) return;
- #ifdef DEADKEYS_MASK
- KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
- if (deadkeycode) write_key(deadkeycode);
- #endif
- write_key(keycode);
- }
-
- KEYCODE_TYPE usb_keyboard_class::deadkey_to_keycode(KEYCODE_TYPE keycode)
- {
- #ifdef DEADKEYS_MASK
- keycode &= DEADKEYS_MASK;
- if (keycode == 0) return 0;
- #ifdef ACUTE_ACCENT_BITS
- if (keycode == ACUTE_ACCENT_BITS) return DEADKEY_ACUTE_ACCENT;
- #endif
- #ifdef CEDILLA_BITS
- if (keycode == CEDILLA_BITS) return DEADKEY_CEDILLA;
- #endif
- #ifdef CIRCUMFLEX_BITS
- if (keycode == CIRCUMFLEX_BITS) return DEADKEY_CIRCUMFLEX;
- #endif
- #ifdef DIAERESIS_BITS
- if (keycode == DIAERESIS_BITS) return DEADKEY_DIAERESIS;
- #endif
- #ifdef GRAVE_ACCENT_BITS
- if (keycode == GRAVE_ACCENT_BITS) return DEADKEY_GRAVE_ACCENT;
- #endif
- #ifdef TILDE_BITS
- if (keycode == TILDE_BITS) return DEADKEY_TILDE;
- #endif
- #ifdef RING_ABOVE_BITS
- if (keycode == RING_ABOVE_BITS) return DEADKEY_RING_ABOVE;
- #endif
- #endif // DEADKEYS_MASK
- return 0;
- }
-
- // Step #4: do each keystroke
- //
- void usb_keyboard_class::write_key(KEYCODE_TYPE keycode)
- {
- keyboard_report_data[0] = keycode_to_modifier(keycode);
- keyboard_report_data[1] = 0;
- keyboard_report_data[2] = keycode_to_key(keycode);
- keyboard_report_data[3] = 0;
- keyboard_report_data[4] = 0;
- keyboard_report_data[5] = 0;
- keyboard_report_data[6] = 0;
- keyboard_report_data[7] = 0;
- send_now();
- keyboard_report_data[0] = 0;
- keyboard_report_data[2] = 0;
- send_now();
- }
-
- uint8_t usb_keyboard_class::keycode_to_modifier(KEYCODE_TYPE keycode)
- {
- uint8_t modifier=0;
-
- #ifdef SHIFT_MASK
- if (keycode & SHIFT_MASK) modifier |= MODIFIERKEY_SHIFT;
- #endif
- #ifdef ALTGR_MASK
- if (keycode & ALTGR_MASK) modifier |= MODIFIERKEY_RIGHT_ALT;
- #endif
- #ifdef RCTRL_MASK
- if (keycode & RCTRL_MASK) modifier |= MODIFIERKEY_RIGHT_CTRL;
- #endif
- return modifier;
- }
-
- uint8_t usb_keyboard_class::keycode_to_key(KEYCODE_TYPE keycode)
- {
- uint8_t key = keycode & 0x3F;
- #ifdef KEY_NON_US_100
- if (key == KEY_NON_US_100) key = 100;
- #endif
- return key;
- }
-
-
-
- void usb_keyboard_class::set_modifier(uint8_t c)
- {
- keyboard_report_data[0] = c;
- }
- void usb_keyboard_class::set_key1(uint8_t c)
- {
- keyboard_report_data[2] = c;
- }
- void usb_keyboard_class::set_key2(uint8_t c)
- {
- keyboard_report_data[3] = c;
- }
- void usb_keyboard_class::set_key3(uint8_t c)
- {
- keyboard_report_data[4] = c;
- }
- void usb_keyboard_class::set_key4(uint8_t c)
- {
- keyboard_report_data[5] = c;
- }
- void usb_keyboard_class::set_key5(uint8_t c)
- {
- keyboard_report_data[6] = c;
- }
- void usb_keyboard_class::set_key6(uint8_t c)
- {
- keyboard_report_data[7] = c;
- }
- void usb_keyboard_class::set_media(uint8_t c)
- {
- keyboard_report_data[1] = c;
- }
-
-
- void usb_keyboard_class::send_now(void)
- {
- uint8_t intr_state, timeout;
-
- if (!usb_configuration) return;
- intr_state = SREG;
- cli();
- UENUM = KEYBOARD_ENDPOINT;
- timeout = UDFNUML + 50;
- while (1) {
- // are we ready to transmit?
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- // has the USB gone offline?
- if (!usb_configuration) return;
- // have we waited too long?
- if (UDFNUML == timeout) return;
- // get ready to try checking again
- intr_state = SREG;
- cli();
- UENUM = KEYBOARD_ENDPOINT;
- }
- UEDATX = keyboard_report_data[0];
- UEDATX = keyboard_report_data[1];
- UEDATX = keyboard_report_data[2];
- UEDATX = keyboard_report_data[3];
- UEDATX = keyboard_report_data[4];
- UEDATX = keyboard_report_data[5];
- UEDATX = keyboard_report_data[6];
- UEDATX = keyboard_report_data[7];
- UEINTX = 0x3A;
- keyboard_idle_count = 0;
- SREG = intr_state;
- }
-
-
- void usb_keyboard_class::press(uint16_t n)
- {
- uint8_t key, mod, msb, modrestore=0;
-
- msb = n >> 8;
- if (msb >= 0xC2 && msb <= 0xDF) {
- n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
- } else
- if (msb == 0x80) {
- presskey(0, n);
- return;
- } else
- if (msb == 0x40) {
- presskey(n, 0);
- return;
- }
- KEYCODE_TYPE keycode = unicode_to_keycode(n);
- if (!keycode) return;
- #ifdef DEADKEYS_MASK
- KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
- if (deadkeycode) {
- modrestore = keyboard_report_data[0];
- if (modrestore) {
- keyboard_report_data[0] = 0;
- send_now();
- }
- // TODO: test if operating systems recognize
- // deadkey sequences when other keys are held
- mod = keycode_to_modifier(deadkeycode);
- key = keycode_to_key(deadkeycode);
- presskey(key, mod);
- releasekey(key, mod);
- }
- #endif
- mod = keycode_to_modifier(keycode);
- key = keycode_to_key(keycode);
- presskey(key, mod | modrestore);
- }
-
- void usb_keyboard_class::release(uint16_t n)
- {
- uint8_t key, mod, msb;
-
- msb = n >> 8;
- if (msb >= 0xC2 && msb <= 0xDF) {
- n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
- } else
- if (msb == 0x80) {
- releasekey(0, n);
- return;
- } else
- if (msb == 0x40) {
- releasekey(n, 0);
- return;
- }
- KEYCODE_TYPE keycode = unicode_to_keycode(n);
- if (!keycode) return;
- mod = keycode_to_modifier(keycode);
- key = keycode_to_key(keycode);
- releasekey(key, mod);
- }
-
- void usb_keyboard_class::presskey(uint8_t key, uint8_t modifier)
- {
- bool send_required = false;
- uint8_t i;
-
- if (modifier) {
- if ((keyboard_report_data[0] & modifier) != modifier) {
- keyboard_report_data[0] |= modifier;
- send_required = true;
- }
- }
- if (key) {
- for (i=2; i < 8; i++) {
- if (keyboard_report_data[i] == key) goto end;
- }
- for (i=2; i < 8; i++) {
- if (keyboard_report_data[i] == 0) {
- keyboard_report_data[i] = key;
- send_required = true;
- goto end;
- }
- }
- }
- end:
- if (send_required) send_now();
- }
-
- void usb_keyboard_class::releasekey(uint8_t key, uint8_t modifier)
- {
- bool send_required = false;
- uint8_t i;
-
- if (modifier) {
- if ((keyboard_report_data[0] & modifier) != 0) {
- keyboard_report_data[0] &= ~modifier;
- send_required = true;
- }
- }
- if (key) {
- for (i=2; i < 8; i++) {
- if (keyboard_report_data[i] == key) {
- keyboard_report_data[i] = 0;
- send_required = true;
- }
- }
- }
- if (send_required) send_now();
- }
-
- void usb_keyboard_class::releaseAll(void)
- {
- uint8_t i, anybits;
-
- anybits = keyboard_report_data[0];
- for (i=2; i < 8; i++) {
- anybits |= keyboard_report_data[i];
- keyboard_report_data[i] = 0;
- }
- if (!anybits) return;
- keyboard_report_data[0] = 0;
- send_now();
- }
-
-
-
-
-
-
- void usb_mouse_class::move(int8_t x, int8_t y, int8_t wheel)
- {
- uint8_t intr_state, timeout;
-
- if (!usb_configuration) return;
- if (x == -128) x = -127;
- if (y == -128) y = -127;
- if (wheel == -128) wheel = -127;
- intr_state = SREG;
- cli();
- UENUM = MOUSE_ENDPOINT;
- timeout = UDFNUML + 50;
- while (1) {
- // are we ready to transmit?
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- // has the USB gone offline?
- if (!usb_configuration) return;
- // have we waited too long?
- if (UDFNUML == timeout) return;
- // get ready to try checking again
- intr_state = SREG;
- cli();
- UENUM = MOUSE_ENDPOINT;
- }
- UEDATX = mouse_buttons;
- UEDATX = x;
- UEDATX = y;
- UEDATX = wheel;
- UEINTX = 0x3A;
- SREG = intr_state;
- }
-
- void usb_mouse_class::click(uint8_t b)
- {
- mouse_buttons = (b & 7);
- move(0, 0);
- mouse_buttons = 0;
- move(0, 0);
- }
-
- void usb_mouse_class::scroll(int8_t wheel)
- {
- move(0, 0, wheel);
- }
-
- void usb_mouse_class::set_buttons(uint8_t left, uint8_t middle, uint8_t right)
- {
- uint8_t mask=0;
-
- if (left) mask |= 1;
- if (middle) mask |= 4;
- if (right) mask |= 2;
- mouse_buttons = mask;
- move(0, 0);
- }
-
- void usb_mouse_class::press(uint8_t b)
- {
- uint8_t prev = mouse_buttons;
- mouse_buttons |= (b & 7);
- if (mouse_buttons != prev) move(0, 0);
- }
-
- void usb_mouse_class::release(uint8_t b)
- {
- uint8_t prev = mouse_buttons;
- mouse_buttons &= ~(b & 7);
- if (mouse_buttons != prev) move(0, 0);
- }
-
- bool usb_mouse_class::isPressed(uint8_t b)
- {
- return ((mouse_buttons & (b & 7)) != 0);
- }
-
-
-
- void usb_joystick_class::send_now(void)
- {
- uint8_t intr_state, timeout;
-
- if (!usb_configuration) return;
- intr_state = SREG;
- cli();
- UENUM = JOYSTICK_ENDPOINT;
- timeout = UDFNUML + 50;
- while (1) {
- // are we ready to transmit?
- if (UEINTX & (1<<RWAL)) break;
- SREG = intr_state;
- // has the USB gone offline?
- if (!usb_configuration) return;
- // have we waited too long?
- if (UDFNUML == timeout) return;
- // get ready to try checking again
- intr_state = SREG;
- cli();
- UENUM = JOYSTICK_ENDPOINT;
- }
- UEDATX = joystick_report_data[0];
- UEDATX = joystick_report_data[1];
- UEDATX = joystick_report_data[2];
- UEDATX = joystick_report_data[3];
- UEDATX = joystick_report_data[4];
- UEDATX = joystick_report_data[5];
- UEDATX = joystick_report_data[6];
- UEDATX = joystick_report_data[7];
- UEDATX = joystick_report_data[8];
- UEDATX = joystick_report_data[9];
- UEDATX = joystick_report_data[10];
- UEDATX = joystick_report_data[11];
- UEINTX = 0x3A;
- SREG = intr_state;
- }
-
-
-
-
-
-
-
- // Preinstantiate Objects //////////////////////////////////////////////////////
-
- usb_serial_class Serial = usb_serial_class();
- usb_keyboard_class Keyboard = usb_keyboard_class();
- usb_mouse_class Mouse = usb_mouse_class();
- usb_joystick_class Joystick = usb_joystick_class();
-
|