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>
27 #include "bootloader.h"
29 #ifdef MOUSEKEY_ENABLE
34 # include "usb_keyboard.h"
35 # ifdef EXTRAKEY_ENABLE
36 # include "usb_extra.h"
45 static bool command_common(uint8_t code
);
46 static void command_common_help(void);
47 static bool command_console(uint8_t code
);
48 static void command_console_help(void);
49 #ifdef MOUSEKEY_ENABLE
50 static bool mousekey_console(uint8_t code
);
51 static void mousekey_console_help(void);
54 static uint8_t numkey2num(uint8_t code
);
55 static void switch_layer(uint8_t layer
);
56 static void clear_keyboard(void);
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");
168 debug_keyboard
= true;
173 case KC_X
: // debug matrix toggle
174 debug_matrix
= !debug_matrix
;
176 print("\nDEBUG: matrix enabled.\n");
179 print("\nDEBUG: matrix disabled.\n");
182 case KC_K
: // debug keyboard toggle
183 debug_keyboard
= !debug_keyboard
;
184 if (debug_keyboard
) {
185 print("\nDEBUG: keyboard enabled.\n");
188 print("\nDEBUG: keyboard disabled.\n");
191 case KC_M
: // debug mouse toggle
192 debug_mouse
= !debug_mouse
;
194 print("\nDEBUG: mouse enabled.\n");
197 print("\nDEBUG: mouse disabled.\n");
200 case KC_V
: // print version & information
201 print("\n\n----- Version -----\n");
202 print(STR(DESCRIPTION
) "\n");
203 print(STR(MANUFACTURER
) "(" STR(VENDOR_ID
) ")/");
204 print(STR(PRODUCT
) "(" STR(PRODUCT_ID
) ") ");
205 print("VERSION: " STR(DEVICE_VER
) "\n");
207 case KC_T
: // print timer
208 print("timer: "); phex16(timer_count
>>16); phex16(timer_count
); print("\n");
210 case KC_P
: // print toggle
212 print("print disabled.\n");
213 print_enable
= false;
216 print("print enabled.\n");
220 print("\n\n----- Status -----\n");
221 print("host_keyboard_leds:"); phex(host_keyboard_leds()); print("\n");
223 print("UDCON: "); phex(UDCON
); print("\n");
224 print("UDIEN: "); phex(UDIEN
); print("\n");
225 print("UDINT: "); phex(UDINT
); print("\n");
226 print("usb_keyboard_leds:"); phex(usb_keyboard_leds
); print("\n");
227 print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol
); print("\n");
228 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config
); print("\n");
229 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count
); print("\n");
234 print("usbSofCount: "); phex(usbSofCount
); print("\n");
240 keyboard_nkro
= !keyboard_nkro
;
242 print("NKRO: enabled\n");
244 print("NKRO: disabled\n");
247 #ifdef EXTRAKEY_ENABLE
249 // TODO: Power key should take this feature? otherwise any key during suspend.
251 if (suspend
&& remote_wakeup
) {
254 host_system_send(SYSTEM_POWER_DOWN
);
259 host_system_send(SYSTEM_POWER_DOWN
);
294 /***********************************************************
296 ***********************************************************/
297 static void command_console_help(void)
300 print("\n\n----- Console Help -----\n");
301 print("ESC/q: quit\n");
302 #ifdef MOUSEKEY_ENABLE
303 print("m: mousekey\n");
307 static bool command_console(uint8_t code
)
311 case KC_SLASH
: /* ? */
312 command_console_help();
316 print("\nQuit Console Mode\n");
319 #ifdef MOUSEKEY_ENABLE
321 mousekey_console_help();
322 print("\nEnter Mousekey Console\n");
336 #ifdef MOUSEKEY_ENABLE
337 /***********************************************************
339 ***********************************************************/
340 static uint8_t mousekey_param
= 0;
342 static void mousekey_param_print(void)
344 print("\n\n----- Mousekey Parameters -----\n");
345 print("1: mk_delay(*10ms): "); pdec(mk_delay
); print("\n");
346 print("2: mk_interval(ms): "); pdec(mk_interval
); print("\n");
347 print("3: mk_max_speed: "); pdec(mk_max_speed
); print("\n");
348 print("4: mk_time_to_max: "); pdec(mk_time_to_max
); print("\n");
349 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed
); print("\n");
350 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max
); print("\n");
353 static void mousekey_param_inc(uint8_t param
, uint8_t inc
)
357 if (mk_delay
+ inc
< UINT8_MAX
)
360 mk_delay
= UINT8_MAX
;
361 print("mk_delay = "); pdec(mk_delay
); print("\n");
364 if (mk_interval
+ inc
< UINT8_MAX
)
367 mk_interval
= UINT8_MAX
;
368 print("mk_interval = "); pdec(mk_interval
); print("\n");
371 if (mk_max_speed
+ inc
< UINT8_MAX
)
374 mk_max_speed
= UINT8_MAX
;
375 print("mk_max_speed = "); pdec(mk_max_speed
); print("\n");
378 if (mk_time_to_max
+ inc
< UINT8_MAX
)
379 mk_time_to_max
+= inc
;
381 mk_time_to_max
= UINT8_MAX
;
382 print("mk_time_to_max = "); pdec(mk_time_to_max
); print("\n");
385 if (mk_wheel_max_speed
+ inc
< UINT8_MAX
)
386 mk_wheel_max_speed
+= inc
;
388 mk_wheel_max_speed
= UINT8_MAX
;
389 print("mk_wheel_max_speed = "); pdec(mk_wheel_max_speed
); print("\n");
392 if (mk_wheel_time_to_max
+ inc
< UINT8_MAX
)
393 mk_wheel_time_to_max
+= inc
;
395 mk_wheel_time_to_max
= UINT8_MAX
;
396 print("mk_wheel_time_to_max = "); pdec(mk_wheel_time_to_max
); print("\n");
401 static void mousekey_param_dec(uint8_t param
, uint8_t dec
)
409 print("mk_delay = "); pdec(mk_delay
); print("\n");
412 if (mk_interval
> dec
)
416 print("mk_interval = "); pdec(mk_interval
); print("\n");
419 if (mk_max_speed
> dec
)
423 print("mk_max_speed = "); pdec(mk_max_speed
); print("\n");
426 if (mk_time_to_max
> dec
)
427 mk_time_to_max
-= dec
;
430 print("mk_time_to_max = "); pdec(mk_time_to_max
); print("\n");
433 if (mk_wheel_max_speed
> dec
)
434 mk_wheel_max_speed
-= dec
;
436 mk_wheel_max_speed
= 0;
437 print("mk_wheel_max_speed = "); pdec(mk_wheel_max_speed
); print("\n");
440 if (mk_wheel_time_to_max
> dec
)
441 mk_wheel_time_to_max
-= dec
;
443 mk_wheel_time_to_max
= 0;
444 print("mk_wheel_time_to_max = "); pdec(mk_wheel_time_to_max
); print("\n");
449 static void mousekey_console_help(void)
451 print("\n\n----- Mousekey Parameters Help -----\n");
452 print("ESC/q: quit\n");
453 print("1: select mk_delay(*10ms)\n");
454 print("2: select mk_interval(ms)\n");
455 print("3: select mk_max_speed\n");
456 print("4: select mk_time_to_max\n");
457 print("5: select mk_wheel_max_speed\n");
458 print("6: select mk_wheel_time_to_max\n");
459 print("p: print prameters\n");
460 print("d: set default values\n");
461 print("up: increase prameters(+1)\n");
462 print("down: decrease prameters(-1)\n");
463 print("pgup: increase prameters(+10)\n");
464 print("pgdown: decrease prameters(-10)\n");
465 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
466 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA
);
467 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA
); print("\n");
468 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
471 static bool mousekey_console(uint8_t code
)
475 case KC_SLASH
: /* ? */
476 mousekey_console_help();
481 print("\nQuit Mousekey Console\n");
486 mousekey_param_print();
498 mousekey_param
= numkey2num(code
);
499 print("selected parameter: "); pdec(mousekey_param
); print("\n");
502 mousekey_param_inc(mousekey_param
, 1);
505 mousekey_param_dec(mousekey_param
, 1);
508 mousekey_param_inc(mousekey_param
, 10);
511 mousekey_param_dec(mousekey_param
, 10);
514 mk_delay
= MOUSEKEY_DELAY
/10;
515 mk_interval
= MOUSEKEY_INTERVAL
;
516 mk_max_speed
= MOUSEKEY_MAX_SPEED
;
517 mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
518 mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
519 mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
520 print("set default values.\n");
526 print("M"); pdec(mousekey_param
); print("> ");
532 /***********************************************************
534 ***********************************************************/
535 static uint8_t numkey2num(uint8_t code
)
552 static void switch_layer(uint8_t layer
)
554 print("current_layer: "); phex(current_layer
); print("\n");
555 print("default_layer: "); phex(default_layer
); print("\n");
556 current_layer
= layer
;
557 default_layer
= layer
;
558 print("switch to Layer: "); phex(layer
); print("\n");
561 static void clear_keyboard(void)
565 host_send_keyboard_report();
568 host_consumer_send(0);
570 #ifdef MOUSEKEY_ENABLE