]> git.gir.st - tmk_keyboard.git/blob - usb_keyboard.c
add build option USB_12KRO.
[tmk_keyboard.git] / usb_keyboard.c
1 #include <avr/interrupt.h>
2 #include <avr/pgmspace.h>
3 #include "usb_keycodes.h"
4 #include "usb_keyboard.h"
5 #include "print.h"
6 #include "debug.h"
7
8
9 // keyboard report.
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;
14
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;
19
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;
23
24 // count until idle timeout
25 uint8_t usb_keyboard_idle_count=0;
26
27 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
28 volatile uint8_t usb_keyboard_leds=0;
29
30
31 int8_t usb_keyboard_send(void)
32 {
33 return usb_keyboard_send_report(usb_keyboard_report);
34 }
35
36
37 int8_t usb_keyboard_send_report(usb_keyboard_report_t *report)
38 {
39 uint8_t i, intr_state, timeout;
40
41 if (!usb_configured()) return -1;
42 intr_state = SREG;
43 cli();
44 UENUM = KEYBOARD_ENDPOINT;
45 timeout = UDFNUML + 50;
46 while (1) {
47 // are we ready to transmit?
48 if (UEINTX & (1<<RWAL)) break;
49 SREG = intr_state;
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
55 intr_state = SREG;
56 cli();
57 UENUM = KEYBOARD_ENDPOINT;
58 }
59 UEDATX = report->mods;
60 UEDATX = 0;
61 for (i = 0; i < 6; i++) {
62 UEDATX = report->keys[i];
63 }
64 UEINTX = 0x3A;
65 SREG = intr_state;
66
67 #ifdef USB_12KRO
68 if (!usb_configured()) return -1;
69 intr_state = SREG;
70 cli();
71 UENUM = KEYBOARD_ENDPOINT2;
72 timeout = UDFNUML + 50;
73 while (1) {
74 // are we ready to transmit?
75 if (UEINTX & (1<<RWAL)) break;
76 SREG = intr_state;
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
82 intr_state = SREG;
83 cli();
84 UENUM = KEYBOARD_ENDPOINT2;
85 }
86 UEDATX = report->mods;
87 UEDATX = 0;
88 for (i = 6; i < 12; i++) {
89 UEDATX = report->keys[i];
90 }
91 UEINTX = 0x3A;
92 SREG = intr_state;
93 #endif
94
95 usb_keyboard_idle_count = 0;
96 report->is_sent =true;
97 usb_keyboard_print_report(report);
98 return 0;
99 }
100
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;
105 }
106
107 void usb_keyboard_clear_report(void) {
108 usb_keyboard_clear_keys();
109 usb_keyboard_clear_mods();
110 usb_keyboard_report->is_sent = false;
111 }
112
113 void usb_keyboard_clear_keys(void) {
114 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) usb_keyboard_report->keys[i] = 0;
115 }
116
117 void usb_keyboard_clear_mods(void)
118 {
119 usb_keyboard_report->mods = 0;
120 }
121
122 void usb_keyboard_add_code(uint8_t code)
123 {
124 if (IS_MOD(code)) {
125 usb_keyboard_add_mod(code);
126 } else {
127 usb_keyboard_add_key(code);
128 }
129 }
130
131 void usb_keyboard_add_key(uint8_t code)
132 {
133 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) {
134 if (!usb_keyboard_report->keys[i]) {
135 usb_keyboard_report->keys[i] = code;
136 return;
137 }
138 }
139 }
140
141 void usb_keyboard_set_keys(uint8_t *keys)
142 {
143 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++)
144 usb_keyboard_report->keys[i] = keys[i];
145 }
146
147 void usb_keyboard_set_mods(uint8_t mods)
148 {
149 usb_keyboard_report->mods = mods;
150 }
151
152 void usb_keyboard_add_mod(uint8_t code)
153 {
154 usb_keyboard_report->mods |= MOD_BIT(code);
155 }
156
157 void usb_keyboard_del_code(uint8_t code)
158 {
159 if (IS_MOD(code)) {
160 usb_keyboard_del_mod(code);
161 } else {
162 usb_keyboard_del_key(code);
163 }
164 }
165
166 void usb_keyboard_del_key(uint8_t code)
167 {
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;
171 return;
172 }
173 }
174 }
175
176 void usb_keyboard_del_mod(uint8_t code)
177 {
178 usb_keyboard_report->mods &= ~MOD_BIT(code);
179 }
180
181 bool usb_keyboard_is_sent(void)
182 {
183 return usb_keyboard_report->is_sent;
184 }
185
186 bool usb_keyboard_has_key(void)
187 {
188 uint8_t keys = 0;
189 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) keys |= usb_keyboard_report->keys[i];
190 return keys ? true : false;
191 }
192
193 bool usb_keyboard_has_mod(void)
194 {
195 return usb_keyboard_report->mods ? true : false;
196 }
197
198 void usb_keyboard_print_report(usb_keyboard_report_t *report)
199 {
200 if (!debug_keyboard) return;
201 print("keys: ");
202 for (int i = 0; i < KEYBOARD_REPORT_MAX; i++) { phex(report->keys[i]); print(" "); }
203 print(" mods: "); phex(report->mods); print("\n");
204 }
Imprint / Impressum