]> git.gir.st - tmk_keyboard.git/blob - key_process.c
PS/2 to USB keyboard converter
[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_keycodes.h"
12 #include "usb.h"
13 #include "layer.h"
14 #include "matrix_skel.h"
15 #include "keymap_skel.h"
16 #include "key_process.h"
17 #ifdef MOUSEKEY_ENABLE
18 # include "mousekey.h"
19 #endif
20 #ifdef PS2_MOUSE_ENABLE
21 # include "ps2_mouse.h"
22 #endif
23 #ifdef USB_EXTRA_ENABLE
24 # include "usb_extra.h"
25 #endif
26 #ifdef USB_MOUSE_ENABLE
27 # include "usb_mouse.h"
28 #endif
29
30
31 // TODO: refactoring
32 void proc_matrix(void) {
33 bool modified = false;
34 uint8_t fn_bits = 0;
35
36 matrix_scan();
37 modified = matrix_is_modified();
38
39 if (modified) {
40 if (debug_matrix) matrix_print();
41 #ifdef DEBUG_LED
42 // LED flash for debug
43 DEBUG_LED_CONFIG;
44 DEBUG_LED_ON;
45 #endif
46 }
47
48 if (matrix_has_ghost()) {
49 // should send error?
50 debug("matrix has ghost!!\n");
51 return;
52 }
53
54 usb_keyboard_swap_report();
55 usb_keyboard_clear_report();
56 for (int row = 0; row < matrix_rows(); row++) {
57 for (int col = 0; col < matrix_cols(); col++) {
58 if (!matrix_is_on(row, col)) continue;
59
60 uint8_t code = layer_get_keycode(row, col);
61 if (code == KB_NO) {
62 // do nothing
63 } else if (IS_MOD(code)) {
64 usb_keyboard_add_mod(code);
65 } else if (IS_FN(code)) {
66 fn_bits |= FN_BIT(code);
67 }
68 #ifdef MOUSEKEY_ENABLE
69 else if (IS_MOUSEKEY(code)) {
70 mousekey_decode(code);
71 }
72 #endif
73 #ifdef USB_EXTRA_ENABLE
74 // audio control & system control
75 else if (code == KB_MUTE) {
76 usb_extra_audio_send(AUDIO_MUTE);
77 usb_extra_audio_send(0);
78 _delay_ms(500);
79 } else if (code == KB_VOLU) {
80 usb_extra_audio_send(AUDIO_VOL_UP);
81 usb_extra_audio_send(0);
82 _delay_ms(200);
83 } else if (code == KB_VOLD) {
84 usb_extra_audio_send(AUDIO_VOL_DOWN);
85 usb_extra_audio_send(0);
86 _delay_ms(200);
87 } else if (code == KB_PWR) {
88 if (suspend && remote_wakeup) {
89 usb_remote_wakeup();
90 } else {
91 usb_extra_system_send(SYSTEM_POWER_DOWN);
92 }
93 _delay_ms(1000);
94 }
95 #endif
96 // normal key
97 else if (IS_KEY(code)) {
98 usb_keyboard_add_key(code);
99 } else {
100 debug("ignore keycode: "); debug_hex(code); debug("\n");
101 }
102 }
103 }
104
105 if (modified) {
106 #ifdef DEBUG_LED
107 // LED flash for debug
108 DEBUG_LED_CONFIG;
109 DEBUG_LED_OFF;
110 #endif
111 }
112
113 layer_switching(fn_bits);
114
115 // TODO: clean code
116 // special mode for control, develop and debug
117 if (keymap_is_special_mode(fn_bits)) {
118 switch (usb_keyboard_get_key()) {
119 case KB_H: // help
120 usb_keyboard_clear_report();
121 usb_keyboard_send();
122 print_enable = true;
123 print("b: jump to bootloader\n");
124 print("d: toggle debug enable\n");
125 print("x: toggle matrix debug\n");
126 print("k: toggle keyboard debug\n");
127 print("m: toggle mouse debug\n");
128 print("p: toggle print enable\n");
129 print("v: print version\n");
130 print("t: print timer count\n");
131 print("s: print status\n");
132 print("`: toggle protcol(boot/report)\n");
133 #ifdef USB_NKRO_ENABLE
134 print("n: toggle USB_NKRO\n");
135 #endif
136 print("Backspace: clear matrix\n");
137 print("ESC: power down/wake up\n");
138 print("0: switch to Layer0 \n");
139 print("1: switch to Layer1 \n");
140 print("2: switch to Layer2 \n");
141 print("3: switch to Layer3 \n");
142 print("4: switch to Layer4 \n");
143 #ifdef PS2_MOUSE_ENABLE
144 print("[: ps2_mouse_init \n");
145 print("]: ps2_mouse_read \n");
146 print("\: ps2_mouse: on/off toggle \n");
147 #endif
148 _delay_ms(500);
149 print_enable = false;
150 break;
151 case KB_BSPC:
152 usb_keyboard_clear_report();
153 usb_keyboard_send();
154 matrix_init();
155 print("clear matrix\n");
156 _delay_ms(500);
157 break;
158 case KB_0:
159 usb_keyboard_clear_report();
160 usb_keyboard_send();
161 print("current_layer: "); phex(current_layer); print("\n");
162 print("default_layer: "); phex(default_layer); print("\n");
163 current_layer = 0;
164 default_layer = 0;
165 print("switch to Layer0 \n");
166 _delay_ms(500);
167 break;
168 case KB_1:
169 usb_keyboard_clear_report();
170 usb_keyboard_send();
171 print("current_layer: "); phex(current_layer); print("\n");
172 print("default_layer: "); phex(default_layer); print("\n");
173 current_layer = 1;
174 default_layer = 1;
175 print("switch to Layer1 \n");
176 _delay_ms(500);
177 break;
178 case KB_2:
179 usb_keyboard_clear_report();
180 usb_keyboard_send();
181 print("current_layer: "); phex(current_layer); print("\n");
182 print("default_layer: "); phex(default_layer); print("\n");
183 current_layer = 2;
184 default_layer = 2;
185 print("switch to Layer2 \n");
186 _delay_ms(500);
187 break;
188 case KB_3:
189 usb_keyboard_clear_report();
190 usb_keyboard_send();
191 print("current_layer: "); phex(current_layer); print("\n");
192 print("default_layer: "); phex(default_layer); print("\n");
193 current_layer = 3;
194 default_layer = 3;
195 print("switch to Layer3 \n");
196 _delay_ms(500);
197 break;
198 case KB_4:
199 usb_keyboard_clear_report();
200 usb_keyboard_send();
201 print("current_layer: "); phex(current_layer); print("\n");
202 print("default_layer: "); phex(default_layer); print("\n");
203 current_layer = 4;
204 default_layer = 4;
205 print("switch to Layer4 \n");
206 _delay_ms(500);
207 break;
208 #ifdef PS2_MOUSE_ENABLE
209 case KB_LBRC:
210 usb_keyboard_clear_report();
211 usb_keyboard_send();
212 print_enable = true;
213 print("ps2_mouse_init...\n");
214 _delay_ms(500);
215 ps2_mouse_init();
216 break;
217 case KB_RBRC:
218 usb_keyboard_clear_report();
219 usb_keyboard_send();
220 print_enable = true;
221 print("ps2_mouse_read[btn x y]: ");
222 _delay_ms(100);
223 ps2_mouse_read();
224 phex(ps2_mouse_btn); print(" ");
225 phex(ps2_mouse_x); print(" ");
226 phex(ps2_mouse_y); print("\n");
227 print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
228 break;
229 case KB_BSLS:
230 ps2_mouse_enable = !ps2_mouse_enable;
231 print("ps2_mouse: ");
232 if (ps2_mouse_enable)
233 print("on");
234 else
235 print("off");
236 print("\n");
237 _delay_ms(500);
238 break;
239 #endif
240 case KB_B: // bootloader
241 usb_keyboard_clear_report();
242 usb_keyboard_send();
243 print_enable = true;
244 print("jump to bootloader...\n");
245 _delay_ms(1000);
246 jump_bootloader(); // not return
247 break;
248 case KB_D: // debug all toggle
249 usb_keyboard_clear_report();
250 usb_keyboard_send();
251 debug_enable = !debug_enable;
252 if (debug_enable) {
253 print_enable = true;
254 print("debug enabled.\n");
255 //debug_matrix = true;
256 //debug_keyboard = true;
257 //debug_mouse = true;
258 } else {
259 print("debug disabled.\n");
260 print_enable = false;
261 //debug_matrix = false;
262 //debug_keyboard = false;
263 //debug_mouse = false;
264 }
265 _delay_ms(1000);
266 break;
267 case KB_X: // debug matrix toggle
268 usb_keyboard_clear_report();
269 usb_keyboard_send();
270 debug_matrix = !debug_matrix;
271 if (debug_matrix)
272 print("debug matrix enabled.\n");
273 else
274 print("debug matrix disabled.\n");
275 _delay_ms(1000);
276 break;
277 case KB_K: // debug keyboard toggle
278 usb_keyboard_clear_report();
279 usb_keyboard_send();
280 debug_keyboard = !debug_keyboard;
281 if (debug_keyboard)
282 print("debug keyboard enabled.\n");
283 else
284 print("debug keyboard disabled.\n");
285 _delay_ms(1000);
286 break;
287 case KB_M: // debug mouse toggle
288 usb_keyboard_clear_report();
289 usb_keyboard_send();
290 debug_mouse = !debug_mouse;
291 if (debug_mouse)
292 print("debug mouse enabled.\n");
293 else
294 print("debug mouse disabled.\n");
295 _delay_ms(1000);
296 break;
297 case KB_V: // print version & information
298 usb_keyboard_clear_report();
299 usb_keyboard_send();
300 print_enable = true;
301 print(STR(DESCRIPTION) "\n");
302 _delay_ms(1000);
303 break;
304 case KB_T: // print timer
305 usb_keyboard_clear_report();
306 usb_keyboard_send();
307 print_enable = true;
308 print("timer: "); phex16(timer_count); print("\n");
309 _delay_ms(500);
310 break;
311 case KB_P: // print toggle
312 usb_keyboard_clear_report();
313 usb_keyboard_send();
314 if (print_enable) {
315 print("print disabled.\n");
316 print_enable = false;
317 } else {
318 print_enable = true;
319 print("print enabled.\n");
320 }
321 _delay_ms(1000);
322 break;
323 case KB_S:
324 usb_keyboard_clear_report();
325 usb_keyboard_send();
326 print("UDCON: "); phex(UDCON); print("\n");
327 print("UDIEN: "); phex(UDIEN); print("\n");
328 print("UDINT: "); phex(UDINT); print("\n");
329 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
330 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
331 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
332 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
333 #ifdef USB_MOUSE_ENABLE
334 print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
335 #endif
336 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
337 _delay_ms(500);
338 break;
339 case KB_GRV:
340 usb_keyboard_clear_report();
341 usb_keyboard_send();
342 usb_keyboard_protocol = !usb_keyboard_protocol;
343 print("keyboard protcol: ");
344 if (usb_keyboard_protocol) print("report"); else print("boot");
345 print("\n");
346
347 #ifdef USB_MOUSE_ENABLE
348 usb_mouse_protocol = !usb_mouse_protocol;
349 print("mouse protcol: ");
350 if (usb_mouse_protocol) print("report"); else print("boot");
351 print("\n");
352 #endif
353 _delay_ms(1000);
354 break;
355 #ifdef USB_NKRO_ENABLE
356 case KB_N:
357 usb_keyboard_clear_report();
358 usb_keyboard_send();
359 usb_keyboard_nkro = !usb_keyboard_nkro;
360 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
361 _delay_ms(1000);
362 break;
363 #endif
364 #ifdef USB_EXTRA_ENABLE
365 case KB_ESC:
366 usb_keyboard_clear_report();
367 usb_keyboard_send();
368 if (suspend && remote_wakeup) {
369 usb_remote_wakeup();
370 } else {
371 usb_extra_system_send(SYSTEM_POWER_DOWN);
372 }
373 _delay_ms(1000);
374 break;
375 #endif
376 }
377 }
378
379
380 if (modified) {
381 usb_keyboard_send();
382 }
383
384 #ifdef MOUSEKEY_ENABLE
385 mousekey_usb_send();
386 #endif
387
388 #ifdef PS2_MOUSE_ENABLE
389 if (ps2_mouse_read() == 0)
390 ps2_mouse_usb_send();
391 #endif
392 }
Imprint / Impressum