3 #include <avr/interrupt.h>
4 #include <util/delay.h>
9 #include "jump_bootloader.h"
10 #include "usb_keyboard.h"
11 #include "usb_keycodes.h"
14 #include "matrix_skel.h"
15 #include "keymap_skel.h"
16 #include "key_process.h"
17 #ifdef MOUSEKEY_ENABLE
18 # include "mousekey.h"
20 #ifdef PS2_MOUSE_ENABLE
21 # include "ps2_mouse.h"
23 #ifdef USB_EXTRA_ENABLE
24 # include "usb_extra.h"
26 #ifdef USB_MOUSE_ENABLE
27 # include "usb_mouse.h"
32 void proc_matrix(void) {
33 bool modified = false;
37 modified = matrix_is_modified();
40 if (debug_matrix) matrix_print();
42 // LED flash for debug
48 if (matrix_has_ghost()) {
50 debug("matrix has ghost!!\n");
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;
60 uint8_t code = layer_get_keycode(row, col);
63 } else if (IS_MOD(code)) {
64 usb_keyboard_add_mod(code);
65 } else if (IS_FN(code)) {
66 fn_bits |= FN_BIT(code);
68 #ifdef MOUSEKEY_ENABLE
69 else if (IS_MOUSEKEY(code)) {
70 mousekey_decode(code);
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);
79 } else if (code == KB_VOLU) {
80 usb_extra_audio_send(AUDIO_VOL_UP);
81 usb_extra_audio_send(0);
83 } else if (code == KB_VOLD) {
84 usb_extra_audio_send(AUDIO_VOL_DOWN);
85 usb_extra_audio_send(0);
87 } else if (code == KB_PWR) {
88 if (suspend && remote_wakeup) {
91 usb_extra_system_send(SYSTEM_POWER_DOWN);
97 else if (IS_KEY(code)) {
98 usb_keyboard_add_key(code);
100 debug("ignore keycode: "); debug_hex(code); debug("\n");
107 // LED flash for debug
113 layer_switching(fn_bits);
116 // special mode for control, develop and debug
117 if (keymap_is_special_mode(fn_bits)) {
118 switch (usb_keyboard_get_key()) {
120 usb_keyboard_clear_report();
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");
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");
149 print_enable = false;
152 usb_keyboard_clear_report();
155 print("clear matrix\n");
159 usb_keyboard_clear_report();
161 print("current_layer: "); phex(current_layer); print("\n");
162 print("default_layer: "); phex(default_layer); print("\n");
165 print("switch to Layer0 \n");
169 usb_keyboard_clear_report();
171 print("current_layer: "); phex(current_layer); print("\n");
172 print("default_layer: "); phex(default_layer); print("\n");
175 print("switch to Layer1 \n");
179 usb_keyboard_clear_report();
181 print("current_layer: "); phex(current_layer); print("\n");
182 print("default_layer: "); phex(default_layer); print("\n");
185 print("switch to Layer2 \n");
189 usb_keyboard_clear_report();
191 print("current_layer: "); phex(current_layer); print("\n");
192 print("default_layer: "); phex(default_layer); print("\n");
195 print("switch to Layer3 \n");
199 usb_keyboard_clear_report();
201 print("current_layer: "); phex(current_layer); print("\n");
202 print("default_layer: "); phex(default_layer); print("\n");
205 print("switch to Layer4 \n");
208 #ifdef PS2_MOUSE_ENABLE
210 usb_keyboard_clear_report();
213 print("ps2_mouse_init...\n");
218 usb_keyboard_clear_report();
221 print("ps2_mouse_read[btn x y]: ");
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");
230 ps2_mouse_enable = !ps2_mouse_enable;
231 print("ps2_mouse: ");
232 if (ps2_mouse_enable)
240 case KB_B: // bootloader
241 usb_keyboard_clear_report();
244 print("jump to bootloader...\n");
246 jump_bootloader(); // not return
248 case KB_D: // debug all toggle
249 usb_keyboard_clear_report();
251 debug_enable = !debug_enable;
254 print("debug enabled.\n");
255 //debug_matrix = true;
256 //debug_keyboard = true;
257 //debug_mouse = true;
259 print("debug disabled.\n");
260 print_enable = false;
261 //debug_matrix = false;
262 //debug_keyboard = false;
263 //debug_mouse = false;
267 case KB_X: // debug matrix toggle
268 usb_keyboard_clear_report();
270 debug_matrix = !debug_matrix;
272 print("debug matrix enabled.\n");
274 print("debug matrix disabled.\n");
277 case KB_K: // debug keyboard toggle
278 usb_keyboard_clear_report();
280 debug_keyboard = !debug_keyboard;
282 print("debug keyboard enabled.\n");
284 print("debug keyboard disabled.\n");
287 case KB_M: // debug mouse toggle
288 usb_keyboard_clear_report();
290 debug_mouse = !debug_mouse;
292 print("debug mouse enabled.\n");
294 print("debug mouse disabled.\n");
297 case KB_V: // print version & information
298 usb_keyboard_clear_report();
301 print(STR(DESCRIPTION) "\n");
304 case KB_T: // print timer
305 usb_keyboard_clear_report();
308 print("timer: "); phex16(timer_count); print("\n");
311 case KB_P: // print toggle
312 usb_keyboard_clear_report();
315 print("print disabled.\n");
316 print_enable = false;
319 print("print enabled.\n");
324 usb_keyboard_clear_report();
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");
336 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
340 usb_keyboard_clear_report();
342 usb_keyboard_protocol = !usb_keyboard_protocol;
343 print("keyboard protcol: ");
344 if (usb_keyboard_protocol) print("report"); else print("boot");
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");
355 #ifdef USB_NKRO_ENABLE
357 usb_keyboard_clear_report();
359 usb_keyboard_nkro = !usb_keyboard_nkro;
360 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
364 #ifdef USB_EXTRA_ENABLE
366 usb_keyboard_clear_report();
368 if (suspend && remote_wakeup) {
371 usb_extra_system_send(SYSTEM_POWER_DOWN);
384 #ifdef MOUSEKEY_ENABLE
388 #ifdef PS2_MOUSE_ENABLE
389 if (ps2_mouse_read() == 0)
390 ps2_mouse_usb_send();