]> git.gir.st - tmk_keyboard.git/blob - tmk_core/common/command.c
core: Fix sleep_led
[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 "wait.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 (void)code;
101 return false;
102 }
103
104 bool command_console_extra(uint8_t code) __attribute__ ((weak));
105 bool command_console_extra(uint8_t code)
106 {
107 (void)code;
108 return false;
109 }
110
111
112 /***********************************************************
113 * Command common
114 ***********************************************************/
115 static void command_common_help(void)
116 {
117 print("\n\t- Magic -\n"
118 "d: debug\n"
119 "x: debug matrix\n"
120 "k: debug keyboard\n"
121 "m: debug mouse\n"
122 "v: version\n"
123 "s: status\n"
124 "c: console mode\n"
125 "0-4: layer0-4(F10-F4)\n"
126 "Paus: bootloader\n"
127
128 #ifdef KEYBOARD_LOCK_ENABLE
129 "Caps: Lock\n"
130 #endif
131
132 #ifdef BOOTMAGIC_ENABLE
133 "e: eeprom\n"
134 #endif
135
136 #ifdef NKRO_ENABLE
137 "n: NKRO\n"
138 #endif
139
140 #ifdef SLEEP_LED_ENABLE
141 "z: sleep LED test\n"
142 #endif
143 );
144 }
145
146 #ifdef BOOTMAGIC_ENABLE
147 static void print_eeconfig(void)
148 {
149 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
150
151 debug_config_t dc;
152 dc.raw = eeconfig_read_debug();
153 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
154 print(".enable: "); print_dec(dc.enable); print("\n");
155 print(".matrix: "); print_dec(dc.matrix); print("\n");
156 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
157 print(".mouse: "); print_dec(dc.mouse); print("\n");
158
159 keymap_config_t kc;
160 kc.raw = eeconfig_read_keymap();
161 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
162 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
163 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
164 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
165 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
166 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
167 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
168 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
169 print(".nkro: "); print_dec(kc.nkro); print("\n");
170
171 #ifdef BACKLIGHT_ENABLE
172 backlight_config_t bc;
173 bc.raw = eeconfig_read_backlight();
174 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
175 print(".enable: "); print_dec(bc.enable); print("\n");
176 print(".level: "); print_dec(bc.level); print("\n");
177 #endif
178 }
179 #endif
180
181 static bool command_common(uint8_t code)
182 {
183 #ifdef KEYBOARD_LOCK_ENABLE
184 static host_driver_t *host_driver = 0;
185 #endif
186 #ifdef SLEEP_LED_ENABLE
187 static bool sleep_led_test = false;
188 #endif
189 switch (code) {
190 #ifdef SLEEP_LED_ENABLE
191 case KC_Z:
192 // test breathing sleep LED
193 print("Sleep LED test\n");
194 if (sleep_led_test) {
195 sleep_led_disable();
196 led_set(host_keyboard_leds());
197 } else {
198 sleep_led_enable();
199 }
200 sleep_led_test = !sleep_led_test;
201 break;
202 #endif
203 #ifdef BOOTMAGIC_ENABLE
204 case KC_E:
205 print("eeconfig:\n");
206 print_eeconfig();
207 break;
208 #endif
209 #ifdef KEYBOARD_LOCK_ENABLE
210 case KC_CAPSLOCK:
211 if (host_get_driver()) {
212 host_driver = host_get_driver();
213 clear_keyboard();
214 host_set_driver(0);
215 print("Locked.\n");
216 } else {
217 host_set_driver(host_driver);
218 print("Unlocked.\n");
219 }
220 break;
221 #endif
222 case KC_H:
223 case KC_SLASH: /* ? */
224 command_common_help();
225 break;
226 case KC_C:
227 debug_matrix = false;
228 debug_keyboard = false;
229 debug_mouse = false;
230 debug_enable = false;
231 command_console_help();
232 print("C> ");
233 command_state = CONSOLE;
234 break;
235 case KC_PAUSE:
236 clear_keyboard();
237 print("\n\nbootloader... ");
238 wait_ms(1000);
239 bootloader_jump(); // not return
240 break;
241 case KC_D:
242 if (debug_enable) {
243 print("\ndebug: off\n");
244 debug_matrix = false;
245 debug_keyboard = false;
246 debug_mouse = false;
247 debug_enable = false;
248 } else {
249 print("\ndebug: on\n");
250 debug_enable = true;
251 }
252 break;
253 case KC_X: // debug matrix toggle
254 debug_matrix = !debug_matrix;
255 if (debug_matrix) {
256 print("\nmatrix: on\n");
257 debug_enable = true;
258 } else {
259 print("\nmatrix: off\n");
260 }
261 break;
262 case KC_K: // debug keyboard toggle
263 debug_keyboard = !debug_keyboard;
264 if (debug_keyboard) {
265 print("\nkeyboard: on\n");
266 debug_enable = true;
267 } else {
268 print("\nkeyboard: off\n");
269 }
270 break;
271 case KC_M: // debug mouse toggle
272 debug_mouse = !debug_mouse;
273 if (debug_mouse) {
274 print("\nmouse: on\n");
275 debug_enable = true;
276 } else {
277 print("\nmouse: off\n");
278 }
279 break;
280 case KC_V: // print version & information
281 print("\n\t- Version -\n");
282 print("DESC: " STR(DESCRIPTION) "\n");
283 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
284 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
285 "VER: " STR(DEVICE_VER) "\n");
286 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
287 /* build options */
288 print("OPTIONS:"
289 #ifdef PROTOCOL_PJRC
290 " PJRC"
291 #endif
292 #ifdef PROTOCOL_LUFA
293 " LUFA"
294 #endif
295 #ifdef PROTOCOL_VUSB
296 " VUSB"
297 #endif
298 #ifdef PROTOCOL_CHIBIOS
299 " CHIBIOS"
300 #endif
301 #ifdef BOOTMAGIC_ENABLE
302 " BOOTMAGIC"
303 #endif
304 #ifdef MOUSEKEY_ENABLE
305 " MOUSEKEY"
306 #endif
307 #ifdef EXTRAKEY_ENABLE
308 " EXTRAKEY"
309 #endif
310 #ifdef CONSOLE_ENABLE
311 " CONSOLE"
312 #endif
313 #ifdef COMMAND_ENABLE
314 " COMMAND"
315 #endif
316 #ifdef NKRO_ENABLE
317 " NKRO"
318 #endif
319 #ifdef KEYMAP_SECTION_ENABLE
320 " KEYMAP_SECTION"
321 #endif
322 " " STR(BOOTLOADER_SIZE) "\n");
323
324 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
325 #if defined(__AVR__)
326 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
327 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
328 #elif defined(__arm__)
329 // TODO
330 );
331 #endif
332 break;
333 case KC_S:
334 print("\n\t- Status -\n");
335 print_val_hex8(host_keyboard_leds());
336 print_val_hex8(keyboard_protocol);
337 print_val_hex8(keyboard_idle);
338 #ifdef NKRO_ENABLE
339 print_val_hex8(keyboard_nkro);
340 #endif
341 print_val_hex32(timer_read32());
342
343 #ifdef PROTOCOL_PJRC
344 print_val_hex8(UDCON);
345 print_val_hex8(UDIEN);
346 print_val_hex8(UDINT);
347 print_val_hex8(usb_keyboard_leds);
348 print_val_hex8(usb_keyboard_idle_count);
349 #endif
350
351 #ifdef PROTOCOL_PJRC
352 # if USB_COUNT_SOF
353 print_val_hex8(usbSofCount);
354 # endif
355 #endif
356 break;
357 #ifdef NKRO_ENABLE
358 case KC_N:
359 clear_keyboard(); //Prevents stuck keys.
360 keyboard_nkro = !keyboard_nkro;
361 if (keyboard_nkro) {
362 print("NKRO: on\n");
363 } else {
364 print("NKRO: off\n");
365 }
366 break;
367 #endif
368 case KC_ESC:
369 case KC_GRV:
370 case KC_0:
371 case KC_F10:
372 switch_default_layer(0);
373 break;
374 case KC_1 ... KC_9:
375 switch_default_layer((code - KC_1) + 1);
376 break;
377 case KC_F1 ... KC_F9:
378 switch_default_layer((code - KC_F1) + 1);
379 break;
380 default:
381 print("?");
382 return false;
383 }
384 return true;
385 }
386
387
388 /***********************************************************
389 * Command console
390 ***********************************************************/
391 static void command_console_help(void)
392 {
393 print("\n\t- Console -\n"
394 "ESC/q: quit\n"
395 #ifdef MOUSEKEY_ENABLE
396 "m: mousekey\n"
397 #endif
398 );
399 }
400
401 static bool command_console(uint8_t code)
402 {
403 switch (code) {
404 case KC_H:
405 case KC_SLASH: /* ? */
406 command_console_help();
407 break;
408 case KC_Q:
409 case KC_ESC:
410 command_state = ONESHOT;
411 return false;
412 #ifdef MOUSEKEY_ENABLE
413 case KC_M:
414 mousekey_console_help();
415 print("M> ");
416 command_state = MOUSEKEY;
417 return true;
418 #endif
419 default:
420 print("?");
421 return false;
422 }
423 print("C> ");
424 return true;
425 }
426
427
428 #ifdef MOUSEKEY_ENABLE
429 /***********************************************************
430 * Mousekey console
431 ***********************************************************/
432 static uint8_t mousekey_param = 0;
433
434 static void mousekey_param_print(void)
435 {
436 print("\n\t- Values -\n");
437 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
438 print("2: interval(ms): "); pdec(mk_interval); print("\n");
439 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
440 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
441 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
442 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
443 }
444
445 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
446 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
447 static void mousekey_param_inc(uint8_t param, uint8_t inc)
448 {
449 switch (param) {
450 case 1:
451 if (mk_delay + inc < UINT8_MAX)
452 mk_delay += inc;
453 else
454 mk_delay = UINT8_MAX;
455 PRINT_SET_VAL(mk_delay);
456 break;
457 case 2:
458 if (mk_interval + inc < UINT8_MAX)
459 mk_interval += inc;
460 else
461 mk_interval = UINT8_MAX;
462 PRINT_SET_VAL(mk_interval);
463 break;
464 case 3:
465 if (mk_max_speed + inc < UINT8_MAX)
466 mk_max_speed += inc;
467 else
468 mk_max_speed = UINT8_MAX;
469 PRINT_SET_VAL(mk_max_speed);
470 break;
471 case 4:
472 if (mk_time_to_max + inc < UINT8_MAX)
473 mk_time_to_max += inc;
474 else
475 mk_time_to_max = UINT8_MAX;
476 PRINT_SET_VAL(mk_time_to_max);
477 break;
478 case 5:
479 if (mk_wheel_max_speed + inc < UINT8_MAX)
480 mk_wheel_max_speed += inc;
481 else
482 mk_wheel_max_speed = UINT8_MAX;
483 PRINT_SET_VAL(mk_wheel_max_speed);
484 break;
485 case 6:
486 if (mk_wheel_time_to_max + inc < UINT8_MAX)
487 mk_wheel_time_to_max += inc;
488 else
489 mk_wheel_time_to_max = UINT8_MAX;
490 PRINT_SET_VAL(mk_wheel_time_to_max);
491 break;
492 }
493 }
494
495 static void mousekey_param_dec(uint8_t param, uint8_t dec)
496 {
497 switch (param) {
498 case 1:
499 if (mk_delay > dec)
500 mk_delay -= dec;
501 else
502 mk_delay = 0;
503 PRINT_SET_VAL(mk_delay);
504 break;
505 case 2:
506 if (mk_interval > dec)
507 mk_interval -= dec;
508 else
509 mk_interval = 0;
510 PRINT_SET_VAL(mk_interval);
511 break;
512 case 3:
513 if (mk_max_speed > dec)
514 mk_max_speed -= dec;
515 else
516 mk_max_speed = 0;
517 PRINT_SET_VAL(mk_max_speed);
518 break;
519 case 4:
520 if (mk_time_to_max > dec)
521 mk_time_to_max -= dec;
522 else
523 mk_time_to_max = 0;
524 PRINT_SET_VAL(mk_time_to_max);
525 break;
526 case 5:
527 if (mk_wheel_max_speed > dec)
528 mk_wheel_max_speed -= dec;
529 else
530 mk_wheel_max_speed = 0;
531 PRINT_SET_VAL(mk_wheel_max_speed);
532 break;
533 case 6:
534 if (mk_wheel_time_to_max > dec)
535 mk_wheel_time_to_max -= dec;
536 else
537 mk_wheel_time_to_max = 0;
538 PRINT_SET_VAL(mk_wheel_time_to_max);
539 break;
540 }
541 }
542
543 static void mousekey_console_help(void)
544 {
545 print("\n\t- Mousekey -\n"
546 "ESC/q: quit\n"
547 "1: delay(*10ms)\n"
548 "2: interval(ms)\n"
549 "3: max_speed\n"
550 "4: time_to_max\n"
551 "5: wheel_max_speed\n"
552 "6: wheel_time_to_max\n"
553 "\n"
554 "p: print values\n"
555 "d: set defaults\n"
556 "up: +1\n"
557 "down: -1\n"
558 "pgup: +10\n"
559 "pgdown: -10\n"
560 "\n"
561 "speed = delta * max_speed * (repeat / time_to_max)\n");
562 xprintf("where delta: cursor=%d, wheel=%d\n"
563 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
564 }
565
566 static bool mousekey_console(uint8_t code)
567 {
568 switch (code) {
569 case KC_H:
570 case KC_SLASH: /* ? */
571 mousekey_console_help();
572 break;
573 case KC_Q:
574 case KC_ESC:
575 if (mousekey_param) {
576 mousekey_param = 0;
577 } else {
578 print("C> ");
579 command_state = CONSOLE;
580 return false;
581 }
582 break;
583 case KC_P:
584 mousekey_param_print();
585 break;
586 case KC_1:
587 case KC_2:
588 case KC_3:
589 case KC_4:
590 case KC_5:
591 case KC_6:
592 mousekey_param = numkey2num(code);
593 break;
594 case KC_UP:
595 mousekey_param_inc(mousekey_param, 1);
596 break;
597 case KC_DOWN:
598 mousekey_param_dec(mousekey_param, 1);
599 break;
600 case KC_PGUP:
601 mousekey_param_inc(mousekey_param, 10);
602 break;
603 case KC_PGDN:
604 mousekey_param_dec(mousekey_param, 10);
605 break;
606 case KC_D:
607 mk_delay = MOUSEKEY_DELAY/10;
608 mk_interval = MOUSEKEY_INTERVAL;
609 mk_max_speed = MOUSEKEY_MAX_SPEED;
610 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
611 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
612 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
613 print("set default\n");
614 break;
615 default:
616 print("?");
617 return false;
618 }
619 if (mousekey_param) {
620 xprintf("M%d> ", mousekey_param);
621 } else {
622 print("M>" );
623 }
624 return true;
625 }
626 #endif
627
628
629 /***********************************************************
630 * Utilities
631 ***********************************************************/
632 static uint8_t numkey2num(uint8_t code)
633 {
634 switch (code) {
635 case KC_1: return 1;
636 case KC_2: return 2;
637 case KC_3: return 3;
638 case KC_4: return 4;
639 case KC_5: return 5;
640 case KC_6: return 6;
641 case KC_7: return 7;
642 case KC_8: return 8;
643 case KC_9: return 9;
644 case KC_0: return 0;
645 }
646 return 0;
647 }
648
649 static void switch_default_layer(uint8_t layer)
650 {
651 xprintf("L%d\n", layer);
652 default_layer_set(1UL<<layer);
653 clear_keyboard();
654 }
Imprint / Impressum