3 #include <util/delay.h>
8 #include "jump_bootloader.h"
9 #include "usb_keyboard.h"
10 #include "usb_mouse.h"
11 #include "usb_extra.h"
12 #include "usb_keycodes.h"
15 #include "matrix_skel.h"
16 #include "keymap_skel.h"
17 #include "controller.h"
18 #include "key_process.h"
21 #define MOUSE_MOVE_UNIT 10
22 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
24 #ifndef MOUSE_DELAY_TIME
25 # define MOUSE_DELAY_TIME 255
27 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
31 void proc_matrix(void) {
32 static int mouse_repeat = 0;
34 bool modified = false;
36 uint8_t mouse_btn = 0;
39 int8_t mouse_vwheel = 0;
40 int8_t mouse_hwheel = 0;
44 modified = matrix_is_modified();
47 if (debug_matrix) matrix_print();
49 // LED flash for debug
55 if (matrix_has_ghost()) {
57 debug("matrix has ghost!!\n");
61 usb_keyboard_clear_report();
62 for (int row = 0; row < matrix_rows(); row++) {
63 for (int col = 0; col < matrix_cols(); col++) {
64 if (!matrix_is_on(row, col)) continue;
67 uint8_t code = layer_get_keycode(row, col);
70 } else if (IS_MOD(code)) {
71 usb_keyboard_mods |= MOD_BIT(code);
72 } else if (IS_FN(code)) {
73 fn_bits |= FN_BIT(code);
74 } else if (IS_MOUSE(code)) {
75 if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
76 if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
77 if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
78 if (code == MS_RGHT) mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
79 if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
80 if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
81 if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
82 if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
83 if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
84 if (code == MS_WH_U) mouse_vwheel += 1;
85 if (code == MS_WH_D) mouse_vwheel -= 1;
86 if (code == MS_WH_L) mouse_hwheel -= 1;
87 if (code == MS_WH_R) mouse_hwheel += 1;
90 // audio control & system control
91 else if (code == KB_MUTE) {
92 usb_extra_audio_send(AUDIO_MUTE);
93 usb_extra_audio_send(0);
95 } else if (code == KB_VOLU) {
96 usb_extra_audio_send(AUDIO_VOL_UP);
97 usb_extra_audio_send(0);
99 } else if (code == KB_VOLD) {
100 usb_extra_audio_send(AUDIO_VOL_DOWN);
101 usb_extra_audio_send(0);
103 } else if (code == KB_PWR) {
104 if (suspend && remote_wakeup) {
107 usb_extra_system_send(SYSTEM_POWER_DOWN);
115 usb_keyboard_keys[key_index] = code;
123 // LED flash for debug
129 layer_switching(fn_bits);
132 // when 4 left modifier keys down
133 if (keymap_is_special_mode(fn_bits)) {
134 switch (usb_keyboard_keys[0]) {
137 print("b: jump to bootloader\n");
138 print("d: debug print toggle\n");
139 print("x: matrix debug toggle\n");
140 print("k: keyboard debug toggle\n");
141 print("m: mouse debug toggle\n");
142 print("p: print enable toggle\n");
143 print("v: print version\n");
144 print("t: print timer count\n");
145 print("r: print registers\n");
146 print("ESC: power down/wake up\n");
148 print_enable = false;
150 case KB_B: // bootloader
151 usb_keyboard_clear_report();
154 print("jump to bootloader...\n");
156 jump_bootloader(); // not return
158 case KB_D: // debug all toggle
159 usb_keyboard_clear_report();
161 debug_enable = !debug_enable;
164 print("debug enabled.\n");
166 debug_keyboard = true;
169 print("debug disabled.\n");
170 print_enable = false;
171 debug_matrix = false;
172 debug_keyboard = false;
177 case KB_X: // debug matrix toggle
178 usb_keyboard_clear_report();
180 debug_matrix = !debug_matrix;
182 print("debug matrix enabled.\n");
184 print("debug matrix disabled.\n");
187 case KB_K: // debug keyboard toggle
188 usb_keyboard_clear_report();
190 debug_keyboard = !debug_keyboard;
192 print("debug keyboard enabled.\n");
194 print("debug keyboard disabled.\n");
197 case KB_M: // debug mouse toggle
198 usb_keyboard_clear_report();
200 debug_mouse = !debug_mouse;
202 print("debug mouse enabled.\n");
204 print("debug mouse disabled.\n");
207 case KB_V: // print version & information
208 usb_keyboard_clear_report();
211 print(STR(DESCRIPTION) "\n");
214 case KB_T: // print timer
215 usb_keyboard_clear_report();
218 print("timer: "); phex16(timer_count); print("\n");
221 case KB_P: // print toggle
222 usb_keyboard_clear_report();
225 print("print disabled.\n");
226 print_enable = false;
229 print("print enabled.\n");
234 usb_keyboard_clear_report();
236 print("UDIEN: "); phex(UDIEN); print("\n");
237 print("UDINT: "); phex(UDINT); print("\n");
241 usb_keyboard_clear_report();
243 if (suspend && remote_wakeup) {
246 usb_extra_system_send(SYSTEM_POWER_DOWN);
254 // send mouse packet to host
255 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
256 mouse_buttons = mouse_btn;
257 if (mouse_x && mouse_y)
258 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
260 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
261 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
264 _delay_ms(MOUSE_DELAY_MS);
271 // send key packet to host