3 #include <util/delay.h>
4 #include "usb_keyboard.h"
6 #include "usb_keycodes.h"
10 #include "jump_bootloader.h"
11 #include "matrix_skel.h"
12 #include "keymap_skel.h"
13 #include "controller.h"
15 #include "key_process.h"
18 #define MOUSE_MOVE_UNIT 10
19 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
21 #ifndef MOUSE_DELAY_TIME
22 # define MOUSE_DELAY_TIME 255
24 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
28 void proc_matrix(void) {
29 static int mouse_repeat = 0;
31 bool modified = false;
32 //bool has_ghost = false;
34 uint8_t mouse_btn = 0;
37 int8_t mouse_vwheel = 0;
38 int8_t mouse_hwheel = 0;
42 modified = matrix_is_modified();
45 if (debug_matrix) matrix_print();
47 // LED flash for debug
53 if (matrix_has_ghost()) {
55 debug("matrix has ghost!!\n");
60 for (int row = 0; row < matrix_rows(); row++) {
61 for (int col = 0; col < matrix_cols(); col++) {
62 if (!matrix_is_on(row, col)) continue;
64 uint8_t code = keymap_get_keycode(row, col);
67 } else if (IS_MOD(code)) {
68 keyboard_modifier_keys |= MOD_BIT(code);
69 } else if (IS_MOUSE(code)) {
72 mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
74 mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
76 mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
78 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_UP) mouse_vwheel += 1;
85 if (code == MS_WH_DOWN) mouse_vwheel -= 1;
86 if (code == MS_WH_LEFT) mouse_hwheel -= 1;
87 if (code == MS_WH_RIGHT) mouse_hwheel += 1;
88 } else if (IS_FN(code)) {
89 fn_bits |= FN_BIT(code);
93 keyboard_keys[key_index] = code;
98 keymap_fn_proc(fn_bits);
100 // when 4 left modifier keys down
101 if (keymap_is_special_mode(fn_bits)) {
102 switch (keyboard_keys[0]) {
103 case KB_B: // bootloader
104 usb_keyboard_clear();
107 print("jump to bootloader...\n");
109 jump_bootloader(); // not return
111 case KB_D: // debug all toggle
112 usb_keyboard_clear();
114 debug_enable = !debug_enable;
116 print("debug enabled.\n");
119 debug_keyboard = true;
122 print("debug disabled.\n");
123 print_enable = false;
124 debug_matrix = false;
125 debug_keyboard = false;
130 case KB_X: // debug matrix toggle
131 usb_keyboard_clear();
133 debug_matrix = !debug_matrix;
135 print("debug matrix enabled.\n");
137 print("debug matrix disabled.\n");
140 case KB_K: // debug keyboard toggle
141 usb_keyboard_clear();
143 debug_keyboard = !debug_keyboard;
145 print("debug keyboard enabled.\n");
147 print("debug keyboard disabled.\n");
150 case KB_M: // debug mouse toggle
151 usb_keyboard_clear();
153 debug_mouse = !debug_mouse;
155 print("debug mouse enabled.\n");
157 print("debug mouse disabled.\n");
160 case KB_V: // print version & information
161 usb_keyboard_clear();
163 print(STR(DESCRIPTION) "\n");
170 // send mouse packet to host
171 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
172 mouse_buttons = mouse_btn;
173 if (mouse_x && mouse_y)
174 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
176 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
177 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
180 _delay_ms(MOUSE_DELAY_MS);
187 // send key packet to host
193 usb_keyboard_print();
195 // LED flash for debug