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"
54 #endif /* AUDIO_ENABLE */
57 static bool command_common(uint8_t code);
58 static void command_common_help(void);
59 static void print_version(void);
60 static void print_status(void);
61 static bool command_console(uint8_t code);
62 static void command_console_help(void);
63 #ifdef MOUSEKEY_ENABLE
64 static bool mousekey_console(uint8_t code);
65 static void mousekey_console_help(void);
68 static uint8_t numkey2num(uint8_t code);
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)
109 bool command_console_extra(uint8_t code) __attribute__ ((weak));
110 bool command_console_extra(uint8_t code)
116 /***********************************************************
118 ***********************************************************/
119 static void command_common_help(void)
121 print( "\n\t- Magic -\n"
122 STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n"
123 STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
124 STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n"
125 STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n"
126 STR(MAGIC_KEY_VERSION ) ": Version\n"
127 STR(MAGIC_KEY_STATUS ) ": Status\n"
128 STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n"
130 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
131 STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n"
132 STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n"
133 STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n"
134 STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n"
135 STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n"
136 STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n"
137 STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n"
138 STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n"
139 STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n"
140 STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n"
143 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
144 "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
147 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
148 "0-9: Switch to Layer 0-9\n"
151 STR(MAGIC_KEY_LAYER0_ALT1 ) ": Switch to Layer 0 (alternate key 1)\n"
152 STR(MAGIC_KEY_LAYER0_ALT2 ) ": Switch to Layer 0 (alternate key 2)\n"
153 STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader (Reset)\n"
155 #ifdef KEYBOARD_LOCK_ENABLE
156 STR(MAGIC_KEY_LOCK ) ": Lock\n"
159 #ifdef BOOTMAGIC_ENABLE
160 STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n"
164 STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n"
167 #ifdef SLEEP_LED_ENABLE
168 STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n"
173 static void print_version(void)
175 // print version & information
176 print("\n\t- Version -\n");
177 print("DESC: " STR(DESCRIPTION) "\n");
178 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
179 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
180 "VER: " STR(DEVICE_VER) "\n");
181 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
195 #ifdef BOOTMAGIC_ENABLE
198 #ifdef MOUSEKEY_ENABLE
201 #ifdef EXTRAKEY_ENABLE
204 #ifdef CONSOLE_ENABLE
207 #ifdef COMMAND_ENABLE
213 #ifdef KEYMAP_SECTION_ENABLE
217 " " STR(BOOTLOADER_SIZE) "\n");
219 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
220 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
221 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
226 static void print_status(void)
229 print("\n\t- Status -\n");
231 print_val_hex8(host_keyboard_leds());
232 print_val_hex8(keyboard_protocol);
233 print_val_hex8(keyboard_idle);
235 print_val_hex8(keyboard_nkro);
237 print_val_hex32(timer_count);
240 print_val_hex8(UDCON);
241 print_val_hex8(UDIEN);
242 print_val_hex8(UDINT);
243 print_val_hex8(usb_keyboard_leds);
244 print_val_hex8(usb_keyboard_idle_count);
249 print_val_hex8(usbSofCount);
255 #ifdef BOOTMAGIC_ENABLE
256 static void print_eeconfig(void)
259 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
262 dc.raw = eeconfig_read_debug();
263 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
264 print(".enable: "); print_dec(dc.enable); print("\n");
265 print(".matrix: "); print_dec(dc.matrix); print("\n");
266 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
267 print(".mouse: "); print_dec(dc.mouse); print("\n");
270 kc.raw = eeconfig_read_keymap();
271 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
272 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
273 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
274 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
275 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
276 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
277 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
278 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
279 print(".nkro: "); print_dec(kc.nkro); print("\n");
281 #ifdef BACKLIGHT_ENABLE
282 backlight_config_t bc;
283 bc.raw = eeconfig_read_backlight();
284 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
285 print(".enable: "); print_dec(bc.enable); print("\n");
286 print(".level: "); print_dec(bc.level); print("\n");
287 #endif /* BACKLIGHT_ENABLE */
289 #endif /* !NO_PRINT */
292 #endif /* BOOTMAGIC_ENABLE */
294 static bool command_common(uint8_t code)
297 #ifdef KEYBOARD_LOCK_ENABLE
298 static host_driver_t *host_driver = 0;
303 #ifdef SLEEP_LED_ENABLE
305 // test breathing sleep LED
306 case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
307 print("Sleep LED Test\n");
309 led_set(host_keyboard_leds());
313 #ifdef BOOTMAGIC_ENABLE
315 // print stored eeprom config
316 case MAGIC_KC(MAGIC_KEY_EEPROM):
317 print("eeconfig:\n");
322 #ifdef KEYBOARD_LOCK_ENABLE
324 // lock/unlock keyboard
325 case MAGIC_KC(MAGIC_KEY_LOCK):
326 if (host_get_driver()) {
327 host_driver = host_get_driver();
332 host_set_driver(host_driver);
333 print("Unlocked.\n");
339 case MAGIC_KC(MAGIC_KEY_HELP1):
340 case MAGIC_KC(MAGIC_KEY_HELP2):
341 command_common_help();
345 case MAGIC_KC(MAGIC_KEY_CONSOLE):
346 debug_matrix = false;
347 debug_keyboard = false;
349 debug_enable = false;
350 command_console_help();
352 command_state = CONSOLE;
355 // jump to bootloader
356 case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
357 clear_keyboard(); // clear to prevent stuck keys
358 print("\n\nJumping to bootloader... ");
363 bootloader_jump(); // not return
367 case MAGIC_KC(MAGIC_KEY_DEBUG):
368 debug_enable = !debug_enable;
370 print("\ndebug: on\n");
372 debug_keyboard = true;
375 print("\ndebug: off\n");
376 debug_matrix = false;
377 debug_keyboard = false;
382 // debug matrix toggle
383 case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
384 debug_matrix = !debug_matrix;
386 print("\nmatrix: on\n");
389 print("\nmatrix: off\n");
393 // debug keyboard toggle
394 case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
395 debug_keyboard = !debug_keyboard;
396 if (debug_keyboard) {
397 print("\nkeyboard: on\n");
400 print("\nkeyboard: off\n");
404 // debug mouse toggle
405 case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
406 debug_mouse = !debug_mouse;
408 print("\nmouse: on\n");
411 print("\nmouse: off\n");
416 case MAGIC_KC(MAGIC_KEY_VERSION):
421 case MAGIC_KC(MAGIC_KEY_STATUS):
428 case MAGIC_KC(MAGIC_KEY_NKRO):
429 clear_keyboard(); // clear to prevent stuck keys
430 keyboard_nkro = !keyboard_nkro;
434 print("NKRO: off\n");
440 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
441 case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
442 switch_default_layer(0);
445 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
447 case MAGIC_KC(MAGIC_KEY_LAYER0):
448 switch_default_layer(0);
451 case MAGIC_KC(MAGIC_KEY_LAYER1):
452 switch_default_layer(1);
455 case MAGIC_KC(MAGIC_KEY_LAYER2):
456 switch_default_layer(2);
459 case MAGIC_KC(MAGIC_KEY_LAYER3):
460 switch_default_layer(3);
463 case MAGIC_KC(MAGIC_KEY_LAYER4):
464 switch_default_layer(4);
467 case MAGIC_KC(MAGIC_KEY_LAYER5):
468 switch_default_layer(5);
471 case MAGIC_KC(MAGIC_KEY_LAYER6):
472 switch_default_layer(6);
475 case MAGIC_KC(MAGIC_KEY_LAYER7):
476 switch_default_layer(7);
479 case MAGIC_KC(MAGIC_KEY_LAYER8):
480 switch_default_layer(8);
483 case MAGIC_KC(MAGIC_KEY_LAYER9):
484 switch_default_layer(9);
489 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
491 case KC_F1 ... KC_F9:
492 switch_default_layer((code - KC_F1) + 1);
495 switch_default_layer(0);
499 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
502 switch_default_layer((code - KC_1) + 1);
505 switch_default_layer(0);
517 /***********************************************************
519 ***********************************************************/
520 static void command_console_help(void)
522 print("\n\t- Console -\n"
524 #ifdef MOUSEKEY_ENABLE
530 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;
557 #ifdef MOUSEKEY_ENABLE
558 /***********************************************************
560 ***********************************************************/
561 static uint8_t mousekey_param = 0;
563 static void mousekey_param_print(void)
566 print("\n\t- Values -\n");
567 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
568 print("2: interval(ms): "); pdec(mk_interval); print("\n");
569 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
570 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
571 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
572 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
573 #endif /* !NO_PRINT */
577 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
578 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
579 static void mousekey_param_inc(uint8_t param, uint8_t inc)
583 if (mk_delay + inc < UINT8_MAX)
586 mk_delay = UINT8_MAX;
587 PRINT_SET_VAL(mk_delay);
590 if (mk_interval + inc < UINT8_MAX)
593 mk_interval = UINT8_MAX;
594 PRINT_SET_VAL(mk_interval);
597 if (mk_max_speed + inc < UINT8_MAX)
600 mk_max_speed = UINT8_MAX;
601 PRINT_SET_VAL(mk_max_speed);
604 if (mk_time_to_max + inc < UINT8_MAX)
605 mk_time_to_max += inc;
607 mk_time_to_max = UINT8_MAX;
608 PRINT_SET_VAL(mk_time_to_max);
611 if (mk_wheel_max_speed + inc < UINT8_MAX)
612 mk_wheel_max_speed += inc;
614 mk_wheel_max_speed = UINT8_MAX;
615 PRINT_SET_VAL(mk_wheel_max_speed);
618 if (mk_wheel_time_to_max + inc < UINT8_MAX)
619 mk_wheel_time_to_max += inc;
621 mk_wheel_time_to_max = UINT8_MAX;
622 PRINT_SET_VAL(mk_wheel_time_to_max);
627 static void mousekey_param_dec(uint8_t param, uint8_t dec)
635 PRINT_SET_VAL(mk_delay);
638 if (mk_interval > dec)
642 PRINT_SET_VAL(mk_interval);
645 if (mk_max_speed > dec)
649 PRINT_SET_VAL(mk_max_speed);
652 if (mk_time_to_max > dec)
653 mk_time_to_max -= dec;
656 PRINT_SET_VAL(mk_time_to_max);
659 if (mk_wheel_max_speed > dec)
660 mk_wheel_max_speed -= dec;
662 mk_wheel_max_speed = 0;
663 PRINT_SET_VAL(mk_wheel_max_speed);
666 if (mk_wheel_time_to_max > dec)
667 mk_wheel_time_to_max -= dec;
669 mk_wheel_time_to_max = 0;
670 PRINT_SET_VAL(mk_wheel_time_to_max);
675 static void mousekey_console_help(void)
677 print("\n\t- Mousekey -\n"
683 "5: wheel_max_speed\n"
684 "6: wheel_time_to_max\n"
693 "speed = delta * max_speed * (repeat / time_to_max)\n");
694 xprintf("where delta: cursor=%d, wheel=%d\n"
695 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
698 static bool mousekey_console(uint8_t code)
702 case KC_SLASH: /* ? */
703 mousekey_console_help();
707 if (mousekey_param) {
711 command_state = CONSOLE;
716 mousekey_param_print();
724 mousekey_param = numkey2num(code);
727 mousekey_param_inc(mousekey_param, 1);
730 mousekey_param_dec(mousekey_param, 1);
733 mousekey_param_inc(mousekey_param, 10);
736 mousekey_param_dec(mousekey_param, 10);
739 mk_delay = MOUSEKEY_DELAY/10;
740 mk_interval = MOUSEKEY_INTERVAL;
741 mk_max_speed = MOUSEKEY_MAX_SPEED;
742 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
743 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
744 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
745 print("set default\n");
752 xprintf("M%d> ", mousekey_param);
760 /***********************************************************
762 ***********************************************************/
763 static uint8_t numkey2num(uint8_t code)
780 static void switch_default_layer(uint8_t layer)
782 xprintf("L%d\n", layer);
783 default_layer_set(1UL<<layer);