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 "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)
103 bool command_console_extra(uint8_t code) __attribute__ ((weak));
104 bool command_console_extra(uint8_t code)
110 /***********************************************************
112 ***********************************************************/
113 static void command_common_help(void)
115 print("\n\t- Magic -\n"
118 "k: debug keyboard\n"
123 "0-4: layer0-4(F10-F4)\n"
126 #ifdef KEYBOARD_LOCK_ENABLE
130 #ifdef BOOTMAGIC_ENABLE
138 #ifdef SLEEP_LED_ENABLE
139 "z: sleep LED test\n"
144 #ifdef BOOTMAGIC_ENABLE
145 static void print_eeconfig(void)
147 print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
150 dc.raw = eeconfig_read_debug();
151 print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
152 print(".enable: "); print_dec(dc.enable); print("\n");
153 print(".matrix: "); print_dec(dc.matrix); print("\n");
154 print(".keyboard: "); print_dec(dc.keyboard); print("\n");
155 print(".mouse: "); print_dec(dc.mouse); print("\n");
158 kc.raw = eeconfig_read_keymap();
159 print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
160 print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
161 print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
162 print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
163 print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
164 print(".no_gui: "); print_dec(kc.no_gui); print("\n");
165 print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
166 print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
167 print(".nkro: "); print_dec(kc.nkro); print("\n");
169 #ifdef BACKLIGHT_ENABLE
170 backlight_config_t bc;
171 bc.raw = eeconfig_read_backlight();
172 print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
173 print(".enable: "); print_dec(bc.enable); print("\n");
174 print(".level: "); print_dec(bc.level); print("\n");
179 static bool command_common(uint8_t code)
181 static host_driver_t *host_driver = 0;
183 #ifdef SLEEP_LED_ENABLE
185 // test breathing sleep LED
186 print("Sleep LED test\n");
188 led_set(host_keyboard_leds());
191 #ifdef BOOTMAGIC_ENABLE
193 print("eeconfig:\n");
197 #ifdef KEYBOARD_LOCK_ENABLE
199 if (host_get_driver()) {
200 host_driver = host_get_driver();
205 host_set_driver(host_driver);
206 print("Unlocked.\n");
211 case KC_SLASH: /* ? */
212 command_common_help();
215 debug_matrix = false;
216 debug_keyboard = false;
218 debug_enable = false;
219 command_console_help();
221 command_state = CONSOLE;
225 print("\n\nbootloader... ");
227 bootloader_jump(); // not return
231 print("\ndebug: off\n");
232 debug_matrix = false;
233 debug_keyboard = false;
235 debug_enable = false;
237 print("\ndebug: on\n");
241 case KC_X: // debug matrix toggle
242 debug_matrix = !debug_matrix;
244 print("\nmatrix: on\n");
247 print("\nmatrix: off\n");
250 case KC_K: // debug keyboard toggle
251 debug_keyboard = !debug_keyboard;
252 if (debug_keyboard) {
253 print("\nkeyboard: on\n");
256 print("\nkeyboard: off\n");
259 case KC_M: // debug mouse toggle
260 debug_mouse = !debug_mouse;
262 print("\nmouse: on\n");
265 print("\nmouse: off\n");
268 case KC_V: // print version & information
269 print("\n\t- Version -\n");
270 print("DESC: " STR(DESCRIPTION) "\n");
271 print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
272 "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
273 "VER: " STR(DEVICE_VER) "\n");
274 print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
286 #ifdef BOOTMAGIC_ENABLE
289 #ifdef MOUSEKEY_ENABLE
292 #ifdef EXTRAKEY_ENABLE
295 #ifdef CONSOLE_ENABLE
298 #ifdef COMMAND_ENABLE
304 #ifdef KEYMAP_SECTION_ENABLE
307 " " STR(BOOTLOADER_SIZE) "\n");
309 print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
310 " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
311 " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
314 print("\n\t- Status -\n");
315 print_val_hex8(host_keyboard_leds());
316 print_val_hex8(keyboard_protocol);
317 print_val_hex8(keyboard_idle);
319 print_val_hex8(keyboard_nkro);
321 print_val_hex32(timer_count);
324 print_val_hex8(UDCON);
325 print_val_hex8(UDIEN);
326 print_val_hex8(UDINT);
327 print_val_hex8(usb_keyboard_leds);
328 print_val_hex8(usb_keyboard_idle_count);
333 print_val_hex8(usbSofCount);
339 clear_keyboard(); //Prevents stuck keys.
340 keyboard_nkro = !keyboard_nkro;
344 print("NKRO: off\n");
351 switch_default_layer(0);
354 switch_default_layer((code - KC_1) + 1);
356 case KC_F1 ... KC_F9:
357 switch_default_layer((code - KC_F1) + 1);
367 /***********************************************************
369 ***********************************************************/
370 static void command_console_help(void)
372 print("\n\t- Console -\n"
374 #ifdef MOUSEKEY_ENABLE
380 static bool command_console(uint8_t code)
384 case KC_SLASH: /* ? */
385 command_console_help();
389 command_state = ONESHOT;
391 #ifdef MOUSEKEY_ENABLE
393 mousekey_console_help();
395 command_state = MOUSEKEY;
407 #ifdef MOUSEKEY_ENABLE
408 /***********************************************************
410 ***********************************************************/
411 static uint8_t mousekey_param = 0;
413 static void mousekey_param_print(void)
415 print("\n\t- Values -\n");
416 print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
417 print("2: interval(ms): "); pdec(mk_interval); print("\n");
418 print("3: max_speed: "); pdec(mk_max_speed); print("\n");
419 print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
420 print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
421 print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
424 //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
425 #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
426 static void mousekey_param_inc(uint8_t param, uint8_t inc)
430 if (mk_delay + inc < UINT8_MAX)
433 mk_delay = UINT8_MAX;
434 PRINT_SET_VAL(mk_delay);
437 if (mk_interval + inc < UINT8_MAX)
440 mk_interval = UINT8_MAX;
441 PRINT_SET_VAL(mk_interval);
444 if (mk_max_speed + inc < UINT8_MAX)
447 mk_max_speed = UINT8_MAX;
448 PRINT_SET_VAL(mk_max_speed);
451 if (mk_time_to_max + inc < UINT8_MAX)
452 mk_time_to_max += inc;
454 mk_time_to_max = UINT8_MAX;
455 PRINT_SET_VAL(mk_time_to_max);
458 if (mk_wheel_max_speed + inc < UINT8_MAX)
459 mk_wheel_max_speed += inc;
461 mk_wheel_max_speed = UINT8_MAX;
462 PRINT_SET_VAL(mk_wheel_max_speed);
465 if (mk_wheel_time_to_max + inc < UINT8_MAX)
466 mk_wheel_time_to_max += inc;
468 mk_wheel_time_to_max = UINT8_MAX;
469 PRINT_SET_VAL(mk_wheel_time_to_max);
474 static void mousekey_param_dec(uint8_t param, uint8_t dec)
482 PRINT_SET_VAL(mk_delay);
485 if (mk_interval > dec)
489 PRINT_SET_VAL(mk_interval);
492 if (mk_max_speed > dec)
496 PRINT_SET_VAL(mk_max_speed);
499 if (mk_time_to_max > dec)
500 mk_time_to_max -= dec;
503 PRINT_SET_VAL(mk_time_to_max);
506 if (mk_wheel_max_speed > dec)
507 mk_wheel_max_speed -= dec;
509 mk_wheel_max_speed = 0;
510 PRINT_SET_VAL(mk_wheel_max_speed);
513 if (mk_wheel_time_to_max > dec)
514 mk_wheel_time_to_max -= dec;
516 mk_wheel_time_to_max = 0;
517 PRINT_SET_VAL(mk_wheel_time_to_max);
522 static void mousekey_console_help(void)
524 print("\n\t- Mousekey -\n"
530 "5: wheel_max_speed\n"
531 "6: wheel_time_to_max\n"
540 "speed = delta * max_speed * (repeat / time_to_max)\n");
541 xprintf("where delta: cursor=%d, wheel=%d\n"
542 "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
545 static bool mousekey_console(uint8_t code)
549 case KC_SLASH: /* ? */
550 mousekey_console_help();
554 if (mousekey_param) {
558 command_state = CONSOLE;
563 mousekey_param_print();
571 mousekey_param = numkey2num(code);
574 mousekey_param_inc(mousekey_param, 1);
577 mousekey_param_dec(mousekey_param, 1);
580 mousekey_param_inc(mousekey_param, 10);
583 mousekey_param_dec(mousekey_param, 10);
586 mk_delay = MOUSEKEY_DELAY/10;
587 mk_interval = MOUSEKEY_INTERVAL;
588 mk_max_speed = MOUSEKEY_MAX_SPEED;
589 mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
590 mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
591 mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
592 print("set default\n");
599 xprintf("M%d> ", mousekey_param);
607 /***********************************************************
609 ***********************************************************/
610 static uint8_t numkey2num(uint8_t code)
627 static void switch_default_layer(uint8_t layer)
629 xprintf("L%d\n", layer);
630 default_layer_set(1UL<<layer);