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");
154 print(".nkro: "); print_dec(kc.nkro); print("\n");
156 #ifdef BACKLIGHT_ENABLE
157 backlight_config_t bc;
158 bc.raw = eeconfig_read_backlight();
159 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
160 print(".enable: "); print_dec(bc.enable); print("\n");
161 print(".level: "); print_dec(bc.level); print("\n");
166 static bool command_common(uint8_t code)
168 static host_driver_t *host_driver = 0;
170 #ifdef SLEEP_LED_ENABLE
172 // test breathing sleep LED
173 print("Sleep LED test\n");
175 led_set(host_keyboard_leds());
178 #ifdef BOOTMAGIC_ENABLE
180 print("eeconfig:\n");
185 if (host_get_driver()) {
186 host_driver = host_get_driver();
190 host_set_driver(host_driver);
191 print("Unlocked.\n");
195 case KC_SLASH: /* ? */
196 command_common_help();
199 debug_matrix = false;
200 debug_keyboard = false;
202 debug_enable = false;
203 command_console_help();
204 print("\nEnter Console Mode\n");
210 print("\n\nJump to bootloader... ");
212 bootloader_jump(); // not return
213 print("not supported.\n");
217 print("\nDEBUG: disabled.\n");
218 debug_matrix = false;
219 debug_keyboard = false;
221 debug_enable = false;
223 print("\nDEBUG: enabled.\n");
227 case KC_X: // debug matrix toggle
228 debug_matrix = !debug_matrix;
230 print("\nDEBUG: matrix enabled.\n");
233 print("\nDEBUG: matrix disabled.\n");
236 case KC_K: // debug keyboard toggle
237 debug_keyboard = !debug_keyboard;
238 if (debug_keyboard) {
239 print("\nDEBUG: keyboard enabled.\n");
242 print("\nDEBUG: keyboard disabled.\n");
245 case KC_M: // debug mouse toggle
246 debug_mouse = !debug_mouse;
248 print("\nDEBUG: mouse enabled.\n");
251 print("\nDEBUG: mouse disabled.\n");
254 case KC_V: // print version & information
255 print("\n\n----- Version -----\n");
256 print("DESC: " STR(DESCRIPTION) "\n");
257 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
258 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
259 "VER: " STR(DEVICE_VER) "\n");
260 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
272 #ifdef BOOTMAGIC_ENABLE
275 #ifdef MOUSEKEY_ENABLE
278 #ifdef EXTRAKEY_ENABLE
281 #ifdef CONSOLE_ENABLE
284 #ifdef COMMAND_ENABLE
290 #ifdef KEYMAP_SECTION_ENABLE
293 " " STR(BOOTLOADER_SIZE) "\n");
295 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
296 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
297 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
299 case KC_T: // print timer
300 print_val_hex32(timer_count);
303 print("\n\n----- Status -----\n");
304 print_val_hex8(host_keyboard_leds());
305 print_val_hex8(keyboard_protocol);
306 print_val_hex8(keyboard_idle);
308 print_val_hex8(UDCON);
309 print_val_hex8(UDIEN);
310 print_val_hex8(UDINT);
311 print_val_hex8(usb_keyboard_leds);
312 print_val_hex8(usb_keyboard_idle_count);
317 print_val_hex8(usbSofCount);
323 clear_keyboard(); //Prevents stuck keys.
324 keyboard_nkro = !keyboard_nkro;
326 print("NKRO: enabled\n");
328 print("NKRO: disabled\n");
331 #ifdef EXTRAKEY_ENABLE
333 // TODO: Power key should take this feature? otherwise any key during suspend.
335 if (suspend && remote_wakeup) {
338 host_system_send(SYSTEM_POWER_DOWN);
343 host_system_send(SYSTEM_POWER_DOWN);
353 switch_default_layer(0);
356 switch_default_layer((code - KC_1) + 1);
358 case KC_F1 ... KC_F12:
359 switch_default_layer((code - KC_F1) + 1);
369 /***********************************************************
371 ***********************************************************/
372 static void command_console_help(void)
374 print("\n\n----- Console Help -----\n");
375 print("ESC/q: quit\n");
376 #ifdef MOUSEKEY_ENABLE
377 print("m: mousekey\n");
381 static bool command_console(uint8_t code)
385 case KC_SLASH: /* ? */
386 command_console_help();
390 print("\nQuit Console Mode\n");
393 #ifdef MOUSEKEY_ENABLE
395 mousekey_console_help();
396 print("\nEnter Mousekey Console\n");
410 #ifdef MOUSEKEY_ENABLE
411 /***********************************************************
413 ***********************************************************/
414 static uint8_t mousekey_param = 0;
416 static void mousekey_param_print(void)
418 print("\n\n----- Mousekey Parameters -----\n");
419 print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
420 print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
421 print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
422 print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
423 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
424 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
427 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
428 static void mousekey_param_inc(uint8_t param, uint8_t inc)
432 if (mk_delay + inc < UINT8_MAX)
435 mk_delay = UINT8_MAX;
436 PRINT_SET_VAL(mk_delay);
439 if (mk_interval + inc < UINT8_MAX)
442 mk_interval = UINT8_MAX;
443 PRINT_SET_VAL(mk_interval);
446 if (mk_max_speed + inc < UINT8_MAX)
449 mk_max_speed = UINT8_MAX;
450 PRINT_SET_VAL(mk_max_speed);
453 if (mk_time_to_max + inc < UINT8_MAX)
454 mk_time_to_max += inc;
456 mk_time_to_max = UINT8_MAX;
457 PRINT_SET_VAL(mk_time_to_max);
460 if (mk_wheel_max_speed + inc < UINT8_MAX)
461 mk_wheel_max_speed += inc;
463 mk_wheel_max_speed = UINT8_MAX;
464 PRINT_SET_VAL(mk_wheel_max_speed);
467 if (mk_wheel_time_to_max + inc < UINT8_MAX)
468 mk_wheel_time_to_max += inc;
470 mk_wheel_time_to_max = UINT8_MAX;
471 PRINT_SET_VAL(mk_wheel_time_to_max);
476 static void mousekey_param_dec(uint8_t param, uint8_t dec)
484 PRINT_SET_VAL(mk_delay);
487 if (mk_interval > dec)
491 PRINT_SET_VAL(mk_interval);
494 if (mk_max_speed > dec)
498 PRINT_SET_VAL(mk_max_speed);
501 if (mk_time_to_max > dec)
502 mk_time_to_max -= dec;
505 PRINT_SET_VAL(mk_time_to_max);
508 if (mk_wheel_max_speed > dec)
509 mk_wheel_max_speed -= dec;
511 mk_wheel_max_speed = 0;
512 PRINT_SET_VAL(mk_wheel_max_speed);
515 if (mk_wheel_time_to_max > dec)
516 mk_wheel_time_to_max -= dec;
518 mk_wheel_time_to_max = 0;
519 PRINT_SET_VAL(mk_wheel_time_to_max);
524 static void mousekey_console_help(void)
526 print("\n\n----- Mousekey Parameters Help -----\n");
527 print("ESC/q: quit\n");
528 print("1: select mk_delay(*10ms)\n");
529 print("2: select mk_interval(ms)\n");
530 print("3: select mk_max_speed\n");
531 print("4: select mk_time_to_max\n");
532 print("5: select mk_wheel_max_speed\n");
533 print("6: select mk_wheel_time_to_max\n");
534 print("p: print prameters\n");
535 print("d: set default values\n");
536 print("up: increase prameters(+1)\n");
537 print("down: decrease prameters(-1)\n");
538 print("pgup: increase prameters(+10)\n");
539 print("pgdown: decrease prameters(-10)\n");
540 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
541 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
542 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
543 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
546 static bool mousekey_console(uint8_t code)
550 case KC_SLASH: /* ? */
551 mousekey_console_help();
556 print("\nQuit Mousekey Console\n");
561 mousekey_param_print();
573 mousekey_param = numkey2num(code);
574 print("selected parameter: "); pdec(mousekey_param); print("\n");
577 mousekey_param_inc(mousekey_param, 1);
580 mousekey_param_dec(mousekey_param, 1);
583 mousekey_param_inc(mousekey_param, 10);
586 mousekey_param_dec(mousekey_param, 10);
589 mk_delay = MOUSEKEY_DELAY/10;
590 mk_interval = MOUSEKEY_INTERVAL;
591 mk_max_speed = MOUSEKEY_MAX_SPEED;
592 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
593 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
594 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
595 print("set default values.\n");
601 print("M"); pdec(mousekey_param); print("> ");
607 /***********************************************************
609 ***********************************************************/
610 static uint8_t numkey2num(uint8_t code)
627 static void switch_default_layer(uint8_t layer)
629 print("switch_default_layer: "); print_dec(biton32(default_layer_state));
630 print(" to "); print_dec(layer); print("\n");
631 default_layer_set(1UL<<layer);