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"
31 #include "sleep_led.h"
34 #include "backlight.h"
36 #ifdef MOUSEKEY_ENABLE
41 # include "usb_keyboard.h"
42 # ifdef EXTRAKEY_ENABLE
43 # include "usb_extra.h"
52 static bool command_common(uint8_t code);
53 static void command_common_help(void);
54 static bool command_console(uint8_t code);
55 static void command_console_help(void);
56 #ifdef MOUSEKEY_ENABLE
57 static bool mousekey_console(uint8_t code);
58 static void mousekey_console_help(void);
61 static uint8_t numkey2num(uint8_t code);
62 static void switch_default_layer(uint8_t layer);
65 typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
66 static cmdstate_t state = ONESHOT;
69 bool command_proc(uint8_t code)
75 return (command_extra(code) || command_common(code));
77 command_console(code);
79 #ifdef MOUSEKEY_ENABLE
81 mousekey_console(code);
91 /* This allows to define extra commands. return false when not processed. */
92 bool command_extra(uint8_t code) __attribute__ ((weak));
93 bool command_extra(uint8_t code)
99 /***********************************************************
101 ***********************************************************/
102 static void command_common_help(void)
104 print("\n\n----- Command Help -----\n");
105 print("c: enter console mode\n");
106 print("d: toggle debug enable\n");
107 print("x: toggle matrix debug\n");
108 print("k: toggle keyboard debug\n");
109 print("m: toggle mouse debug\n");
110 #ifdef SLEEP_LED_ENABLE
111 print("z: toggle sleep LED test\n");
113 print("v: print device version & info\n");
114 print("t: print timer count\n");
115 print("s: print status\n");
116 print("e: print eeprom config\n");
118 print("n: toggle NKRO\n");
120 print("0/F10: switch to Layer0 \n");
121 print("1/F1: switch to Layer1 \n");
122 print("2/F2: switch to Layer2 \n");
123 print("3/F3: switch to Layer3 \n");
124 print("4/F4: switch to Layer4 \n");
125 print("PScr: power down/remote wake-up\n");
126 print("Caps: Lock Keyboard(Child Proof)\n");
127 print("Paus: jump to bootloader\n");
130 #ifdef BOOTMAGIC_ENABLE
131 static void print_eeconfig(void)
133 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
136 dc.raw = eeconfig_read_debug();
137 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
138 print(".enable: "); print_dec(dc.enable); print("\n");
139 print(".matrix: "); print_dec(dc.matrix); print("\n");
140 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
141 print(".mouse: "); print_dec(dc.mouse); print("\n");
144 kc.raw = eeconfig_read_keymap();
145 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
146 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
147 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
148 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
149 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
150 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
151 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
152 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
154 #ifdef BACKLIGHT_ENABLE
155 backlight_config_t bc;
156 bc.raw = eeconfig_read_backlight();
157 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
158 print(".enable: "); print_dec(bc.enable); print("\n");
159 print(".level: "); print_dec(bc.level); print("\n");
164 static bool command_common(uint8_t code)
166 static host_driver_t *host_driver = 0;
168 #ifdef SLEEP_LED_ENABLE
170 // test breathing sleep LED
171 print("Sleep LED test\n");
173 led_set(host_keyboard_leds());
176 #ifdef BOOTMAGIC_ENABLE
178 print("eeconfig:\n");
183 if (host_get_driver()) {
184 host_driver = host_get_driver();
188 host_set_driver(host_driver);
189 print("Unlocked.\n");
193 case KC_SLASH: /* ? */
194 command_common_help();
197 debug_matrix = false;
198 debug_keyboard = false;
200 debug_enable = false;
201 command_console_help();
202 print("\nEnter Console Mode\n");
208 print("\n\nJump to bootloader... ");
210 bootloader_jump(); // not return
211 print("not supported.\n");
215 print("\nDEBUG: disabled.\n");
216 debug_matrix = false;
217 debug_keyboard = false;
219 debug_enable = false;
221 print("\nDEBUG: enabled.\n");
225 case KC_X: // debug matrix toggle
226 debug_matrix = !debug_matrix;
228 print("\nDEBUG: matrix enabled.\n");
231 print("\nDEBUG: matrix disabled.\n");
234 case KC_K: // debug keyboard toggle
235 debug_keyboard = !debug_keyboard;
236 if (debug_keyboard) {
237 print("\nDEBUG: keyboard enabled.\n");
240 print("\nDEBUG: keyboard disabled.\n");
243 case KC_M: // debug mouse toggle
244 debug_mouse = !debug_mouse;
246 print("\nDEBUG: mouse enabled.\n");
249 print("\nDEBUG: mouse disabled.\n");
252 case KC_V: // print version & information
253 print("\n\n----- Version -----\n");
254 print(STR(DESCRIPTION) "\n");
255 print(STR(MANUFACTURER) "(" STR(VENDOR_ID) ")/");
256 print(STR(PRODUCT) "(" STR(PRODUCT_ID) ") ");
257 print("VERSION: " STR(DEVICE_VER) "\n");
259 case KC_T: // print timer
260 print_val_hex32(timer_count);
263 print("\n\n----- Status -----\n");
264 print_val_hex8(host_keyboard_leds());
266 print_val_hex8(UDCON);
267 print_val_hex8(UDIEN);
268 print_val_hex8(UDINT);
269 print_val_hex8(usb_keyboard_leds);
270 print_val_hex8(usb_keyboard_protocol);
271 print_val_hex8(usb_keyboard_idle_config);
272 print_val_hex8(usb_keyboard_idle_count);
277 print_val_hex8(usbSofCount);
283 clear_keyboard(); //Prevents stuck keys.
284 keyboard_nkro = !keyboard_nkro;
286 print("NKRO: enabled\n");
288 print("NKRO: disabled\n");
291 #ifdef EXTRAKEY_ENABLE
293 // TODO: Power key should take this feature? otherwise any key during suspend.
295 if (suspend && remote_wakeup) {
298 host_system_send(SYSTEM_POWER_DOWN);
303 host_system_send(SYSTEM_POWER_DOWN);
313 switch_default_layer(0);
316 switch_default_layer((code - KC_1) + 1);
318 case KC_F1 ... KC_F12:
319 switch_default_layer((code - KC_F1) + 1);
329 /***********************************************************
331 ***********************************************************/
332 static void command_console_help(void)
334 print("\n\n----- Console Help -----\n");
335 print("ESC/q: quit\n");
336 #ifdef MOUSEKEY_ENABLE
337 print("m: mousekey\n");
341 static bool command_console(uint8_t code)
345 case KC_SLASH: /* ? */
346 command_console_help();
350 print("\nQuit Console Mode\n");
353 #ifdef MOUSEKEY_ENABLE
355 mousekey_console_help();
356 print("\nEnter Mousekey Console\n");
370 #ifdef MOUSEKEY_ENABLE
371 /***********************************************************
373 ***********************************************************/
374 static uint8_t mousekey_param = 0;
376 static void mousekey_param_print(void)
378 print("\n\n----- Mousekey Parameters -----\n");
379 print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
380 print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
381 print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
382 print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
383 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
384 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
387 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
388 static void mousekey_param_inc(uint8_t param, uint8_t inc)
392 if (mk_delay + inc < UINT8_MAX)
395 mk_delay = UINT8_MAX;
396 PRINT_SET_VAL(mk_delay);
399 if (mk_interval + inc < UINT8_MAX)
402 mk_interval = UINT8_MAX;
403 PRINT_SET_VAL(mk_interval);
406 if (mk_max_speed + inc < UINT8_MAX)
409 mk_max_speed = UINT8_MAX;
410 PRINT_SET_VAL(mk_max_speed);
413 if (mk_time_to_max + inc < UINT8_MAX)
414 mk_time_to_max += inc;
416 mk_time_to_max = UINT8_MAX;
417 PRINT_SET_VAL(mk_time_to_max);
420 if (mk_wheel_max_speed + inc < UINT8_MAX)
421 mk_wheel_max_speed += inc;
423 mk_wheel_max_speed = UINT8_MAX;
424 PRINT_SET_VAL(mk_wheel_max_speed);
427 if (mk_wheel_time_to_max + inc < UINT8_MAX)
428 mk_wheel_time_to_max += inc;
430 mk_wheel_time_to_max = UINT8_MAX;
431 PRINT_SET_VAL(mk_wheel_time_to_max);
436 static void mousekey_param_dec(uint8_t param, uint8_t dec)
444 PRINT_SET_VAL(mk_delay);
447 if (mk_interval > dec)
451 PRINT_SET_VAL(mk_interval);
454 if (mk_max_speed > dec)
458 PRINT_SET_VAL(mk_max_speed);
461 if (mk_time_to_max > dec)
462 mk_time_to_max -= dec;
465 PRINT_SET_VAL(mk_time_to_max);
468 if (mk_wheel_max_speed > dec)
469 mk_wheel_max_speed -= dec;
471 mk_wheel_max_speed = 0;
472 PRINT_SET_VAL(mk_wheel_max_speed);
475 if (mk_wheel_time_to_max > dec)
476 mk_wheel_time_to_max -= dec;
478 mk_wheel_time_to_max = 0;
479 PRINT_SET_VAL(mk_wheel_time_to_max);
484 static void mousekey_console_help(void)
486 print("\n\n----- Mousekey Parameters Help -----\n");
487 print("ESC/q: quit\n");
488 print("1: select mk_delay(*10ms)\n");
489 print("2: select mk_interval(ms)\n");
490 print("3: select mk_max_speed\n");
491 print("4: select mk_time_to_max\n");
492 print("5: select mk_wheel_max_speed\n");
493 print("6: select mk_wheel_time_to_max\n");
494 print("p: print prameters\n");
495 print("d: set default values\n");
496 print("up: increase prameters(+1)\n");
497 print("down: decrease prameters(-1)\n");
498 print("pgup: increase prameters(+10)\n");
499 print("pgdown: decrease prameters(-10)\n");
500 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
501 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
502 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
503 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
506 static bool mousekey_console(uint8_t code)
510 case KC_SLASH: /* ? */
511 mousekey_console_help();
516 print("\nQuit Mousekey Console\n");
521 mousekey_param_print();
533 mousekey_param = numkey2num(code);
534 print("selected parameter: "); pdec(mousekey_param); print("\n");
537 mousekey_param_inc(mousekey_param, 1);
540 mousekey_param_dec(mousekey_param, 1);
543 mousekey_param_inc(mousekey_param, 10);
546 mousekey_param_dec(mousekey_param, 10);
549 mk_delay = MOUSEKEY_DELAY/10;
550 mk_interval = MOUSEKEY_INTERVAL;
551 mk_max_speed = MOUSEKEY_MAX_SPEED;
552 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
553 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
554 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
555 print("set default values.\n");
561 print("M"); pdec(mousekey_param); print("> ");
567 /***********************************************************
569 ***********************************************************/
570 static uint8_t numkey2num(uint8_t code)
587 static void switch_default_layer(uint8_t layer)
589 print("switch_default_layer: "); print_dec(biton32(default_layer_state));
590 print(" to "); print_dec(layer); print("\n");
591 default_layer_set(1UL<<layer);