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