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/>.
19 #include <util/delay.h>
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 uint8_t numkey2num(uint8_t code);
70 static void switch_default_layer(uint8_t layer);
73 command_state_t command_state = ONESHOT;
76 bool command_proc(uint8_t code)
78 switch (command_state) {
82 return (command_extra(code) || command_common(code));
86 return (command_extra(code) || command_common(code));
88 return (command_console_extra(code) || command_console(code));
90 #ifdef MOUSEKEY_ENABLE
92 mousekey_console(code);
96 command_state = ONESHOT;
102 /* TODO: Refactoring is needed. */
103 /* This allows to define extra commands. return false when not processed. */
104 bool command_extra(uint8_t code) __attribute__ ((weak));
105 bool command_extra(uint8_t code)
110 bool command_console_extra(uint8_t code) __attribute__ ((weak));
111 bool command_console_extra(uint8_t code)
117 /***********************************************************
119 ***********************************************************/
120 static void command_common_help(void)
122 print( "\n\t- Magic -\n"
123 STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n"
124 STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
125 STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n"
126 STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n"
127 STR(MAGIC_KEY_VERSION ) ": Version\n"
128 STR(MAGIC_KEY_STATUS ) ": Status\n"
129 STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n"
131 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
132 STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n"
133 STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n"
134 STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n"
135 STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n"
136 STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n"
137 STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n"
138 STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n"
139 STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n"
140 STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n"
141 STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n"
144 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
145 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
148 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
149 "0-9: Switch to Layer 0-9\n"
152 STR(MAGIC_KEY_LAYER0_ALT1 ) ": Switch to Layer 0 (alternate key 1)\n"
153 STR(MAGIC_KEY_LAYER0_ALT2 ) ": Switch to Layer 0 (alternate key 2)\n"
154 STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader (Reset)\n"
156 #ifdef KEYBOARD_LOCK_ENABLE
157 STR(MAGIC_KEY_LOCK ) ": Lock\n"
160 #ifdef BOOTMAGIC_ENABLE
161 STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n"
165 STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n"
168 #ifdef SLEEP_LED_ENABLE
169 STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n"
174 static void print_version(void)
176 // print version & information
177 print("\n\t- Version -\n");
178 print("DESC: " STR(DESCRIPTION) "\n");
179 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
180 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
181 "VER: " STR(DEVICE_VER) "\n");
182 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
196 #ifdef BOOTMAGIC_ENABLE
199 #ifdef MOUSEKEY_ENABLE
202 #ifdef EXTRAKEY_ENABLE
205 #ifdef CONSOLE_ENABLE
208 #ifdef COMMAND_ENABLE
214 #ifdef KEYMAP_SECTION_ENABLE
218 " " STR(BOOTLOADER_SIZE) "\n");
220 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
221 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
222 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
227 static void print_status(void)
230 print("\n\t- Status -\n");
232 print_val_hex8(host_keyboard_leds());
233 print_val_hex8(keyboard_protocol);
234 print_val_hex8(keyboard_idle);
236 print_val_hex8(keyboard_nkro);
238 print_val_hex32(timer_count);
241 print_val_hex8(UDCON);
242 print_val_hex8(UDIEN);
243 print_val_hex8(UDINT);
244 print_val_hex8(usb_keyboard_leds);
245 print_val_hex8(usb_keyboard_idle_count);
250 print_val_hex8(usbSofCount);
256 #ifdef BOOTMAGIC_ENABLE
257 static void print_eeconfig(void)
260 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
263 dc.raw = eeconfig_read_debug();
264 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
265 print(".enable: "); print_dec(dc.enable); print("\n");
266 print(".matrix: "); print_dec(dc.matrix); print("\n");
267 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
268 print(".mouse: "); print_dec(dc.mouse); print("\n");
271 kc.raw = eeconfig_read_keymap();
272 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
273 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
274 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
275 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
276 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
277 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
278 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
279 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
280 print(".nkro: "); print_dec(kc.nkro); print("\n");
282 #ifdef BACKLIGHT_ENABLE
283 backlight_config_t bc;
284 bc.raw = eeconfig_read_backlight();
285 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
286 print(".enable: "); print_dec(bc.enable); print("\n");
287 print(".level: "); print_dec(bc.level); print("\n");
288 #endif /* BACKLIGHT_ENABLE */
290 #endif /* !NO_PRINT */
293 #endif /* BOOTMAGIC_ENABLE */
295 static bool command_common(uint8_t code)
298 #ifdef KEYBOARD_LOCK_ENABLE
299 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 #ifdef BOOTMAGIC_ENABLE
316 // print stored eeprom config
317 case MAGIC_KC(MAGIC_KEY_EEPROM):
318 print("eeconfig:\n");
323 #ifdef KEYBOARD_LOCK_ENABLE
325 // lock/unlock keyboard
326 case MAGIC_KC(MAGIC_KEY_LOCK):
327 if (host_get_driver()) {
328 host_driver = host_get_driver();
333 host_set_driver(host_driver);
334 print("Unlocked.\n");
340 case MAGIC_KC(MAGIC_KEY_HELP1):
341 case MAGIC_KC(MAGIC_KEY_HELP2):
342 command_common_help();
346 case MAGIC_KC(MAGIC_KEY_CONSOLE):
347 debug_matrix = false;
348 debug_keyboard = false;
350 debug_enable = false;
351 command_console_help();
353 command_state = CONSOLE;
356 // jump to bootloader
357 case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
358 clear_keyboard(); // clear to prevent stuck keys
359 print("\n\nJumping to bootloader... ");
366 bootloader_jump(); // not return
370 case MAGIC_KC(MAGIC_KEY_DEBUG):
371 debug_enable = !debug_enable;
373 print("\ndebug: on\n");
375 debug_keyboard = true;
378 print("\ndebug: off\n");
379 debug_matrix = false;
380 debug_keyboard = false;
385 // debug matrix toggle
386 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
387 debug_matrix = !debug_matrix;
389 print("\nmatrix: on\n");
392 print("\nmatrix: off\n");
396 // debug keyboard toggle
397 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
398 debug_keyboard = !debug_keyboard;
399 if (debug_keyboard) {
400 print("\nkeyboard: on\n");
403 print("\nkeyboard: off\n");
407 // debug mouse toggle
408 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
409 debug_mouse = !debug_mouse;
411 print("\nmouse: on\n");
414 print("\nmouse: off\n");
419 case MAGIC_KC(MAGIC_KEY_VERSION):
424 case MAGIC_KC(MAGIC_KEY_STATUS):
431 case MAGIC_KC(MAGIC_KEY_NKRO):
432 clear_keyboard(); // clear to prevent stuck keys
433 keyboard_nkro = !keyboard_nkro;
437 print("NKRO: off\n");
443 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
444 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
445 switch_default_layer(0);
448 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
450 case MAGIC_KC(MAGIC_KEY_LAYER0):
451 switch_default_layer(0);
454 case MAGIC_KC(MAGIC_KEY_LAYER1):
455 switch_default_layer(1);
458 case MAGIC_KC(MAGIC_KEY_LAYER2):
459 switch_default_layer(2);
462 case MAGIC_KC(MAGIC_KEY_LAYER3):
463 switch_default_layer(3);
466 case MAGIC_KC(MAGIC_KEY_LAYER4):
467 switch_default_layer(4);
470 case MAGIC_KC(MAGIC_KEY_LAYER5):
471 switch_default_layer(5);
474 case MAGIC_KC(MAGIC_KEY_LAYER6):
475 switch_default_layer(6);
478 case MAGIC_KC(MAGIC_KEY_LAYER7):
479 switch_default_layer(7);
482 case MAGIC_KC(MAGIC_KEY_LAYER8):
483 switch_default_layer(8);
486 case MAGIC_KC(MAGIC_KEY_LAYER9):
487 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);
520 /***********************************************************
522 ***********************************************************/
523 static void command_console_help(void)
525 print("\n\t- Console -\n"
527 #ifdef MOUSEKEY_ENABLE
533 static bool command_console(uint8_t code)
537 case KC_SLASH: /* ? */
538 command_console_help();
542 command_state = ONESHOT;
544 #ifdef MOUSEKEY_ENABLE
546 mousekey_console_help();
548 command_state = MOUSEKEY;
560 #ifdef MOUSEKEY_ENABLE
561 /***********************************************************
563 ***********************************************************/
564 static uint8_t mousekey_param = 0;
566 static void mousekey_param_print(void)
569 print("\n\t- Values -\n");
570 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
571 print("2: interval(ms): "); pdec(mk_interval); print("\n");
572 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
573 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
574 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
575 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
576 #endif /* !NO_PRINT */
580 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
581 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
582 static void mousekey_param_inc(uint8_t param, uint8_t inc)
586 if (mk_delay + inc < UINT8_MAX)
589 mk_delay = UINT8_MAX;
590 PRINT_SET_VAL(mk_delay);
593 if (mk_interval + inc < UINT8_MAX)
596 mk_interval = UINT8_MAX;
597 PRINT_SET_VAL(mk_interval);
600 if (mk_max_speed + inc < UINT8_MAX)
603 mk_max_speed = UINT8_MAX;
604 PRINT_SET_VAL(mk_max_speed);
607 if (mk_time_to_max + inc < UINT8_MAX)
608 mk_time_to_max += inc;
610 mk_time_to_max = UINT8_MAX;
611 PRINT_SET_VAL(mk_time_to_max);
614 if (mk_wheel_max_speed + inc < UINT8_MAX)
615 mk_wheel_max_speed += inc;
617 mk_wheel_max_speed = UINT8_MAX;
618 PRINT_SET_VAL(mk_wheel_max_speed);
621 if (mk_wheel_time_to_max + inc < UINT8_MAX)
622 mk_wheel_time_to_max += inc;
624 mk_wheel_time_to_max = UINT8_MAX;
625 PRINT_SET_VAL(mk_wheel_time_to_max);
630 static void mousekey_param_dec(uint8_t param, uint8_t dec)
638 PRINT_SET_VAL(mk_delay);
641 if (mk_interval > dec)
645 PRINT_SET_VAL(mk_interval);
648 if (mk_max_speed > dec)
652 PRINT_SET_VAL(mk_max_speed);
655 if (mk_time_to_max > dec)
656 mk_time_to_max -= dec;
659 PRINT_SET_VAL(mk_time_to_max);
662 if (mk_wheel_max_speed > dec)
663 mk_wheel_max_speed -= dec;
665 mk_wheel_max_speed = 0;
666 PRINT_SET_VAL(mk_wheel_max_speed);
669 if (mk_wheel_time_to_max > dec)
670 mk_wheel_time_to_max -= dec;
672 mk_wheel_time_to_max = 0;
673 PRINT_SET_VAL(mk_wheel_time_to_max);
678 static void mousekey_console_help(void)
680 print("\n\t- Mousekey -\n"
686 "5: wheel_max_speed\n"
687 "6: wheel_time_to_max\n"
696 "speed = delta * max_speed * (repeat / time_to_max)\n");
697 xprintf("where delta: cursor=%d, wheel=%d\n"
698 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
701 static bool mousekey_console(uint8_t code)
705 case KC_SLASH: /* ? */
706 mousekey_console_help();
710 if (mousekey_param) {
714 command_state = CONSOLE;
719 mousekey_param_print();
727 mousekey_param = numkey2num(code);
730 mousekey_param_inc(mousekey_param, 1);
733 mousekey_param_dec(mousekey_param, 1);
736 mousekey_param_inc(mousekey_param, 10);
739 mousekey_param_dec(mousekey_param, 10);
742 mk_delay = MOUSEKEY_DELAY/10;
743 mk_interval = MOUSEKEY_INTERVAL;
744 mk_max_speed = MOUSEKEY_MAX_SPEED;
745 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
746 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
747 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
748 print("set default\n");
755 xprintf("M%d> ", mousekey_param);
763 /***********************************************************
765 ***********************************************************/
766 static uint8_t numkey2num(uint8_t code)
783 static void switch_default_layer(uint8_t layer)
785 xprintf("L%d\n", layer);
786 default_layer_set(1UL<<layer);