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"
30 #ifdef MOUSEKEY_ENABLE
35 # include "usb_keyboard.h"
36 # ifdef EXTRAKEY_ENABLE
37 # include "usb_extra.h"
46 static bool command_common(uint8_t code
);
47 static void command_common_help(void);
48 static bool command_console(uint8_t code
);
49 static void command_console_help(void);
50 #ifdef MOUSEKEY_ENABLE
51 static bool mousekey_console(uint8_t code
);
52 static void mousekey_console_help(void);
55 static uint8_t numkey2num(uint8_t code
);
56 static void switch_layer(uint8_t layer
);
59 typedef enum { ONESHOT
, CONSOLE
, MOUSEKEY
} cmdstate_t
;
60 static cmdstate_t state
= ONESHOT
;
63 bool command_proc(uint8_t code
)
69 return (command_extra(code
) || command_common(code
));
71 command_console(code
);
73 #ifdef MOUSEKEY_ENABLE
75 mousekey_console(code
);
85 /* This allows to define extra commands. return false when not processed. */
86 bool command_extra(uint8_t code
) __attribute__ ((weak
));
87 bool command_extra(uint8_t code
)
93 /***********************************************************
95 ***********************************************************/
96 static void command_common_help(void)
99 print("\n\n----- Command Help -----\n");
100 print("c: enter console mode\n");
101 print("d: toggle debug enable\n");
102 print("x: toggle matrix debug\n");
103 print("k: toggle keyboard debug\n");
104 print("m: toggle mouse debug\n");
105 print("p: toggle print enable\n");
106 print("v: print device version & info\n");
107 print("t: print timer count\n");
108 print("s: print status\n");
110 print("n: toggle NKRO\n");
112 print("0/F10: switch to Layer0 \n");
113 print("1/F1: switch to Layer1 \n");
114 print("2/F2: switch to Layer2 \n");
115 print("3/F3: switch to Layer3 \n");
116 print("4/F4: switch to Layer4 \n");
117 print("PScr: power down/remote wake-up\n");
118 print("Caps: Lock Keyboard(Child Proof)\n");
119 print("Paus: jump to bootloader\n");
122 static bool command_common(uint8_t code
)
124 static host_driver_t
*host_driver
= 0;
127 if (host_get_driver()) {
128 host_driver
= host_get_driver();
132 host_set_driver(host_driver
);
133 print("Unlocked.\n");
137 case KC_SLASH
: /* ? */
138 command_common_help();
142 debug_matrix
= false;
143 debug_keyboard
= false;
145 debug_enable
= false;
146 command_console_help();
147 print("\nEnter Console Mode\n");
153 print("\n\nJump to bootloader... ");
155 bootloader_jump(); // not return
156 print("not supported.\n");
160 print("\nDEBUG: disabled.\n");
161 debug_matrix
= false;
162 debug_keyboard
= false;
164 debug_enable
= false;
166 print("\nDEBUG: enabled.\n");
170 case KC_X
: // debug matrix toggle
171 debug_matrix
= !debug_matrix
;
173 print("\nDEBUG: matrix enabled.\n");
176 print("\nDEBUG: matrix disabled.\n");
179 case KC_K
: // debug keyboard toggle
180 debug_keyboard
= !debug_keyboard
;
181 if (debug_keyboard
) {
182 print("\nDEBUG: keyboard enabled.\n");
185 print("\nDEBUG: keyboard disabled.\n");
188 case KC_M
: // debug mouse toggle
189 debug_mouse
= !debug_mouse
;
191 print("\nDEBUG: mouse enabled.\n");
194 print("\nDEBUG: mouse disabled.\n");
197 case KC_V
: // print version & information
198 print("\n\n----- Version -----\n");
199 print(STR(DESCRIPTION
) "\n");
200 print(STR(MANUFACTURER
) "(" STR(VENDOR_ID
) ")/");
201 print(STR(PRODUCT
) "(" STR(PRODUCT_ID
) ") ");
202 print("VERSION: " STR(DEVICE_VER
) "\n");
204 case KC_T
: // print timer
205 print_val_hex32(timer_count
);
207 case KC_P
: // print toggle
209 print("print disabled.\n");
210 print_enable
= false;
213 print("print enabled.\n");
217 print("\n\n----- Status -----\n");
218 print_val_hex8(host_keyboard_leds());
220 print_val_hex8(UDCON
);
221 print_val_hex8(UDIEN
);
222 print_val_hex8(UDINT
);
223 print_val_hex8(usb_keyboard_leds
);
224 print_val_hex8(usb_keyboard_protocol
);
225 print_val_hex8(usb_keyboard_idle_config
);
226 print_val_hex8(usb_keyboard_idle_count
);
231 print_val_hex8(usbSofCount
);
237 clear_keyboard(); //Prevents stuck keys.
238 keyboard_nkro
= !keyboard_nkro
;
240 print("NKRO: enabled\n");
242 print("NKRO: disabled\n");
245 #ifdef EXTRAKEY_ENABLE
247 // TODO: Power key should take this feature? otherwise any key during suspend.
249 if (suspend
&& remote_wakeup
) {
252 host_system_send(SYSTEM_POWER_DOWN
);
257 host_system_send(SYSTEM_POWER_DOWN
);
271 switch_layer((code
- KC_1
) + 1);
273 case KC_F1
... KC_F9
:
275 switch_layer((code
- KC_F1
) + 1);
285 /***********************************************************
287 ***********************************************************/
288 static void command_console_help(void)
291 print("\n\n----- Console Help -----\n");
292 print("ESC/q: quit\n");
293 #ifdef MOUSEKEY_ENABLE
294 print("m: mousekey\n");
298 static bool command_console(uint8_t code
)
302 case KC_SLASH
: /* ? */
303 command_console_help();
307 print("\nQuit Console Mode\n");
310 #ifdef MOUSEKEY_ENABLE
312 mousekey_console_help();
313 print("\nEnter Mousekey Console\n");
327 #ifdef MOUSEKEY_ENABLE
328 /***********************************************************
330 ***********************************************************/
331 static uint8_t mousekey_param
= 0;
333 static void mousekey_param_print(void)
335 print("\n\n----- Mousekey Parameters -----\n");
336 print("1: mk_delay(*10ms): "); pdec(mk_delay
); print("\n");
337 print("2: mk_interval(ms): "); pdec(mk_interval
); print("\n");
338 print("3: mk_max_speed: "); pdec(mk_max_speed
); print("\n");
339 print("4: mk_time_to_max: "); pdec(mk_time_to_max
); print("\n");
340 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed
); print("\n");
341 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max
); print("\n");
344 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
345 static void mousekey_param_inc(uint8_t param
, uint8_t inc
)
349 if (mk_delay
+ inc
< UINT8_MAX
)
352 mk_delay
= UINT8_MAX
;
353 PRINT_SET_VAL(mk_delay
);
356 if (mk_interval
+ inc
< UINT8_MAX
)
359 mk_interval
= UINT8_MAX
;
360 PRINT_SET_VAL(mk_interval
);
363 if (mk_max_speed
+ inc
< UINT8_MAX
)
366 mk_max_speed
= UINT8_MAX
;
367 PRINT_SET_VAL(mk_max_speed
);
370 if (mk_time_to_max
+ inc
< UINT8_MAX
)
371 mk_time_to_max
+= inc
;
373 mk_time_to_max
= UINT8_MAX
;
374 PRINT_SET_VAL(mk_time_to_max
);
377 if (mk_wheel_max_speed
+ inc
< UINT8_MAX
)
378 mk_wheel_max_speed
+= inc
;
380 mk_wheel_max_speed
= UINT8_MAX
;
381 PRINT_SET_VAL(mk_wheel_max_speed
);
384 if (mk_wheel_time_to_max
+ inc
< UINT8_MAX
)
385 mk_wheel_time_to_max
+= inc
;
387 mk_wheel_time_to_max
= UINT8_MAX
;
388 PRINT_SET_VAL(mk_wheel_time_to_max
);
393 static void mousekey_param_dec(uint8_t param
, uint8_t dec
)
401 PRINT_SET_VAL(mk_delay
);
404 if (mk_interval
> dec
)
408 PRINT_SET_VAL(mk_interval
);
411 if (mk_max_speed
> dec
)
415 PRINT_SET_VAL(mk_max_speed
);
418 if (mk_time_to_max
> dec
)
419 mk_time_to_max
-= dec
;
422 PRINT_SET_VAL(mk_time_to_max
);
425 if (mk_wheel_max_speed
> dec
)
426 mk_wheel_max_speed
-= dec
;
428 mk_wheel_max_speed
= 0;
429 PRINT_SET_VAL(mk_wheel_max_speed
);
432 if (mk_wheel_time_to_max
> dec
)
433 mk_wheel_time_to_max
-= dec
;
435 mk_wheel_time_to_max
= 0;
436 PRINT_SET_VAL(mk_wheel_time_to_max
);
441 static void mousekey_console_help(void)
443 print("\n\n----- Mousekey Parameters Help -----\n");
444 print("ESC/q: quit\n");
445 print("1: select mk_delay(*10ms)\n");
446 print("2: select mk_interval(ms)\n");
447 print("3: select mk_max_speed\n");
448 print("4: select mk_time_to_max\n");
449 print("5: select mk_wheel_max_speed\n");
450 print("6: select mk_wheel_time_to_max\n");
451 print("p: print prameters\n");
452 print("d: set default values\n");
453 print("up: increase prameters(+1)\n");
454 print("down: decrease prameters(-1)\n");
455 print("pgup: increase prameters(+10)\n");
456 print("pgdown: decrease prameters(-10)\n");
457 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
458 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA
);
459 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA
); print("\n");
460 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
463 static bool mousekey_console(uint8_t code
)
467 case KC_SLASH
: /* ? */
468 mousekey_console_help();
473 print("\nQuit Mousekey Console\n");
478 mousekey_param_print();
490 mousekey_param
= numkey2num(code
);
491 print("selected parameter: "); pdec(mousekey_param
); print("\n");
494 mousekey_param_inc(mousekey_param
, 1);
497 mousekey_param_dec(mousekey_param
, 1);
500 mousekey_param_inc(mousekey_param
, 10);
503 mousekey_param_dec(mousekey_param
, 10);
506 mk_delay
= MOUSEKEY_DELAY
/10;
507 mk_interval
= MOUSEKEY_INTERVAL
;
508 mk_max_speed
= MOUSEKEY_MAX_SPEED
;
509 mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
510 mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
511 mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
512 print("set default values.\n");
518 print("M"); pdec(mousekey_param
); print("> ");
524 /***********************************************************
526 ***********************************************************/
527 static uint8_t numkey2num(uint8_t code
)
544 static void switch_layer(uint8_t layer
)
546 print_val_hex8(current_layer
);
547 print_val_hex8(default_layer
);
548 current_layer
= layer
;
549 default_layer
= layer
;
550 print("switch to "); print_val_hex8(layer
);