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,0,0,0,0}, 0 };
11 static usb_keyboard_report_t _report1 = { {0,0,0,0,0,0}, 0 };
12 usb_keyboard_report_t *usb_keyboard_report = &_report0;
13 usb_keyboard_report_t *usb_keyboard_report_back = &_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;
62 UEDATX = report->keys[i];
65 usb_keyboard_idle_count = 0;
68 report->is_sent =true;
70 usb_keyboard_print_report(report);
74 void usb_keyboard_swap_report(void) {
75 usb_keyboard_report_t *tmp = usb_keyboard_report_back;
76 usb_keyboard_report_back = usb_keyboard_report;
77 usb_keyboard_report = tmp;
80 void usb_keyboard_clear_report(void) {
81 usb_keyboard_clear_keys();
82 usb_keyboard_clear_mods();
83 usb_keyboard_report->is_sent = false;
86 void usb_keyboard_clear_keys(void) {
87 for (int i = 0; i < 6; i++) usb_keyboard_report->keys[i] = 0;
90 void usb_keyboard_clear_mods(void)
92 usb_keyboard_report->mods = 0;
95 void usb_keyboard_add_code(uint8_t code)
98 usb_keyboard_add_mod(code);
100 usb_keyboard_add_key(code);
104 void usb_keyboard_add_key(uint8_t code)
106 for (int i = 0; i < 6; i++) {
107 if (!usb_keyboard_report->keys[i]) {
108 usb_keyboard_report->keys[i] = code;
114 void usb_keyboard_set_keys(uint8_t keys[6])
116 for (int i = 0; i < 6; i++)
117 usb_keyboard_report->keys[i] = keys[i];
120 void usb_keyboard_set_mods(uint8_t mods)
122 usb_keyboard_report->mods = mods;
125 void usb_keyboard_add_mod(uint8_t code)
127 usb_keyboard_report->mods |= MOD_BIT(code);
130 void usb_keyboard_del_code(uint8_t code)
133 usb_keyboard_del_mod(code);
135 usb_keyboard_del_key(code);
139 void usb_keyboard_del_key(uint8_t code)
141 for (int i = 0; i < 6; i++) {
142 if (usb_keyboard_report->keys[i] == code) {
143 usb_keyboard_report->keys[i] = KB_NO;
149 void usb_keyboard_del_mod(uint8_t code)
151 usb_keyboard_report->mods &= ~MOD_BIT(code);
154 bool usb_keyboard_is_sent(void)
156 return usb_keyboard_report->is_sent;
159 bool usb_keyboard_has_key(void)
162 for (int i = 0; i < 6; i++) keys |= usb_keyboard_report->keys[i];
163 return keys ? true : false;
166 bool usb_keyboard_has_mod(void)
168 return usb_keyboard_report->mods ? true : false;
171 void usb_keyboard_print_report(usb_keyboard_report_t *report)
173 if (!debug_keyboard) return;
175 for (int i = 0; i < 6; i++) { phex(report->keys[i]); print(" "); }
176 print(" mods: "); phex(report->mods); print("\n");