1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
10 static usb_keyboard_report_t _report0 = { {0}, 0 };
11 static usb_keyboard_report_t _report1 = { {0}, 0 };
12 usb_keyboard_report_t *usb_keyboard_report = &_report0;
13 usb_keyboard_report_t *usb_keyboard_report_prev = &_report1;
15 // protocol setting from the host. We use exactly the same report
16 // either way, so this variable only stores the setting since we
17 // are required to be able to report which setting is in use.
18 uint8_t usb_keyboard_protocol=1;
20 // the idle configuration, how often we send the report to the
21 // host (ms * 4) even when it hasn't changed
22 uint8_t usb_keyboard_idle_config=125;
24 // count until idle timeout
25 uint8_t usb_keyboard_idle_count=0;
27 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
28 volatile uint8_t usb_keyboard_leds=0;
31 int8_t usb_keyboard_send(void)
33 return usb_keyboard_send_report(usb_keyboard_report);
37 int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
39 uint8_t i, intr_state, timeout;
41 if (!usb_configured()) return -1;
44 UENUM = KEYBOARD_ENDPOINT;
45 timeout = UDFNUML + 50;
47 // are we ready to transmit?
48 if (UEINTX & (1<<RWAL)) break;
50 // has the USB gone offline?
51 if (!usb_configured()) return -1;
52 // have we waited too long?
53 if (UDFNUML == timeout) return -1;
54 // get ready to try checking again
57 UENUM = KEYBOARD_ENDPOINT;
59 UEDATX = report->mods;
61 for (i = 0; i < 6; i++) {
62 UEDATX = report->keys[i];
68 if (!usb_configured()) return -1;
71 UENUM = KEYBOARD_ENDPOINT2;
72 timeout = UDFNUML + 50;
74 // are we ready to transmit?
75 if (UEINTX & (1<<RWAL)) break;
77 // has the USB gone offline?
78 if (!usb_configured()) return -1;
79 // have we waited too long?
80 if (UDFNUML == timeout) return -1;
81 // get ready to try checking again
84 UENUM = KEYBOARD_ENDPOINT2;
86 UEDATX = report->mods;
88 for (i = 6; i < 12; i++) {
89 UEDATX = report->keys[i];
95 usb_keyboard_idle_count = 0;
96 report->is_sent =true;
97 usb_keyboard_print_report(report);
101 void usb_keyboard_swap_report(void) {
102 usb_keyboard_report_t *tmp = usb_keyboard_report_prev;
103 usb_keyboard_report_prev = usb_keyboard_report;
104 usb_keyboard_report = tmp;
107 void usb_keyboard_clear_report(void) {
108 usb_keyboard_clear_keys();
109 usb_keyboard_clear_mods();
110 usb_keyboard_report->is_sent = false;
113 void usb_keyboard_clear_keys(void) {
114 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) usb_keyboard_report->keys[i] = 0;
117 void usb_keyboard_clear_mods(void)
119 usb_keyboard_report->mods = 0;
122 void usb_keyboard_add_code(uint8_t code)
125 usb_keyboard_add_mod(code);
127 usb_keyboard_add_key(code);
131 void usb_keyboard_add_key(uint8_t code)
133 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
134 if (!usb_keyboard_report->keys[i]) {
135 usb_keyboard_report->keys[i] = code;
141 void usb_keyboard_set_keys(uint8_t *keys)
143 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++)
144 usb_keyboard_report->keys[i] = keys[i];
147 void usb_keyboard_set_mods(uint8_t mods)
149 usb_keyboard_report->mods = mods;
152 void usb_keyboard_add_mod(uint8_t code)
154 usb_keyboard_report->mods |= MOD_BIT(code);
157 void usb_keyboard_del_code(uint8_t code)
160 usb_keyboard_del_mod(code);
162 usb_keyboard_del_key(code);
166 void usb_keyboard_del_key(uint8_t code)
168 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
169 if (usb_keyboard_report->keys[i] == code) {
170 usb_keyboard_report->keys[i] = KB_NO;
176 void usb_keyboard_del_mod(uint8_t code)
178 usb_keyboard_report->mods &= ~MOD_BIT(code);
181 bool usb_keyboard_is_sent(void)
183 return usb_keyboard_report->is_sent;
186 bool usb_keyboard_has_key(void)
189 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) keys |= usb_keyboard_report->keys[i];
190 return keys ? true : false;
193 bool usb_keyboard_has_mod(void)
195 return usb_keyboard_report->mods ? true : false;
198 void usb_keyboard_print_report(usb_keyboard_report_t *report)
200 if (!debug_keyboard) return;
202 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) { phex(report->keys[i]); print(" "); }
203 print(" mods: "); phex(report->mods); print("\n");