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 command_state_t command_state = ONESHOT;
69 bool command_proc(uint8_t code)
71 switch (command_state) {
75 return (command_extra(code) || command_common(code));
79 return (command_extra(code) || command_common(code));
81 return (command_console_extra(code) || command_console(code));
83 #ifdef MOUSEKEY_ENABLE
85 mousekey_console(code);
89 command_state = ONESHOT;
95 /* TODO: Refactoring is needed. */
96 /* This allows to define extra commands. return false when not processed. */
97 bool command_extra(uint8_t code) __attribute__ ((weak));
98 bool command_extra(uint8_t code)
103 bool command_console_extra(uint8_t code) __attribute__ ((weak));
104 bool command_console_extra(uint8_t code)
110 /***********************************************************
112 ***********************************************************/
113 static void command_common_help(void)
115 print("\n\n----- Command Help -----\n");
116 print("c: enter console mode\n");
117 print("d: toggle debug enable\n");
118 print("x: toggle matrix debug\n");
119 print("k: toggle keyboard debug\n");
120 print("m: toggle mouse debug\n");
121 #ifdef SLEEP_LED_ENABLE
122 print("z: toggle sleep LED test\n");
124 print("v: print device version & info\n");
125 print("t: print timer count\n");
126 print("s: print status\n");
127 print("e: print eeprom config\n");
129 print("n: toggle NKRO\n");
131 print("0/F10: switch to Layer0 \n");
132 print("1/F1: switch to Layer1 \n");
133 print("2/F2: switch to Layer2 \n");
134 print("3/F3: switch to Layer3 \n");
135 print("4/F4: switch to Layer4 \n");
136 print("PScr: power down/remote wake-up\n");
137 print("Caps: Lock Keyboard(Child Proof)\n");
138 print("Paus: jump to bootloader\n");
141 #ifdef BOOTMAGIC_ENABLE
142 static void print_eeconfig(void)
144 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
147 dc.raw = eeconfig_read_debug();
148 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
149 print(".enable: "); print_dec(dc.enable); print("\n");
150 print(".matrix: "); print_dec(dc.matrix); print("\n");
151 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
152 print(".mouse: "); print_dec(dc.mouse); print("\n");
155 kc.raw = eeconfig_read_keymap();
156 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
157 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
158 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
159 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
160 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
161 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
162 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
163 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
164 print(".nkro: "); print_dec(kc.nkro); print("\n");
166 #ifdef BACKLIGHT_ENABLE
167 backlight_config_t bc;
168 bc.raw = eeconfig_read_backlight();
169 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
170 print(".enable: "); print_dec(bc.enable); print("\n");
171 print(".level: "); print_dec(bc.level); print("\n");
176 static bool command_common(uint8_t code)
178 static host_driver_t *host_driver = 0;
180 #ifdef SLEEP_LED_ENABLE
182 // test breathing sleep LED
183 print("Sleep LED test\n");
185 led_set(host_keyboard_leds());
188 #ifdef BOOTMAGIC_ENABLE
190 print("eeconfig:\n");
195 if (host_get_driver()) {
196 host_driver = host_get_driver();
201 host_set_driver(host_driver);
202 print("Unlocked.\n");
206 case KC_SLASH: /* ? */
207 command_common_help();
210 debug_matrix = false;
211 debug_keyboard = false;
213 debug_enable = false;
214 command_console_help();
215 print("\nEnter Console Mode\n");
217 command_state = CONSOLE;
221 print("\n\nJump to bootloader... ");
223 bootloader_jump(); // not return
224 print("not supported.\n");
228 print("\nDEBUG: disabled.\n");
229 debug_matrix = false;
230 debug_keyboard = false;
232 debug_enable = false;
234 print("\nDEBUG: enabled.\n");
238 case KC_X: // debug matrix toggle
239 debug_matrix = !debug_matrix;
241 print("\nDEBUG: matrix enabled.\n");
244 print("\nDEBUG: matrix disabled.\n");
247 case KC_K: // debug keyboard toggle
248 debug_keyboard = !debug_keyboard;
249 if (debug_keyboard) {
250 print("\nDEBUG: keyboard enabled.\n");
253 print("\nDEBUG: keyboard disabled.\n");
256 case KC_M: // debug mouse toggle
257 debug_mouse = !debug_mouse;
259 print("\nDEBUG: mouse enabled.\n");
262 print("\nDEBUG: mouse disabled.\n");
265 case KC_V: // print version & information
266 print("\n\n----- Version -----\n");
267 print("DESC: " STR(DESCRIPTION) "\n");
268 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
269 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
270 "VER: " STR(DEVICE_VER) "\n");
271 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
283 #ifdef BOOTMAGIC_ENABLE
286 #ifdef MOUSEKEY_ENABLE
289 #ifdef EXTRAKEY_ENABLE
292 #ifdef CONSOLE_ENABLE
295 #ifdef COMMAND_ENABLE
301 #ifdef KEYMAP_SECTION_ENABLE
304 " " STR(BOOTLOADER_SIZE) "\n");
306 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
307 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
308 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
310 case KC_T: // print timer
311 print_val_hex32(timer_count);
314 print("\n\n----- Status -----\n");
315 print_val_hex8(host_keyboard_leds());
316 print_val_hex8(keyboard_protocol);
317 print_val_hex8(keyboard_idle);
319 print_val_hex8(UDCON);
320 print_val_hex8(UDIEN);
321 print_val_hex8(UDINT);
322 print_val_hex8(usb_keyboard_leds);
323 print_val_hex8(usb_keyboard_idle_count);
328 print_val_hex8(usbSofCount);
334 clear_keyboard(); //Prevents stuck keys.
335 keyboard_nkro = !keyboard_nkro;
337 print("NKRO: enabled\n");
339 print("NKRO: disabled\n");
342 #ifdef EXTRAKEY_ENABLE
344 // TODO: Power key should take this feature? otherwise any key during suspend.
346 if (suspend && remote_wakeup) {
349 host_system_send(SYSTEM_POWER_DOWN);
354 host_system_send(SYSTEM_POWER_DOWN);
364 switch_default_layer(0);
367 switch_default_layer((code - KC_1) + 1);
369 case KC_F1 ... KC_F12:
370 switch_default_layer((code - KC_F1) + 1);
380 /***********************************************************
382 ***********************************************************/
383 static void command_console_help(void)
385 print("\n\n----- Console Help -----\n");
386 print("ESC/q: quit\n");
387 #ifdef MOUSEKEY_ENABLE
388 print("m: mousekey\n");
392 static bool command_console(uint8_t code)
396 case KC_SLASH: /* ? */
397 command_console_help();
401 print("\nQuit Console Mode\n");
402 command_state = ONESHOT;
404 #ifdef MOUSEKEY_ENABLE
406 mousekey_console_help();
407 print("\nEnter Mousekey Console\n");
409 command_state = MOUSEKEY;
421 #ifdef MOUSEKEY_ENABLE
422 /***********************************************************
424 ***********************************************************/
425 static uint8_t mousekey_param = 0;
427 static void mousekey_param_print(void)
429 print("\n\n----- Mousekey Parameters -----\n");
430 print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
431 print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
432 print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
433 print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
434 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
435 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
438 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
439 static void mousekey_param_inc(uint8_t param, uint8_t inc)
443 if (mk_delay + inc < UINT8_MAX)
446 mk_delay = UINT8_MAX;
447 PRINT_SET_VAL(mk_delay);
450 if (mk_interval + inc < UINT8_MAX)
453 mk_interval = UINT8_MAX;
454 PRINT_SET_VAL(mk_interval);
457 if (mk_max_speed + inc < UINT8_MAX)
460 mk_max_speed = UINT8_MAX;
461 PRINT_SET_VAL(mk_max_speed);
464 if (mk_time_to_max + inc < UINT8_MAX)
465 mk_time_to_max += inc;
467 mk_time_to_max = UINT8_MAX;
468 PRINT_SET_VAL(mk_time_to_max);
471 if (mk_wheel_max_speed + inc < UINT8_MAX)
472 mk_wheel_max_speed += inc;
474 mk_wheel_max_speed = UINT8_MAX;
475 PRINT_SET_VAL(mk_wheel_max_speed);
478 if (mk_wheel_time_to_max + inc < UINT8_MAX)
479 mk_wheel_time_to_max += inc;
481 mk_wheel_time_to_max = UINT8_MAX;
482 PRINT_SET_VAL(mk_wheel_time_to_max);
487 static void mousekey_param_dec(uint8_t param, uint8_t dec)
495 PRINT_SET_VAL(mk_delay);
498 if (mk_interval > dec)
502 PRINT_SET_VAL(mk_interval);
505 if (mk_max_speed > dec)
509 PRINT_SET_VAL(mk_max_speed);
512 if (mk_time_to_max > dec)
513 mk_time_to_max -= dec;
516 PRINT_SET_VAL(mk_time_to_max);
519 if (mk_wheel_max_speed > dec)
520 mk_wheel_max_speed -= dec;
522 mk_wheel_max_speed = 0;
523 PRINT_SET_VAL(mk_wheel_max_speed);
526 if (mk_wheel_time_to_max > dec)
527 mk_wheel_time_to_max -= dec;
529 mk_wheel_time_to_max = 0;
530 PRINT_SET_VAL(mk_wheel_time_to_max);
535 static void mousekey_console_help(void)
537 print("\n\n----- Mousekey Parameters Help -----\n");
538 print("ESC/q: quit\n");
539 print("1: select mk_delay(*10ms)\n");
540 print("2: select mk_interval(ms)\n");
541 print("3: select mk_max_speed\n");
542 print("4: select mk_time_to_max\n");
543 print("5: select mk_wheel_max_speed\n");
544 print("6: select mk_wheel_time_to_max\n");
545 print("p: print parameters\n");
546 print("d: set default values\n");
547 print("up: increase parameters(+1)\n");
548 print("down: decrease parameters(-1)\n");
549 print("pgup: increase parameters(+10)\n");
550 print("pgdown: decrease parameters(-10)\n");
551 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
552 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
553 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
554 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
557 static bool mousekey_console(uint8_t code)
561 case KC_SLASH: /* ? */
562 mousekey_console_help();
567 print("\nQuit Mousekey Console\n");
569 command_state = CONSOLE;
572 mousekey_param_print();
584 mousekey_param = numkey2num(code);
585 print("selected parameter: "); pdec(mousekey_param); print("\n");
588 mousekey_param_inc(mousekey_param, 1);
591 mousekey_param_dec(mousekey_param, 1);
594 mousekey_param_inc(mousekey_param, 10);
597 mousekey_param_dec(mousekey_param, 10);
600 mk_delay = MOUSEKEY_DELAY/10;
601 mk_interval = MOUSEKEY_INTERVAL;
602 mk_max_speed = MOUSEKEY_MAX_SPEED;
603 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
604 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
605 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
606 print("set default values.\n");
612 print("M"); pdec(mousekey_param); print("> ");
618 /***********************************************************
620 ***********************************************************/
621 static uint8_t numkey2num(uint8_t code)
638 static void switch_default_layer(uint8_t layer)
640 print("switch_default_layer: "); print_dec(biton32(default_layer_state));
641 print(" to "); print_dec(layer); print("\n");
642 default_layer_set(1UL<<layer);