2 #include <avr/interrupt.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
11 #ifdef USB_NKRO_ENABLE
12 bool keyboard_nkro = false;
15 static report_keyboard_t report0;
16 static report_keyboard_t report1;
17 report_keyboard_t *keyboard_report = &report0;
18 report_keyboard_t *keyboard_report_prev = &report1;
20 static inline void add_key_byte(uint8_t code);
21 static inline void add_key_bit(uint8_t code);
24 uint8_t host_keyboard_leds(void)
26 return usb_keyboard_leds;
29 /* keyboard report operations */
30 void host_add_key(uint8_t key)
32 #ifdef USB_NKRO_ENABLE
41 void host_add_mod_bit(uint8_t mod)
43 keyboard_report->mods |= mod;
46 void host_set_mods(uint8_t mods)
48 keyboard_report->mods = mods;
51 void host_add_code(uint8_t code)
54 host_add_mod_bit(MOD_BIT(code));
60 void host_swap_keyboard_report(void)
64 report_keyboard_t *tmp = keyboard_report_prev;
65 keyboard_report_prev = keyboard_report;
66 keyboard_report = tmp;
70 void host_clear_keyboard_report(void)
72 keyboard_report->mods = 0;
73 for (int8_t i = 0; i < REPORT_KEYS; i++) {
74 keyboard_report->keys[i] = 0;
78 uint8_t host_has_anykey(void)
81 for (int i = 0; i < REPORT_KEYS; i++) {
82 if (keyboard_report->keys[i])
88 uint8_t host_get_first_key(void)
90 #ifdef USB_NKRO_ENABLE
93 for (; i < REPORT_KEYS && !keyboard_report->keys[i]; i++)
95 return i<<3 | biton(keyboard_report->keys[i]);
98 return keyboard_report->keys[0];
102 void host_send_keyboard_report(void)
104 usb_keyboard_send_report(keyboard_report);
107 void host_mouse_send(report_mouse_t *report)
109 usb_mouse_send(report->x, report->y, report->v, report->h, report->buttons);
113 static inline void add_key_byte(uint8_t code)
115 // TODO: fix ugly code
118 for (; i < REPORT_KEYS; i++) {
119 if (keyboard_report_prev->keys[i] == code) {
120 keyboard_report->keys[i] = code;
124 keyboard_report_prev->keys[i] == 0 &&
125 keyboard_report->keys[i] == 0) {
129 if (i == REPORT_KEYS) {
131 keyboard_report->keys[empty] = code;
136 static inline void add_key_bit(uint8_t code)
138 if ((code>>3) < REPORT_KEYS) {
139 keyboard_report->keys[code>>3] |= 1<<(code&7);
141 debug("add_key_bit: can't add: "); phex(code); debug("\n");