3 #include <avr/interrupt.h>
4 #include <util/delay.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"
16 #include "matrix_skel.h"
17 #include "keymap_skel.h"
18 #include "key_process.h"
19 #ifdef MOUSEKEY_ENABLE
20 # include "mousekey.h"
22 #ifdef PS2_MOUSE_ENABLE
23 # include "ps2_mouse.h"
28 void proc_matrix(void) {
29 bool modified = false;
33 modified = matrix_is_modified();
36 if (debug_matrix) matrix_print();
38 // LED flash for debug
44 if (matrix_has_ghost()) {
46 debug("matrix has ghost!!\n");
50 usb_keyboard_swap_report();
51 usb_keyboard_clear_report();
52 for (int row = 0; row < matrix_rows(); row++) {
53 for (int col = 0; col < matrix_cols(); col++) {
54 if (!matrix_is_on(row, col)) continue;
57 uint8_t code = layer_get_keycode(row, col);
60 } else if (IS_MOD(code)) {
61 usb_keyboard_add_mod(code);
62 } else if (IS_FN(code)) {
63 fn_bits |= FN_BIT(code);
64 } else if (IS_MOUSE(code)) {
65 #ifdef MOUSEKEY_ENABLE
66 mousekey_decode(code);
70 // audio control & system control
71 else if (code == KB_MUTE) {
72 usb_extra_audio_send(AUDIO_MUTE);
73 usb_extra_audio_send(0);
75 } else if (code == KB_VOLU) {
76 usb_extra_audio_send(AUDIO_VOL_UP);
77 usb_extra_audio_send(0);
79 } else if (code == KB_VOLD) {
80 usb_extra_audio_send(AUDIO_VOL_DOWN);
81 usb_extra_audio_send(0);
83 } else if (code == KB_PWR) {
84 if (suspend && remote_wakeup) {
87 usb_extra_system_send(SYSTEM_POWER_DOWN);
94 usb_keyboard_add_key(code);
101 // LED flash for debug
107 layer_switching(fn_bits);
110 // special mode for control, develop and debug
111 if (keymap_is_special_mode(fn_bits)) {
112 switch (usb_keyboard_get_key()) {
115 print("b: jump to bootloader\n");
116 print("d: toggle debug enable\n");
117 print("x: toggle matrix debug\n");
118 print("k: toggle keyboard debug\n");
119 print("m: toggle mouse debug\n");
120 print("p: toggle print enable\n");
121 print("v: print version\n");
122 print("t: print timer count\n");
123 print("s: print status\n");
124 print("`: toggle protcol(boot/report)\n");
125 #ifdef USB_NKRO_ENABLE
126 print("n: toggle USB_NKRO\n");
128 print("ESC: power down/wake up\n");
129 #ifdef PS2_MOUSE_ENABLE
130 print("1: ps2_mouse_init \n");
131 print("2: ps2_mouse_read \n");
134 print_enable = false;
136 #ifdef PS2_MOUSE_ENABLE
138 usb_keyboard_clear_report();
141 print("ps2_mouse_init...\n");
146 usb_keyboard_clear_report();
149 print("ps2_mouse_read[btn x y]: ");
152 phex(ps2_mouse_btn); print(" ");
153 phex(ps2_mouse_x); print(" ");
154 phex(ps2_mouse_y); print("\n");
155 print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
158 case KB_B: // bootloader
159 usb_keyboard_clear_report();
162 print("jump to bootloader...\n");
164 jump_bootloader(); // not return
166 case KB_D: // debug all toggle
167 usb_keyboard_clear_report();
169 debug_enable = !debug_enable;
172 print("debug enabled.\n");
174 debug_keyboard = true;
177 print("debug disabled.\n");
178 print_enable = false;
179 debug_matrix = false;
180 debug_keyboard = false;
185 case KB_X: // debug matrix toggle
186 usb_keyboard_clear_report();
188 debug_matrix = !debug_matrix;
190 print("debug matrix enabled.\n");
192 print("debug matrix disabled.\n");
195 case KB_K: // debug keyboard toggle
196 usb_keyboard_clear_report();
198 debug_keyboard = !debug_keyboard;
200 print("debug keyboard enabled.\n");
202 print("debug keyboard disabled.\n");
205 case KB_M: // debug mouse toggle
206 usb_keyboard_clear_report();
208 debug_mouse = !debug_mouse;
210 print("debug mouse enabled.\n");
212 print("debug mouse disabled.\n");
215 case KB_V: // print version & information
216 usb_keyboard_clear_report();
219 print(STR(DESCRIPTION) "\n");
222 case KB_T: // print timer
223 usb_keyboard_clear_report();
226 print("timer: "); phex16(timer_count); print("\n");
229 case KB_P: // print toggle
230 usb_keyboard_clear_report();
233 print("print disabled.\n");
234 print_enable = false;
237 print("print enabled.\n");
242 usb_keyboard_clear_report();
244 print("UDCON: "); phex(UDCON); print("\n");
245 print("UDIEN: "); phex(UDIEN); print("\n");
246 print("UDINT: "); phex(UDINT); print("\n");
247 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
248 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
249 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
250 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
251 print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
252 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
256 usb_keyboard_clear_report();
258 usb_keyboard_protocol = !usb_keyboard_protocol;
259 usb_mouse_protocol = !usb_mouse_protocol;
260 print("keyboard protcol: ");
261 if (usb_keyboard_protocol) print("report"); else print("boot");
263 print("mouse protcol: ");
264 if (usb_mouse_protocol) print("report"); else print("boot");
268 #ifdef USB_NKRO_ENABLE
270 usb_keyboard_clear_report();
272 usb_keyboard_nkro = !usb_keyboard_nkro;
273 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
278 usb_keyboard_clear_report();
280 if (suspend && remote_wakeup) {
283 usb_extra_system_send(SYSTEM_POWER_DOWN);
295 #ifdef MOUSEKEY_ENABLE
300 #ifdef PS2_MOUSE_ENABLE
302 //if (ps2_mouse_error_count > 10) {
304 ps2_mouse_usb_send();