]>
git.gir.st - tmk_keyboard.git/blob - key_process.c
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
9 #include "jump_bootloader.h"
10 #include "usb_keyboard.h"
11 #include "usb_mouse.h"
12 #include "usb_extra.h"
13 #include "usb_keycodes.h"
16 #include "matrix_skel.h"
17 #include "keymap_skel.h"
18 #include "controller.h"
19 #include "key_process.h"
22 #define MOUSE_MOVE_UNIT 10
23 #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
25 #ifndef MOUSE_DELAY_TIME
26 # define MOUSE_DELAY_TIME 255
28 #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
32 void proc_matrix(void) {
33 static int mouse_repeat
= 0;
35 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_swap_report();
62 usb_keyboard_clear_report();
63 for (int row
= 0; row
< matrix_rows(); row
++) {
64 for (int col
= 0; col
< matrix_cols(); col
++) {
65 if (!matrix_is_on(row
, col
)) continue;
68 uint8_t code
= layer_get_keycode(row
, col
);
71 } else if (IS_MOD(code
)) {
72 usb_keyboard_add_mod(code
);
73 } else if (IS_FN(code
)) {
74 fn_bits
|= FN_BIT(code
);
75 } else if (IS_MOUSE(code
)) {
76 if (code
== MS_UP
) mouse_y
-= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
77 if (code
== MS_DOWN
) mouse_y
+= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
78 if (code
== MS_LEFT
) mouse_x
-= MOUSE_MOVE_UNIT
+ MOUSE_MOVE_ACCEL
;
79 if (code
== MS_RGHT
) 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_U
) mouse_vwheel
+= 1;
86 if (code
== MS_WH_D
) mouse_vwheel
-= 1;
87 if (code
== MS_WH_L
) mouse_hwheel
-= 1;
88 if (code
== MS_WH_R
) mouse_hwheel
+= 1;
91 // audio control & system control
92 else if (code
== KB_MUTE
) {
93 usb_extra_audio_send(AUDIO_MUTE
);
94 usb_extra_audio_send(0);
96 } else if (code
== KB_VOLU
) {
97 usb_extra_audio_send(AUDIO_VOL_UP
);
98 usb_extra_audio_send(0);
100 } else if (code
== KB_VOLD
) {
101 usb_extra_audio_send(AUDIO_VOL_DOWN
);
102 usb_extra_audio_send(0);
104 } else if (code
== KB_PWR
) {
105 if (suspend
&& remote_wakeup
) {
108 usb_extra_system_send(SYSTEM_POWER_DOWN
);
115 usb_keyboard_add_key(code
);
122 // LED flash for debug
128 layer_switching(fn_bits
);
131 // special mode for control, develop and debug
132 if (keymap_is_special_mode(fn_bits
)) {
133 switch (usb_keyboard_get_key()) {
136 print("b: jump to bootloader\n");
137 print("d: toggle debug enable\n");
138 print("x: toggle matrix debug\n");
139 print("k: toggle keyboard debug\n");
140 print("m: toggle mouse debug\n");
141 print("p: toggle print enable\n");
142 print("v: print version\n");
143 print("t: print timer count\n");
144 print("s: print status\n");
145 print("`: toggle protcol(boot/report)\n");
147 print("n: toggle NKRO\n");
149 print("ESC: power down/wake up\n");
151 print_enable
= false;
153 case KB_B
: // bootloader
154 usb_keyboard_clear_report();
157 print("jump to bootloader...\n");
159 jump_bootloader(); // not return
161 case KB_D
: // debug all toggle
162 usb_keyboard_clear_report();
164 debug_enable
= !debug_enable
;
167 print("debug enabled.\n");
169 debug_keyboard
= true;
172 print("debug disabled.\n");
173 print_enable
= false;
174 debug_matrix
= false;
175 debug_keyboard
= false;
180 case KB_X
: // debug matrix toggle
181 usb_keyboard_clear_report();
183 debug_matrix
= !debug_matrix
;
185 print("debug matrix enabled.\n");
187 print("debug matrix disabled.\n");
190 case KB_K
: // debug keyboard toggle
191 usb_keyboard_clear_report();
193 debug_keyboard
= !debug_keyboard
;
195 print("debug keyboard enabled.\n");
197 print("debug keyboard disabled.\n");
200 case KB_M
: // debug mouse toggle
201 usb_keyboard_clear_report();
203 debug_mouse
= !debug_mouse
;
205 print("debug mouse enabled.\n");
207 print("debug mouse disabled.\n");
210 case KB_V
: // print version & information
211 usb_keyboard_clear_report();
214 print(STR(DESCRIPTION
) "\n");
217 case KB_T
: // print timer
218 usb_keyboard_clear_report();
221 print("timer: "); phex16(timer_count
); print("\n");
224 case KB_P
: // print toggle
225 usb_keyboard_clear_report();
228 print("print disabled.\n");
229 print_enable
= false;
232 print("print enabled.\n");
237 usb_keyboard_clear_report();
239 print("UDCON: "); phex(UDCON
); print("\n");
240 print("UDIEN: "); phex(UDIEN
); print("\n");
241 print("UDINT: "); phex(UDINT
); print("\n");
242 print("usb_keyboard_leds:"); phex(usb_keyboard_leds
); print("\n");
243 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol
); print("\n");
244 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config
); print("\n");
245 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count
); print("\n");
246 print("mouse_protocol:"); phex(mouse_protocol
); print("\n");
247 if (usb_keyboard_nkro
) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
251 usb_keyboard_clear_report();
253 usb_keyboard_protocol
= !usb_keyboard_protocol
;
254 mouse_protocol
= !mouse_protocol
;
255 print("keyboard protcol: ");
256 if (usb_keyboard_protocol
) print("report"); else print("boot");
258 print("mouse protcol: ");
259 if (mouse_protocol
) print("report"); else print("boot");
265 usb_keyboard_clear_report();
267 usb_keyboard_nkro
= !usb_keyboard_nkro
;
268 if (usb_keyboard_nkro
) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
273 usb_keyboard_clear_report();
275 if (suspend
&& remote_wakeup
) {
278 usb_extra_system_send(SYSTEM_POWER_DOWN
);
286 // send mouse packet to host
287 if (mouse_x
|| mouse_y
|| mouse_vwheel
|| mouse_hwheel
|| mouse_btn
!= mouse_buttons
) {
288 mouse_buttons
= mouse_btn
;
289 if (mouse_x
&& mouse_y
)
290 usb_mouse_move(mouse_x
*0.7, mouse_y
*0.7, mouse_vwheel
, mouse_hwheel
);
292 usb_mouse_move(mouse_x
, mouse_y
, mouse_vwheel
, mouse_hwheel
);
293 usb_mouse_print(mouse_x
, mouse_y
, mouse_vwheel
, mouse_hwheel
);
296 _delay_ms(MOUSE_DELAY_MS
);
303 // send key packet to host