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()) {
121 print("b: jump to bootloader\n");
122 print("d: toggle debug enable\n");
123 print("x: toggle matrix debug\n");
124 print("k: toggle keyboard debug\n");
125 print("m: toggle mouse debug\n");
126 print("p: toggle print enable\n");
127 print("v: print version\n");
128 print("t: print timer count\n");
129 print("s: print status\n");
130 print("`: toggle protcol(boot/report)\n");
131 #ifdef USB_NKRO_ENABLE
132 print("n: toggle USB_NKRO\n");
134 print("ESC: power down/wake up\n");
135 #ifdef PS2_MOUSE_ENABLE
136 print("1: ps2_mouse_init \n");
137 print("2: ps2_mouse_read \n");
138 print("3: ps2_mouse: on/off toggle \n");
141 print_enable = false;
143 #ifdef PS2_MOUSE_ENABLE
145 usb_keyboard_clear_report();
148 print("ps2_mouse_init...\n");
153 usb_keyboard_clear_report();
156 print("ps2_mouse_read[btn x y]: ");
159 phex(ps2_mouse_btn); print(" ");
160 phex(ps2_mouse_x); print(" ");
161 phex(ps2_mouse_y); print("\n");
162 print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
165 ps2_mouse_enable = !ps2_mouse_enable;
166 print("ps2_mouse: ");
167 if (ps2_mouse_enable)
175 case KB_B: // bootloader
176 usb_keyboard_clear_report();
179 print("jump to bootloader...\n");
181 jump_bootloader(); // not return
183 case KB_D: // debug all toggle
184 usb_keyboard_clear_report();
186 debug_enable = !debug_enable;
189 print("debug enabled.\n");
191 debug_keyboard = true;
194 print("debug disabled.\n");
195 print_enable = false;
196 debug_matrix = false;
197 debug_keyboard = false;
202 case KB_X: // debug matrix toggle
203 usb_keyboard_clear_report();
205 debug_matrix = !debug_matrix;
207 print("debug matrix enabled.\n");
209 print("debug matrix disabled.\n");
212 case KB_K: // debug keyboard toggle
213 usb_keyboard_clear_report();
215 debug_keyboard = !debug_keyboard;
217 print("debug keyboard enabled.\n");
219 print("debug keyboard disabled.\n");
222 case KB_M: // debug mouse toggle
223 usb_keyboard_clear_report();
225 debug_mouse = !debug_mouse;
227 print("debug mouse enabled.\n");
229 print("debug mouse disabled.\n");
232 case KB_V: // print version & information
233 usb_keyboard_clear_report();
236 print(STR(DESCRIPTION) "\n");
239 case KB_T: // print timer
240 usb_keyboard_clear_report();
243 print("timer: "); phex16(timer_count); print("\n");
246 case KB_P: // print toggle
247 usb_keyboard_clear_report();
250 print("print disabled.\n");
251 print_enable = false;
254 print("print enabled.\n");
259 usb_keyboard_clear_report();
261 print("UDCON: "); phex(UDCON); print("\n");
262 print("UDIEN: "); phex(UDIEN); print("\n");
263 print("UDINT: "); phex(UDINT); print("\n");
264 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
265 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
266 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
267 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
268 #ifdef USB_MOUSE_ENABLE
269 print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
271 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
275 usb_keyboard_clear_report();
277 usb_keyboard_protocol = !usb_keyboard_protocol;
278 print("keyboard protcol: ");
279 if (usb_keyboard_protocol) print("report"); else print("boot");
282 #ifdef USB_MOUSE_ENABLE
283 usb_mouse_protocol = !usb_mouse_protocol;
284 print("mouse protcol: ");
285 if (usb_mouse_protocol) print("report"); else print("boot");
290 #ifdef USB_NKRO_ENABLE
292 usb_keyboard_clear_report();
294 usb_keyboard_nkro = !usb_keyboard_nkro;
295 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
299 #ifdef USB_EXTRA_ENABLE
301 usb_keyboard_clear_report();
303 if (suspend && remote_wakeup) {
306 usb_extra_system_send(SYSTEM_POWER_DOWN);
319 #ifdef MOUSEKEY_ENABLE
323 #ifdef PS2_MOUSE_ENABLE
324 if (ps2_mouse_read() == 0)
325 ps2_mouse_usb_send();