]> git.gir.st - tmk_keyboard.git/blob - tmk_core/common/command.c
core: Fix for disabling NKRO in Boot protocol
[tmk_keyboard.git] / tmk_core / common / command.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <stdint.h>
18 #include <stdbool.h>
19 #include <util/delay.h>
20 #include "keycode.h"
21 #include "host.h"
22 #include "keymap.h"
23 #include "print.h"
24 #include "debug.h"
25 #include "util.h"
26 #include "timer.h"
27 #include "keyboard.h"
28 #include "bootloader.h"
29 #include "action_layer.h"
30 #include "action_util.h"
31 #include "eeconfig.h"
32 #include "sleep_led.h"
33 #include "led.h"
34 #include "command.h"
35 #include "backlight.h"
36
37 #ifdef MOUSEKEY_ENABLE
38 #include "mousekey.h"
39 #endif
40
41 #ifdef PROTOCOL_PJRC
42 # include "usb_keyboard.h"
43 # ifdef EXTRAKEY_ENABLE
44 # include "usb_extra.h"
45 # endif
46 #endif
47
48 #ifdef PROTOCOL_VUSB
49 # include "usbdrv.h"
50 #endif
51
52
53 static bool command_common(uint8_t code);
54 static void command_common_help(void);
55 static bool command_console(uint8_t code);
56 static void command_console_help(void);
57 #ifdef MOUSEKEY_ENABLE
58 static bool mousekey_console(uint8_t code);
59 static void mousekey_console_help(void);
60 #endif
61
62 static uint8_t numkey2num(uint8_t code);
63 static void switch_default_layer(uint8_t layer);
64
65
66 command_state_t command_state = ONESHOT;
67
68
69 bool command_proc(uint8_t code)
70 {
71 switch (command_state) {
72 case ONESHOT:
73 if (!IS_COMMAND())
74 return false;
75 return (command_extra(code) || command_common(code));
76 break;
77 case CONSOLE:
78 if (IS_COMMAND())
79 return (command_extra(code) || command_common(code));
80 else
81 return (command_console_extra(code) || command_console(code));
82 break;
83 #ifdef MOUSEKEY_ENABLE
84 case MOUSEKEY:
85 mousekey_console(code);
86 break;
87 #endif
88 default:
89 command_state = ONESHOT;
90 return false;
91 }
92 return true;
93 }
94
95 /* TODO: Refactoring is needed. */
96 /* This allows to define extra commands. return false when not processed. */
97 bool command_extra(uint8_t code) __attribute__ ((weak));
98 bool command_extra(uint8_t code)
99 {
100 return false;
101 }
102
103 bool command_console_extra(uint8_t code) __attribute__ ((weak));
104 bool command_console_extra(uint8_t code)
105 {
106 return false;
107 }
108
109
110 /***********************************************************
111 * Command common
112 ***********************************************************/
113 static void command_common_help(void)
114 {
115 print("\n\t- Magic -\n"
116 "d: debug\n"
117 "x: debug matrix\n"
118 "k: debug keyboard\n"
119 "m: debug mouse\n"
120 "v: version\n"
121 "s: status\n"
122 "c: console mode\n"
123 "0-4: layer0-4(F10-F4)\n"
124 "Paus: bootloader\n"
125
126 #ifdef KEYBOARD_LOCK_ENABLE
127 "Caps: Lock\n"
128 #endif
129
130 #ifdef BOOTMAGIC_ENABLE
131 "e: eeprom\n"
132 #endif
133
134 #ifdef NKRO_ENABLE
135 "n: NKRO\n"
136 #endif
137
138 #ifdef SLEEP_LED_ENABLE
139 "z: sleep LED test\n"
140 #endif
141 );
142 }
143
144 #ifdef BOOTMAGIC_ENABLE
145 static void print_eeconfig(void)
146 {
147 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
148
149 debug_config_t dc;
150 dc.raw = eeconfig_read_debug();
151 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
152 print(".enable: "); print_dec(dc.enable); print("\n");
153 print(".matrix: "); print_dec(dc.matrix); print("\n");
154 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
155 print(".mouse: "); print_dec(dc.mouse); print("\n");
156
157 keymap_config_t kc;
158 kc.raw = eeconfig_read_keymap();
159 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
160 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
161 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
162 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
163 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
164 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
165 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
166 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
167 print(".nkro: "); print_dec(kc.nkro); print("\n");
168
169 #ifdef BACKLIGHT_ENABLE
170 backlight_config_t bc;
171 bc.raw = eeconfig_read_backlight();
172 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
173 print(".enable: "); print_dec(bc.enable); print("\n");
174 print(".level: "); print_dec(bc.level); print("\n");
175 #endif
176 }
177 #endif
178
179 static bool command_common(uint8_t code)
180 {
181 static host_driver_t *host_driver = 0;
182 switch (code) {
183 #ifdef SLEEP_LED_ENABLE
184 case KC_Z:
185 // test breathing sleep LED
186 print("Sleep LED test\n");
187 sleep_led_toggle();
188 led_set(host_keyboard_leds());
189 break;
190 #endif
191 #ifdef BOOTMAGIC_ENABLE
192 case KC_E:
193 print("eeconfig:\n");
194 print_eeconfig();
195 break;
196 #endif
197 #ifdef KEYBOARD_LOCK_ENABLE
198 case KC_CAPSLOCK:
199 if (host_get_driver()) {
200 host_driver = host_get_driver();
201 clear_keyboard();
202 host_set_driver(0);
203 print("Locked.\n");
204 } else {
205 host_set_driver(host_driver);
206 print("Unlocked.\n");
207 }
208 break;
209 #endif
210 case KC_H:
211 case KC_SLASH: /* ? */
212 command_common_help();
213 break;
214 case KC_C:
215 debug_matrix = false;
216 debug_keyboard = false;
217 debug_mouse = false;
218 debug_enable = false;
219 command_console_help();
220 print("C> ");
221 command_state = CONSOLE;
222 break;
223 case KC_PAUSE:
224 clear_keyboard();
225 print("\n\nbootloader... ");
226 _delay_ms(1000);
227 bootloader_jump(); // not return
228 break;
229 case KC_D:
230 if (debug_enable) {
231 print("\ndebug: off\n");
232 debug_matrix = false;
233 debug_keyboard = false;
234 debug_mouse = false;
235 debug_enable = false;
236 } else {
237 print("\ndebug: on\n");
238 debug_enable = true;
239 }
240 break;
241 case KC_X: // debug matrix toggle
242 debug_matrix = !debug_matrix;
243 if (debug_matrix) {
244 print("\nmatrix: on\n");
245 debug_enable = true;
246 } else {
247 print("\nmatrix: off\n");
248 }
249 break;
250 case KC_K: // debug keyboard toggle
251 debug_keyboard = !debug_keyboard;
252 if (debug_keyboard) {
253 print("\nkeyboard: on\n");
254 debug_enable = true;
255 } else {
256 print("\nkeyboard: off\n");
257 }
258 break;
259 case KC_M: // debug mouse toggle
260 debug_mouse = !debug_mouse;
261 if (debug_mouse) {
262 print("\nmouse: on\n");
263 debug_enable = true;
264 } else {
265 print("\nmouse: off\n");
266 }
267 break;
268 case KC_V: // print version & information
269 print("\n\t- Version -\n");
270 print("DESC: " STR(DESCRIPTION) "\n");
271 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
272 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
273 "VER: " STR(DEVICE_VER) "\n");
274 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
275 /* build options */
276 print("OPTIONS:"
277 #ifdef PROTOCOL_PJRC
278 " PJRC"
279 #endif
280 #ifdef PROTOCOL_LUFA
281 " LUFA"
282 #endif
283 #ifdef PROTOCOL_VUSB
284 " VUSB"
285 #endif
286 #ifdef BOOTMAGIC_ENABLE
287 " BOOTMAGIC"
288 #endif
289 #ifdef MOUSEKEY_ENABLE
290 " MOUSEKEY"
291 #endif
292 #ifdef EXTRAKEY_ENABLE
293 " EXTRAKEY"
294 #endif
295 #ifdef CONSOLE_ENABLE
296 " CONSOLE"
297 #endif
298 #ifdef COMMAND_ENABLE
299 " COMMAND"
300 #endif
301 #ifdef NKRO_ENABLE
302 " NKRO"
303 #endif
304 #ifdef KEYMAP_SECTION_ENABLE
305 " KEYMAP_SECTION"
306 #endif
307 " " STR(BOOTLOADER_SIZE) "\n");
308
309 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
310 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
311 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
312 break;
313 case KC_S:
314 print("\n\t- Status -\n");
315 print_val_hex8(host_keyboard_leds());
316 print_val_hex8(keyboard_protocol);
317 print_val_hex8(keyboard_idle);
318 print_val_hex8(keyboard_nkro);
319 print_val_hex32(timer_count);
320
321 #ifdef PROTOCOL_PJRC
322 print_val_hex8(UDCON);
323 print_val_hex8(UDIEN);
324 print_val_hex8(UDINT);
325 print_val_hex8(usb_keyboard_leds);
326 print_val_hex8(usb_keyboard_idle_count);
327 #endif
328
329 #ifdef PROTOCOL_PJRC
330 # if USB_COUNT_SOF
331 print_val_hex8(usbSofCount);
332 # endif
333 #endif
334 break;
335 #ifdef NKRO_ENABLE
336 case KC_N:
337 clear_keyboard(); //Prevents stuck keys.
338 keyboard_nkro = !keyboard_nkro;
339 if (keyboard_nkro)
340 print("NKRO: on\n");
341 else
342 print("NKRO: off\n");
343 break;
344 #endif
345 case KC_ESC:
346 case KC_GRV:
347 case KC_0:
348 case KC_F10:
349 switch_default_layer(0);
350 break;
351 case KC_1 ... KC_9:
352 switch_default_layer((code - KC_1) + 1);
353 break;
354 case KC_F1 ... KC_F9:
355 switch_default_layer((code - KC_F1) + 1);
356 break;
357 default:
358 print("?");
359 return false;
360 }
361 return true;
362 }
363
364
365 /***********************************************************
366 * Command console
367 ***********************************************************/
368 static void command_console_help(void)
369 {
370 print("\n\t- Console -\n"
371 "ESC/q: quit\n"
372 #ifdef MOUSEKEY_ENABLE
373 "m: mousekey\n"
374 #endif
375 );
376 }
377
378 static bool command_console(uint8_t code)
379 {
380 switch (code) {
381 case KC_H:
382 case KC_SLASH: /* ? */
383 command_console_help();
384 break;
385 case KC_Q:
386 case KC_ESC:
387 command_state = ONESHOT;
388 return false;
389 #ifdef MOUSEKEY_ENABLE
390 case KC_M:
391 mousekey_console_help();
392 print("M> ");
393 command_state = MOUSEKEY;
394 return true;
395 #endif
396 default:
397 print("?");
398 return false;
399 }
400 print("C> ");
401 return true;
402 }
403
404
405 #ifdef MOUSEKEY_ENABLE
406 /***********************************************************
407 * Mousekey console
408 ***********************************************************/
409 static uint8_t mousekey_param = 0;
410
411 static void mousekey_param_print(void)
412 {
413 print("\n\t- Values -\n");
414 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
415 print("2: interval(ms): "); pdec(mk_interval); print("\n");
416 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
417 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
418 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
419 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
420 }
421
422 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
423 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
424 static void mousekey_param_inc(uint8_t param, uint8_t inc)
425 {
426 switch (param) {
427 case 1:
428 if (mk_delay + inc < UINT8_MAX)
429 mk_delay += inc;
430 else
431 mk_delay = UINT8_MAX;
432 PRINT_SET_VAL(mk_delay);
433 break;
434 case 2:
435 if (mk_interval + inc < UINT8_MAX)
436 mk_interval += inc;
437 else
438 mk_interval = UINT8_MAX;
439 PRINT_SET_VAL(mk_interval);
440 break;
441 case 3:
442 if (mk_max_speed + inc < UINT8_MAX)
443 mk_max_speed += inc;
444 else
445 mk_max_speed = UINT8_MAX;
446 PRINT_SET_VAL(mk_max_speed);
447 break;
448 case 4:
449 if (mk_time_to_max + inc < UINT8_MAX)
450 mk_time_to_max += inc;
451 else
452 mk_time_to_max = UINT8_MAX;
453 PRINT_SET_VAL(mk_time_to_max);
454 break;
455 case 5:
456 if (mk_wheel_max_speed + inc < UINT8_MAX)
457 mk_wheel_max_speed += inc;
458 else
459 mk_wheel_max_speed = UINT8_MAX;
460 PRINT_SET_VAL(mk_wheel_max_speed);
461 break;
462 case 6:
463 if (mk_wheel_time_to_max + inc < UINT8_MAX)
464 mk_wheel_time_to_max += inc;
465 else
466 mk_wheel_time_to_max = UINT8_MAX;
467 PRINT_SET_VAL(mk_wheel_time_to_max);
468 break;
469 }
470 }
471
472 static void mousekey_param_dec(uint8_t param, uint8_t dec)
473 {
474 switch (param) {
475 case 1:
476 if (mk_delay > dec)
477 mk_delay -= dec;
478 else
479 mk_delay = 0;
480 PRINT_SET_VAL(mk_delay);
481 break;
482 case 2:
483 if (mk_interval > dec)
484 mk_interval -= dec;
485 else
486 mk_interval = 0;
487 PRINT_SET_VAL(mk_interval);
488 break;
489 case 3:
490 if (mk_max_speed > dec)
491 mk_max_speed -= dec;
492 else
493 mk_max_speed = 0;
494 PRINT_SET_VAL(mk_max_speed);
495 break;
496 case 4:
497 if (mk_time_to_max > dec)
498 mk_time_to_max -= dec;
499 else
500 mk_time_to_max = 0;
501 PRINT_SET_VAL(mk_time_to_max);
502 break;
503 case 5:
504 if (mk_wheel_max_speed > dec)
505 mk_wheel_max_speed -= dec;
506 else
507 mk_wheel_max_speed = 0;
508 PRINT_SET_VAL(mk_wheel_max_speed);
509 break;
510 case 6:
511 if (mk_wheel_time_to_max > dec)
512 mk_wheel_time_to_max -= dec;
513 else
514 mk_wheel_time_to_max = 0;
515 PRINT_SET_VAL(mk_wheel_time_to_max);
516 break;
517 }
518 }
519
520 static void mousekey_console_help(void)
521 {
522 print("\n\t- Mousekey -\n"
523 "ESC/q: quit\n"
524 "1: delay(*10ms)\n"
525 "2: interval(ms)\n"
526 "3: max_speed\n"
527 "4: time_to_max\n"
528 "5: wheel_max_speed\n"
529 "6: wheel_time_to_max\n"
530 "\n"
531 "p: print values\n"
532 "d: set defaults\n"
533 "up: +1\n"
534 "down: -1\n"
535 "pgup: +10\n"
536 "pgdown: -10\n"
537 "\n"
538 "speed = delta * max_speed * (repeat / time_to_max)\n");
539 xprintf("where delta: cursor=%d, wheel=%d\n"
540 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
541 }
542
543 static bool mousekey_console(uint8_t code)
544 {
545 switch (code) {
546 case KC_H:
547 case KC_SLASH: /* ? */
548 mousekey_console_help();
549 break;
550 case KC_Q:
551 case KC_ESC:
552 if (mousekey_param) {
553 mousekey_param = 0;
554 } else {
555 print("C> ");
556 command_state = CONSOLE;
557 return false;
558 }
559 break;
560 case KC_P:
561 mousekey_param_print();
562 break;
563 case KC_1:
564 case KC_2:
565 case KC_3:
566 case KC_4:
567 case KC_5:
568 case KC_6:
569 mousekey_param = numkey2num(code);
570 break;
571 case KC_UP:
572 mousekey_param_inc(mousekey_param, 1);
573 break;
574 case KC_DOWN:
575 mousekey_param_dec(mousekey_param, 1);
576 break;
577 case KC_PGUP:
578 mousekey_param_inc(mousekey_param, 10);
579 break;
580 case KC_PGDN:
581 mousekey_param_dec(mousekey_param, 10);
582 break;
583 case KC_D:
584 mk_delay = MOUSEKEY_DELAY/10;
585 mk_interval = MOUSEKEY_INTERVAL;
586 mk_max_speed = MOUSEKEY_MAX_SPEED;
587 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
588 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
589 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
590 print("set default\n");
591 break;
592 default:
593 print("?");
594 return false;
595 }
596 if (mousekey_param)
597 xprintf("M%d> ", mousekey_param);
598 else
599 print("M>" );
600 return true;
601 }
602 #endif
603
604
605 /***********************************************************
606 * Utilities
607 ***********************************************************/
608 static uint8_t numkey2num(uint8_t code)
609 {
610 switch (code) {
611 case KC_1: return 1;
612 case KC_2: return 2;
613 case KC_3: return 3;
614 case KC_4: return 4;
615 case KC_5: return 5;
616 case KC_6: return 6;
617 case KC_7: return 7;
618 case KC_8: return 8;
619 case KC_9: return 9;
620 case KC_0: return 0;
621 }
622 return 0;
623 }
624
625 static void switch_default_layer(uint8_t layer)
626 {
627 xprintf("L%d\n", layer);
628 default_layer_set(1UL<<layer);
629 clear_keyboard();
630 }
Imprint / Impressum