]> git.gir.st - tmk_keyboard.git/blob - key_process.c
FIX: LED debug
[tmk_keyboard.git] / key_process.c
1 #include <stdbool.h>
2 #include <avr/io.h>
3 #include <util/delay.h>
4 #include "print.h"
5 #include "debug.h"
6 #include "timer.h"
7 #include "util.h"
8 #include "jump_bootloader.h"
9 #include "usb_keyboard.h"
10 #include "usb_mouse.h"
11 #include "usb_keycodes.h"
12 #include "layer.h"
13 #include "matrix_skel.h"
14 #include "keymap_skel.h"
15 #include "controller.h"
16 #include "key_process.h"
17
18
19 #define MOUSE_MOVE_UNIT 10
20 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
21
22 #ifndef MOUSE_DELAY_TIME
23 # define MOUSE_DELAY_TIME 255
24 #endif
25 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
26
27
28 // TODO: refactoring
29 void proc_matrix(void) {
30 static int mouse_repeat = 0;
31
32 bool modified = false;
33 //bool has_ghost = false;
34 int key_index = 0;
35 uint8_t mouse_btn = 0;
36 int8_t mouse_x = 0;
37 int8_t mouse_y = 0;
38 int8_t mouse_vwheel = 0;
39 int8_t mouse_hwheel = 0;
40 uint8_t fn_bits = 0;
41
42 matrix_scan();
43 modified = matrix_is_modified();
44
45 if (modified) {
46 if (debug_matrix) matrix_print();
47 #ifdef DEBUG_LED
48 // LED flash for debug
49 DEBUG_LED_CONFIG;
50 DEBUG_LED_ON;
51 #endif
52 }
53
54 if (matrix_has_ghost()) {
55 // should send error?
56 debug("matrix has ghost!!\n");
57 return;
58 }
59
60 usb_keyboard_clear_report();
61 for (int row = 0; row < matrix_rows(); row++) {
62 for (int col = 0; col < matrix_cols(); col++) {
63 if (!matrix_is_on(row, col)) continue;
64
65 uint8_t code = layer_get_keycode(row, col);
66 if (code == KB_NO) {
67 // do nothing
68 } else if (IS_MOD(code)) {
69 usb_keyboard_mods |= MOD_BIT(code);
70 } else if (IS_MOUSE(code)) {
71 // mouse
72 if (code == MS_UP)
73 mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
74 if (code == MS_DOWN)
75 mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
76 if (code == MS_LEFT)
77 mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
78 if (code == MS_RIGHT)
79 mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
80 if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
81 if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
82 if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
83 if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
84 if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
85 if (code == MS_WH_UP) mouse_vwheel += 1;
86 if (code == MS_WH_DOWN) mouse_vwheel -= 1;
87 if (code == MS_WH_LEFT) mouse_hwheel -= 1;
88 if (code == MS_WH_RIGHT) mouse_hwheel += 1;
89 } else if (IS_FN(code)) {
90 fn_bits |= FN_BIT(code);
91 } else {
92 // normal keys
93 if (key_index < 6)
94 usb_keyboard_keys[key_index] = code;
95 key_index++;
96 }
97 }
98 }
99
100 if (modified) {
101 #ifdef DEBUG_LED
102 // LED flash for debug
103 DEBUG_LED_CONFIG;
104 DEBUG_LED_OFF;
105 #endif
106 }
107
108 layer_switching(fn_bits);
109
110 // when 4 left modifier keys down
111 if (keymap_is_special_mode(fn_bits)) {
112 switch (usb_keyboard_keys[0]) {
113 case KB_H: // help
114 print_enable = true;
115 print("b: jump to bootloader\n");
116 print("d: debug print toggle\n");
117 print("k: keyboard debug toggle\n");
118 print("m: mouse debug toggle\n");
119 print("x: matrix debug toggle\n");
120 print("v: print version\n");
121 print("t: print timer count\n");
122 print("p: print enable toggle\n");
123 _delay_ms(500);
124 print_enable = false;
125 break;
126 case KB_B: // bootloader
127 usb_keyboard_clear_report();
128 usb_keyboard_send();
129 print_enable = true;
130 print("jump to bootloader...\n");
131 _delay_ms(1000);
132 jump_bootloader(); // not return
133 break;
134 case KB_D: // debug all toggle
135 usb_keyboard_clear_report();
136 usb_keyboard_send();
137 debug_enable = !debug_enable;
138 if (debug_enable) {
139 print_enable = true;
140 print("debug enabled.\n");
141 debug_matrix = true;
142 debug_keyboard = true;
143 debug_mouse = true;
144 } else {
145 print("debug disabled.\n");
146 print_enable = false;
147 debug_matrix = false;
148 debug_keyboard = false;
149 debug_mouse = false;
150 }
151 _delay_ms(1000);
152 break;
153 case KB_X: // debug matrix toggle
154 usb_keyboard_clear_report();
155 usb_keyboard_send();
156 debug_matrix = !debug_matrix;
157 if (debug_matrix)
158 print("debug matrix enabled.\n");
159 else
160 print("debug matrix disabled.\n");
161 _delay_ms(1000);
162 break;
163 case KB_K: // debug keyboard toggle
164 usb_keyboard_clear_report();
165 usb_keyboard_send();
166 debug_keyboard = !debug_keyboard;
167 if (debug_keyboard)
168 print("debug keyboard enabled.\n");
169 else
170 print("debug keyboard disabled.\n");
171 _delay_ms(1000);
172 break;
173 case KB_M: // debug mouse toggle
174 usb_keyboard_clear_report();
175 usb_keyboard_send();
176 debug_mouse = !debug_mouse;
177 if (debug_mouse)
178 print("debug mouse enabled.\n");
179 else
180 print("debug mouse disabled.\n");
181 _delay_ms(1000);
182 break;
183 case KB_V: // print version & information
184 usb_keyboard_clear_report();
185 usb_keyboard_send();
186 print_enable = true;
187 print(STR(DESCRIPTION) "\n");
188 _delay_ms(1000);
189 break;
190 case KB_T: // print timer
191 usb_keyboard_clear_report();
192 usb_keyboard_send();
193 print_enable = true;
194 print("timer: "); phex16(timer_count); print("\n");
195 _delay_ms(500);
196 break;
197 case KB_P: // print toggle
198 usb_keyboard_clear_report();
199 usb_keyboard_send();
200 if (print_enable) {
201 print("print disabled.\n");
202 print_enable = false;
203 } else {
204 print_enable = true;
205 print("print enabled.\n");
206 }
207 _delay_ms(1000);
208 break;
209 }
210 }
211
212
213 // send mouse packet to host
214 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
215 mouse_buttons = mouse_btn;
216 if (mouse_x && mouse_y)
217 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
218 else
219 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
220 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
221
222 // acceleration
223 _delay_ms(MOUSE_DELAY_MS);
224 mouse_repeat++;
225 } else {
226 mouse_repeat = 0;
227 }
228
229
230 // send key packet to host
231 if (modified) {
232 if (key_index > 6) {
233 //Rollover
234 }
235 usb_keyboard_send();
236 }
237 }
Imprint / Impressum