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 static bool mousekey_console(uint8_t code
);
50 static void mousekey_console_help(void);
52 static uint8_t kc2int(uint8_t code
);
53 static void switch_layer(uint8_t layer
);
54 static void clear_keyboard(void);
57 typedef enum { ONESHOT
, CONSOLE
, MOUSEKEY
} cmdstate_t
;
58 static cmdstate_t state
= ONESHOT
;
61 bool command_proc(uint8_t code
)
67 return (command_extra(code
) || command_common(code
));
69 command_console(code
);
72 mousekey_console(code
);
81 /* This allows to define extra commands. return false when not processed. */
82 bool command_extra(uint8_t code
) __attribute__ ((weak
));
83 bool command_extra(uint8_t code
)
89 /***********************************************************
91 ***********************************************************/
92 static void command_common_help(void)
95 print("\n\n----- Command Help -----\n");
96 print("c: enter console mode\n");
97 print("d: toggle debug enable\n");
98 print("x: toggle matrix debug\n");
99 print("k: toggle keyboard debug\n");
100 print("m: toggle mouse debug\n");
101 print("p: toggle print enable\n");
102 print("v: print device version & info\n");
103 print("t: print timer count\n");
104 print("s: print status\n");
106 print("n: toggle NKRO\n");
108 print("0/F10: switch to Layer0 \n");
109 print("1/F1: switch to Layer1 \n");
110 print("2/F2: switch to Layer2 \n");
111 print("3/F3: switch to Layer3 \n");
112 print("4/F4: switch to Layer4 \n");
113 print("PScr: power down/remote wake-up\n");
114 print("Paus: jump to bootloader\n");
117 static bool command_common(uint8_t code
)
121 case KC_SLASH
: /* ? */
122 command_common_help();
126 debug_matrix
= false;
127 debug_keyboard
= false;
129 debug_enable
= false;
130 command_console_help();
131 print("\nEnter Console Mode\n");
137 print("\n\nJump to bootloader... ");
139 bootloader_jump(); // not return
140 print("not supported.\n");
144 print("\nDEBUG: disabled.\n");
145 debug_matrix
= false;
146 debug_keyboard
= false;
148 debug_enable
= false;
150 print("\nDEBUG: enabled.\n");
152 debug_keyboard
= true;
157 case KC_X
: // debug matrix toggle
158 debug_matrix
= !debug_matrix
;
160 print("\nDEBUG: matrix enabled.\n");
163 print("\nDEBUG: matrix disabled.\n");
166 case KC_K
: // debug keyboard toggle
167 debug_keyboard
= !debug_keyboard
;
168 if (debug_keyboard
) {
169 print("\nDEBUG: keyboard enabled.\n");
172 print("\nDEBUG: keyboard disabled.\n");
175 case KC_M
: // debug mouse toggle
176 debug_mouse
= !debug_mouse
;
178 print("\nDEBUG: mouse enabled.\n");
181 print("\nDEBUG: mouse disabled.\n");
184 case KC_V
: // print version & information
185 print("\n\n----- Version -----\n");
186 print(STR(DESCRIPTION
) "\n");
187 print(STR(MANUFACTURER
) "(" STR(VENDOR_ID
) ")/");
188 print(STR(PRODUCT
) "(" STR(PRODUCT_ID
) ") ");
189 print("VERSION: " STR(DEVICE_VER
) "\n");
191 case KC_T
: // print timer
192 print("timer: "); phex16(timer_count
>>16); phex16(timer_count
); print("\n");
194 case KC_P
: // print toggle
196 print("print disabled.\n");
197 print_enable
= false;
200 print("print enabled.\n");
204 print("\n\n----- Status -----\n");
205 print("host_keyboard_leds:"); phex(host_keyboard_leds()); print("\n");
207 print("UDCON: "); phex(UDCON
); print("\n");
208 print("UDIEN: "); phex(UDIEN
); print("\n");
209 print("UDINT: "); phex(UDINT
); print("\n");
210 print("usb_keyboard_leds:"); phex(usb_keyboard_leds
); print("\n");
211 print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol
); print("\n");
212 print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config
); print("\n");
213 print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count
); print("\n");
218 print("usbSofCount: "); phex(usbSofCount
); print("\n");
224 keyboard_nkro
= !keyboard_nkro
;
226 print("NKRO: enabled\n");
228 print("NKRO: disabled\n");
231 #ifdef EXTRAKEY_ENABLE
233 // TODO: Power key should take this feature? otherwise any key during suspend.
235 if (suspend
&& remote_wakeup
) {
238 host_system_send(SYSTEM_POWER_DOWN
);
243 host_system_send(SYSTEM_POWER_DOWN
);
277 /***********************************************************
279 ***********************************************************/
280 static void command_console_help(void)
283 print("\n\n----- Console Help -----\n");
284 print("ESC/q: quit\n");
285 #ifdef MOUSEKEY_ENABLE
286 print("m: mousekey\n");
290 static bool command_console(uint8_t code
)
294 case KC_SLASH
: /* ? */
295 command_console_help();
299 print("\nQuit Console Mode\n");
302 #ifdef MOUSEKEY_ENABLE
304 mousekey_console_help();
305 print("\nEnter Mousekey Console\n");
319 #ifdef MOUSEKEY_ENABLE
320 /***********************************************************
322 ***********************************************************/
323 static uint8_t mousekey_param
= 0;
325 static void mousekey_param_print(void)
327 print("\n\n----- Mousekey Parameters -----\n");
328 print("1: mk_delay(*10ms): "); pdec(mk_delay
); print("\n");
329 print("2: mk_interval(ms): "); pdec(mk_interval
); print("\n");
330 print("3: mk_max_speed: "); pdec(mk_max_speed
); print("\n");
331 print("4: mk_time_to_max: "); pdec(mk_time_to_max
); print("\n");
332 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed
); print("\n");
333 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max
); print("\n");
336 static void mousekey_param_inc(uint8_t param
, uint8_t inc
)
340 if (mk_delay
+ inc
< UINT8_MAX
)
343 mk_delay
= UINT8_MAX
;
344 print("mk_delay = "); pdec(mk_delay
); print("\n");
347 if (mk_interval
+ inc
< UINT8_MAX
)
350 mk_interval
= UINT8_MAX
;
351 print("mk_interval = "); pdec(mk_interval
); print("\n");
354 if (mk_max_speed
+ inc
< UINT8_MAX
)
357 mk_max_speed
= UINT8_MAX
;
358 print("mk_max_speed = "); pdec(mk_max_speed
); print("\n");
361 if (mk_time_to_max
+ inc
< UINT8_MAX
)
362 mk_time_to_max
+= inc
;
364 mk_time_to_max
= UINT8_MAX
;
365 print("mk_time_to_max = "); pdec(mk_time_to_max
); print("\n");
368 if (mk_wheel_max_speed
+ inc
< UINT8_MAX
)
369 mk_wheel_max_speed
+= inc
;
371 mk_wheel_max_speed
= UINT8_MAX
;
372 print("mk_wheel_max_speed = "); pdec(mk_wheel_max_speed
); print("\n");
375 if (mk_wheel_time_to_max
+ inc
< UINT8_MAX
)
376 mk_wheel_time_to_max
+= inc
;
378 mk_wheel_time_to_max
= UINT8_MAX
;
379 print("mk_wheel_time_to_max = "); pdec(mk_wheel_time_to_max
); print("\n");
384 static void mousekey_param_dec(uint8_t param
, uint8_t dec
)
392 print("mk_delay = "); pdec(mk_delay
); print("\n");
395 if (mk_interval
> dec
)
399 print("mk_interval = "); pdec(mk_interval
); print("\n");
402 if (mk_max_speed
> dec
)
406 print("mk_max_speed = "); pdec(mk_max_speed
); print("\n");
409 if (mk_time_to_max
> dec
)
410 mk_time_to_max
-= dec
;
413 print("mk_time_to_max = "); pdec(mk_time_to_max
); print("\n");
416 if (mk_wheel_max_speed
> dec
)
417 mk_wheel_max_speed
-= dec
;
419 mk_wheel_max_speed
= 0;
420 print("mk_wheel_max_speed = "); pdec(mk_wheel_max_speed
); print("\n");
423 if (mk_wheel_time_to_max
> dec
)
424 mk_wheel_time_to_max
-= dec
;
426 mk_wheel_time_to_max
= 0;
427 print("mk_wheel_time_to_max = "); pdec(mk_wheel_time_to_max
); print("\n");
432 static void mousekey_console_help(void)
434 print("\n\n----- Mousekey Parameters Help -----\n");
435 print("ESC/q: quit\n");
436 print("1: select mk_delay(*10ms)\n");
437 print("2: select mk_interval(ms)\n");
438 print("3: select mk_max_speed\n");
439 print("4: select mk_time_to_max\n");
440 print("5: select mk_wheel_max_speed\n");
441 print("6: select mk_wheel_time_to_max\n");
442 print("p: print prameters\n");
443 print("d: set default values\n");
444 print("up: increase prameters(+1)\n");
445 print("down: decrease prameters(-1)\n");
446 print("pgup: increase prameters(+10)\n");
447 print("pgdown: decrease prameters(-10)\n");
448 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
449 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA
);
450 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA
); print("\n");
451 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
454 static bool mousekey_console(uint8_t code
)
458 case KC_SLASH
: /* ? */
459 mousekey_console_help();
464 print("\nQuit Mousekey Console\n");
469 mousekey_param_print();
481 mousekey_param
= kc2int(code
);
482 print("selected parameter: "); pdec(mousekey_param
); print("\n");
485 mousekey_param_inc(mousekey_param
, 1);
488 mousekey_param_dec(mousekey_param
, 1);
491 mousekey_param_inc(mousekey_param
, 10);
494 mousekey_param_dec(mousekey_param
, 10);
497 mk_delay
= MOUSEKEY_DELAY
/10;
498 mk_interval
= MOUSEKEY_INTERVAL
;
499 mk_max_speed
= MOUSEKEY_MAX_SPEED
;
500 mk_time_to_max
= MOUSEKEY_TIME_TO_MAX
;
501 mk_wheel_max_speed
= MOUSEKEY_WHEEL_MAX_SPEED
;
502 mk_wheel_time_to_max
= MOUSEKEY_WHEEL_TIME_TO_MAX
;
503 print("set default values.\n");
509 print("M"); pdec(mousekey_param
); print("> ");
515 /***********************************************************
517 ***********************************************************/
518 static uint8_t kc2int(uint8_t code
)
535 static void switch_layer(uint8_t layer
)
537 print("current_layer: "); phex(current_layer
); print("\n");
538 print("default_layer: "); phex(default_layer
); print("\n");
539 current_layer
= layer
;
540 default_layer
= layer
;
541 print("switch to Layer: "); phex(layer
); print("\n");
544 static void clear_keyboard(void)
548 host_send_keyboard_report();
551 host_consumer_send(0);
553 #ifdef MOUSEKEY_ENABLE