diff options
author | tmk <nobody@nowhere> | 2011-02-09 00:03:58 +0900 |
---|---|---|
committer | tmk <nobody@nowhere> | 2011-02-22 03:08:59 +0900 |
commit | acc974c64b1e17e6807133fdc50de5bb34aedda5 (patch) | |
tree | 7af7511a56d680a4d93b535c52891a8ffc15dd04 /keyboard.c | |
parent | 5552b5afeaa9ce7432f9ded3586984253f292d80 (diff) |
added protocol stack: pjrc, vusb
Diffstat (limited to 'keyboard.c')
-rw-r--r-- | keyboard.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/keyboard.c b/keyboard.c new file mode 100644 index 0000000000..6bf2b3a9ed --- /dev/null +++ b/keyboard.c @@ -0,0 +1,93 @@ +#include "usb_keycodes.h" +#include "host.h" +#include "led.h" +#include "keyboard.h" +#include "print.h" + +static report_keyboard_t report0; +static report_keyboard_t report1; +report_keyboard_t *keyboard_report = &report0; +report_keyboard_t *keyboard_report_prev = &report1; + + +void keyboard_set_led(uint8_t usb_led) +{ + led_set(usb_led); +} + +void keyboard_send(void) +{ + host_keyboard_send(keyboard_report); +} + +void keyboard_add_key(uint8_t code) +{ + int8_t i = 0; + int8_t empty = -1; + for (; i < REPORT_KEYS; i++) { + if (keyboard_report_prev->keys[i] == code) { + keyboard_report->keys[i] = code; + break; + } + if (empty == -1 && keyboard_report_prev->keys[i] == KB_NO && keyboard_report->keys[i] == KB_NO) { + empty = i; + } + } + if (i == REPORT_KEYS && empty != -1) { + keyboard_report->keys[empty] = code; + } +} + +void keyboard_add_mod_bit(uint8_t mod) +{ + keyboard_report->mods |= mod; +} + +void keyboard_set_mods(uint8_t mods) +{ + keyboard_report->mods = mods; +} + +void keyboard_add_code(uint8_t code) +{ + if (IS_MOD(code)) { + keyboard_add_mod_bit(MOD_BIT(code)); + } else { + keyboard_add_key(code); + } +} + +void keyboard_swap_report(void) +{ + report_keyboard_t *tmp = keyboard_report_prev; + keyboard_report_prev = keyboard_report; + keyboard_report = tmp; +} + +void keyboard_clear_report(void) +{ + keyboard_report->mods = 0; + for (int8_t i = 0; i < REPORT_KEYS; i++) { + keyboard_report->keys[i] = 0; + } +} + +uint8_t keyboard_has_anykey(void) +{ + uint8_t cnt = 0; + for (int i = 0; i < REPORT_KEYS; i++) { + if (keyboard_report->keys[i]) + cnt++; + } + return cnt; +} + +uint8_t *keyboard_get_keys(void) +{ + return keyboard_report->keys; +} + +uint8_t keyboard_get_mods(void) +{ + return keyboard_report->mods; +} |