]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - usb_keyboard.c
add build option: NKRO_ENABLE(remove: USB_12KRO)
[max/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 #include "util.h"
8
9
10 // keyboard report.
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;
15
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;
20
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;
24
25 // count until idle timeout
26 uint8_t usb_keyboard_idle_count=0;
27
28 // 1=num lock, 2=caps lock, 4=scroll lock, 8=compose, 16=kana
29 volatile uint8_t usb_keyboard_leds=0;
30
31 // enable NKRO
32 bool usb_keyboard_nkro = false;
33
34
35 int8_t usb_keyboard_send(void)
36 {
37     return usb_keyboard_send_report(usb_keyboard_report);
38 }
39
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)
42 {
43     int8_t result = 0;
44
45 #ifdef NKRO_ENABLE
46     if (usb_keyboard_nkro)
47         result = _send_report(report, KBD2_ENDPOINT, 0, KBD2_REPORT_KEYS);
48     else
49 #endif
50     {
51         if (usb_keyboard_protocol)
52             result = _send_report(report, KBD_ENDPOINT, 0, KBD_REPORT_KEYS);
53         else
54             result = _send_report(report, KBD_ENDPOINT, 0, 6);
55     }
56
57     if (result) return result;
58     usb_keyboard_idle_count = 0;
59     report->is_sent =true;
60     usb_keyboard_print_report(report);
61     return 0;
62 }
63
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;
68 }
69
70 void usb_keyboard_clear_report(void) {
71     usb_keyboard_clear_keys();
72     usb_keyboard_clear_mods();
73     usb_keyboard_report->is_sent = false;
74 }
75
76 void usb_keyboard_clear_keys(void) {
77     for (int i = 0; i < KEYS_MAX; i++) usb_keyboard_report->keys[i] = 0;
78 }
79
80 void usb_keyboard_clear_mods(void)
81 {
82     usb_keyboard_report->mods = 0;
83 }
84
85 void usb_keyboard_set_keys(uint8_t *keys)
86 {
87     for (int i = 0; i < KEYS_MAX; i++)
88         usb_keyboard_report->keys[i] = keys[i];
89 }
90
91 void usb_keyboard_set_mods(uint8_t mods)
92 {
93     usb_keyboard_report->mods = mods;
94 }
95
96 void usb_keyboard_add_code(uint8_t code)
97 {
98     if (IS_MOD(code)) {
99         usb_keyboard_add_mod(code);
100     } else {
101         usb_keyboard_add_key(code);
102     }
103 }
104
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)
108 {
109 #ifdef NKRO_ENABLE
110     if (usb_keyboard_nkro) {
111         _add_key_bit(code);
112         return;
113     }
114 #endif
115     _add_key_byte(code);
116 }
117
118 void usb_keyboard_add_mod(uint8_t code)
119 {
120     usb_keyboard_report->mods |= MOD_BIT(code);
121 }
122
123 void usb_keyboard_del_code(uint8_t code)
124 {
125     if (IS_MOD(code)) {
126         usb_keyboard_del_mod(code);
127     } else {
128         usb_keyboard_del_key(code);
129     }
130 }
131
132 void usb_keyboard_del_key(uint8_t code)
133 {
134 #ifdef NKRO_ENABLE
135     if ((code>>3) < KEYS_MAX) {
136         usb_keyboard_keys[code>>3] &= ~(1<<(code&7));
137     }
138 #else
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;
142             return;
143         }
144     }
145 #endif
146 }
147
148 void usb_keyboard_del_mod(uint8_t code)
149 {
150     usb_keyboard_report->mods &= ~MOD_BIT(code);
151 }
152
153 bool usb_keyboard_is_sent(void)
154 {
155     return usb_keyboard_report->is_sent;
156 }
157
158 bool usb_keyboard_has_key(void)
159 {
160     uint8_t keys = 0;    
161     for (int i = 0; i < KEYS_MAX; i++) keys |= usb_keyboard_report->keys[i];
162     return keys ? true : false;
163 }
164
165 bool usb_keyboard_has_mod(void)
166 {
167     return usb_keyboard_report->mods ? true : false;
168 }
169
170 uint8_t usb_keyboard_get_key(void)
171 {
172 #ifdef NKRO_ENABLE
173     if (usb_keyboard_nkro) {
174         uint8_t i = 0;
175         for (; i < KEYS_MAX && !usb_keyboard_keys[i]; i++);
176         return i<<3 | biton(usb_keyboard_keys[i]);
177     }
178 #endif
179     return usb_keyboard_keys[0];
180 }
181
182 void usb_keyboard_print_report(usb_keyboard_report_t *report)
183 {
184     if (!debug_keyboard) return;
185     print("keys: ");
186     for (int i = 0; i < KEYS_MAX; i++) { phex(report->keys[i]); print(" "); }
187     print(" mods: "); phex(report->mods); print("\n");
188 }
189
190
191 static inline int8_t _send_report(usb_keyboard_report_t *report, uint8_t endpoint, uint8_t keys_start, uint8_t keys_end)
192 {
193     uint8_t intr_state, timeout;
194
195     if (!usb_configured()) return -1;
196     intr_state = SREG;
197     cli();
198     UENUM = endpoint;
199     timeout = UDFNUML + 50;
200     while (1) {
201             // are we ready to transmit?
202             if (UEINTX & (1<<RWAL)) break;
203             SREG = intr_state;
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
209             intr_state = SREG;
210             cli();
211             UENUM = endpoint;
212     }
213     UEDATX = report->mods;
214     UEDATX = 0;
215     for (uint8_t i = keys_start; i < keys_end; i++) {
216             UEDATX = report->keys[i];
217     }
218     UEINTX = 0x3A;
219     SREG = intr_state;
220     return 0;
221 }
222
223 static inline void _add_key_byte(uint8_t code)
224 {
225     // TODO: fix ugly code
226     int8_t i = 0;
227     int8_t empty = -1;
228     for (; i < KEYS_MAX; i++) {
229         if (usb_keyboard_keys_prev[i] == code) {
230             usb_keyboard_keys[i] = code;
231             break;
232         }
233         if (empty == -1 &&
234                 usb_keyboard_keys_prev[i] == 0 &&
235                 usb_keyboard_keys[i] == 0) {
236             empty = i;
237         }
238     }
239     if (i == KEYS_MAX) {
240         if (empty != -1) {
241             usb_keyboard_keys[empty] = code;
242         }
243     }
244 }
245
246 static inline void _add_key_bit(uint8_t code)
247 {
248     if ((code>>3) < KEYS_MAX) {
249         usb_keyboard_keys[code>>3] |= 1<<(code&7);
250     }
251 }
252