2 #include "usb_keycodes.h"
3 #include "usb_keyboard.h"
10 #ifdef USB_NKRO_ENABLE
11 bool keyboard_nkro = false;
14 static report_keyboard_t report0;
15 static report_keyboard_t report1;
16 report_keyboard_t *keyboard_report = &report0;
17 report_keyboard_t *keyboard_report_prev = &report1;
19 static inline void add_key_byte(uint8_t code);
20 static inline void add_key_bit(uint8_t code);
23 uint8_t host_keyboard_leds(void)
25 return usb_keyboard_leds;
28 /* keyboard report operations */
29 void host_add_key(uint8_t key)
31 #ifdef USB_NKRO_ENABLE
40 void host_add_mod_bit(uint8_t mod)
42 keyboard_report->mods |= mod;
45 void host_set_mods(uint8_t mods)
47 keyboard_report->mods = mods;
50 void host_add_code(uint8_t code)
53 host_add_mod_bit(MOD_BIT(code));
59 void host_swap_keyboard_report(void)
61 report_keyboard_t *tmp = keyboard_report_prev;
62 keyboard_report_prev = keyboard_report;
63 keyboard_report = tmp;
66 void host_clear_keyboard_report(void)
68 keyboard_report->mods = 0;
69 for (int8_t i = 0; i < REPORT_KEYS; i++) {
70 keyboard_report->keys[i] = 0;
74 uint8_t host_has_anykey(void)
77 for (int i = 0; i < REPORT_KEYS; i++) {
78 if (keyboard_report->keys[i])
84 uint8_t host_get_first_key(void)
86 #ifdef USB_NKRO_ENABLE
89 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
91 return i<<3 | biton(keyboard_report->keys[i]);
94 return keyboard_report->keys[0];
98 void host_send_keyboard_report(void)
100 usb_keyboard_send_report(keyboard_report);
103 void host_mouse_send(report_mouse_t *report)
105 usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
109 static inline void add_key_byte(uint8_t code)
111 // TODO: fix ugly code
114 for (; i < REPORT_KEYS; i++) {
115 if (keyboard_report_prev->keys[i] == code) {
116 keyboard_report->keys[i] = code;
120 keyboard_report_prev->keys[i] == 0 &&
121 keyboard_report->keys[i] == 0) {
125 if (i == REPORT_KEYS) {
127 keyboard_report->keys[empty] = code;
132 static inline void add_key_bit(uint8_t code)
134 if ((code>>3) < REPORT_KEYS) {
135 keyboard_report->keys[code>>3] |= 1<<(code&7);
137 debug("add_key_bit: can't add: "); phex(code); debug("\n");