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