]> git.gir.st - tmk_keyboard.git/blob - key_process.c
add build option: NKRO_ENABLE(remove: USB_12KRO)
[tmk_keyboard.git] / key_process.c
1 #include <stdbool.h>
2 #include <avr/io.h>
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
5 #include "print.h"
6 #include "debug.h"
7 #include "timer.h"
8 #include "util.h"
9 #include "jump_bootloader.h"
10 #include "usb_keyboard.h"
11 #include "usb_mouse.h"
12 #include "usb_extra.h"
13 #include "usb_keycodes.h"
14 #include "usb.h"
15 #include "layer.h"
16 #include "matrix_skel.h"
17 #include "keymap_skel.h"
18 #include "controller.h"
19 #include "key_process.h"
20
21
22 #define MOUSE_MOVE_UNIT 10
23 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
24
25 #ifndef MOUSE_DELAY_TIME
26 # define MOUSE_DELAY_TIME 255
27 #endif
28 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
29
30
31 // TODO: refactoring
32 void proc_matrix(void) {
33 static int mouse_repeat = 0;
34
35 bool modified = false;
36 uint8_t mouse_btn = 0;
37 int8_t mouse_x = 0;
38 int8_t mouse_y = 0;
39 int8_t mouse_vwheel = 0;
40 int8_t mouse_hwheel = 0;
41 uint8_t fn_bits = 0;
42
43 matrix_scan();
44 modified = matrix_is_modified();
45
46 if (modified) {
47 if (debug_matrix) matrix_print();
48 #ifdef DEBUG_LED
49 // LED flash for debug
50 DEBUG_LED_CONFIG;
51 DEBUG_LED_ON;
52 #endif
53 }
54
55 if (matrix_has_ghost()) {
56 // should send error?
57 debug("matrix has ghost!!\n");
58 return;
59 }
60
61 usb_keyboard_swap_report();
62 usb_keyboard_clear_report();
63 for (int row = 0; row < matrix_rows(); row++) {
64 for (int col = 0; col < matrix_cols(); col++) {
65 if (!matrix_is_on(row, col)) continue;
66
67 // TODO: clean code
68 uint8_t code = layer_get_keycode(row, col);
69 if (code == KB_NO) {
70 // do nothing
71 } else if (IS_MOD(code)) {
72 usb_keyboard_add_mod(code);
73 } else if (IS_FN(code)) {
74 fn_bits |= FN_BIT(code);
75 } else if (IS_MOUSE(code)) {
76 if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
77 if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
78 if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
79 if (code == MS_RGHT) mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
80 if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
81 if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
82 if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
83 if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
84 if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
85 if (code == MS_WH_U) mouse_vwheel += 1;
86 if (code == MS_WH_D) mouse_vwheel -= 1;
87 if (code == MS_WH_L) mouse_hwheel -= 1;
88 if (code == MS_WH_R) mouse_hwheel += 1;
89 }
90
91 // audio control & system control
92 else if (code == KB_MUTE) {
93 usb_extra_audio_send(AUDIO_MUTE);
94 usb_extra_audio_send(0);
95 _delay_ms(500);
96 } else if (code == KB_VOLU) {
97 usb_extra_audio_send(AUDIO_VOL_UP);
98 usb_extra_audio_send(0);
99 _delay_ms(100);
100 } else if (code == KB_VOLD) {
101 usb_extra_audio_send(AUDIO_VOL_DOWN);
102 usb_extra_audio_send(0);
103 _delay_ms(100);
104 } else if (code == KB_PWR) {
105 if (suspend && remote_wakeup) {
106 usb_remote_wakeup();
107 } else {
108 usb_extra_system_send(SYSTEM_POWER_DOWN);
109 }
110 _delay_ms(1000);
111 }
112
113 // normal keys
114 else {
115 usb_keyboard_add_key(code);
116 }
117 }
118 }
119
120 if (modified) {
121 #ifdef DEBUG_LED
122 // LED flash for debug
123 DEBUG_LED_CONFIG;
124 DEBUG_LED_OFF;
125 #endif
126 }
127
128 layer_switching(fn_bits);
129
130 // TODO: clean code
131 // special mode for control, develop and debug
132 if (keymap_is_special_mode(fn_bits)) {
133 switch (usb_keyboard_get_key()) {
134 case KB_H: // help
135 print_enable = true;
136 print("b: jump to bootloader\n");
137 print("d: toggle debug enable\n");
138 print("x: toggle matrix debug\n");
139 print("k: toggle keyboard debug\n");
140 print("m: toggle mouse debug\n");
141 print("p: toggle print enable\n");
142 print("v: print version\n");
143 print("t: print timer count\n");
144 print("s: print status\n");
145 print("`: toggle protcol(boot/report)\n");
146 #ifdef NKRO_ENABLE
147 print("n: toggle NKRO\n");
148 #endif
149 print("ESC: power down/wake up\n");
150 _delay_ms(500);
151 print_enable = false;
152 break;
153 case KB_B: // bootloader
154 usb_keyboard_clear_report();
155 usb_keyboard_send();
156 print_enable = true;
157 print("jump to bootloader...\n");
158 _delay_ms(1000);
159 jump_bootloader(); // not return
160 break;
161 case KB_D: // debug all toggle
162 usb_keyboard_clear_report();
163 usb_keyboard_send();
164 debug_enable = !debug_enable;
165 if (debug_enable) {
166 print_enable = true;
167 print("debug enabled.\n");
168 debug_matrix = true;
169 debug_keyboard = true;
170 debug_mouse = true;
171 } else {
172 print("debug disabled.\n");
173 print_enable = false;
174 debug_matrix = false;
175 debug_keyboard = false;
176 debug_mouse = false;
177 }
178 _delay_ms(1000);
179 break;
180 case KB_X: // debug matrix toggle
181 usb_keyboard_clear_report();
182 usb_keyboard_send();
183 debug_matrix = !debug_matrix;
184 if (debug_matrix)
185 print("debug matrix enabled.\n");
186 else
187 print("debug matrix disabled.\n");
188 _delay_ms(1000);
189 break;
190 case KB_K: // debug keyboard toggle
191 usb_keyboard_clear_report();
192 usb_keyboard_send();
193 debug_keyboard = !debug_keyboard;
194 if (debug_keyboard)
195 print("debug keyboard enabled.\n");
196 else
197 print("debug keyboard disabled.\n");
198 _delay_ms(1000);
199 break;
200 case KB_M: // debug mouse toggle
201 usb_keyboard_clear_report();
202 usb_keyboard_send();
203 debug_mouse = !debug_mouse;
204 if (debug_mouse)
205 print("debug mouse enabled.\n");
206 else
207 print("debug mouse disabled.\n");
208 _delay_ms(1000);
209 break;
210 case KB_V: // print version & information
211 usb_keyboard_clear_report();
212 usb_keyboard_send();
213 print_enable = true;
214 print(STR(DESCRIPTION) "\n");
215 _delay_ms(1000);
216 break;
217 case KB_T: // print timer
218 usb_keyboard_clear_report();
219 usb_keyboard_send();
220 print_enable = true;
221 print("timer: "); phex16(timer_count); print("\n");
222 _delay_ms(500);
223 break;
224 case KB_P: // print toggle
225 usb_keyboard_clear_report();
226 usb_keyboard_send();
227 if (print_enable) {
228 print("print disabled.\n");
229 print_enable = false;
230 } else {
231 print_enable = true;
232 print("print enabled.\n");
233 }
234 _delay_ms(1000);
235 break;
236 case KB_S:
237 usb_keyboard_clear_report();
238 usb_keyboard_send();
239 print("UDCON: "); phex(UDCON); print("\n");
240 print("UDIEN: "); phex(UDIEN); print("\n");
241 print("UDINT: "); phex(UDINT); print("\n");
242 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
243 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
244 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
245 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
246 print("mouse_protocol:"); phex(mouse_protocol); print("\n");
247 if (usb_keyboard_nkro) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
248 _delay_ms(500);
249 break;
250 case KB_GRV:
251 usb_keyboard_clear_report();
252 usb_keyboard_send();
253 usb_keyboard_protocol = !usb_keyboard_protocol;
254 mouse_protocol = !mouse_protocol;
255 print("keyboard protcol: ");
256 if (usb_keyboard_protocol) print("report"); else print("boot");
257 print("\n");
258 print("mouse protcol: ");
259 if (mouse_protocol) print("report"); else print("boot");
260 print("\n");
261 _delay_ms(1000);
262 break;
263 #ifdef NKRO_ENABLE
264 case KB_N:
265 usb_keyboard_clear_report();
266 usb_keyboard_send();
267 usb_keyboard_nkro = !usb_keyboard_nkro;
268 if (usb_keyboard_nkro) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
269 _delay_ms(1000);
270 break;
271 #endif
272 case KB_ESC:
273 usb_keyboard_clear_report();
274 usb_keyboard_send();
275 if (suspend && remote_wakeup) {
276 usb_remote_wakeup();
277 } else {
278 usb_extra_system_send(SYSTEM_POWER_DOWN);
279 }
280 _delay_ms(1000);
281 break;
282 }
283 }
284
285
286 // send mouse packet to host
287 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
288 mouse_buttons = mouse_btn;
289 if (mouse_x && mouse_y)
290 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
291 else
292 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
293 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
294
295 // acceleration
296 _delay_ms(MOUSE_DELAY_MS);
297 mouse_repeat++;
298 } else {
299 mouse_repeat = 0;
300 }
301
302
303 // send key packet to host
304 if (modified) {
305 usb_keyboard_send();
306 }
307 }
Imprint / Impressum