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"
35 #include "backlight.h"
38 #ifdef MOUSEKEY_ENABLE
43 #include "usb_keyboard.h"
44 #ifdef EXTRAKEY_ENABLE
45 #include "usb_extra.h"
55 #endif /* AUDIO_ENABLE */
58 static bool command_common(uint8_t code);
59 static void command_common_help(void);
60 static void print_version(void);
61 static void print_status(void);
62 static bool command_console(uint8_t code);
63 static void command_console_help(void);
64 #ifdef MOUSEKEY_ENABLE
65 static bool mousekey_console(uint8_t code);
66 static void mousekey_console_help(void);
69 static void switch_default_layer(uint8_t layer);
72 command_state_t command_state = ONESHOT;
75 bool command_proc(uint8_t code)
77 switch (command_state) {
81 return (command_extra(code) || command_common(code));
85 return (command_extra(code) || command_common(code));
87 return (command_console_extra(code) || command_console(code));
89 #ifdef MOUSEKEY_ENABLE
91 mousekey_console(code);
95 command_state = ONESHOT;
101 /* TODO: Refactoring is needed. */
102 /* This allows to define extra commands. return false when not processed. */
103 bool command_extra(uint8_t code) __attribute__ ((weak));
104 bool command_extra(uint8_t code)
110 bool command_console_extra(uint8_t code) __attribute__ ((weak));
111 bool command_console_extra(uint8_t code)
118 /***********************************************************
120 ***********************************************************/
121 static void command_common_help(void)
123 print( "\n\t- Magic -\n"
124 STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n"
125 STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
126 STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n"
127 STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n"
128 STR(MAGIC_KEY_VERSION ) ": Version\n"
129 STR(MAGIC_KEY_STATUS ) ": Status\n"
130 STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n"
132 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
133 STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n"
134 STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n"
135 STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n"
136 STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n"
137 STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n"
138 STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n"
139 STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n"
140 STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n"
141 STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n"
142 STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n"
145 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
146 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
149 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
150 "0-9: Switch to Layer 0-9\n"
153 STR(MAGIC_KEY_LAYER0_ALT1 ) ": Switch to Layer 0 (alternate key 1)\n"
154 STR(MAGIC_KEY_LAYER0_ALT2 ) ": Switch to Layer 0 (alternate key 2)\n"
155 STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader (Reset)\n"
157 #ifdef KEYBOARD_LOCK_ENABLE
158 STR(MAGIC_KEY_LOCK ) ": Lock\n"
161 #ifdef BOOTMAGIC_ENABLE
162 STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n"
166 STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n"
169 #ifdef SLEEP_LED_ENABLE
170 STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n"
175 static void print_version(void)
177 // print version & information
178 print("\n\t- Version -\n");
179 print("DESC: " STR(DESCRIPTION) "\n");
180 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
181 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
182 "VER: " STR(DEVICE_VER) "\n");
183 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
197 #ifdef BOOTMAGIC_ENABLE
200 #ifdef MOUSEKEY_ENABLE
203 #ifdef EXTRAKEY_ENABLE
206 #ifdef CONSOLE_ENABLE
209 #ifdef COMMAND_ENABLE
215 #ifdef KEYMAP_SECTION_ENABLE
219 " " STR(BOOTLOADER_SIZE) "\n");
221 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
223 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
224 " AVR_ARCH: avr" STR(__AVR_ARCH__)
231 static void print_status(void)
234 print("\n\t- Status -\n");
236 print_val_hex8(host_keyboard_leds());
237 print_val_hex8(keyboard_protocol);
238 print_val_hex8(keyboard_idle);
240 print_val_hex8(keyboard_nkro);
242 print_val_hex32(timer_read32());
245 print_val_hex8(UDCON);
246 print_val_hex8(UDIEN);
247 print_val_hex8(UDINT);
248 print_val_hex8(usb_keyboard_leds);
249 print_val_hex8(usb_keyboard_idle_count);
254 print_val_hex8(usbSofCount);
260 #ifdef BOOTMAGIC_ENABLE
261 static void print_eeconfig(void)
264 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
267 dc.raw = eeconfig_read_debug();
268 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
269 print(".enable: "); print_dec(dc.enable); print("\n");
270 print(".matrix: "); print_dec(dc.matrix); print("\n");
271 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
272 print(".mouse: "); print_dec(dc.mouse); print("\n");
275 kc.raw = eeconfig_read_keymap();
276 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
277 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
278 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
279 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
280 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
281 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
282 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
283 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
284 print(".nkro: "); print_dec(kc.nkro); print("\n");
286 #ifdef BACKLIGHT_ENABLE
287 backlight_config_t bc;
288 bc.raw = eeconfig_read_backlight();
289 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
290 print(".enable: "); print_dec(bc.enable); print("\n");
291 print(".level: "); print_dec(bc.level); print("\n");
292 #endif /* BACKLIGHT_ENABLE */
294 #endif /* !NO_PRINT */
297 #endif /* BOOTMAGIC_ENABLE */
299 static bool command_common(uint8_t code)
302 #ifdef KEYBOARD_LOCK_ENABLE
303 static host_driver_t *host_driver = 0;
308 #ifdef SLEEP_LED_ENABLE
310 // test breathing sleep LED
311 case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
312 print("Sleep LED Test\n");
314 led_set(host_keyboard_leds());
318 #ifdef BOOTMAGIC_ENABLE
320 // print stored eeprom config
321 case MAGIC_KC(MAGIC_KEY_EEPROM):
322 print("eeconfig:\n");
327 #ifdef KEYBOARD_LOCK_ENABLE
329 // lock/unlock keyboard
330 case MAGIC_KC(MAGIC_KEY_LOCK):
331 if (host_get_driver()) {
332 host_driver = host_get_driver();
337 host_set_driver(host_driver);
338 print("Unlocked.\n");
344 case MAGIC_KC(MAGIC_KEY_HELP1):
345 case MAGIC_KC(MAGIC_KEY_HELP2):
346 command_common_help();
350 case MAGIC_KC(MAGIC_KEY_CONSOLE):
351 debug_matrix = false;
352 debug_keyboard = false;
354 debug_enable = false;
355 command_console_help();
357 command_state = CONSOLE;
360 // jump to bootloader
361 case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
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 debug_keyboard = true;
382 print("\ndebug: off\n");
383 debug_matrix = false;
384 debug_keyboard = false;
389 // debug matrix toggle
390 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
391 debug_matrix = !debug_matrix;
393 print("\nmatrix: on\n");
396 print("\nmatrix: off\n");
400 // debug keyboard toggle
401 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
402 debug_keyboard = !debug_keyboard;
403 if (debug_keyboard) {
404 print("\nkeyboard: on\n");
407 print("\nkeyboard: off\n");
411 // debug mouse toggle
412 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
413 debug_mouse = !debug_mouse;
415 print("\nmouse: on\n");
418 print("\nmouse: off\n");
423 case MAGIC_KC(MAGIC_KEY_VERSION):
428 case MAGIC_KC(MAGIC_KEY_STATUS):
435 case MAGIC_KC(MAGIC_KEY_NKRO):
436 clear_keyboard(); // clear to prevent stuck keys
437 keyboard_nkro = !keyboard_nkro;
441 print("NKRO: off\n");
448 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
449 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
450 switch_default_layer(0);
453 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
455 case MAGIC_KC(MAGIC_KEY_LAYER0):
456 switch_default_layer(0);
459 case MAGIC_KC(MAGIC_KEY_LAYER1):
460 switch_default_layer(1);
463 case MAGIC_KC(MAGIC_KEY_LAYER2):
464 switch_default_layer(2);
467 case MAGIC_KC(MAGIC_KEY_LAYER3):
468 switch_default_layer(3);
471 case MAGIC_KC(MAGIC_KEY_LAYER4):
472 switch_default_layer(4);
475 case MAGIC_KC(MAGIC_KEY_LAYER5):
476 switch_default_layer(5);
479 case MAGIC_KC(MAGIC_KEY_LAYER6):
480 switch_default_layer(6);
483 case MAGIC_KC(MAGIC_KEY_LAYER7):
484 switch_default_layer(7);
487 case MAGIC_KC(MAGIC_KEY_LAYER8):
488 switch_default_layer(8);
491 case MAGIC_KC(MAGIC_KEY_LAYER9):
492 switch_default_layer(9);
497 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
499 case KC_F1 ... KC_F9:
500 switch_default_layer((code - KC_F1) + 1);
503 switch_default_layer(0);
507 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
510 switch_default_layer((code - KC_1) + 1);
513 switch_default_layer(0);
525 /***********************************************************
527 ***********************************************************/
528 static void command_console_help(void)
530 print("\n\t- Console -\n"
532 #ifdef MOUSEKEY_ENABLE
538 static bool command_console(uint8_t code)
542 case KC_SLASH: /* ? */
543 command_console_help();
547 command_state = ONESHOT;
549 #ifdef MOUSEKEY_ENABLE
551 mousekey_console_help();
553 command_state = MOUSEKEY;
565 #ifdef MOUSEKEY_ENABLE
566 /***********************************************************
568 ***********************************************************/
569 static uint8_t mousekey_param = 0;
571 static void mousekey_param_print(void)
574 print("\n\t- Values -\n");
575 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
576 print("2: interval(ms): "); pdec(mk_interval); print("\n");
577 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
578 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
579 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
580 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
581 #endif /* !NO_PRINT */
585 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
586 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
587 static void mousekey_param_inc(uint8_t param, uint8_t inc)
591 if (mk_delay + inc < UINT8_MAX)
594 mk_delay = UINT8_MAX;
595 PRINT_SET_VAL(mk_delay);
598 if (mk_interval + inc < UINT8_MAX)
601 mk_interval = UINT8_MAX;
602 PRINT_SET_VAL(mk_interval);
605 if (mk_max_speed + inc < UINT8_MAX)
608 mk_max_speed = UINT8_MAX;
609 PRINT_SET_VAL(mk_max_speed);
612 if (mk_time_to_max + inc < UINT8_MAX)
613 mk_time_to_max += inc;
615 mk_time_to_max = UINT8_MAX;
616 PRINT_SET_VAL(mk_time_to_max);
619 if (mk_wheel_max_speed + inc < UINT8_MAX)
620 mk_wheel_max_speed += inc;
622 mk_wheel_max_speed = UINT8_MAX;
623 PRINT_SET_VAL(mk_wheel_max_speed);
626 if (mk_wheel_time_to_max + inc < UINT8_MAX)
627 mk_wheel_time_to_max += inc;
629 mk_wheel_time_to_max = UINT8_MAX;
630 PRINT_SET_VAL(mk_wheel_time_to_max);
635 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)
685 print("\n\t- Mousekey -\n"
691 "5: wheel_max_speed\n"
692 "6: wheel_time_to_max\n"
701 "speed = delta * max_speed * (repeat / time_to_max)\n");
702 xprintf("where delta: cursor=%d, wheel=%d\n"
703 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
706 static bool mousekey_console(uint8_t code)
710 case KC_SLASH: /* ? */
711 mousekey_console_help();
715 if (mousekey_param) {
719 command_state = CONSOLE;
724 mousekey_param_print();
732 mousekey_param = numkey2num(code);
735 mousekey_param_inc(mousekey_param, 1);
738 mousekey_param_dec(mousekey_param, 1);
741 mousekey_param_inc(mousekey_param, 10);
744 mousekey_param_dec(mousekey_param, 10);
747 mk_delay = MOUSEKEY_DELAY/10;
748 mk_interval = MOUSEKEY_INTERVAL;
749 mk_max_speed = MOUSEKEY_MAX_SPEED;
750 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
751 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
752 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
753 print("set default\n");
759 if (mousekey_param) {
760 xprintf("M%d> ", mousekey_param);
769 /***********************************************************
771 ***********************************************************/
772 uint8_t numkey2num(uint8_t code)
789 static void switch_default_layer(uint8_t layer)
791 xprintf("L%d\n", layer);
792 default_layer_set(1UL<<layer);