]> git.gir.st - tmk_keyboard.git/blob - key_process.c
revise Fn key processing.
[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 layer_switching(fn_bits);
100
101 // when 4 left modifier keys down
102 if (keymap_is_special_mode(fn_bits)) {
103 switch (usb_keyboard_keys[0]) {
104 case KB_H: // help
105 print_enable = true;
106 print("b: jump to bootloader\n");
107 print("d: debug print toggle\n");
108 print("k: keyboard debug toggle\n");
109 print("m: mouse debug toggle\n");
110 print("x: matrix debug toggle\n");
111 print("v: print version\n");
112 print("t: print timer count\n");
113 print("p: print enable toggle\n");
114 _delay_ms(500);
115 print_enable = false;
116 break;
117 case KB_B: // bootloader
118 usb_keyboard_clear_report();
119 usb_keyboard_send();
120 print_enable = true;
121 print("jump to bootloader...\n");
122 _delay_ms(1000);
123 jump_bootloader(); // not return
124 break;
125 case KB_D: // debug all toggle
126 usb_keyboard_clear_report();
127 usb_keyboard_send();
128 debug_enable = !debug_enable;
129 if (debug_enable) {
130 print_enable = true;
131 print("debug enabled.\n");
132 debug_matrix = true;
133 debug_keyboard = true;
134 debug_mouse = true;
135 } else {
136 print("debug disabled.\n");
137 print_enable = false;
138 debug_matrix = false;
139 debug_keyboard = false;
140 debug_mouse = false;
141 }
142 _delay_ms(1000);
143 break;
144 case KB_X: // debug matrix toggle
145 usb_keyboard_clear_report();
146 usb_keyboard_send();
147 debug_matrix = !debug_matrix;
148 if (debug_matrix)
149 print("debug matrix enabled.\n");
150 else
151 print("debug matrix disabled.\n");
152 _delay_ms(1000);
153 break;
154 case KB_K: // debug keyboard toggle
155 usb_keyboard_clear_report();
156 usb_keyboard_send();
157 debug_keyboard = !debug_keyboard;
158 if (debug_keyboard)
159 print("debug keyboard enabled.\n");
160 else
161 print("debug keyboard disabled.\n");
162 _delay_ms(1000);
163 break;
164 case KB_M: // debug mouse toggle
165 usb_keyboard_clear_report();
166 usb_keyboard_send();
167 debug_mouse = !debug_mouse;
168 if (debug_mouse)
169 print("debug mouse enabled.\n");
170 else
171 print("debug mouse disabled.\n");
172 _delay_ms(1000);
173 break;
174 case KB_V: // print version & information
175 usb_keyboard_clear_report();
176 usb_keyboard_send();
177 print_enable = true;
178 print(STR(DESCRIPTION) "\n");
179 _delay_ms(1000);
180 break;
181 case KB_T: // print timer
182 usb_keyboard_clear_report();
183 usb_keyboard_send();
184 print_enable = true;
185 print("timer: "); phex16(timer_count); print("\n");
186 _delay_ms(500);
187 break;
188 case KB_P: // print toggle
189 usb_keyboard_clear_report();
190 usb_keyboard_send();
191 if (print_enable) {
192 print("print disabled.\n");
193 print_enable = false;
194 } else {
195 print_enable = true;
196 print("print enabled.\n");
197 }
198 _delay_ms(1000);
199 break;
200 }
201 }
202
203
204 // send mouse packet to host
205 if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
206 mouse_buttons = mouse_btn;
207 if (mouse_x && mouse_y)
208 usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
209 else
210 usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
211 usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
212
213 // acceleration
214 _delay_ms(MOUSE_DELAY_MS);
215 mouse_repeat++;
216 } else {
217 mouse_repeat = 0;
218 }
219
220
221 // send key packet to host
222 if (modified) {
223 if (key_index > 6) {
224 //Rollover
225 }
226 usb_keyboard_send();
227 #ifdef DEBUG_LED
228 // LED flash for debug
229 DEBUG_LED_CONFIG;
230 DEBUG_LED_OFF;
231 #endif
232 }
233 }
Imprint / Impressum