]> git.gir.st - tmk_keyboard.git/blob - tmk_core/common/command.c
core: Fix NKRO ifdef
[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 #ifdef NKRO_ENABLE
319 print_val_hex8(keyboard_nkro);
320 #endif
321 print_val_hex32(timer_count);
322
323 #ifdef PROTOCOL_PJRC
324 print_val_hex8(UDCON);
325 print_val_hex8(UDIEN);
326 print_val_hex8(UDINT);
327 print_val_hex8(usb_keyboard_leds);
328 print_val_hex8(usb_keyboard_idle_count);
329 #endif
330
331 #ifdef PROTOCOL_PJRC
332 # if USB_COUNT_SOF
333 print_val_hex8(usbSofCount);
334 # endif
335 #endif
336 break;
337 #ifdef NKRO_ENABLE
338 case KC_N:
339 clear_keyboard(); //Prevents stuck keys.
340 keyboard_nkro = !keyboard_nkro;
341 if (keyboard_nkro)
342 print("NKRO: on\n");
343 else
344 print("NKRO: off\n");
345 break;
346 #endif
347 case KC_ESC:
348 case KC_GRV:
349 case KC_0:
350 case KC_F10:
351 switch_default_layer(0);
352 break;
353 case KC_1 ... KC_9:
354 switch_default_layer((code - KC_1) + 1);
355 break;
356 case KC_F1 ... KC_F9:
357 switch_default_layer((code - KC_F1) + 1);
358 break;
359 default:
360 print("?");
361 return false;
362 }
363 return true;
364 }
365
366
367 /***********************************************************
368 * Command console
369 ***********************************************************/
370 static void command_console_help(void)
371 {
372 print("\n\t- Console -\n"
373 "ESC/q: quit\n"
374 #ifdef MOUSEKEY_ENABLE
375 "m: mousekey\n"
376 #endif
377 );
378 }
379
380 static bool command_console(uint8_t code)
381 {
382 switch (code) {
383 case KC_H:
384 case KC_SLASH: /* ? */
385 command_console_help();
386 break;
387 case KC_Q:
388 case KC_ESC:
389 command_state = ONESHOT;
390 return false;
391 #ifdef MOUSEKEY_ENABLE
392 case KC_M:
393 mousekey_console_help();
394 print("M> ");
395 command_state = MOUSEKEY;
396 return true;
397 #endif
398 default:
399 print("?");
400 return false;
401 }
402 print("C> ");
403 return true;
404 }
405
406
407 #ifdef MOUSEKEY_ENABLE
408 /***********************************************************
409 * Mousekey console
410 ***********************************************************/
411 static uint8_t mousekey_param = 0;
412
413 static void mousekey_param_print(void)
414 {
415 print("\n\t- Values -\n");
416 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
417 print("2: interval(ms): "); pdec(mk_interval); print("\n");
418 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
419 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
420 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
421 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
422 }
423
424 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
425 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
426 static void mousekey_param_inc(uint8_t param, uint8_t inc)
427 {
428 switch (param) {
429 case 1:
430 if (mk_delay + inc < UINT8_MAX)
431 mk_delay += inc;
432 else
433 mk_delay = UINT8_MAX;
434 PRINT_SET_VAL(mk_delay);
435 break;
436 case 2:
437 if (mk_interval + inc < UINT8_MAX)
438 mk_interval += inc;
439 else
440 mk_interval = UINT8_MAX;
441 PRINT_SET_VAL(mk_interval);
442 break;
443 case 3:
444 if (mk_max_speed + inc < UINT8_MAX)
445 mk_max_speed += inc;
446 else
447 mk_max_speed = UINT8_MAX;
448 PRINT_SET_VAL(mk_max_speed);
449 break;
450 case 4:
451 if (mk_time_to_max + inc < UINT8_MAX)
452 mk_time_to_max += inc;
453 else
454 mk_time_to_max = UINT8_MAX;
455 PRINT_SET_VAL(mk_time_to_max);
456 break;
457 case 5:
458 if (mk_wheel_max_speed + inc < UINT8_MAX)
459 mk_wheel_max_speed += inc;
460 else
461 mk_wheel_max_speed = UINT8_MAX;
462 PRINT_SET_VAL(mk_wheel_max_speed);
463 break;
464 case 6:
465 if (mk_wheel_time_to_max + inc < UINT8_MAX)
466 mk_wheel_time_to_max += inc;
467 else
468 mk_wheel_time_to_max = UINT8_MAX;
469 PRINT_SET_VAL(mk_wheel_time_to_max);
470 break;
471 }
472 }
473
474 static void mousekey_param_dec(uint8_t param, uint8_t dec)
475 {
476 switch (param) {
477 case 1:
478 if (mk_delay > dec)
479 mk_delay -= dec;
480 else
481 mk_delay = 0;
482 PRINT_SET_VAL(mk_delay);
483 break;
484 case 2:
485 if (mk_interval > dec)
486 mk_interval -= dec;
487 else
488 mk_interval = 0;
489 PRINT_SET_VAL(mk_interval);
490 break;
491 case 3:
492 if (mk_max_speed > dec)
493 mk_max_speed -= dec;
494 else
495 mk_max_speed = 0;
496 PRINT_SET_VAL(mk_max_speed);
497 break;
498 case 4:
499 if (mk_time_to_max > dec)
500 mk_time_to_max -= dec;
501 else
502 mk_time_to_max = 0;
503 PRINT_SET_VAL(mk_time_to_max);
504 break;
505 case 5:
506 if (mk_wheel_max_speed > dec)
507 mk_wheel_max_speed -= dec;
508 else
509 mk_wheel_max_speed = 0;
510 PRINT_SET_VAL(mk_wheel_max_speed);
511 break;
512 case 6:
513 if (mk_wheel_time_to_max > dec)
514 mk_wheel_time_to_max -= dec;
515 else
516 mk_wheel_time_to_max = 0;
517 PRINT_SET_VAL(mk_wheel_time_to_max);
518 break;
519 }
520 }
521
522 static void mousekey_console_help(void)
523 {
524 print("\n\t- Mousekey -\n"
525 "ESC/q: quit\n"
526 "1: delay(*10ms)\n"
527 "2: interval(ms)\n"
528 "3: max_speed\n"
529 "4: time_to_max\n"
530 "5: wheel_max_speed\n"
531 "6: wheel_time_to_max\n"
532 "\n"
533 "p: print values\n"
534 "d: set defaults\n"
535 "up: +1\n"
536 "down: -1\n"
537 "pgup: +10\n"
538 "pgdown: -10\n"
539 "\n"
540 "speed = delta * max_speed * (repeat / time_to_max)\n");
541 xprintf("where delta: cursor=%d, wheel=%d\n"
542 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
543 }
544
545 static bool mousekey_console(uint8_t code)
546 {
547 switch (code) {
548 case KC_H:
549 case KC_SLASH: /* ? */
550 mousekey_console_help();
551 break;
552 case KC_Q:
553 case KC_ESC:
554 if (mousekey_param) {
555 mousekey_param = 0;
556 } else {
557 print("C> ");
558 command_state = CONSOLE;
559 return false;
560 }
561 break;
562 case KC_P:
563 mousekey_param_print();
564 break;
565 case KC_1:
566 case KC_2:
567 case KC_3:
568 case KC_4:
569 case KC_5:
570 case KC_6:
571 mousekey_param = numkey2num(code);
572 break;
573 case KC_UP:
574 mousekey_param_inc(mousekey_param, 1);
575 break;
576 case KC_DOWN:
577 mousekey_param_dec(mousekey_param, 1);
578 break;
579 case KC_PGUP:
580 mousekey_param_inc(mousekey_param, 10);
581 break;
582 case KC_PGDN:
583 mousekey_param_dec(mousekey_param, 10);
584 break;
585 case KC_D:
586 mk_delay = MOUSEKEY_DELAY/10;
587 mk_interval = MOUSEKEY_INTERVAL;
588 mk_max_speed = MOUSEKEY_MAX_SPEED;
589 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
590 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
591 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
592 print("set default\n");
593 break;
594 default:
595 print("?");
596 return false;
597 }
598 if (mousekey_param)
599 xprintf("M%d> ", mousekey_param);
600 else
601 print("M>" );
602 return true;
603 }
604 #endif
605
606
607 /***********************************************************
608 * Utilities
609 ***********************************************************/
610 static uint8_t numkey2num(uint8_t code)
611 {
612 switch (code) {
613 case KC_1: return 1;
614 case KC_2: return 2;
615 case KC_3: return 3;
616 case KC_4: return 4;
617 case KC_5: return 5;
618 case KC_6: return 6;
619 case KC_7: return 7;
620 case KC_8: return 8;
621 case KC_9: return 9;
622 case KC_0: return 0;
623 }
624 return 0;
625 }
626
627 static void switch_default_layer(uint8_t layer)
628 {
629 xprintf("L%d\n", layer);
630 default_layer_set(1UL<<layer);
631 clear_keyboard();
632 }
Imprint / Impressum