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 "layer_switch.h"
33 #ifdef MOUSEKEY_ENABLE
38 # include "usb_keyboard.h"
39 # ifdef EXTRAKEY_ENABLE
40 # include "usb_extra.h"
49 static bool command_common(uint8_t code);
50 static void command_common_help(void);
51 static bool command_console(uint8_t code);
52 static void command_console_help(void);
53 #ifdef MOUSEKEY_ENABLE
54 static bool mousekey_console(uint8_t code);
55 static void mousekey_console_help(void);
58 static uint8_t numkey2num(uint8_t code);
59 static void switch_default_layer(uint8_t layer);
62 typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
63 static cmdstate_t state = ONESHOT;
66 bool command_proc(uint8_t code)
72 return (command_extra(code) || command_common(code));
74 command_console(code);
76 #ifdef MOUSEKEY_ENABLE
78 mousekey_console(code);
88 /* This allows to define extra commands. return false when not processed. */
89 bool command_extra(uint8_t code) __attribute__ ((weak));
90 bool command_extra(uint8_t code)
96 /***********************************************************
98 ***********************************************************/
99 static void command_common_help(void)
101 print("\n\n----- Command Help -----\n");
102 print("c: enter console mode\n");
103 print("d: toggle debug enable\n");
104 print("x: toggle matrix debug\n");
105 print("k: toggle keyboard debug\n");
106 print("m: toggle mouse debug\n");
107 print("p: toggle print enable\n");
108 print("v: print device version & info\n");
109 print("t: print timer count\n");
110 print("s: print status\n");
111 print("e: print eeprom boot config\n");
113 print("n: toggle NKRO\n");
115 print("0/F10: switch to Layer0 \n");
116 print("1/F1: switch to Layer1 \n");
117 print("2/F2: switch to Layer2 \n");
118 print("3/F3: switch to Layer3 \n");
119 print("4/F4: switch to Layer4 \n");
120 print("PScr: power down/remote wake-up\n");
121 print("Caps: Lock Keyboard(Child Proof)\n");
122 print("Paus: jump to bootloader\n");
125 #ifdef BOOTMAGIC_ENABLE
126 static void print_eeprom_config(void)
130 eebyte = eeconfig_read_debug();
131 print("debug: "); print_hex8(eebyte); print("\n");
133 eebyte = eeconfig_read_defalt_layer();
134 print("defalt_layer: "); print_hex8(eebyte); print("\n");
136 eebyte = eeconfig_read_keyconf();
137 print("keyconf: "); print_hex8(eebyte); print("\n");
140 kc = (keyconf){ .raw = eebyte };
141 print("keyconf.swap_control_capslock: "); print_hex8(kc.swap_control_capslock); print("\n");
142 print("keyconf.capslock_to_control: "); print_hex8(kc.capslock_to_control); print("\n");
143 print("keyconf.swap_lalt_lgui: "); print_hex8(kc.swap_lalt_lgui); print("\n");
144 print("keyconf.swap_ralt_rgui: "); print_hex8(kc.swap_ralt_rgui); print("\n");
145 print("keyconf.no_gui: "); print_hex8(kc.no_gui); print("\n");
146 print("keyconf.swap_grave_esc: "); print_hex8(kc.swap_grave_esc); print("\n");
147 print("keyconf.swap_backslash_backspace: "); print_hex8(kc.swap_backslash_backspace); print("\n");
151 static bool command_common(uint8_t code)
153 static host_driver_t *host_driver = 0;
155 #ifdef BOOTMAGIC_ENABLE
157 print("eeprom config\n");
158 print_eeprom_config();
162 if (host_get_driver()) {
163 host_driver = host_get_driver();
167 host_set_driver(host_driver);
168 print("Unlocked.\n");
172 case KC_SLASH: /* ? */
173 command_common_help();
176 debug_matrix = false;
177 debug_keyboard = false;
179 debug_enable = false;
180 command_console_help();
181 print("\nEnter Console Mode\n");
187 print("\n\nJump to bootloader... ");
189 bootloader_jump(); // not return
190 print("not supported.\n");
194 print("\nDEBUG: disabled.\n");
195 debug_matrix = false;
196 debug_keyboard = false;
198 debug_enable = false;
200 print("\nDEBUG: enabled.\n");
204 case KC_X: // debug matrix toggle
205 debug_matrix = !debug_matrix;
207 print("\nDEBUG: matrix enabled.\n");
210 print("\nDEBUG: matrix disabled.\n");
213 case KC_K: // debug keyboard toggle
214 debug_keyboard = !debug_keyboard;
215 if (debug_keyboard) {
216 print("\nDEBUG: keyboard enabled.\n");
219 print("\nDEBUG: keyboard disabled.\n");
222 case KC_M: // debug mouse toggle
223 debug_mouse = !debug_mouse;
225 print("\nDEBUG: mouse enabled.\n");
228 print("\nDEBUG: mouse disabled.\n");
231 case KC_V: // print version & information
232 print("\n\n----- Version -----\n");
233 print(STR(DESCRIPTION) "\n");
234 print(STR(MANUFACTURER) "(" STR(VENDOR_ID) ")/");
235 print(STR(PRODUCT) "(" STR(PRODUCT_ID) ") ");
236 print("VERSION: " STR(DEVICE_VER) "\n");
238 case KC_T: // print timer
239 print_val_hex32(timer_count);
242 print("\n\n----- Status -----\n");
243 print_val_hex8(host_keyboard_leds());
245 print_val_hex8(UDCON);
246 print_val_hex8(UDIEN);
247 print_val_hex8(UDINT);
248 print_val_hex8(usb_keyboard_leds);
249 print_val_hex8(usb_keyboard_protocol);
250 print_val_hex8(usb_keyboard_idle_config);
251 print_val_hex8(usb_keyboard_idle_count);
256 print_val_hex8(usbSofCount);
262 clear_keyboard(); //Prevents stuck keys.
263 keyboard_nkro = !keyboard_nkro;
265 print("NKRO: enabled\n");
267 print("NKRO: disabled\n");
270 #ifdef EXTRAKEY_ENABLE
272 // TODO: Power key should take this feature? otherwise any key during suspend.
274 if (suspend && remote_wakeup) {
277 host_system_send(SYSTEM_POWER_DOWN);
282 host_system_send(SYSTEM_POWER_DOWN);
292 switch_default_layer(0);
295 switch_default_layer((code - KC_1) + 1);
297 case KC_F1 ... KC_F12:
298 switch_default_layer((code - KC_F1) + 1);
308 /***********************************************************
310 ***********************************************************/
311 static void command_console_help(void)
313 print("\n\n----- Console Help -----\n");
314 print("ESC/q: quit\n");
315 #ifdef MOUSEKEY_ENABLE
316 print("m: mousekey\n");
320 static bool command_console(uint8_t code)
324 case KC_SLASH: /* ? */
325 command_console_help();
329 print("\nQuit Console Mode\n");
332 #ifdef MOUSEKEY_ENABLE
334 mousekey_console_help();
335 print("\nEnter Mousekey Console\n");
349 #ifdef MOUSEKEY_ENABLE
350 /***********************************************************
352 ***********************************************************/
353 static uint8_t mousekey_param = 0;
355 static void mousekey_param_print(void)
357 print("\n\n----- Mousekey Parameters -----\n");
358 print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
359 print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
360 print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
361 print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
362 print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
363 print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
366 #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
367 static void mousekey_param_inc(uint8_t param, uint8_t inc)
371 if (mk_delay + inc < UINT8_MAX)
374 mk_delay = UINT8_MAX;
375 PRINT_SET_VAL(mk_delay);
378 if (mk_interval + inc < UINT8_MAX)
381 mk_interval = UINT8_MAX;
382 PRINT_SET_VAL(mk_interval);
385 if (mk_max_speed + inc < UINT8_MAX)
388 mk_max_speed = UINT8_MAX;
389 PRINT_SET_VAL(mk_max_speed);
392 if (mk_time_to_max + inc < UINT8_MAX)
393 mk_time_to_max += inc;
395 mk_time_to_max = UINT8_MAX;
396 PRINT_SET_VAL(mk_time_to_max);
399 if (mk_wheel_max_speed + inc < UINT8_MAX)
400 mk_wheel_max_speed += inc;
402 mk_wheel_max_speed = UINT8_MAX;
403 PRINT_SET_VAL(mk_wheel_max_speed);
406 if (mk_wheel_time_to_max + inc < UINT8_MAX)
407 mk_wheel_time_to_max += inc;
409 mk_wheel_time_to_max = UINT8_MAX;
410 PRINT_SET_VAL(mk_wheel_time_to_max);
415 static void mousekey_param_dec(uint8_t param, uint8_t dec)
423 PRINT_SET_VAL(mk_delay);
426 if (mk_interval > dec)
430 PRINT_SET_VAL(mk_interval);
433 if (mk_max_speed > dec)
437 PRINT_SET_VAL(mk_max_speed);
440 if (mk_time_to_max > dec)
441 mk_time_to_max -= dec;
444 PRINT_SET_VAL(mk_time_to_max);
447 if (mk_wheel_max_speed > dec)
448 mk_wheel_max_speed -= dec;
450 mk_wheel_max_speed = 0;
451 PRINT_SET_VAL(mk_wheel_max_speed);
454 if (mk_wheel_time_to_max > dec)
455 mk_wheel_time_to_max -= dec;
457 mk_wheel_time_to_max = 0;
458 PRINT_SET_VAL(mk_wheel_time_to_max);
463 static void mousekey_console_help(void)
465 print("\n\n----- Mousekey Parameters Help -----\n");
466 print("ESC/q: quit\n");
467 print("1: select mk_delay(*10ms)\n");
468 print("2: select mk_interval(ms)\n");
469 print("3: select mk_max_speed\n");
470 print("4: select mk_time_to_max\n");
471 print("5: select mk_wheel_max_speed\n");
472 print("6: select mk_wheel_time_to_max\n");
473 print("p: print prameters\n");
474 print("d: set default values\n");
475 print("up: increase prameters(+1)\n");
476 print("down: decrease prameters(-1)\n");
477 print("pgup: increase prameters(+10)\n");
478 print("pgdown: decrease prameters(-10)\n");
479 print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
480 print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
481 print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
482 print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
485 static bool mousekey_console(uint8_t code)
489 case KC_SLASH: /* ? */
490 mousekey_console_help();
495 print("\nQuit Mousekey Console\n");
500 mousekey_param_print();
512 mousekey_param = numkey2num(code);
513 print("selected parameter: "); pdec(mousekey_param); print("\n");
516 mousekey_param_inc(mousekey_param, 1);
519 mousekey_param_dec(mousekey_param, 1);
522 mousekey_param_inc(mousekey_param, 10);
525 mousekey_param_dec(mousekey_param, 10);
528 mk_delay = MOUSEKEY_DELAY/10;
529 mk_interval = MOUSEKEY_INTERVAL;
530 mk_max_speed = MOUSEKEY_MAX_SPEED;
531 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
532 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
533 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
534 print("set default values.\n");
540 print("M"); pdec(mousekey_param); print("> ");
546 /***********************************************************
548 ***********************************************************/
549 static uint8_t numkey2num(uint8_t code)
566 static void switch_default_layer(uint8_t layer)
568 print("switch_default_layer: "); print_dec(default_layer); print(" to "); print_dec(layer); print("\n");
569 default_layer_set(layer);