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"
37 #ifdef MOUSEKEY_ENABLE
42 # include "usb_keyboard.h"
43 # ifdef EXTRAKEY_ENABLE
44 # include "usb_extra.h"
53 static bool command_common(uint8_t code);
54 static void command_common_help(void);
55 static void print_version(void);
56 static void print_status(void);
57 static bool command_console(uint8_t code);
58 static void command_console_help(void);
59 #ifdef MOUSEKEY_ENABLE
60 static bool mousekey_console(uint8_t code);
61 static void mousekey_console_help(void);
64 static uint8_t numkey2num(uint8_t code);
65 static void switch_default_layer(uint8_t layer);
68 command_state_t command_state = ONESHOT;
71 bool command_proc(uint8_t code)
73 switch (command_state) {
77 return (command_extra(code) || command_common(code));
81 return (command_extra(code) || command_common(code));
83 return (command_console_extra(code) || command_console(code));
85 #ifdef MOUSEKEY_ENABLE
87 mousekey_console(code);
91 command_state = ONESHOT;
97 /* TODO: Refactoring is needed. */
98 /* This allows to define extra commands. return false when not processed. */
99 bool command_extra(uint8_t code) __attribute__ ((weak));
100 bool command_extra(uint8_t code)
105 bool command_console_extra(uint8_t code) __attribute__ ((weak));
106 bool command_console_extra(uint8_t code)
112 /***********************************************************
114 ***********************************************************/
115 static void command_common_help(void)
117 print( "\n\t- Magic -\n"
118 STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n"
119 STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
120 STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n"
121 STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n"
122 STR(MAGIC_KEY_VERSION ) ": Version\n"
123 STR(MAGIC_KEY_STATUS ) ": Status\n"
124 STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n"
126 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
127 STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n"
128 STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n"
129 STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n"
130 STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n"
131 STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n"
132 STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n"
133 STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n"
134 STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n"
135 STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n"
136 STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n"
139 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
140 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
143 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
144 "0-9: Switch to Layer 0-9\n"
147 STR(MAGIC_KEY_LAYER0_ALT1 ) ": Switch to Layer 0 (alternate key 1)\n"
148 STR(MAGIC_KEY_LAYER0_ALT2 ) ": Switch to Layer 0 (alternate key 2)\n"
149 STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader (Reset)\n"
151 #ifdef KEYBOARD_LOCK_ENABLE
152 STR(MAGIC_KEY_LOCK ) ": Lock\n"
155 #ifdef BOOTMAGIC_ENABLE
156 STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n"
160 STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n"
163 #ifdef SLEEP_LED_ENABLE
164 STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n"
169 static void print_version(void)
171 // print version & information
172 print("\n\t- Version -\n");
173 print("DESC: " STR(DESCRIPTION) "\n");
174 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
175 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
176 "VER: " STR(DEVICE_VER) "\n");
177 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
191 #ifdef BOOTMAGIC_ENABLE
194 #ifdef MOUSEKEY_ENABLE
197 #ifdef EXTRAKEY_ENABLE
200 #ifdef CONSOLE_ENABLE
203 #ifdef COMMAND_ENABLE
209 #ifdef KEYMAP_SECTION_ENABLE
213 " " STR(BOOTLOADER_SIZE) "\n");
215 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
216 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
217 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
222 static void print_status(void)
225 print("\n\t- Status -\n");
227 print_val_hex8(host_keyboard_leds());
228 print_val_hex8(keyboard_protocol);
229 print_val_hex8(keyboard_idle);
231 print_val_hex8(keyboard_nkro);
233 print_val_hex32(timer_count);
236 print_val_hex8(UDCON);
237 print_val_hex8(UDIEN);
238 print_val_hex8(UDINT);
239 print_val_hex8(usb_keyboard_leds);
240 print_val_hex8(usb_keyboard_idle_count);
245 print_val_hex8(usbSofCount);
251 #ifdef BOOTMAGIC_ENABLE
252 static void print_eeconfig(void)
255 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
258 dc.raw = eeconfig_read_debug();
259 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
260 print(".enable: "); print_dec(dc.enable); print("\n");
261 print(".matrix: "); print_dec(dc.matrix); print("\n");
262 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
263 print(".mouse: "); print_dec(dc.mouse); print("\n");
266 kc.raw = eeconfig_read_keymap();
267 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
268 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
269 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
270 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
271 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
272 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
273 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
274 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
275 print(".nkro: "); print_dec(kc.nkro); print("\n");
277 #ifdef BACKLIGHT_ENABLE
278 backlight_config_t bc;
279 bc.raw = eeconfig_read_backlight();
280 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
281 print(".enable: "); print_dec(bc.enable); print("\n");
282 print(".level: "); print_dec(bc.level); print("\n");
283 #endif /* BACKLIGHT_ENABLE */
285 #endif /* !NO_PRINT */
288 #endif /* BOOTMAGIC_ENABLE */
290 static bool command_common(uint8_t code)
293 #ifdef KEYBOARD_LOCK_ENABLE
294 static host_driver_t *host_driver = 0;
299 #ifdef SLEEP_LED_ENABLE
301 // test breathing sleep LED
302 case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
303 print("Sleep LED Test\n");
305 led_set(host_keyboard_leds());
309 #ifdef BOOTMAGIC_ENABLE
311 // print stored eeprom config
312 case MAGIC_KC(MAGIC_KEY_EEPROM):
313 print("eeconfig:\n");
318 #ifdef KEYBOARD_LOCK_ENABLE
320 // lock/unlock keyboard
321 case MAGIC_KC(MAGIC_KEY_LOCK):
322 if (host_get_driver()) {
323 host_driver = host_get_driver();
328 host_set_driver(host_driver);
329 print("Unlocked.\n");
335 case MAGIC_KC(MAGIC_KEY_HELP1):
336 case MAGIC_KC(MAGIC_KEY_HELP2):
337 command_common_help();
341 case MAGIC_KC(MAGIC_KEY_CONSOLE):
342 debug_matrix = false;
343 debug_keyboard = false;
345 debug_enable = false;
346 command_console_help();
348 command_state = CONSOLE;
351 // jump to bootloader
352 case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
353 clear_keyboard(); // clear to prevent stuck keys
354 print("\n\nJumping to bootloader... ");
356 bootloader_jump(); // not return
360 case MAGIC_KC(MAGIC_KEY_DEBUG):
361 debug_enable = !debug_enable;
363 print("\ndebug: on\n");
365 debug_keyboard = true;
368 print("\ndebug: off\n");
369 debug_matrix = false;
370 debug_keyboard = false;
375 // debug matrix toggle
376 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
377 debug_matrix = !debug_matrix;
379 print("\nmatrix: on\n");
382 print("\nmatrix: off\n");
386 // debug keyboard toggle
387 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
388 debug_keyboard = !debug_keyboard;
389 if (debug_keyboard) {
390 print("\nkeyboard: on\n");
393 print("\nkeyboard: off\n");
397 // debug mouse toggle
398 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
399 debug_mouse = !debug_mouse;
401 print("\nmouse: on\n");
404 print("\nmouse: off\n");
409 case MAGIC_KC(MAGIC_KEY_VERSION):
414 case MAGIC_KC(MAGIC_KEY_STATUS):
421 case MAGIC_KC(MAGIC_KEY_NKRO):
422 clear_keyboard(); // clear to prevent stuck keys
423 keyboard_nkro = !keyboard_nkro;
427 print("NKRO: off\n");
433 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
434 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
435 switch_default_layer(0);
438 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
440 case MAGIC_KC(MAGIC_KEY_LAYER0):
441 switch_default_layer(0);
444 case MAGIC_KC(MAGIC_KEY_LAYER1):
445 switch_default_layer(1);
448 case MAGIC_KC(MAGIC_KEY_LAYER2):
449 switch_default_layer(2);
452 case MAGIC_KC(MAGIC_KEY_LAYER3):
453 switch_default_layer(3);
456 case MAGIC_KC(MAGIC_KEY_LAYER4):
457 switch_default_layer(4);
460 case MAGIC_KC(MAGIC_KEY_LAYER5):
461 switch_default_layer(5);
464 case MAGIC_KC(MAGIC_KEY_LAYER6):
465 switch_default_layer(6);
468 case MAGIC_KC(MAGIC_KEY_LAYER7):
469 switch_default_layer(7);
472 case MAGIC_KC(MAGIC_KEY_LAYER8):
473 switch_default_layer(8);
476 case MAGIC_KC(MAGIC_KEY_LAYER9):
477 switch_default_layer(9);
482 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
484 case KC_F1 ... KC_F9:
485 switch_default_layer((code - KC_F1) + 1);
488 switch_default_layer(0);
492 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
495 switch_default_layer((code - KC_1) + 1);
498 switch_default_layer(0);
510 /***********************************************************
512 ***********************************************************/
513 static void command_console_help(void)
515 print("\n\t- Console -\n"
517 #ifdef MOUSEKEY_ENABLE
523 static bool command_console(uint8_t code)
527 case KC_SLASH: /* ? */
528 command_console_help();
532 command_state = ONESHOT;
534 #ifdef MOUSEKEY_ENABLE
536 mousekey_console_help();
538 command_state = MOUSEKEY;
550 #ifdef MOUSEKEY_ENABLE
551 /***********************************************************
553 ***********************************************************/
554 static uint8_t mousekey_param = 0;
556 static void mousekey_param_print(void)
559 print("\n\t- Values -\n");
560 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
561 print("2: interval(ms): "); pdec(mk_interval); print("\n");
562 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
563 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
564 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
565 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
566 #endif /* !NO_PRINT */
570 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
571 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
572 static void mousekey_param_inc(uint8_t param, uint8_t inc)
576 if (mk_delay + inc < UINT8_MAX)
579 mk_delay = UINT8_MAX;
580 PRINT_SET_VAL(mk_delay);
583 if (mk_interval + inc < UINT8_MAX)
586 mk_interval = UINT8_MAX;
587 PRINT_SET_VAL(mk_interval);
590 if (mk_max_speed + inc < UINT8_MAX)
593 mk_max_speed = UINT8_MAX;
594 PRINT_SET_VAL(mk_max_speed);
597 if (mk_time_to_max + inc < UINT8_MAX)
598 mk_time_to_max += inc;
600 mk_time_to_max = UINT8_MAX;
601 PRINT_SET_VAL(mk_time_to_max);
604 if (mk_wheel_max_speed + inc < UINT8_MAX)
605 mk_wheel_max_speed += inc;
607 mk_wheel_max_speed = UINT8_MAX;
608 PRINT_SET_VAL(mk_wheel_max_speed);
611 if (mk_wheel_time_to_max + inc < UINT8_MAX)
612 mk_wheel_time_to_max += inc;
614 mk_wheel_time_to_max = UINT8_MAX;
615 PRINT_SET_VAL(mk_wheel_time_to_max);
620 static void mousekey_param_dec(uint8_t param, uint8_t dec)
628 PRINT_SET_VAL(mk_delay);
631 if (mk_interval > dec)
635 PRINT_SET_VAL(mk_interval);
638 if (mk_max_speed > dec)
642 PRINT_SET_VAL(mk_max_speed);
645 if (mk_time_to_max > dec)
646 mk_time_to_max -= dec;
649 PRINT_SET_VAL(mk_time_to_max);
652 if (mk_wheel_max_speed > dec)
653 mk_wheel_max_speed -= dec;
655 mk_wheel_max_speed = 0;
656 PRINT_SET_VAL(mk_wheel_max_speed);
659 if (mk_wheel_time_to_max > dec)
660 mk_wheel_time_to_max -= dec;
662 mk_wheel_time_to_max = 0;
663 PRINT_SET_VAL(mk_wheel_time_to_max);
668 static void mousekey_console_help(void)
670 print("\n\t- Mousekey -\n"
676 "5: wheel_max_speed\n"
677 "6: wheel_time_to_max\n"
686 "speed = delta * max_speed * (repeat / time_to_max)\n");
687 xprintf("where delta: cursor=%d, wheel=%d\n"
688 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
691 static bool mousekey_console(uint8_t code)
695 case KC_SLASH: /* ? */
696 mousekey_console_help();
700 if (mousekey_param) {
704 command_state = CONSOLE;
709 mousekey_param_print();
717 mousekey_param = numkey2num(code);
720 mousekey_param_inc(mousekey_param, 1);
723 mousekey_param_dec(mousekey_param, 1);
726 mousekey_param_inc(mousekey_param, 10);
729 mousekey_param_dec(mousekey_param, 10);
732 mk_delay = MOUSEKEY_DELAY/10;
733 mk_interval = MOUSEKEY_INTERVAL;
734 mk_max_speed = MOUSEKEY_MAX_SPEED;
735 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
736 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
737 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
738 print("set default\n");
745 xprintf("M%d> ", mousekey_param);
753 /***********************************************************
755 ***********************************************************/
756 static uint8_t numkey2num(uint8_t code)
773 static void switch_default_layer(uint8_t layer)
775 xprintf("L%d\n", layer);
776 default_layer_set(1UL<<layer);