#include "usb_dev.h"
#include "usb_keyboard.h"
#include "core_pins.h"
#include "keylayouts.h"
#include <string.h>
#ifdef KEYBOARD_INTERFACE
#if F_CPU >= 20000000
uint8_t keyboard_modifier_keys=0;
uint8_t keyboard_keys[6]={0,0,0,0,0,0};
#ifdef KEYMEDIA_INTERFACE
uint16_t keymedia_consumer_keys[4];
uint8_t keymedia_system_keys[3];
#endif
uint8_t keyboard_protocol=1;
uint8_t keyboard_idle_config=125;
uint8_t keyboard_idle_count=0;
volatile uint8_t keyboard_leds=0;
static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint);
static void write_key(KEYCODE_TYPE keycode);
static uint8_t keycode_to_modifier(KEYCODE_TYPE keycode);
static uint8_t keycode_to_key(KEYCODE_TYPE keycode);
static void usb_keyboard_press_key(uint8_t key, uint8_t modifier);
static void usb_keyboard_release_key(uint8_t key, uint8_t modifier);
#ifdef DEADKEYS_MASK
static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode);
#endif
#ifdef KEYMEDIA_INTERFACE
static void usb_keymedia_press_consumer_key(uint16_t key);
static void usb_keymedia_release_consumer_key(uint16_t key);
static void usb_keymedia_press_system_key(uint8_t key);
static void usb_keymedia_release_system_key(uint8_t key);
static int usb_keymedia_send(void);
#endif
void usb_keyboard_write(uint8_t c)
{
static int utf8_state=0;
static uint16_t unicode_wchar=0;
if (c < 0x80) {
utf8_state = 0;
usb_keyboard_write_unicode(c);
} else if (c < 0xC0) {
c &= 0x3F;
if (utf8_state == 1) {
utf8_state = 0;
usb_keyboard_write_unicode(unicode_wchar | c);
} else if (utf8_state == 2) {
unicode_wchar |= ((uint16_t)c << 6);
utf8_state = 1;
}
} else if (c < 0xE0) {
unicode_wchar = (uint16_t)(c & 0x1F) << 6;
utf8_state = 1;
} else if (c < 0xF0) {
unicode_wchar = (uint16_t)(c & 0x0F) << 12;
utf8_state = 2;
} else {
utf8_state = 255;
}
}
static KEYCODE_TYPE unicode_to_keycode(uint16_t cpoint)
{
if (cpoint < 32) {
if (cpoint == 10) return KEY_ENTER & KEYCODE_MASK;
if (cpoint == 11) return KEY_TAB & KEYCODE_MASK;
return 0;
}
if (cpoint < 128) {
return keycodes_ascii[cpoint - 0x20];
}
#ifdef ISO_8859_1_A0
if (cpoint >= 0xA0 && cpoint < 0x100) {
return keycodes_iso_8859_1[cpoint - 0xA0];
}
#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
#ifdef KEYCODE_EXTRA0A
if (cpoint == UNICODE_EXTRA0A) return (KEYCODE_EXTRA0A) & 0x3FFF;
#endif
return 0;
}
#ifdef DEADKEYS_MASK
static KEYCODE_TYPE deadkey_to_keycode(KEYCODE_TYPE keycode)
{
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
#ifdef DEGREE_SIGN_BITS
if (keycode == DEGREE_SIGN_BITS) return DEADKEY_DEGREE_SIGN;
#endif
#ifdef CARON_BITS
if (keycode == CARON_BITS) return DEADKEY_CARON;
#endif
#ifdef BREVE_BITS
if (keycode == BREVE_BITS) return DEADKEY_BREVE;
#endif
#ifdef OGONEK_BITS
if (keycode == OGONEK_BITS) return DEADKEY_OGONEK;
#endif
#ifdef DOT_ABOVE_BITS
if (keycode == DOT_ABOVE_BITS) return DEADKEY_DOT_ABOVE;
#endif
#ifdef DOUBLE_ACUTE_BITS
if (keycode == DOUBLE_ACUTE_BITS) return DEADKEY_DOUBLE_ACUTE;
#endif
return 0;
}
#endif
void usb_keyboard_write_unicode(uint16_t cpoint)
{
KEYCODE_TYPE keycode;
keycode = unicode_to_keycode(cpoint);
if (keycode) {
#ifdef DEADKEYS_MASK
KEYCODE_TYPE deadkeycode = deadkey_to_keycode(keycode);
if (deadkeycode) write_key(deadkeycode);
#endif
write_key(keycode);
}
}
static void write_key(KEYCODE_TYPE keycode)
{
usb_keyboard_press(keycode_to_key(keycode), keycode_to_modifier(keycode));
}
static uint8_t 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;
}
static uint8_t 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_press_keycode(uint16_t n)
{
uint8_t key, mod, msb, modrestore=0;
KEYCODE_TYPE keycode;
#ifdef DEADKEYS_MASK
KEYCODE_TYPE deadkeycode;
#endif
msb = n >> 8;
if (msb >= 0xC2) {
if (msb <= 0xDF) {
n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
} else if (msb == 0xF0) {
usb_keyboard_press_key(n, 0);
return;
} else if (msb == 0xE0) {
usb_keyboard_press_key(0, n);
return;
#ifdef KEYMEDIA_INTERFACE
} else if (msb == 0xE2) {
usb_keymedia_press_system_key(n);
return;
} else if (msb >= 0xE4 && msb <= 0xE7) {
usb_keymedia_press_consumer_key(n & 0x3FF);
return;
#endif
} else {
return;
}
}
keycode = unicode_to_keycode(n);
if (!keycode) return;
#ifdef DEADKEYS_MASK
deadkeycode = deadkey_to_keycode(keycode);
if (deadkeycode) {
modrestore = keyboard_modifier_keys;
if (modrestore) {
keyboard_modifier_keys = 0;
usb_keyboard_send();
}
mod = keycode_to_modifier(deadkeycode);
key = keycode_to_key(deadkeycode);
usb_keyboard_press_key(key, mod);
usb_keyboard_release_key(key, mod);
}
#endif
mod = keycode_to_modifier(keycode);
key = keycode_to_key(keycode);
usb_keyboard_press_key(key, mod | modrestore);
}
void usb_keyboard_release_keycode(uint16_t n)
{
uint8_t key, mod, msb;
msb = n >> 8;
if (msb >= 0xC2) {
if (msb <= 0xDF) {
n = (n & 0x3F) | ((uint16_t)(msb & 0x1F) << 6);
} else if (msb == 0xF0) {
usb_keyboard_release_key(n, 0);
return;
} else if (msb == 0xE0) {
usb_keyboard_release_key(0, n);
return;
#ifdef KEYMEDIA_INTERFACE
} else if (msb == 0xE2) {
usb_keymedia_release_system_key(n);
return;
} else if (msb >= 0xE4 && msb <= 0xE7) {
usb_keymedia_release_consumer_key(n & 0x3FF);
return;
#endif
} else {
return;
}
}
KEYCODE_TYPE keycode = unicode_to_keycode(n);
if (!keycode) return;
mod = keycode_to_modifier(keycode);
key = keycode_to_key(keycode);
usb_keyboard_release_key(key, mod);
}
static void usb_keyboard_press_key(uint8_t key, uint8_t modifier)
{
int i, send_required = 0;
if (modifier) {
if ((keyboard_modifier_keys & modifier) != modifier) {
keyboard_modifier_keys |= modifier;
send_required = 1;
}
}
if (key) {
for (i=0; i < 6; i++) {
if (keyboard_keys[i] == key) goto end;
}
for (i=0; i < 6; i++) {
if (keyboard_keys[i] == 0) {
keyboard_keys[i] = key;
send_required = 1;
goto end;
}
}
}
end:
if (send_required) usb_keyboard_send();
}
static void usb_keyboard_release_key(uint8_t key, uint8_t modifier)
{
int i, send_required = 0;
if (modifier) {
if ((keyboard_modifier_keys & modifier) != 0) {
keyboard_modifier_keys &= ~modifier;
send_required = 1;
}
}
if (key) {
for (i=0; i < 6; i++) {
if (keyboard_keys[i] == key) {
keyboard_keys[i] = 0;
send_required = 1;
}
}
}
if (send_required) usb_keyboard_send();
}
void usb_keyboard_release_all(void)
{
uint8_t i, anybits;
anybits = keyboard_modifier_keys;
keyboard_modifier_keys = 0;
for (i=0; i < 6; i++) {
anybits |= keyboard_keys[i];
keyboard_keys[i] = 0;
}
if (anybits) usb_keyboard_send();
#ifdef KEYMEDIA_INTERFACE
anybits = 0;
for (i=0; i < 4; i++) {
if (keymedia_consumer_keys[i] != 0) anybits = 1;
keymedia_consumer_keys[i] = 0;
}
for (i=0; i < 3; i++) {
if (keymedia_system_keys[i] != 0) anybits = 1;
keymedia_system_keys[i] = 0;
}
if (anybits) usb_keymedia_send();
#endif
}
int usb_keyboard_press(uint8_t key, uint8_t modifier)
{
int r;
keyboard_modifier_keys = modifier;
keyboard_keys[0] = key;
keyboard_keys[1] = 0;
keyboard_keys[2] = 0;
keyboard_keys[3] = 0;
keyboard_keys[4] = 0;
keyboard_keys[5] = 0;
r = usb_keyboard_send();
if (r) return r;
keyboard_modifier_keys = 0;
keyboard_keys[0] = 0;
return usb_keyboard_send();
}
#define TX_PACKET_LIMIT 4
static uint8_t transmit_previous_timeout=0;
#define TX_TIMEOUT_MSEC 50
#if F_CPU == 240000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1600)
#elif F_CPU == 216000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1440)
#elif F_CPU == 192000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1280)
#elif F_CPU == 180000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1200)
#elif F_CPU == 168000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 1100)
#elif F_CPU == 144000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 932)
#elif F_CPU == 120000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 764)
#elif F_CPU == 96000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 596)
#elif F_CPU == 72000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 512)
#elif F_CPU == 48000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 428)
#elif F_CPU == 24000000
#define TX_TIMEOUT (TX_TIMEOUT_MSEC * 262)
#endif
int usb_keyboard_send(void)
{
#if 0#endif
#if 1
uint32_t wait_count=0;
usb_packet_t *tx_packet;
while (1) {
if (!usb_configuration) {
return -1;
}
if (usb_tx_packet_count(KEYBOARD_ENDPOINT) < TX_PACKET_LIMIT) {
tx_packet = usb_malloc();
if (tx_packet) break;
}
if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
transmit_previous_timeout = 1;
return -1;
}
yield();
}
*(tx_packet->buf) = keyboard_modifier_keys;
*(tx_packet->buf + 1) = 0;
memcpy(tx_packet->buf + 2, keyboard_keys, 6);
tx_packet->len = 8;
usb_tx(KEYBOARD_ENDPOINT, tx_packet);
#endif
return 0;
}
#ifdef KEYMEDIA_INTERFACE
static void usb_keymedia_press_consumer_key(uint16_t key)
{
int i;
if (key == 0) return;
for (i=0; i < 4; i++) {
if (keymedia_consumer_keys[i] == key) return;
}
for (i=0; i < 4; i++) {
if (keymedia_consumer_keys[i] == 0) {
keymedia_consumer_keys[i] = key;
usb_keymedia_send();
return;
}
}
}
static void usb_keymedia_release_consumer_key(uint16_t key)
{
int i;
if (key == 0) return;
for (i=0; i < 4; i++) {
if (keymedia_consumer_keys[i] == key) {
keymedia_consumer_keys[i] = 0;
usb_keymedia_send();
return;
}
}
}
static void usb_keymedia_press_system_key(uint8_t key)
{
int i;
if (key == 0) return;
for (i=0; i < 3; i++) {
if (keymedia_system_keys[i] == key) return;
}
for (i=0; i < 3; i++) {
if (keymedia_system_keys[i] == 0) {
keymedia_system_keys[i] = key;
usb_keymedia_send();
return;
}
}
}
static void usb_keymedia_release_system_key(uint8_t key)
{
int i;
if (key == 0) return;
for (i=0; i < 3; i++) {
if (keymedia_system_keys[i] == key) {
keymedia_system_keys[i] = 0;
usb_keymedia_send();
return;
}
}
}
void usb_keymedia_release_all(void)
{
uint8_t i, anybits;
anybits = 0;
for (i=0; i < 4; i++) {
if (keymedia_consumer_keys[i] != 0) anybits = 1;
keymedia_consumer_keys[i] = 0;
}
for (i=0; i < 3; i++) {
if (keymedia_system_keys[i] != 0) anybits = 1;
keymedia_system_keys[i] = 0;
}
if (anybits) usb_keymedia_send();
}
static int usb_keymedia_send(void)
{
uint32_t wait_count=0;
usb_packet_t *tx_packet;
const uint16_t *consumer;
while (1) {
if (!usb_configuration) {
return -1;
}
if (usb_tx_packet_count(KEYMEDIA_ENDPOINT) < TX_PACKET_LIMIT) {
tx_packet = usb_malloc();
if (tx_packet) break;
}
if (++wait_count > TX_TIMEOUT || transmit_previous_timeout) {
transmit_previous_timeout = 1;
return -1;
}
yield();
}
consumer = keymedia_consumer_keys;
*(tx_packet->buf + 0) = consumer[0];
*(tx_packet->buf + 1) = (consumer[1] << 2) | ((consumer[0] >> 8) & 0x03);
*(tx_packet->buf + 2) = (consumer[2] << 4) | ((consumer[1] >> 6) & 0x0F);
*(tx_packet->buf + 3) = (consumer[3] << 6) | ((consumer[2] >> 4) & 0x3F);
*(tx_packet->buf + 4) = consumer[3] >> 2;
*(tx_packet->buf + 5) = keymedia_system_keys[0];
*(tx_packet->buf + 6) = keymedia_system_keys[1];
*(tx_packet->buf + 7) = keymedia_system_keys[2];
tx_packet->len = 8;
usb_tx(KEYMEDIA_ENDPOINT, tx_packet);
return 0;
}
#endif
#endif #endif