1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
11 static usb_keyboard_report_t _report0 = { {0}, 0, false };
12 static usb_keyboard_report_t _report1 = { {0}, 0, false };
13 usb_keyboard_report_t *usb_keyboard_report = &_report0;
14 usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
16 // protocol setting from the host. We use exactly the same report
17 // either way, so this variable only stores the setting since we
18 // are required to be able to report which setting is in use.
19 uint8_t usb_keyboard_protocol=1;
21 // the idle configuration, how often we send the report to the
22 // host (ms * 4) even when it hasn't changed
23 uint8_t usb_keyboard_idle_config=125;
25 // count until idle timeout
26 uint8_t usb_keyboard_idle_count=0;
28 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
29 volatile uint8_t usb_keyboard_leds=0;
32 bool usb_keyboard_nkro = false;
35 int8_t usb_keyboard_send(void)
37 return usb_keyboard_send_report(usb_keyboard_report);
40 static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end);
41 int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
45 #ifdef USB_NKRO_ENABLE
46 if (usb_keyboard_nkro)
47 result = _send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
51 if (usb_keyboard_protocol)
52 result = _send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
54 result = _send_report(report, KBD_ENDPOINT, 0, 6);
57 if (result) return result;
58 usb_keyboard_idle_count = 0;
59 report->is_sent =true;
60 usb_keyboard_print_report(report);
64 void usb_keyboard_swap_report(void) {
65 usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
66 usb_keyboard_report_prev = usb_keyboard_report;
67 usb_keyboard_report = tmp;
70 void usb_keyboard_clear_report(void) {
71 usb_keyboard_clear_keys();
72 usb_keyboard_clear_mods();
73 usb_keyboard_report->is_sent = false;
76 void usb_keyboard_clear_keys(void) {
77 for (int i = 0; i < KEYS_MAX; i++) usb_keyboard_report->keys[i] = 0;
80 void usb_keyboard_clear_mods(void)
82 usb_keyboard_report->mods = 0;
85 void usb_keyboard_set_keys(uint8_t *keys)
87 for (int i = 0; i < KEYS_MAX; i++)
88 usb_keyboard_report->keys[i] = keys[i];
91 void usb_keyboard_set_mods(uint8_t mods)
93 usb_keyboard_report->mods = mods;
96 void usb_keyboard_add_code(uint8_t code)
99 usb_keyboard_add_mod(code);
101 usb_keyboard_add_key(code);
105 static inline void _add_key_byte(uint8_t code);
106 static inline void _add_key_bit(uint8_t code);
107 void usb_keyboard_add_key(uint8_t code)
109 #ifdef USB_NKRO_ENABLE
110 if (usb_keyboard_nkro) {
118 void usb_keyboard_add_mod(uint8_t code)
120 usb_keyboard_report->mods |= MOD_BIT(code);
123 void usb_keyboard_del_code(uint8_t code)
126 usb_keyboard_del_mod(code);
128 usb_keyboard_del_key(code);
132 void usb_keyboard_del_key(uint8_t code)
134 #ifdef USB_NKRO_ENABLE
135 if ((code>>3) < KEYS_MAX) {
136 usb_keyboard_keys[code>>3] &= ~(1<<(code&7));
139 for (int i = 0; i < KEYS_MAX; i++) {
140 if (usb_keyboard_report->keys[i] == code) {
141 usb_keyboard_report->keys[i] = KB_NO;
148 void usb_keyboard_del_mod(uint8_t code)
150 usb_keyboard_report->mods &= ~MOD_BIT(code);
153 bool usb_keyboard_is_sent(void)
155 return usb_keyboard_report->is_sent;
158 bool usb_keyboard_has_key(void)
161 for (int i = 0; i < KEYS_MAX; i++) keys |= usb_keyboard_report->keys[i];
162 return keys ? true : false;
165 bool usb_keyboard_has_mod(void)
167 return usb_keyboard_report->mods ? true : false;
170 uint8_t usb_keyboard_get_key(void)
172 #ifdef USB_NKRO_ENABLE
173 if (usb_keyboard_nkro) {
175 for (; i < KEYS_MAX && !usb_keyboard_keys[i]; i++);
176 return i<<3 | biton(usb_keyboard_keys[i]);
179 return usb_keyboard_keys[0];
182 void usb_keyboard_print_report(usb_keyboard_report_t *report)
184 if (!debug_keyboard) return;
186 for (int i = 0; i < KEYS_MAX; i++) { phex(report->keys[i]); print(" "); }
187 print(" mods: "); phex(report->mods); print("\n");
191 static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
193 uint8_t intr_state, timeout;
195 if (!usb_configured()) return -1;
199 timeout = UDFNUML + 50;
201 // are we ready to transmit?
202 if (UEINTX & (1<<RWAL)) break;
204 // has the USB gone offline?
205 if (!usb_configured()) return -1;
206 // have we waited too long?
207 if (UDFNUML == timeout) return -1;
208 // get ready to try checking again
213 UEDATX = report->mods;
214 if (!usb_keyboard_nkro)
216 for (uint8_t i = keys_start; i < keys_end; i++) {
217 UEDATX = report->keys[i];
224 static inline void _add_key_byte(uint8_t code)
226 // TODO: fix ugly code
229 for (; i < KEYS_MAX; i++) {
230 if (usb_keyboard_keys_prev[i] == code) {
231 usb_keyboard_keys[i] = code;
235 usb_keyboard_keys_prev[i] == 0 &&
236 usb_keyboard_keys[i] == 0) {
242 usb_keyboard_keys[empty] = code;
247 static inline void _add_key_bit(uint8_t code)
249 if ((code>>3) < KEYS_MAX) {
250 usb_keyboard_keys[code>>3] |= 1<<(code&7);