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"
35 #ifdef MOUSEKEY_ENABLE
40 # include "usb_keyboard.h"
41 # ifdef EXTRAKEY_ENABLE
42 # include "usb_extra.h"
51 static bool command_common(uint8_t code
);
52 static void command_common_help(void);
53 static bool command_console(uint8_t code
);
54 static void command_console_help(void);
55 #ifdef MOUSEKEY_ENABLE
56 static bool mousekey_console(uint8_t code
);
57 static void mousekey_console_help(void);
60 static uint8_t numkey2num(uint8_t code
);
61 static void switch_default_layer(uint8_t layer
);
64 typedef enum { ONESHOT
, CONSOLE
, MOUSEKEY
} cmdstate_t
;
65 static cmdstate_t state
= ONESHOT
;
68 bool command_proc(uint8_t code
)
74 return (command_extra(code
) || command_common(code
));
76 command_console(code
);
78 #ifdef MOUSEKEY_ENABLE
80 mousekey_console(code
);
90 /* This allows to define extra commands. return false when not processed. */
91 bool command_extra(uint8_t code
) __attribute__ ((weak
));
92 bool command_extra(uint8_t code
)
98 /***********************************************************
100 ***********************************************************/
101 static void command_common_help(void)
103 print("\n\n----- Command Help -----\n");
104 print("c: enter console mode\n");
105 print("d: toggle debug enable\n");
106 print("x: toggle matrix debug\n");
107 print("k: toggle keyboard debug\n");
108 print("m: toggle mouse debug\n");
109 #ifdef SLEEP_LED_ENABLE
110 print("z: toggle sleep LED test\n");
112 print("v: print device version & info\n");
113 print("t: print timer count\n");
114 print("s: print status\n");
115 print("e: print eeprom config\n");
117 print("n: toggle NKRO\n");
119 print("0/F10: switch to Layer0 \n");
120 print("1/F1: switch to Layer1 \n");
121 print("2/F2: switch to Layer2 \n");
122 print("3/F3: switch to Layer3 \n");
123 print("4/F4: switch to Layer4 \n");
124 print("PScr: power down/remote wake-up\n");
125 print("Caps: Lock Keyboard(Child Proof)\n");
126 print("Paus: jump to bootloader\n");
129 #ifdef BOOTMAGIC_ENABLE
130 static void print_eeconfig(void)
132 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
135 dc
.raw
= eeconfig_read_debug();
136 print("debug_config.raw: "); print_hex8(dc
.raw
); print("\n");
137 print(".enable: "); print_dec(dc
.enable
); print("\n");
138 print(".matrix: "); print_dec(dc
.matrix
); print("\n");
139 print(".keyboard: "); print_dec(dc
.keyboard
); print("\n");
140 print(".mouse: "); print_dec(dc
.mouse
); print("\n");
143 kc
.raw
= eeconfig_read_keymap();
144 print("keymap_config.raw: "); print_hex8(kc
.raw
); print("\n");
145 print(".swap_control_capslock: "); print_dec(kc
.swap_control_capslock
); print("\n");
146 print(".capslock_to_control: "); print_dec(kc
.capslock_to_control
); print("\n");
147 print(".swap_lalt_lgui: "); print_dec(kc
.swap_lalt_lgui
); print("\n");
148 print(".swap_ralt_rgui: "); print_dec(kc
.swap_ralt_rgui
); print("\n");
149 print(".no_gui: "); print_dec(kc
.no_gui
); print("\n");
150 print(".swap_grave_esc: "); print_dec(kc
.swap_grave_esc
); print("\n");
151 print(".swap_backslash_backspace: "); print_dec(kc
.swap_backslash_backspace
); print("\n");
155 static bool command_common(uint8_t code
)
157 static host_driver_t
*host_driver
= 0;
159 #ifdef SLEEP_LED_ENABLE
161 // test breathing sleep LED
162 print("Sleep LED test\n");
164 led_set(host_keyboard_leds());
167 #ifdef BOOTMAGIC_ENABLE
169 print("eeconfig:\n");
174 if (host_get_driver()) {
175 host_driver
= host_get_driver();
179 host_set_driver(host_driver
);
180 print("Unlocked.\n");
184 case KC_SLASH
: /* ? */
185 command_common_help();
188 debug_matrix
= false;
189 debug_keyboard
= false;
191 debug_enable
= false;
192 command_console_help();
193 print("\nEnter Console Mode\n");
199 print("\n\nJump to bootloader... ");
201 bootloader_jump(); // not return
202 print("not supported.\n");
206 print("\nDEBUG: disabled.\n");
207 debug_matrix
= false;
208 debug_keyboard
= false;
210 debug_enable
= false;
212 print("\nDEBUG: enabled.\n");
216 case KC_X
: // debug matrix toggle
217 debug_matrix
= !debug_matrix
;
219 print("\nDEBUG: matrix enabled.\n");
222 print("\nDEBUG: matrix disabled.\n");
225 case KC_K
: // debug keyboard toggle
226 debug_keyboard
= !debug_keyboard
;
227 if (debug_keyboard
) {
228 print("\nDEBUG: keyboard enabled.\n");
231 print("\nDEBUG: keyboard disabled.\n");
234 case KC_M
: // debug mouse toggle
235 debug_mouse
= !debug_mouse
;
237 print("\nDEBUG: mouse enabled.\n");
240 print("\nDEBUG: mouse disabled.\n");
243 case KC_V
: // print version & information
244 print("\n\n----- Version -----\n");
245 print(STR(DESCRIPTION
) "\n");
246 print(STR(MANUFACTURER
) "(" STR(VENDOR_ID
) ")/");
247 print(STR(PRODUCT
) "(" STR(PRODUCT_ID
) ") ");
248 print("VERSION: " STR(DEVICE_VER
) "\n");
250 case KC_T
: // print timer
251 print_val_hex32(timer_count
);
254 print("\n\n----- Status -----\n");
255 print_val_hex8(host_keyboard_leds());
257 print_val_hex8(UDCON
);
258 print_val_hex8(UDIEN
);
259 print_val_hex8(UDINT
);
260 print_val_hex8(usb_keyboard_leds
);
261 print_val_hex8(usb_keyboard_protocol
);
262 print_val_hex8(usb_keyboard_idle_config
);
263 print_val_hex8(usb_keyboard_idle_count
);
268 print_val_hex8(usbSofCount
);
274 clear_keyboard(); //Prevents stuck keys.
275 keyboard_nkro
= !keyboard_nkro
;
277 print("NKRO: enabled\n");
279 print("NKRO: disabled\n");
282 #ifdef EXTRAKEY_ENABLE
284 // TODO: Power key should take this feature? otherwise any key during suspend.
286 if (suspend
&& remote_wakeup
) {
289 host_system_send(SYSTEM_POWER_DOWN
);
294 host_system_send(SYSTEM_POWER_DOWN
);
304 switch_default_layer(0);
307 switch_default_layer((code
- KC_1
) + 1);
309 case KC_F1
... KC_F12
:
310 switch_default_layer((code
- KC_F1
) + 1);
320 /***********************************************************
322 ***********************************************************/
323 static void command_console_help(void)
325 print("\n\n----- Console Help -----\n");
326 print("ESC/q: quit\n");
327 #ifdef MOUSEKEY_ENABLE
328 print("m: mousekey\n");
332 static bool command_console(uint8_t code
)
336 case KC_SLASH
: /* ? */
337 command_console_help();
341 print("\nQuit Console Mode\n");
344 #ifdef MOUSEKEY_ENABLE
346 mousekey_console_help();
347 print("\nEnter Mousekey Console\n");
361 #ifdef MOUSEKEY_ENABLE
362 /***********************************************************
364 ***********************************************************/
365 static uint8_t mousekey_param
= 0;
367 static void mousekey_param_print(void)
369 print("\n\n----- Mousekey Parameters -----\n");
370 print("1: mk_delay(*10ms): "); pdec(mk_delay
); print("\n");
371 print("2: mk_interval(ms): "); pdec(mk_interval
); print("\n");
372 print("3: mk_max_speed: "); pdec(mk_max_speed
); print("\n");
373 print("4: mk_time_to_max: "); pdec(mk_time_to_max
); print("\n");
374 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed
); print("\n");
375 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max
); print("\n");
378 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
379 static void mousekey_param_inc(uint8_t param
, uint8_t inc
)
383 if (mk_delay
+ inc
< UINT8_MAX
)
386 mk_delay
= UINT8_MAX
;
387 PRINT_SET_VAL(mk_delay
);
390 if (mk_interval
+ inc
< UINT8_MAX
)
393 mk_interval
= UINT8_MAX
;
394 PRINT_SET_VAL(mk_interval
);
397 if (mk_max_speed
+ inc
< UINT8_MAX
)
400 mk_max_speed
= UINT8_MAX
;
401 PRINT_SET_VAL(mk_max_speed
);
404 if (mk_time_to_max
+ inc
< UINT8_MAX
)
405 mk_time_to_max
+= inc
;
407 mk_time_to_max
= UINT8_MAX
;
408 PRINT_SET_VAL(mk_time_to_max
);
411 if (mk_wheel_max_speed
+ inc
< UINT8_MAX
)
412 mk_wheel_max_speed
+= inc
;
414 mk_wheel_max_speed
= UINT8_MAX
;
415 PRINT_SET_VAL(mk_wheel_max_speed
);
418 if (mk_wheel_time_to_max
+ inc
< UINT8_MAX
)
419 mk_wheel_time_to_max
+= inc
;
421 mk_wheel_time_to_max
= UINT8_MAX
;
422 PRINT_SET_VAL(mk_wheel_time_to_max
);
427 static void mousekey_param_dec(uint8_t param
, uint8_t dec
)
435 PRINT_SET_VAL(mk_delay
);
438 if (mk_interval
> dec
)
442 PRINT_SET_VAL(mk_interval
);
445 if (mk_max_speed
> dec
)
449 PRINT_SET_VAL(mk_max_speed
);
452 if (mk_time_to_max
> dec
)
453 mk_time_to_max
-= dec
;
456 PRINT_SET_VAL(mk_time_to_max
);
459 if (mk_wheel_max_speed
> dec
)
460 mk_wheel_max_speed
-= dec
;
462 mk_wheel_max_speed
= 0;
463 PRINT_SET_VAL(mk_wheel_max_speed
);
466 if (mk_wheel_time_to_max
> dec
)
467 mk_wheel_time_to_max
-= dec
;
469 mk_wheel_time_to_max
= 0;
470 PRINT_SET_VAL(mk_wheel_time_to_max
);
475 static void mousekey_console_help(void)
477 print("\n\n----- Mousekey Parameters Help -----\n");
478 print("ESC/q: quit\n");
479 print("1: select mk_delay(*10ms)\n");
480 print("2: select mk_interval(ms)\n");
481 print("3: select mk_max_speed\n");
482 print("4: select mk_time_to_max\n");
483 print("5: select mk_wheel_max_speed\n");
484 print("6: select mk_wheel_time_to_max\n");
485 print("p: print prameters\n");
486 print("d: set default values\n");
487 print("up: increase prameters(+1)\n");
488 print("down: decrease prameters(-1)\n");
489 print("pgup: increase prameters(+10)\n");
490 print("pgdown: decrease prameters(-10)\n");
491 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
492 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA
);
493 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA
); print("\n");
494 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
497 static bool mousekey_console(uint8_t code
)
501 case KC_SLASH
: /* ? */
502 mousekey_console_help();
507 print("\nQuit Mousekey Console\n");
512 mousekey_param_print();
524 mousekey_param
= numkey2num(code
);
525 print("selected parameter: "); pdec(mousekey_param
); print("\n");
528 mousekey_param_inc(mousekey_param
, 1);
531 mousekey_param_dec(mousekey_param
, 1);
534 mousekey_param_inc(mousekey_param
, 10);
537 mousekey_param_dec(mousekey_param
, 10);
540 mk_delay
= MOUSEKEY_DELAY
/10;
541 mk_interval
= MOUSEKEY_INTERVAL
;
542 mk_max_speed
= MOUSEKEY_MAX_SPEED
;
543 mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
544 mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
545 mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
546 print("set default values.\n");
552 print("M"); pdec(mousekey_param
); print("> ");
558 /***********************************************************
560 ***********************************************************/
561 static uint8_t numkey2num(uint8_t code
)
578 static void switch_default_layer(uint8_t layer
)
580 print("switch_default_layer: "); print_dec(biton32(default_layer_state
));
581 print(" to "); print_dec(layer
); print("\n");
582 default_layer_set(1UL<<layer
);