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/>.
28 #include "bootloader.h"
29 #include "action_layer.h"
30 #include "action_util.h"
32 #include "sleep_led.h"
35 #include "backlight.h"
37 #ifdef MOUSEKEY_ENABLE
42 # include "usb_keyboard.h"
43 # ifdef EXTRAKEY_ENABLE
44 # include "usb_extra.h"
53 static bool command_common(uint8_t code);
54 static void command_common_help(void);
55 static bool command_console(uint8_t code);
56 static void command_console_help(void);
57 #ifdef MOUSEKEY_ENABLE
58 static bool mousekey_console(uint8_t code);
59 static void mousekey_console_help(void);
62 static uint8_t numkey2num(uint8_t code);
63 static void switch_default_layer(uint8_t layer);
66 command_state_t command_state = ONESHOT;
69 bool command_proc(uint8_t code)
71 switch (command_state) {
75 return (command_extra(code) || command_common(code));
79 return (command_extra(code) || command_common(code));
81 return (command_console_extra(code) || command_console(code));
83 #ifdef MOUSEKEY_ENABLE
85 mousekey_console(code);
89 command_state = ONESHOT;
95 /* TODO: Refactoring is needed. */
96 /* This allows to define extra commands. return false when not processed. */
97 bool command_extra(uint8_t code) __attribute__ ((weak));
98 bool command_extra(uint8_t code)
104 bool command_console_extra(uint8_t code) __attribute__ ((weak));
105 bool command_console_extra(uint8_t code)
112 /***********************************************************
114 ***********************************************************/
115 static void command_common_help(void)
117 print("\n\t- Magic -\n"
120 "k: debug keyboard\n"
125 "0-4: layer0-4(F10-F4)\n"
128 #ifdef KEYBOARD_LOCK_ENABLE
132 #ifdef BOOTMAGIC_ENABLE
140 #ifdef SLEEP_LED_ENABLE
141 "z: sleep LED test\n"
146 #ifdef BOOTMAGIC_ENABLE
147 static void print_eeconfig(void)
149 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
152 dc.raw = eeconfig_read_debug();
153 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
154 print(".enable: "); print_dec(dc.enable); print("\n");
155 print(".matrix: "); print_dec(dc.matrix); print("\n");
156 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
157 print(".mouse: "); print_dec(dc.mouse); print("\n");
160 kc.raw = eeconfig_read_keymap();
161 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
162 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
163 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
164 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
165 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
166 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
167 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
168 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
169 print(".nkro: "); print_dec(kc.nkro); print("\n");
171 #ifdef BACKLIGHT_ENABLE
172 backlight_config_t bc;
173 bc.raw = eeconfig_read_backlight();
174 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
175 print(".enable: "); print_dec(bc.enable); print("\n");
176 print(".level: "); print_dec(bc.level); print("\n");
181 static bool command_common(uint8_t code)
183 #ifdef KEYBOARD_LOCK_ENABLE
184 static host_driver_t *host_driver = 0;
187 #ifdef SLEEP_LED_ENABLE
189 // test breathing sleep LED
190 print("Sleep LED test\n");
192 led_set(host_keyboard_leds());
195 #ifdef BOOTMAGIC_ENABLE
197 print("eeconfig:\n");
201 #ifdef KEYBOARD_LOCK_ENABLE
203 if (host_get_driver()) {
204 host_driver = host_get_driver();
209 host_set_driver(host_driver);
210 print("Unlocked.\n");
215 case KC_SLASH: /* ? */
216 command_common_help();
219 debug_matrix = false;
220 debug_keyboard = false;
222 debug_enable = false;
223 command_console_help();
225 command_state = CONSOLE;
229 print("\n\nbootloader... ");
231 bootloader_jump(); // not return
235 print("\ndebug: off\n");
236 debug_matrix = false;
237 debug_keyboard = false;
239 debug_enable = false;
241 print("\ndebug: on\n");
245 case KC_X: // debug matrix toggle
246 debug_matrix = !debug_matrix;
248 print("\nmatrix: on\n");
251 print("\nmatrix: off\n");
254 case KC_K: // debug keyboard toggle
255 debug_keyboard = !debug_keyboard;
256 if (debug_keyboard) {
257 print("\nkeyboard: on\n");
260 print("\nkeyboard: off\n");
263 case KC_M: // debug mouse toggle
264 debug_mouse = !debug_mouse;
266 print("\nmouse: on\n");
269 print("\nmouse: off\n");
272 case KC_V: // print version & information
273 print("\n\t- Version -\n");
274 print("DESC: " STR(DESCRIPTION) "\n");
275 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
276 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
277 "VER: " STR(DEVICE_VER) "\n");
278 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
290 #ifdef PROTOCOL_CHIBIOS
293 #ifdef BOOTMAGIC_ENABLE
296 #ifdef MOUSEKEY_ENABLE
299 #ifdef EXTRAKEY_ENABLE
302 #ifdef CONSOLE_ENABLE
305 #ifdef COMMAND_ENABLE
311 #ifdef KEYMAP_SECTION_ENABLE
314 " " STR(BOOTLOADER_SIZE) "\n");
316 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
318 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
319 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
320 #elif defined(__arm__)
326 print("\n\t- Status -\n");
327 print_val_hex8(host_keyboard_leds());
328 print_val_hex8(keyboard_protocol);
329 print_val_hex8(keyboard_idle);
331 print_val_hex8(keyboard_nkro);
333 print_val_hex32(timer_read32());
336 print_val_hex8(UDCON);
337 print_val_hex8(UDIEN);
338 print_val_hex8(UDINT);
339 print_val_hex8(usb_keyboard_leds);
340 print_val_hex8(usb_keyboard_idle_count);
345 print_val_hex8(usbSofCount);
351 clear_keyboard(); //Prevents stuck keys.
352 keyboard_nkro = !keyboard_nkro;
356 print("NKRO: off\n");
364 switch_default_layer(0);
367 switch_default_layer((code - KC_1) + 1);
369 case KC_F1 ... KC_F9:
370 switch_default_layer((code - KC_F1) + 1);
380 /***********************************************************
382 ***********************************************************/
383 static void command_console_help(void)
385 print("\n\t- Console -\n"
387 #ifdef MOUSEKEY_ENABLE
393 static bool command_console(uint8_t code)
397 case KC_SLASH: /* ? */
398 command_console_help();
402 command_state = ONESHOT;
404 #ifdef MOUSEKEY_ENABLE
406 mousekey_console_help();
408 command_state = MOUSEKEY;
420 #ifdef MOUSEKEY_ENABLE
421 /***********************************************************
423 ***********************************************************/
424 static uint8_t mousekey_param = 0;
426 static void mousekey_param_print(void)
428 print("\n\t- Values -\n");
429 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
430 print("2: interval(ms): "); pdec(mk_interval); print("\n");
431 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
432 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
433 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
434 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
437 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
438 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
439 static void mousekey_param_inc(uint8_t param, uint8_t inc)
443 if (mk_delay + inc < UINT8_MAX)
446 mk_delay = UINT8_MAX;
447 PRINT_SET_VAL(mk_delay);
450 if (mk_interval + inc < UINT8_MAX)
453 mk_interval = UINT8_MAX;
454 PRINT_SET_VAL(mk_interval);
457 if (mk_max_speed + inc < UINT8_MAX)
460 mk_max_speed = UINT8_MAX;
461 PRINT_SET_VAL(mk_max_speed);
464 if (mk_time_to_max + inc < UINT8_MAX)
465 mk_time_to_max += inc;
467 mk_time_to_max = UINT8_MAX;
468 PRINT_SET_VAL(mk_time_to_max);
471 if (mk_wheel_max_speed + inc < UINT8_MAX)
472 mk_wheel_max_speed += inc;
474 mk_wheel_max_speed = UINT8_MAX;
475 PRINT_SET_VAL(mk_wheel_max_speed);
478 if (mk_wheel_time_to_max + inc < UINT8_MAX)
479 mk_wheel_time_to_max += inc;
481 mk_wheel_time_to_max = UINT8_MAX;
482 PRINT_SET_VAL(mk_wheel_time_to_max);
487 static void mousekey_param_dec(uint8_t param, uint8_t dec)
495 PRINT_SET_VAL(mk_delay);
498 if (mk_interval > dec)
502 PRINT_SET_VAL(mk_interval);
505 if (mk_max_speed > dec)
509 PRINT_SET_VAL(mk_max_speed);
512 if (mk_time_to_max > dec)
513 mk_time_to_max -= dec;
516 PRINT_SET_VAL(mk_time_to_max);
519 if (mk_wheel_max_speed > dec)
520 mk_wheel_max_speed -= dec;
522 mk_wheel_max_speed = 0;
523 PRINT_SET_VAL(mk_wheel_max_speed);
526 if (mk_wheel_time_to_max > dec)
527 mk_wheel_time_to_max -= dec;
529 mk_wheel_time_to_max = 0;
530 PRINT_SET_VAL(mk_wheel_time_to_max);
535 static void mousekey_console_help(void)
537 print("\n\t- Mousekey -\n"
543 "5: wheel_max_speed\n"
544 "6: wheel_time_to_max\n"
553 "speed = delta * max_speed * (repeat / time_to_max)\n");
554 xprintf("where delta: cursor=%d, wheel=%d\n"
555 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
558 static bool mousekey_console(uint8_t code)
562 case KC_SLASH: /* ? */
563 mousekey_console_help();
567 if (mousekey_param) {
571 command_state = CONSOLE;
576 mousekey_param_print();
584 mousekey_param = numkey2num(code);
587 mousekey_param_inc(mousekey_param, 1);
590 mousekey_param_dec(mousekey_param, 1);
593 mousekey_param_inc(mousekey_param, 10);
596 mousekey_param_dec(mousekey_param, 10);
599 mk_delay = MOUSEKEY_DELAY/10;
600 mk_interval = MOUSEKEY_INTERVAL;
601 mk_max_speed = MOUSEKEY_MAX_SPEED;
602 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
603 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
604 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
605 print("set default\n");
611 if (mousekey_param) {
612 xprintf("M%d> ", mousekey_param);
621 /***********************************************************
623 ***********************************************************/
624 static uint8_t numkey2num(uint8_t code)
641 static void switch_default_layer(uint8_t layer)
643 xprintf("L%d\n", layer);
644 default_layer_set(1UL<<layer);