diff options
author | tmk <nobody@nowhere> | 2010-11-03 17:33:20 +0900 |
---|---|---|
committer | tmk <nobody@nowhere> | 2010-11-03 17:33:20 +0900 |
commit | a31b31e717a2fa67642be33b321cde4b9b3b496a (patch) | |
tree | 94be4f4bc54bc93dc407f1bf0cef7624e346f3dd /usb_keyboard.c | |
parent | 45d4a7a89883c3433604d4e011b665796a583008 (diff) |
revise Fn key processing.
Diffstat (limited to 'usb_keyboard.c')
-rw-r--r-- | usb_keyboard.c | 158 |
1 files changed, 107 insertions, 51 deletions
diff --git a/usb_keyboard.c b/usb_keyboard.c index dc781f0172..d289af8e0a 100644 --- a/usb_keyboard.c +++ b/usb_keyboard.c @@ -1,52 +1,40 @@ #include <avr/interrupt.h> #include <avr/pgmspace.h> +#include "usb_keycodes.h" #include "usb_keyboard.h" #include "print.h" #include "debug.h" -static bool is_sent = false; - -// which modifier keys are currently pressed -// 1=left ctrl, 2=left shift, 4=left alt, 8=left gui -// 16=right ctrl, 32=right shift, 64=right alt, 128=right gui -uint8_t keyboard_modifier_keys=0; - -// which keys are currently pressed, up to 6 keys may be down at once -uint8_t keyboard_keys[6]={0,0,0,0,0,0}; +// keyboard report. +static usb_keyboard_report_t _report0 = { {0,0,0,0,0,0}, 0 }; +static usb_keyboard_report_t _report1 = { {0,0,0,0,0,0}, 0 }; +usb_keyboard_report_t *usb_keyboard_report = &_report0; +usb_keyboard_report_t *usb_keyboard_report_back = &_report1; // 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. -uint8_t keyboard_protocol=1; +uint8_t usb_keyboard_protocol=1; // the idle configuration, how often we send the report to the // host (ms * 4) even when it hasn't changed -uint8_t keyboard_idle_config=125; +uint8_t usb_keyboard_idle_config=125; // count until idle timeout -uint8_t keyboard_idle_count=0; +uint8_t usb_keyboard_idle_count=0; // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana -volatile uint8_t keyboard_leds=0; +volatile uint8_t usb_keyboard_leds=0; -// perform a single keystroke -int8_t usb_keyboard_press(uint8_t key, uint8_t modifier) +int8_t usb_keyboard_send(void) { - int8_t r; - - keyboard_modifier_keys = modifier; - keyboard_keys[0] = key; - r = usb_keyboard_send(); - if (r) return r; - keyboard_modifier_keys = 0; - keyboard_keys[0] = 0; - return usb_keyboard_send(); + return usb_keyboard_send_report(usb_keyboard_report); } -// send the contents of keyboard_keys and keyboard_modifier_keys -int8_t usb_keyboard_send(void) + +int8_t usb_keyboard_send_report(usb_keyboard_report_t *report) { uint8_t i, intr_state, timeout; @@ -68,54 +56,122 @@ int8_t usb_keyboard_send(void) cli(); UENUM = KEYBOARD_ENDPOINT; } - UEDATX = keyboard_modifier_keys; + UEDATX = report->mods; UEDATX = 0; for (i=0; i<6; i++) { - UEDATX = keyboard_keys[i]; + UEDATX = report->keys[i]; } UEINTX = 0x3A; - keyboard_idle_count = 0; + usb_keyboard_idle_count = 0; SREG = intr_state; - is_sent = true; + + report->is_sent =true; + + usb_keyboard_print_report(report); return 0; } -void usb_keyboard_init(void) { - usb_keyboard_clear(); - is_sent = false; +void usb_keyboard_swap_report(void) { + usb_keyboard_report_t *tmp = usb_keyboard_report_back; + usb_keyboard_report_back = usb_keyboard_report; + usb_keyboard_report = tmp; } -void usb_keyboard_clear(void) { - usb_keyboard_clear_key(); - usb_keyboard_clear_mod(); +void usb_keyboard_clear_report(void) { + usb_keyboard_clear_keys(); + usb_keyboard_clear_mods(); + usb_keyboard_report->is_sent = false; } -void usb_keyboard_clear_key(void) { - for (int i = 0; i < 6; i++) keyboard_keys[i] = 0; +void usb_keyboard_clear_keys(void) { + for (int i = 0; i < 6; i++) usb_keyboard_report->keys[i] = 0; } -void usb_keyboard_clear_mod(void) { - keyboard_modifier_keys = 0; +void usb_keyboard_clear_mods(void) +{ + usb_keyboard_report->mods = 0; } -bool usb_keyboard_is_sent(void) { - return is_sent; +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) +{ + for (int i = 0; i < 6; i++) { + if (!usb_keyboard_report->keys[i]) { + usb_keyboard_report->keys[i] = code; + return; + } + } +} + +void usb_keyboard_set_keys(uint8_t keys[6]) +{ + for (int i = 0; i < 6; i++) + usb_keyboard_report->keys[i] = keys[i]; +} + +void usb_keyboard_set_mods(uint8_t mods) +{ + usb_keyboard_report->mods = mods; } -bool usb_keyboard_has_key(void) { +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) +{ + for (int i = 0; i < 6; i++) { + 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) +{ uint8_t keys = 0; - for (int i = 0; i < 6; i++) keys |= keyboard_keys[i]; + for (int i = 0; i < 6; i++) keys |= usb_keyboard_report->keys[i]; return keys ? true : false; } -bool usb_keyboard_has_mod(void) { - return keyboard_modifier_keys ? true : false; +bool usb_keyboard_has_mod(void) +{ + return usb_keyboard_report->mods ? true : false; } -void usb_keyboard_print(void) { +void usb_keyboard_print_report(usb_keyboard_report_t *report) +{ if (!debug_keyboard) return; - print("\nkeys: "); - for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); } - print("\n"); - print("mods: "); phex(keyboard_modifier_keys); print("\n"); + print("keys: "); + for (int i = 0; i < 6; i++) { phex(report->keys[i]); print(" "); } + print(" mods: "); phex(report->mods); print("\n"); } |