]> git.donarmstrong.com Git - qmk_firmware.git/blob - key_process.c
changed wait time for volume control.
[qmk_firmware.git] / key_process.c
1 #include <stdbool.h>
2 #include <avr/io.h>
3 #include <avr/interrupt.h>
4 #include <util/delay.h>
5 #include "print.h"
6 #include "debug.h"
7 #include "timer.h"
8 #include "util.h"
9 #include "jump_bootloader.h"
10 #include "usb_keyboard.h"
11 #include "usb_keycodes.h"
12 #include "usb.h"
13 #include "layer.h"
14 #include "matrix_skel.h"
15 #include "keymap_skel.h"
16 #include "key_process.h"
17 #ifdef MOUSEKEY_ENABLE
18 #   include "mousekey.h"
19 #endif
20 #ifdef PS2_MOUSE_ENABLE
21 #   include "ps2_mouse.h"
22 #endif
23 #ifdef USB_EXTRA_ENABLE
24 #   include "usb_extra.h"
25 #endif
26 #ifdef USB_MOUSE_ENABLE
27 #   include "usb_mouse.h"
28 #endif
29
30
31 // TODO: refactoring
32 void proc_matrix(void) {
33     bool modified = false;
34     uint8_t fn_bits = 0;
35
36     matrix_scan();
37     modified = matrix_is_modified();
38     
39     if (modified) {
40         if (debug_matrix) matrix_print();
41 #ifdef DEBUG_LED
42         // LED flash for debug
43         DEBUG_LED_CONFIG;
44         DEBUG_LED_ON;
45 #endif
46     }
47
48     if (matrix_has_ghost()) {
49         // should send error?
50         debug("matrix has ghost!!\n");
51         return;
52     }
53
54     usb_keyboard_swap_report();
55     usb_keyboard_clear_report();
56     for (int row = 0; row < matrix_rows(); row++) {
57         for (int col = 0; col < matrix_cols(); col++) {
58             if (!matrix_is_on(row, col)) continue;
59
60             uint8_t code = layer_get_keycode(row, col);
61             if (code == KB_NO) {
62                 // do nothing
63             } else if (IS_MOD(code)) {
64                 usb_keyboard_add_mod(code);
65             } else if (IS_FN(code)) {
66                 fn_bits |= FN_BIT(code);
67             }
68 #ifdef MOUSEKEY_ENABLE
69             else if (IS_MOUSEKEY(code)) {
70                 mousekey_decode(code);
71             }
72 #endif
73 #ifdef USB_EXTRA_ENABLE
74             // audio control & system control
75             else if (code == KB_MUTE) {
76                 usb_extra_audio_send(AUDIO_MUTE);
77                 usb_extra_audio_send(0);
78                 _delay_ms(500);
79             } else if (code == KB_VOLU) {
80                 usb_extra_audio_send(AUDIO_VOL_UP);
81                 usb_extra_audio_send(0);
82                 _delay_ms(200);
83             } else if (code == KB_VOLD) {
84                 usb_extra_audio_send(AUDIO_VOL_DOWN);
85                 usb_extra_audio_send(0);
86                 _delay_ms(200);
87             } else if (code == KB_PWR) {
88                 if (suspend && remote_wakeup) {
89                     usb_remote_wakeup();
90                 } else {
91                     usb_extra_system_send(SYSTEM_POWER_DOWN);
92                 }
93                 _delay_ms(1000);
94             }
95 #endif
96             // normal key
97             else if (IS_KEY(code)) {
98                 usb_keyboard_add_key(code);
99             } else {
100                 debug("ignore keycode: "); debug_hex(code); debug("\n");
101             }
102         }
103     }
104
105     if (modified) {
106 #ifdef DEBUG_LED
107         // LED flash for debug
108         DEBUG_LED_CONFIG;
109         DEBUG_LED_OFF;
110 #endif
111     }
112
113     layer_switching(fn_bits);
114
115     // TODO: clean code
116     // special mode for control, develop and debug
117     if (keymap_is_special_mode(fn_bits)) {
118         switch (usb_keyboard_get_key()) {
119             case KB_H: // help
120                 print_enable = true;
121                 print("b: jump to bootloader\n");
122                 print("d: toggle debug enable\n");
123                 print("x: toggle matrix debug\n");
124                 print("k: toggle keyboard debug\n");
125                 print("m: toggle mouse debug\n");
126                 print("p: toggle print enable\n");
127                 print("v: print version\n");
128                 print("t: print timer count\n");
129                 print("s: print status\n");
130                 print("`: toggle protcol(boot/report)\n");
131 #ifdef USB_NKRO_ENABLE
132                 print("n: toggle USB_NKRO\n");
133 #endif
134                 print("ESC: power down/wake up\n");
135 #ifdef PS2_MOUSE_ENABLE
136                 print("1: ps2_mouse_init \n");
137                 print("2: ps2_mouse_read \n");
138                 print("3: ps2_mouse: on/off toggle \n");
139 #endif
140                 _delay_ms(500);
141                 print_enable = false;
142                 break;
143 #ifdef PS2_MOUSE_ENABLE
144             case KB_1:
145                 usb_keyboard_clear_report();
146                 usb_keyboard_send();
147                 print_enable = true;
148                 print("ps2_mouse_init...\n");
149                 _delay_ms(500);
150                 ps2_mouse_init();
151                 break;
152             case KB_2:
153                 usb_keyboard_clear_report();
154                 usb_keyboard_send();
155                 print_enable = true;
156                 print("ps2_mouse_read[btn x y]: ");
157                 _delay_ms(100);
158                 ps2_mouse_read();
159                 phex(ps2_mouse_btn); print(" ");
160                 phex(ps2_mouse_x); print(" ");
161                 phex(ps2_mouse_y); print("\n");
162                 print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
163                 break;
164             case KB_3:
165                 ps2_mouse_enable = !ps2_mouse_enable;
166                 print("ps2_mouse: ");
167                 if (ps2_mouse_enable)
168                     print("on");
169                 else
170                     print("off");
171                 print("\n");
172                 _delay_ms(500);
173                 break;
174 #endif
175             case KB_B: // bootloader
176                 usb_keyboard_clear_report();
177                 usb_keyboard_send();
178                 print_enable = true;
179                 print("jump to bootloader...\n");
180                 _delay_ms(1000);
181                 jump_bootloader(); // not return
182                 break;
183             case KB_D: // debug all toggle
184                 usb_keyboard_clear_report();
185                 usb_keyboard_send();
186                 debug_enable = !debug_enable;
187                 if (debug_enable) {
188                     print_enable = true;
189                     print("debug enabled.\n");
190                     debug_matrix = true;
191                     debug_keyboard = true;
192                     debug_mouse = true;
193                 } else {
194                     print("debug disabled.\n");
195                     print_enable = false;
196                     debug_matrix = false;
197                     debug_keyboard = false;
198                     debug_mouse = false;
199                 }
200                 _delay_ms(1000);
201                 break;
202             case KB_X: // debug matrix toggle
203                 usb_keyboard_clear_report();
204                 usb_keyboard_send();
205                 debug_matrix = !debug_matrix;
206                 if (debug_matrix)
207                     print("debug matrix enabled.\n");
208                 else
209                     print("debug matrix disabled.\n");
210                 _delay_ms(1000);
211                 break;
212             case KB_K: // debug keyboard toggle
213                 usb_keyboard_clear_report();
214                 usb_keyboard_send();
215                 debug_keyboard = !debug_keyboard;
216                 if (debug_keyboard)
217                     print("debug keyboard enabled.\n");
218                 else
219                     print("debug keyboard disabled.\n");
220                 _delay_ms(1000);
221                 break;
222             case KB_M: // debug mouse toggle
223                 usb_keyboard_clear_report();
224                 usb_keyboard_send();
225                 debug_mouse = !debug_mouse;
226                 if (debug_mouse)
227                     print("debug mouse enabled.\n");
228                 else
229                     print("debug mouse disabled.\n");
230                 _delay_ms(1000);
231                 break;
232             case KB_V: // print version & information
233                 usb_keyboard_clear_report();
234                 usb_keyboard_send();
235                 print_enable = true;
236                 print(STR(DESCRIPTION) "\n");
237                 _delay_ms(1000);
238                 break;
239             case KB_T: // print timer
240                 usb_keyboard_clear_report();
241                 usb_keyboard_send();
242                 print_enable = true;
243                 print("timer: "); phex16(timer_count); print("\n");
244                 _delay_ms(500);
245                 break;
246             case KB_P: // print toggle
247                 usb_keyboard_clear_report();
248                 usb_keyboard_send();
249                 if (print_enable) {
250                     print("print disabled.\n");
251                     print_enable = false;
252                 } else {
253                     print_enable = true;
254                     print("print enabled.\n");
255                 }
256                 _delay_ms(1000);
257                 break;
258             case KB_S:
259                 usb_keyboard_clear_report();
260                 usb_keyboard_send();
261                 print("UDCON: "); phex(UDCON); print("\n");
262                 print("UDIEN: "); phex(UDIEN); print("\n");
263                 print("UDINT: "); phex(UDINT); print("\n");
264                 print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
265                 print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
266                 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
267                 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
268 #ifdef USB_MOUSE_ENABLE
269                 print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
270 #endif
271                 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
272                 _delay_ms(500);
273                 break;
274             case KB_GRV:
275                 usb_keyboard_clear_report();
276                 usb_keyboard_send();
277                 usb_keyboard_protocol = !usb_keyboard_protocol;
278                 print("keyboard protcol: ");
279                 if (usb_keyboard_protocol) print("report"); else print("boot");
280                 print("\n");
281
282 #ifdef USB_MOUSE_ENABLE
283                 usb_mouse_protocol = !usb_mouse_protocol;
284                 print("mouse protcol: ");
285                 if (usb_mouse_protocol) print("report"); else print("boot");
286                 print("\n");
287 #endif
288                 _delay_ms(1000);
289                 break;
290 #ifdef USB_NKRO_ENABLE
291             case KB_N:
292                 usb_keyboard_clear_report();
293                 usb_keyboard_send();
294                 usb_keyboard_nkro = !usb_keyboard_nkro;
295                 if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
296                 _delay_ms(1000);
297                 break;
298 #endif
299 #ifdef USB_EXTRA_ENABLE
300             case KB_ESC:
301                 usb_keyboard_clear_report();
302                 usb_keyboard_send();
303                 if (suspend && remote_wakeup) {
304                     usb_remote_wakeup();
305                 } else {
306                     usb_extra_system_send(SYSTEM_POWER_DOWN);
307                 }
308                 _delay_ms(1000);
309                 break;
310 #endif
311         }
312     }
313
314
315     if (modified) {
316         usb_keyboard_send();
317     }
318
319 #ifdef MOUSEKEY_ENABLE
320     mousekey_usb_send();
321 #endif
322
323 #ifdef PS2_MOUSE_ENABLE
324     if (ps2_mouse_read() == 0)
325         ps2_mouse_usb_send();
326 #endif
327 }