2010-09-23 13:23:50 +02:00
|
|
|
#include <avr/interrupt.h>
|
|
|
|
#include <avr/pgmspace.h>
|
2010-11-03 09:33:20 +01:00
|
|
|
#include "usb_keycodes.h"
|
2010-09-23 13:23:50 +02:00
|
|
|
#include "usb_keyboard.h"
|
2010-10-23 18:17:26 +02:00
|
|
|
#include "print.h"
|
2010-10-26 14:32:45 +02:00
|
|
|
#include "debug.h"
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
// keyboard report.
|
2010-11-24 14:17:35 +01:00
|
|
|
static usb_keyboard_report_t _report0 = { {0}, 0 };
|
|
|
|
static usb_keyboard_report_t _report1 = { {0}, 0 };
|
2010-11-03 09:33:20 +01:00
|
|
|
usb_keyboard_report_t *usb_keyboard_report = &_report0;
|
2010-11-24 14:17:35 +01:00
|
|
|
usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
// protocol setting from the host. We use exactly the same report
|
|
|
|
// either way, so this variable only stores the setting since we
|
|
|
|
// are required to be able to report which setting is in use.
|
2010-11-03 09:33:20 +01:00
|
|
|
uint8_t usb_keyboard_protocol=1;
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
// the idle configuration, how often we send the report to the
|
|
|
|
// host (ms * 4) even when it hasn't changed
|
2010-11-03 09:33:20 +01:00
|
|
|
uint8_t usb_keyboard_idle_config=125;
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
// count until idle timeout
|
2010-11-03 09:33:20 +01:00
|
|
|
uint8_t usb_keyboard_idle_count=0;
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
// 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
|
2010-11-03 09:33:20 +01:00
|
|
|
volatile uint8_t usb_keyboard_leds=0;
|
2010-09-23 13:23:50 +02:00
|
|
|
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
int8_t usb_keyboard_send(void)
|
2010-09-23 13:23:50 +02:00
|
|
|
{
|
2010-11-03 09:33:20 +01:00
|
|
|
return usb_keyboard_send_report(usb_keyboard_report);
|
2010-09-23 13:23:50 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
|
|
|
|
int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
|
2010-09-23 13:23:50 +02:00
|
|
|
{
|
|
|
|
uint8_t i, intr_state, timeout;
|
|
|
|
|
|
|
|
if (!usb_configured()) return -1;
|
|
|
|
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_configured()) return -1;
|
|
|
|
// have we waited too long?
|
|
|
|
if (UDFNUML == timeout) return -1;
|
|
|
|
// get ready to try checking again
|
|
|
|
intr_state = SREG;
|
|
|
|
cli();
|
|
|
|
UENUM = KEYBOARD_ENDPOINT;
|
|
|
|
}
|
2010-11-03 09:33:20 +01:00
|
|
|
UEDATX = report->mods;
|
2010-09-23 13:23:50 +02:00
|
|
|
UEDATX = 0;
|
2010-11-24 14:17:35 +01:00
|
|
|
for (i = 0; i < 6; i++) {
|
2010-11-03 09:33:20 +01:00
|
|
|
UEDATX = report->keys[i];
|
2010-09-23 13:23:50 +02:00
|
|
|
}
|
|
|
|
UEINTX = 0x3A;
|
|
|
|
SREG = intr_state;
|
2010-11-03 09:33:20 +01:00
|
|
|
|
2010-11-25 17:56:21 +01:00
|
|
|
#ifdef USB_12KRO
|
2010-11-24 14:17:35 +01:00
|
|
|
if (!usb_configured()) return -1;
|
|
|
|
intr_state = SREG;
|
|
|
|
cli();
|
|
|
|
UENUM = KEYBOARD_ENDPOINT2;
|
|
|
|
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_configured()) return -1;
|
|
|
|
// have we waited too long?
|
|
|
|
if (UDFNUML == timeout) return -1;
|
|
|
|
// get ready to try checking again
|
|
|
|
intr_state = SREG;
|
|
|
|
cli();
|
|
|
|
UENUM = KEYBOARD_ENDPOINT2;
|
|
|
|
}
|
2010-11-25 17:56:21 +01:00
|
|
|
UEDATX = report->mods;
|
2010-11-24 14:17:35 +01:00
|
|
|
UEDATX = 0;
|
|
|
|
for (i = 6; i < 12; i++) {
|
|
|
|
UEDATX = report->keys[i];
|
|
|
|
}
|
|
|
|
UEINTX = 0x3A;
|
|
|
|
SREG = intr_state;
|
2010-11-25 17:56:21 +01:00
|
|
|
#endif
|
2010-11-03 09:33:20 +01:00
|
|
|
|
2010-11-24 14:17:35 +01:00
|
|
|
usb_keyboard_idle_count = 0;
|
2010-11-25 17:56:21 +01:00
|
|
|
report->is_sent =true;
|
|
|
|
usb_keyboard_print_report(report);
|
2010-09-23 13:23:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2010-10-23 18:17:26 +02:00
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_swap_report(void) {
|
2010-11-24 14:17:35 +01:00
|
|
|
usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
|
|
|
|
usb_keyboard_report_prev = usb_keyboard_report;
|
2010-11-03 09:33:20 +01:00
|
|
|
usb_keyboard_report = tmp;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_clear_report(void) {
|
|
|
|
usb_keyboard_clear_keys();
|
|
|
|
usb_keyboard_clear_mods();
|
|
|
|
usb_keyboard_report->is_sent = false;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_clear_keys(void) {
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) usb_keyboard_report->keys[i] = 0;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_clear_mods(void)
|
|
|
|
{
|
|
|
|
usb_keyboard_report->mods = 0;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_add_code(uint8_t code)
|
|
|
|
{
|
|
|
|
if (IS_MOD(code)) {
|
|
|
|
usb_keyboard_add_mod(code);
|
|
|
|
} else {
|
|
|
|
usb_keyboard_add_key(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_keyboard_add_key(uint8_t code)
|
|
|
|
{
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
|
2010-11-03 09:33:20 +01:00
|
|
|
if (!usb_keyboard_report->keys[i]) {
|
|
|
|
usb_keyboard_report->keys[i] = code;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 14:17:35 +01:00
|
|
|
void usb_keyboard_set_keys(uint8_t *keys)
|
2010-11-03 09:33:20 +01:00
|
|
|
{
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++)
|
2010-11-03 09:33:20 +01:00
|
|
|
usb_keyboard_report->keys[i] = keys[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_keyboard_set_mods(uint8_t mods)
|
|
|
|
{
|
|
|
|
usb_keyboard_report->mods = mods;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_add_mod(uint8_t code)
|
|
|
|
{
|
|
|
|
usb_keyboard_report->mods |= MOD_BIT(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_keyboard_del_code(uint8_t code)
|
|
|
|
{
|
|
|
|
if (IS_MOD(code)) {
|
|
|
|
usb_keyboard_del_mod(code);
|
|
|
|
} else {
|
|
|
|
usb_keyboard_del_key(code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_keyboard_del_key(uint8_t code)
|
|
|
|
{
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
|
2010-11-03 09:33:20 +01:00
|
|
|
if (usb_keyboard_report->keys[i] == code) {
|
|
|
|
usb_keyboard_report->keys[i] = KB_NO;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void usb_keyboard_del_mod(uint8_t code)
|
|
|
|
{
|
|
|
|
usb_keyboard_report->mods &= ~MOD_BIT(code);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_keyboard_is_sent(void)
|
|
|
|
{
|
|
|
|
return usb_keyboard_report->is_sent;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool usb_keyboard_has_key(void)
|
|
|
|
{
|
2010-10-23 18:17:26 +02:00
|
|
|
uint8_t keys = 0;
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) keys |= usb_keyboard_report->keys[i];
|
2010-10-23 18:17:26 +02:00
|
|
|
return keys ? true : false;
|
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
bool usb_keyboard_has_mod(void)
|
|
|
|
{
|
|
|
|
return usb_keyboard_report->mods ? true : false;
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|
|
|
|
|
2010-11-03 09:33:20 +01:00
|
|
|
void usb_keyboard_print_report(usb_keyboard_report_t *report)
|
|
|
|
{
|
2010-10-26 14:32:45 +02:00
|
|
|
if (!debug_keyboard) return;
|
2010-11-03 09:33:20 +01:00
|
|
|
print("keys: ");
|
2010-11-24 14:17:35 +01:00
|
|
|
for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) { phex(report->keys[i]); print(" "); }
|
2010-11-03 09:33:20 +01:00
|
|
|
print(" mods: "); phex(report->mods); print("\n");
|
2010-10-23 18:17:26 +02:00
|
|
|
}
|