2 Copyright 2011 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
28 #include "bootloader.h"
29 #include "action_layer.h"
30 #include "action_util.h"
32 #include "sleep_led.h"
38 #ifdef BACKLIGHT_ENABLE
39 # include "backlight.h"
42 #ifdef MOUSEKEY_ENABLE
43 # include "mousekey.h"
52 #endif /* AUDIO_ENABLE */
54 static bool command_common(uint8_t code
);
55 static void command_common_help(void);
56 static void print_version(void);
57 static void print_status(void);
58 static bool command_console(uint8_t code
);
59 static void command_console_help(void);
60 #ifdef MOUSEKEY_ENABLE
61 static bool mousekey_console(uint8_t code
);
62 static void mousekey_console_help(void);
65 static void switch_default_layer(uint8_t layer
);
67 command_state_t command_state
= ONESHOT
;
69 bool command_proc(uint8_t code
) {
70 switch (command_state
) {
72 if (!IS_COMMAND()) return false;
73 return (command_extra(code
) || command_common(code
));
77 return (command_extra(code
) || command_common(code
));
79 return (command_console_extra(code
) || command_console(code
));
81 #ifdef MOUSEKEY_ENABLE
83 mousekey_console(code
);
87 command_state
= ONESHOT
;
93 /* TODO: Refactoring is needed. */
94 /* This allows to define extra commands. return false when not processed. */
95 bool command_extra(uint8_t code
) __attribute__((weak
));
96 bool command_extra(uint8_t code
) {
101 bool command_console_extra(uint8_t code
) __attribute__((weak
));
102 bool command_console_extra(uint8_t code
) {
107 /***********************************************************
109 ***********************************************************/
110 static void command_common_help(void) {
111 print("\n\t- Magic -\n" STR(MAGIC_KEY_DEBUG
) ": Debug Message Toggle\n" STR(MAGIC_KEY_DEBUG_MATRIX
) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" STR(MAGIC_KEY_DEBUG_KBD
) ": Keyboard Debug Toggle - Show keypress report\n" STR(MAGIC_KEY_DEBUG_MOUSE
) ": Debug Mouse Toggle\n" STR(MAGIC_KEY_VERSION
) ": Version\n" STR(MAGIC_KEY_STATUS
) ": Status\n" STR(MAGIC_KEY_CONSOLE
) ": Activate Console Mode\n"
113 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
114 STR(MAGIC_KEY_LAYER0
) ": Switch to Layer 0\n" STR(MAGIC_KEY_LAYER1
) ": Switch to Layer 1\n" STR(MAGIC_KEY_LAYER2
) ": Switch to Layer 2\n" STR(MAGIC_KEY_LAYER3
) ": Switch to Layer 3\n" STR(MAGIC_KEY_LAYER4
) ": Switch to Layer 4\n" STR(MAGIC_KEY_LAYER5
) ": Switch to Layer 5\n" STR(MAGIC_KEY_LAYER6
) ": Switch to Layer 6\n" STR(MAGIC_KEY_LAYER7
) ": Switch to Layer 7\n" STR(MAGIC_KEY_LAYER8
) ": Switch to Layer 8\n" STR(MAGIC_KEY_LAYER9
) ": Switch to Layer 9\n"
117 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
118 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
121 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
122 "0-9: Switch to Layer 0-9\n"
125 STR(MAGIC_KEY_LAYER0_ALT
) ": Switch to Layer 0 (alternate)\n"
127 STR(MAGIC_KEY_BOOTLOADER
) ": Jump to Bootloader\n" STR(MAGIC_KEY_BOOTLOADER_ALT
) ": Jump to Bootloader (alternate)\n"
129 #ifdef KEYBOARD_LOCK_ENABLE
130 STR(MAGIC_KEY_LOCK
) ": Lock Keyboard\n"
133 STR(MAGIC_KEY_EEPROM
) ": Print EEPROM Settings\n" STR(MAGIC_KEY_EEPROM_CLEAR
) ": Clear EEPROM\n"
136 STR(MAGIC_KEY_NKRO
) ": NKRO Toggle\n"
139 #ifdef SLEEP_LED_ENABLE
140 STR(MAGIC_KEY_SLEEP_LED
) ": Sleep LED Test\n"
145 static void print_version(void) {
146 // print version & information
147 print("\n\t- Version -\n");
148 print("DESC: " STR(DESCRIPTION
) "\n");
149 print("VID: " STR(VENDOR_ID
) "(" STR(MANUFACTURER
) ") "
150 "PID: " STR(PRODUCT_ID
) "(" STR(PRODUCT
) ") "
151 "VER: " STR(DEVICE_VER
) "\n");
153 print("BUILD: (" __DATE__
")\n");
155 print("BUILD: " STR(QMK_VERSION
) " (" __TIME__
" " __DATE__
")\n");
156 # ifdef PROTOCOL_CHIBIOS
157 print("CHIBIOS: " STR(CHIBIOS_VERSION
) ", CONTRIB: " STR(CHIBIOS_CONTRIB_VERSION
) "\n");
170 #ifdef BOOTMAGIC_ENABLE
173 #ifdef MOUSEKEY_ENABLE
176 #ifdef EXTRAKEY_ENABLE
179 #ifdef CONSOLE_ENABLE
182 #ifdef COMMAND_ENABLE
188 #ifdef LINK_TIME_OPTIMIZATION_ENABLE
192 " " STR(BOOTLOADER_SIZE
) "\n");
194 print("GCC: " STR(__GNUC__
) "." STR(__GNUC_MINOR__
) "." STR(__GNUC_PATCHLEVEL__
)
196 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
" AVR_ARCH: avr" STR(__AVR_ARCH__
)
203 static void print_status(void) {
204 print("\n\t- Status -\n");
206 print_val_hex8(host_keyboard_leds());
207 #ifndef PROTOCOL_VUSB
208 // these aren't set on the V-USB protocol, so we just ignore them for now
209 print_val_hex8(keyboard_protocol
);
210 print_val_hex8(keyboard_idle
);
213 print_val_hex8(keymap_config
.nkro
);
215 print_val_hex32(timer_read32());
219 static void print_eeconfig(void) {
220 // Print these variables if NO_PRINT or USER_PRINT are not defined.
221 #if !defined(NO_PRINT) && !defined(USER_PRINT)
223 print("default_layer: ");
224 print_dec(eeconfig_read_default_layer());
228 dc
.raw
= eeconfig_read_debug();
229 print("debug_config.raw: ");
233 print_dec(dc
.enable
);
236 print_dec(dc
.matrix
);
238 print(".keyboard: ");
239 print_dec(dc
.keyboard
);
246 kc
.raw
= eeconfig_read_keymap();
247 print("keymap_config.raw: ");
250 print(".swap_control_capslock: ");
251 print_dec(kc
.swap_control_capslock
);
253 print(".capslock_to_control: ");
254 print_dec(kc
.capslock_to_control
);
256 print(".swap_lctl_lgui: ");
257 print_dec(kc
.swap_lctl_lgui
);
259 print(".swap_rctl_rgui: ");
260 print_dec(kc
.swap_rctl_rgui
);
262 print(".swap_lalt_lgui: ");
263 print_dec(kc
.swap_lalt_lgui
);
265 print(".swap_ralt_rgui: ");
266 print_dec(kc
.swap_ralt_rgui
);
269 print_dec(kc
.no_gui
);
271 print(".swap_grave_esc: ");
272 print_dec(kc
.swap_grave_esc
);
274 print(".swap_backslash_backspace: ");
275 print_dec(kc
.swap_backslash_backspace
);
281 # ifdef BACKLIGHT_ENABLE
282 backlight_config_t bc
;
283 bc
.raw
= eeconfig_read_backlight();
284 print("backlight_config.raw: ");
288 print_dec(bc
.enable
);
293 # endif /* BACKLIGHT_ENABLE */
295 #endif /* !NO_PRINT */
298 static bool command_common(uint8_t code
) {
299 #ifdef KEYBOARD_LOCK_ENABLE
300 static host_driver_t
*host_driver
= 0;
304 #ifdef SLEEP_LED_ENABLE
306 // test breathing sleep LED
307 case MAGIC_KC(MAGIC_KEY_SLEEP_LED
):
308 print("Sleep LED Test\n");
310 led_set(host_keyboard_leds());
314 // print stored eeprom config
315 case MAGIC_KC(MAGIC_KEY_EEPROM
):
316 print("eeconfig:\n");
321 case MAGIC_KC(MAGIC_KEY_EEPROM_CLEAR
):
322 print("Clearing EEPROM\n");
326 #ifdef KEYBOARD_LOCK_ENABLE
328 // lock/unlock keyboard
329 case MAGIC_KC(MAGIC_KEY_LOCK
):
330 if (host_get_driver()) {
331 host_driver
= host_get_driver();
336 host_set_driver(host_driver
);
337 print("Unlocked.\n");
343 case MAGIC_KC(MAGIC_KEY_HELP
):
344 case MAGIC_KC(MAGIC_KEY_HELP_ALT
):
345 command_common_help();
349 case MAGIC_KC(MAGIC_KEY_CONSOLE
):
350 debug_matrix
= false;
351 debug_keyboard
= false;
353 debug_enable
= false;
354 command_console_help();
356 command_state
= CONSOLE
;
359 // jump to bootloader
360 case MAGIC_KC(MAGIC_KEY_BOOTLOADER
):
361 case MAGIC_KC(MAGIC_KEY_BOOTLOADER_ALT
):
362 clear_keyboard(); // clear to prevent stuck keys
363 print("\n\nJumping to bootloader... ");
370 bootloader_jump(); // not return
374 case MAGIC_KC(MAGIC_KEY_DEBUG
):
375 debug_enable
= !debug_enable
;
377 print("\ndebug: on\n");
379 print("\ndebug: off\n");
380 debug_matrix
= false;
381 debug_keyboard
= false;
386 // debug matrix toggle
387 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX
):
388 debug_matrix
= !debug_matrix
;
390 print("\nmatrix: on\n");
393 print("\nmatrix: off\n");
397 // debug keyboard toggle
398 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD
):
399 debug_keyboard
= !debug_keyboard
;
400 if (debug_keyboard
) {
401 print("\nkeyboard: on\n");
404 print("\nkeyboard: off\n");
408 // debug mouse toggle
409 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE
):
410 debug_mouse
= !debug_mouse
;
412 print("\nmouse: on\n");
415 print("\nmouse: off\n");
420 case MAGIC_KC(MAGIC_KEY_VERSION
):
425 case MAGIC_KC(MAGIC_KEY_STATUS
):
432 case MAGIC_KC(MAGIC_KEY_NKRO
):
433 clear_keyboard(); // clear to prevent stuck keys
434 keymap_config
.nkro
= !keymap_config
.nkro
;
435 if (keymap_config
.nkro
) {
438 print("NKRO: off\n");
445 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT
):
446 switch_default_layer(0);
449 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
451 case MAGIC_KC(MAGIC_KEY_LAYER0
):
452 switch_default_layer(0);
455 case MAGIC_KC(MAGIC_KEY_LAYER1
):
456 switch_default_layer(1);
459 case MAGIC_KC(MAGIC_KEY_LAYER2
):
460 switch_default_layer(2);
463 case MAGIC_KC(MAGIC_KEY_LAYER3
):
464 switch_default_layer(3);
467 case MAGIC_KC(MAGIC_KEY_LAYER4
):
468 switch_default_layer(4);
471 case MAGIC_KC(MAGIC_KEY_LAYER5
):
472 switch_default_layer(5);
475 case MAGIC_KC(MAGIC_KEY_LAYER6
):
476 switch_default_layer(6);
479 case MAGIC_KC(MAGIC_KEY_LAYER7
):
480 switch_default_layer(7);
483 case MAGIC_KC(MAGIC_KEY_LAYER8
):
484 switch_default_layer(8);
487 case MAGIC_KC(MAGIC_KEY_LAYER9
):
488 switch_default_layer(9);
492 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
494 case KC_F1
... KC_F9
:
495 switch_default_layer((code
- KC_F1
) + 1);
498 switch_default_layer(0);
502 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
505 switch_default_layer((code
- KC_1
) + 1);
508 switch_default_layer(0);
519 /***********************************************************
521 ***********************************************************/
522 static void command_console_help(void) {
523 print("\n\t- Console -\n"
525 #ifdef MOUSEKEY_ENABLE
531 static bool command_console(uint8_t code
) {
534 case KC_SLASH
: /* ? */
535 command_console_help();
539 command_state
= ONESHOT
;
541 #ifdef MOUSEKEY_ENABLE
543 mousekey_console_help();
545 command_state
= MOUSEKEY
;
556 #ifdef MOUSEKEY_ENABLE
557 /***********************************************************
559 ***********************************************************/
560 static uint8_t mousekey_param
= 0;
562 static void mousekey_param_print(void) {
563 // Print these variables if NO_PRINT or USER_PRINT are not defined.
564 # if !defined(NO_PRINT) && !defined(USER_PRINT)
565 print("\n\t- Values -\n");
566 print("1: delay(*10ms): ");
569 print("2: interval(ms): ");
572 print("3: max_speed: ");
575 print("4: time_to_max: ");
576 pdec(mk_time_to_max
);
578 print("5: wheel_max_speed: ");
579 pdec(mk_wheel_max_speed
);
581 print("6: wheel_time_to_max: ");
582 pdec(mk_wheel_time_to_max
);
584 # endif /* !NO_PRINT */
587 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
588 # define PRINT_SET_VAL(v) xprintf(# v " = %d\n", (v))
589 static void mousekey_param_inc(uint8_t param
, uint8_t inc
) {
592 if (mk_delay
+ inc
< UINT8_MAX
)
595 mk_delay
= UINT8_MAX
;
596 PRINT_SET_VAL(mk_delay
);
599 if (mk_interval
+ inc
< UINT8_MAX
)
602 mk_interval
= UINT8_MAX
;
603 PRINT_SET_VAL(mk_interval
);
606 if (mk_max_speed
+ inc
< UINT8_MAX
)
609 mk_max_speed
= UINT8_MAX
;
610 PRINT_SET_VAL(mk_max_speed
);
613 if (mk_time_to_max
+ inc
< UINT8_MAX
)
614 mk_time_to_max
+= inc
;
616 mk_time_to_max
= UINT8_MAX
;
617 PRINT_SET_VAL(mk_time_to_max
);
620 if (mk_wheel_max_speed
+ inc
< UINT8_MAX
)
621 mk_wheel_max_speed
+= inc
;
623 mk_wheel_max_speed
= UINT8_MAX
;
624 PRINT_SET_VAL(mk_wheel_max_speed
);
627 if (mk_wheel_time_to_max
+ inc
< UINT8_MAX
)
628 mk_wheel_time_to_max
+= inc
;
630 mk_wheel_time_to_max
= UINT8_MAX
;
631 PRINT_SET_VAL(mk_wheel_time_to_max
);
636 static void mousekey_param_dec(uint8_t param
, uint8_t dec
) {
643 PRINT_SET_VAL(mk_delay
);
646 if (mk_interval
> dec
)
650 PRINT_SET_VAL(mk_interval
);
653 if (mk_max_speed
> dec
)
657 PRINT_SET_VAL(mk_max_speed
);
660 if (mk_time_to_max
> dec
)
661 mk_time_to_max
-= dec
;
664 PRINT_SET_VAL(mk_time_to_max
);
667 if (mk_wheel_max_speed
> dec
)
668 mk_wheel_max_speed
-= dec
;
670 mk_wheel_max_speed
= 0;
671 PRINT_SET_VAL(mk_wheel_max_speed
);
674 if (mk_wheel_time_to_max
> dec
)
675 mk_wheel_time_to_max
-= dec
;
677 mk_wheel_time_to_max
= 0;
678 PRINT_SET_VAL(mk_wheel_time_to_max
);
683 static void mousekey_console_help(void) {
684 print("\n\t- Mousekey -\n"
690 "5: wheel_max_speed\n"
691 "6: wheel_time_to_max\n"
700 "speed = delta * max_speed * (repeat / time_to_max)\n");
701 xprintf("where delta: cursor=%d, wheel=%d\n"
702 "See http://en.wikipedia.org/wiki/Mouse_keys\n",
703 MOUSEKEY_MOVE_DELTA
, MOUSEKEY_WHEEL_DELTA
);
706 static bool mousekey_console(uint8_t code
) {
709 case KC_SLASH
: /* ? */
710 mousekey_console_help();
714 if (mousekey_param
) {
718 command_state
= CONSOLE
;
723 mousekey_param_print();
731 mousekey_param
= numkey2num(code
);
734 mousekey_param_inc(mousekey_param
, 1);
737 mousekey_param_dec(mousekey_param
, 1);
740 mousekey_param_inc(mousekey_param
, 10);
743 mousekey_param_dec(mousekey_param
, 10);
746 mk_delay
= MOUSEKEY_DELAY
/ 10;
747 mk_interval
= MOUSEKEY_INTERVAL
;
748 mk_max_speed
= MOUSEKEY_MAX_SPEED
;
749 mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
750 mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
751 mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
752 print("set default\n");
758 if (mousekey_param
) {
759 xprintf("M%d> ", mousekey_param
);
767 /***********************************************************
769 ***********************************************************/
770 uint8_t numkey2num(uint8_t code
) {
796 static void switch_default_layer(uint8_t layer
) {
797 xprintf("L%d\n", layer
);
798 default_layer_set(1UL << layer
);