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 bool command_console(uint8_t code);
56 static void command_console_help(void);
57 #ifdef MOUSEKEY_ENABLE
58 static bool mousekey_console(uint8_t code);
59 static void mousekey_console_help(void);
62 static uint8_t numkey2num(uint8_t code);
63 static void switch_default_layer(uint8_t layer);
66 typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
67 static cmdstate_t state = ONESHOT;
70 bool command_proc(uint8_t code)
76 return (command_extra(code) || command_common(code));
78 command_console(code);
80 #ifdef MOUSEKEY_ENABLE
82 mousekey_console(code);
92 /* This allows to define extra commands. return false when not processed. */
93 bool command_extra(uint8_t code) __attribute__ ((weak));
94 bool command_extra(uint8_t code)
100 /***********************************************************
102 ***********************************************************/
103 static void command_common_help(void)
105 print("\n\n----- Command Help -----\n");
106 print("c: enter console mode\n");
107 print("d: toggle debug enable\n");
108 print("x: toggle matrix debug\n");
109 print("k: toggle keyboard debug\n");
110 print("m: toggle mouse debug\n");
111 #ifdef SLEEP_LED_ENABLE
112 print("z: toggle sleep LED test\n");
114 print("v: print device version & info\n");
115 print("t: print timer count\n");
116 print("s: print status\n");
117 print("e: print eeprom config\n");
119 print("n: toggle NKRO\n");
121 print("0/F10: switch to Layer0 \n");
122 print("1/F1: switch to Layer1 \n");
123 print("2/F2: switch to Layer2 \n");
124 print("3/F3: switch to Layer3 \n");
125 print("4/F4: switch to Layer4 \n");
126 print("PScr: power down/remote wake-up\n");
127 print("Caps: Lock Keyboard(Child Proof)\n");
128 print("Paus: jump to bootloader\n");
131 #ifdef BOOTMAGIC_ENABLE
132 static void print_eeconfig(void)
134 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
137 dc.raw = eeconfig_read_debug();
138 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
139 print(".enable: "); print_dec(dc.enable); print("\n");
140 print(".matrix: "); print_dec(dc.matrix); print("\n");
141 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
142 print(".mouse: "); print_dec(dc.mouse); print("\n");
145 kc.raw = eeconfig_read_keymap();
146 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
147 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
148 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
149 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
150 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
151 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
152 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
153 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
155 #ifdef BACKLIGHT_ENABLE
156 backlight_config_t bc;
157 bc.raw = eeconfig_read_backlight();
158 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
159 print(".enable: "); print_dec(bc.enable); print("\n");
160 print(".level: "); print_dec(bc.level); print("\n");
165 static bool command_common(uint8_t code)
167 static host_driver_t *host_driver = 0;
169 #ifdef SLEEP_LED_ENABLE
171 // test breathing sleep LED
172 print("Sleep LED test\n");
174 led_set(host_keyboard_leds());
177 #ifdef BOOTMAGIC_ENABLE
179 print("eeconfig:\n");
184 if (host_get_driver()) {
185 host_driver = host_get_driver();
189 host_set_driver(host_driver);
190 print("Unlocked.\n");
194 case KC_SLASH: /* ? */
195 command_common_help();
198 debug_matrix = false;
199 debug_keyboard = false;
201 debug_enable = false;
202 command_console_help();
203 print("\nEnter Console Mode\n");
209 print("\n\nJump to bootloader... ");
211 bootloader_jump(); // not return
212 print("not supported.\n");
216 print("\nDEBUG: disabled.\n");
217 debug_matrix = false;
218 debug_keyboard = false;
220 debug_enable = false;
222 print("\nDEBUG: enabled.\n");
226 case KC_X: // debug matrix toggle
227 debug_matrix = !debug_matrix;
229 print("\nDEBUG: matrix enabled.\n");
232 print("\nDEBUG: matrix disabled.\n");
235 case KC_K: // debug keyboard toggle
236 debug_keyboard = !debug_keyboard;
237 if (debug_keyboard) {
238 print("\nDEBUG: keyboard enabled.\n");
241 print("\nDEBUG: keyboard disabled.\n");
244 case KC_M: // debug mouse toggle
245 debug_mouse = !debug_mouse;
247 print("\nDEBUG: mouse enabled.\n");
250 print("\nDEBUG: mouse disabled.\n");
253 case KC_V: // print version & information
254 print("\n\n----- Version -----\n");
255 print("DESC: " STR(DESCRIPTION) "\n");
256 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
257 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
258 "VER: " STR(DEVICE_VER) "\n");
259 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
271 #ifdef BOOTMAGIC_ENABLE
274 #ifdef MOUSEKEY_ENABLE
277 #ifdef EXTRAKEY_ENABLE
280 #ifdef CONSOLE_ENABLE
283 #ifdef COMMAND_ENABLE
289 #ifdef KEYMAP_SECTION_ENABLE
292 " " STR(BOOTLOADER_SIZE) "\n");
294 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
295 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
296 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
298 case KC_T: // print timer
299 print_val_hex32(timer_count);
302 print("\n\n----- Status -----\n");
303 print_val_hex8(host_keyboard_leds());
305 print_val_hex8(UDCON);
306 print_val_hex8(UDIEN);
307 print_val_hex8(UDINT);
308 print_val_hex8(usb_keyboard_leds);
309 print_val_hex8(usb_keyboard_protocol);
310 print_val_hex8(usb_keyboard_idle_config);
311 print_val_hex8(usb_keyboard_idle_count);
316 print_val_hex8(usbSofCount);
322 clear_keyboard(); //Prevents stuck keys.
323 keyboard_nkro = !keyboard_nkro;
325 print("NKRO: enabled\n");
327 print("NKRO: disabled\n");
330 #ifdef EXTRAKEY_ENABLE
332 // TODO: Power key should take this feature? otherwise any key during suspend.
334 if (suspend && remote_wakeup) {
337 host_system_send(SYSTEM_POWER_DOWN);
342 host_system_send(SYSTEM_POWER_DOWN);
352 switch_default_layer(0);
355 switch_default_layer((code - KC_1) + 1);
357 case KC_F1 ... KC_F12:
358 switch_default_layer((code - KC_F1) + 1);
368 /***********************************************************
370 ***********************************************************/
371 static void command_console_help(void)
373 print("\n\n----- Console Help -----\n");
374 print("ESC/q: quit\n");
375 #ifdef MOUSEKEY_ENABLE
376 print("m: mousekey\n");
380 static bool command_console(uint8_t code)
384 case KC_SLASH: /* ? */
385 command_console_help();
389 print("\nQuit Console Mode\n");
392 #ifdef MOUSEKEY_ENABLE
394 mousekey_console_help();
395 print("\nEnter Mousekey Console\n");
409 #ifdef MOUSEKEY_ENABLE
410 /***********************************************************
412 ***********************************************************/
413 static uint8_t mousekey_param = 0;
415 static void mousekey_param_print(void)
417 print("\n\n----- Mousekey Parameters -----\n");
418 print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
419 print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
420 print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
421 print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
422 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
423 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
426 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
427 static void mousekey_param_inc(uint8_t param, uint8_t inc)
431 if (mk_delay + inc < UINT8_MAX)
434 mk_delay = UINT8_MAX;
435 PRINT_SET_VAL(mk_delay);
438 if (mk_interval + inc < UINT8_MAX)
441 mk_interval = UINT8_MAX;
442 PRINT_SET_VAL(mk_interval);
445 if (mk_max_speed + inc < UINT8_MAX)
448 mk_max_speed = UINT8_MAX;
449 PRINT_SET_VAL(mk_max_speed);
452 if (mk_time_to_max + inc < UINT8_MAX)
453 mk_time_to_max += inc;
455 mk_time_to_max = UINT8_MAX;
456 PRINT_SET_VAL(mk_time_to_max);
459 if (mk_wheel_max_speed + inc < UINT8_MAX)
460 mk_wheel_max_speed += inc;
462 mk_wheel_max_speed = UINT8_MAX;
463 PRINT_SET_VAL(mk_wheel_max_speed);
466 if (mk_wheel_time_to_max + inc < UINT8_MAX)
467 mk_wheel_time_to_max += inc;
469 mk_wheel_time_to_max = UINT8_MAX;
470 PRINT_SET_VAL(mk_wheel_time_to_max);
475 static void mousekey_param_dec(uint8_t param, uint8_t dec)
483 PRINT_SET_VAL(mk_delay);
486 if (mk_interval > dec)
490 PRINT_SET_VAL(mk_interval);
493 if (mk_max_speed > dec)
497 PRINT_SET_VAL(mk_max_speed);
500 if (mk_time_to_max > dec)
501 mk_time_to_max -= dec;
504 PRINT_SET_VAL(mk_time_to_max);
507 if (mk_wheel_max_speed > dec)
508 mk_wheel_max_speed -= dec;
510 mk_wheel_max_speed = 0;
511 PRINT_SET_VAL(mk_wheel_max_speed);
514 if (mk_wheel_time_to_max > dec)
515 mk_wheel_time_to_max -= dec;
517 mk_wheel_time_to_max = 0;
518 PRINT_SET_VAL(mk_wheel_time_to_max);
523 static void mousekey_console_help(void)
525 print("\n\n----- Mousekey Parameters Help -----\n");
526 print("ESC/q: quit\n");
527 print("1: select mk_delay(*10ms)\n");
528 print("2: select mk_interval(ms)\n");
529 print("3: select mk_max_speed\n");
530 print("4: select mk_time_to_max\n");
531 print("5: select mk_wheel_max_speed\n");
532 print("6: select mk_wheel_time_to_max\n");
533 print("p: print prameters\n");
534 print("d: set default values\n");
535 print("up: increase prameters(+1)\n");
536 print("down: decrease prameters(-1)\n");
537 print("pgup: increase prameters(+10)\n");
538 print("pgdown: decrease prameters(-10)\n");
539 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
540 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
541 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
542 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
545 static bool mousekey_console(uint8_t code)
549 case KC_SLASH: /* ? */
550 mousekey_console_help();
555 print("\nQuit Mousekey Console\n");
560 mousekey_param_print();
572 mousekey_param = numkey2num(code);
573 print("selected parameter: "); pdec(mousekey_param); print("\n");
576 mousekey_param_inc(mousekey_param, 1);
579 mousekey_param_dec(mousekey_param, 1);
582 mousekey_param_inc(mousekey_param, 10);
585 mousekey_param_dec(mousekey_param, 10);
588 mk_delay = MOUSEKEY_DELAY/10;
589 mk_interval = MOUSEKEY_INTERVAL;
590 mk_max_speed = MOUSEKEY_MAX_SPEED;
591 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
592 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
593 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
594 print("set default values.\n");
600 print("M"); pdec(mousekey_param); print("> ");
606 /***********************************************************
608 ***********************************************************/
609 static uint8_t numkey2num(uint8_t code)
626 static void switch_default_layer(uint8_t layer)
628 print("switch_default_layer: "); print_dec(biton32(default_layer_state));
629 print(" to "); print_dec(layer); print("\n");
630 default_layer_set(1UL<<layer);