qmk

QMK Firmware
git clone git://git.z3bra.org/qmk.git
Log | Files | Refs | Submodules | LICENSE

command.c (23332B)


      1 /*
      2 Copyright 2011 Jun Wako <wakojun@gmail.com>
      3 
      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.
      8 
      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.
     13 
     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/>.
     16 */
     17 #include <stdint.h>
     18 #include <stdbool.h>
     19 #include "wait.h"
     20 #include "keycode.h"
     21 #include "host.h"
     22 #include "keymap.h"
     23 #include "print.h"
     24 #include "debug.h"
     25 #include "util.h"
     26 #include "timer.h"
     27 #include "keyboard.h"
     28 #include "bootloader.h"
     29 #include "action_layer.h"
     30 #include "action_util.h"
     31 #include "eeconfig.h"
     32 #include "sleep_led.h"
     33 #include "led.h"
     34 #include "command.h"
     35 #include "backlight.h"
     36 #include "quantum.h"
     37 #include "version.h"
     38 
     39 #ifdef MOUSEKEY_ENABLE
     40 #    include "mousekey.h"
     41 #endif
     42 
     43 #ifdef PROTOCOL_PJRC
     44 #    include "usb_keyboard.h"
     45 #    ifdef EXTRAKEY_ENABLE
     46 #        include "usb_extra.h"
     47 #    endif
     48 #endif
     49 
     50 #ifdef PROTOCOL_VUSB
     51 #    include "usbdrv.h"
     52 #endif
     53 
     54 #ifdef AUDIO_ENABLE
     55 #    include "audio.h"
     56 #endif /* AUDIO_ENABLE */
     57 
     58 static bool command_common(uint8_t code);
     59 static void command_common_help(void);
     60 static void print_version(void);
     61 static void print_status(void);
     62 static bool command_console(uint8_t code);
     63 static void command_console_help(void);
     64 #ifdef MOUSEKEY_ENABLE
     65 static bool mousekey_console(uint8_t code);
     66 static void mousekey_console_help(void);
     67 #endif
     68 
     69 static void switch_default_layer(uint8_t layer);
     70 
     71 command_state_t command_state = ONESHOT;
     72 
     73 bool command_proc(uint8_t code) {
     74     switch (command_state) {
     75         case ONESHOT:
     76             if (!IS_COMMAND()) return false;
     77             return (command_extra(code) || command_common(code));
     78             break;
     79         case CONSOLE:
     80             if (IS_COMMAND())
     81                 return (command_extra(code) || command_common(code));
     82             else
     83                 return (command_console_extra(code) || command_console(code));
     84             break;
     85 #ifdef MOUSEKEY_ENABLE
     86         case MOUSEKEY:
     87             mousekey_console(code);
     88             break;
     89 #endif
     90         default:
     91             command_state = ONESHOT;
     92             return false;
     93     }
     94     return true;
     95 }
     96 
     97 /* TODO: Refactoring is needed. */
     98 /* This allows to define extra commands. return false when not processed. */
     99 bool command_extra(uint8_t code) __attribute__((weak));
    100 bool command_extra(uint8_t code) {
    101     (void)code;
    102     return false;
    103 }
    104 
    105 bool command_console_extra(uint8_t code) __attribute__((weak));
    106 bool command_console_extra(uint8_t code) {
    107     (void)code;
    108     return false;
    109 }
    110 
    111 /***********************************************************
    112  * Command common
    113  ***********************************************************/
    114 static void command_common_help(void) {
    115     print("\n\t- Magic -\n" STR(MAGIC_KEY_DEBUG) ":	Debug Message Toggle\n" STR(MAGIC_KEY_DEBUG_MATRIX) ":	Matrix Debug Mode Toggle - Show keypresses in matrix grid\n" STR(MAGIC_KEY_DEBUG_KBD) ":	Keyboard Debug Toggle - Show keypress report\n" STR(MAGIC_KEY_DEBUG_MOUSE) ":	Debug Mouse Toggle\n" STR(MAGIC_KEY_VERSION) ":	Version\n" STR(MAGIC_KEY_STATUS) ":	Status\n" STR(MAGIC_KEY_CONSOLE) ":	Activate Console Mode\n"
    116 
    117 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
    118           STR(MAGIC_KEY_LAYER0) ":	Switch to Layer 0\n" STR(MAGIC_KEY_LAYER1) ":	Switch to Layer 1\n" STR(MAGIC_KEY_LAYER2) ":	Switch to Layer 2\n" STR(MAGIC_KEY_LAYER3) ":	Switch to Layer 3\n" STR(MAGIC_KEY_LAYER4) ":	Switch to Layer 4\n" STR(MAGIC_KEY_LAYER5) ":	Switch to Layer 5\n" STR(MAGIC_KEY_LAYER6) ":	Switch to Layer 6\n" STR(MAGIC_KEY_LAYER7) ":	Switch to Layer 7\n" STR(MAGIC_KEY_LAYER8) ":	Switch to Layer 8\n" STR(MAGIC_KEY_LAYER9) ":	Switch to Layer 9\n"
    119 #endif
    120 
    121 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
    122                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "F1-F10:	Switch to Layer 0-9 (F10 = L0)\n"
    123 #endif
    124 
    125 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
    126                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "0-9:	Switch to Layer 0-9\n"
    127 #endif
    128 
    129           STR(MAGIC_KEY_LAYER0_ALT) ":	Switch to Layer 0 (alternate)\n"
    130 
    131           STR(MAGIC_KEY_BOOTLOADER) ":	Jump to Bootloader\n" STR(MAGIC_KEY_BOOTLOADER_ALT) ":	Jump to Bootloader (alternate)\n"
    132 
    133 #ifdef KEYBOARD_LOCK_ENABLE
    134           STR(MAGIC_KEY_LOCK) ":	Lock Keyboard\n"
    135 #endif
    136 
    137           STR(MAGIC_KEY_EEPROM) ":	Print EEPROM Settings\n" STR(MAGIC_KEY_EEPROM_CLEAR) ":	Clear EEPROM\n"
    138 
    139 #ifdef NKRO_ENABLE
    140           STR(MAGIC_KEY_NKRO) ":	NKRO Toggle\n"
    141 #endif
    142 
    143 #ifdef SLEEP_LED_ENABLE
    144           STR(MAGIC_KEY_SLEEP_LED) ":	Sleep LED Test\n"
    145 #endif
    146     );
    147 }
    148 
    149 static void print_version(void) {
    150     // print version & information
    151     print("\n\t- Version -\n");
    152     print("DESC: " STR(DESCRIPTION) "\n");
    153     print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
    154                                                        "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
    155                                                                                                 "VER: " STR(DEVICE_VER) "\n");
    156 #ifdef SKIP_VERSION
    157     print("BUILD:  (" __DATE__ ")\n");
    158 #else
    159     print("BUILD: " STR(QMK_VERSION) " (" __TIME__ " " __DATE__ ")\n");
    160 #endif
    161 
    162     /* build options */
    163     print("OPTIONS:"
    164 
    165 #ifdef PROTOCOL_PJRC
    166           " PJRC"
    167 #endif
    168 #ifdef PROTOCOL_LUFA
    169           " LUFA"
    170 #endif
    171 #ifdef PROTOCOL_VUSB
    172           " VUSB"
    173 #endif
    174 #ifdef BOOTMAGIC_ENABLE
    175           " BOOTMAGIC"
    176 #endif
    177 #ifdef MOUSEKEY_ENABLE
    178           " MOUSEKEY"
    179 #endif
    180 #ifdef EXTRAKEY_ENABLE
    181           " EXTRAKEY"
    182 #endif
    183 #ifdef CONSOLE_ENABLE
    184           " CONSOLE"
    185 #endif
    186 #ifdef COMMAND_ENABLE
    187           " COMMAND"
    188 #endif
    189 #ifdef NKRO_ENABLE
    190           " NKRO"
    191 #endif
    192 #ifdef KEYMAP_SECTION_ENABLE
    193           " KEYMAP_SECTION"
    194 #endif
    195 
    196           " " STR(BOOTLOADER_SIZE) "\n");
    197 
    198     print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
    199 #if defined(__AVR__)
    200               " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ " AVR_ARCH: avr" STR(__AVR_ARCH__)
    201 #endif
    202                   "\n");
    203 
    204     return;
    205 }
    206 
    207 static void print_status(void) {
    208     print("\n\t- Status -\n");
    209 
    210     print_val_hex8(host_keyboard_leds());
    211 #ifndef PROTOCOL_VUSB
    212     // these aren't set on the V-USB protocol, so we just ignore them for now
    213     print_val_hex8(keyboard_protocol);
    214     print_val_hex8(keyboard_idle);
    215 #endif
    216 #ifdef NKRO_ENABLE
    217     print_val_hex8(keymap_config.nkro);
    218 #endif
    219     print_val_hex32(timer_read32());
    220 
    221 #ifdef PROTOCOL_PJRC
    222     print_val_hex8(UDCON);
    223     print_val_hex8(UDIEN);
    224     print_val_hex8(UDINT);
    225     print_val_hex8(usb_keyboard_leds);
    226     print_val_hex8(usb_keyboard_idle_count);
    227 #endif
    228 
    229 #ifdef PROTOCOL_PJRC
    230 #    if USB_COUNT_SOF
    231     print_val_hex8(usbSofCount);
    232 #    endif
    233 #endif
    234     return;
    235 }
    236 
    237 static void print_eeconfig(void) {
    238 // Print these variables if NO_PRINT or USER_PRINT are not defined.
    239 #if !defined(NO_PRINT) && !defined(USER_PRINT)
    240 
    241     print("default_layer: ");
    242     print_dec(eeconfig_read_default_layer());
    243     print("\n");
    244 
    245     debug_config_t dc;
    246     dc.raw = eeconfig_read_debug();
    247     print("debug_config.raw: ");
    248     print_hex8(dc.raw);
    249     print("\n");
    250     print(".enable: ");
    251     print_dec(dc.enable);
    252     print("\n");
    253     print(".matrix: ");
    254     print_dec(dc.matrix);
    255     print("\n");
    256     print(".keyboard: ");
    257     print_dec(dc.keyboard);
    258     print("\n");
    259     print(".mouse: ");
    260     print_dec(dc.mouse);
    261     print("\n");
    262 
    263     keymap_config_t kc;
    264     kc.raw = eeconfig_read_keymap();
    265     print("keymap_config.raw: ");
    266     print_hex8(kc.raw);
    267     print("\n");
    268     print(".swap_control_capslock: ");
    269     print_dec(kc.swap_control_capslock);
    270     print("\n");
    271     print(".capslock_to_control: ");
    272     print_dec(kc.capslock_to_control);
    273     print("\n");
    274     print(".swap_lctl_lgui: ");
    275     print_dec(kc.swap_lctl_lgui);
    276     print("\n");
    277     print(".swap_rctl_rgui: ");
    278     print_dec(kc.swap_rctl_rgui);
    279     print("\n");
    280     print(".swap_lalt_lgui: ");
    281     print_dec(kc.swap_lalt_lgui);
    282     print("\n");
    283     print(".swap_ralt_rgui: ");
    284     print_dec(kc.swap_ralt_rgui);
    285     print("\n");
    286     print(".no_gui: ");
    287     print_dec(kc.no_gui);
    288     print("\n");
    289     print(".swap_grave_esc: ");
    290     print_dec(kc.swap_grave_esc);
    291     print("\n");
    292     print(".swap_backslash_backspace: ");
    293     print_dec(kc.swap_backslash_backspace);
    294     print("\n");
    295     print(".nkro: ");
    296     print_dec(kc.nkro);
    297     print("\n");
    298 
    299 #    ifdef BACKLIGHT_ENABLE
    300     backlight_config_t bc;
    301     bc.raw = eeconfig_read_backlight();
    302     print("backlight_config.raw: ");
    303     print_hex8(bc.raw);
    304     print("\n");
    305     print(".enable: ");
    306     print_dec(bc.enable);
    307     print("\n");
    308     print(".level: ");
    309     print_dec(bc.level);
    310     print("\n");
    311 #    endif /* BACKLIGHT_ENABLE */
    312 
    313 #endif /* !NO_PRINT */
    314 }
    315 
    316 static bool command_common(uint8_t code) {
    317 #ifdef KEYBOARD_LOCK_ENABLE
    318     static host_driver_t *host_driver = 0;
    319 #endif
    320 
    321     switch (code) {
    322 #ifdef SLEEP_LED_ENABLE
    323 
    324         // test breathing sleep LED
    325         case MAGIC_KC(MAGIC_KEY_SLEEP_LED):
    326             print("Sleep LED Test\n");
    327             sleep_led_toggle();
    328             led_set(host_keyboard_leds());
    329             break;
    330 #endif
    331 
    332         // print stored eeprom config
    333         case MAGIC_KC(MAGIC_KEY_EEPROM):
    334             print("eeconfig:\n");
    335             print_eeconfig();
    336             break;
    337 
    338         // clear eeprom
    339         case MAGIC_KC(MAGIC_KEY_EEPROM_CLEAR):
    340             print("Clearing EEPROM\n");
    341             eeconfig_init();
    342             break;
    343 
    344 #ifdef KEYBOARD_LOCK_ENABLE
    345 
    346         // lock/unlock keyboard
    347         case MAGIC_KC(MAGIC_KEY_LOCK):
    348             if (host_get_driver()) {
    349                 host_driver = host_get_driver();
    350                 clear_keyboard();
    351                 host_set_driver(0);
    352                 print("Locked.\n");
    353             } else {
    354                 host_set_driver(host_driver);
    355                 print("Unlocked.\n");
    356             }
    357             break;
    358 #endif
    359 
    360         // print help
    361         case MAGIC_KC(MAGIC_KEY_HELP):
    362         case MAGIC_KC(MAGIC_KEY_HELP_ALT):
    363             command_common_help();
    364             break;
    365 
    366         // activate console
    367         case MAGIC_KC(MAGIC_KEY_CONSOLE):
    368             debug_matrix   = false;
    369             debug_keyboard = false;
    370             debug_mouse    = false;
    371             debug_enable   = false;
    372             command_console_help();
    373             print("C> ");
    374             command_state = CONSOLE;
    375             break;
    376 
    377         // jump to bootloader
    378         case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
    379         case MAGIC_KC(MAGIC_KEY_BOOTLOADER_ALT):
    380             clear_keyboard();  // clear to prevent stuck keys
    381             print("\n\nJumping to bootloader... ");
    382 #ifdef AUDIO_ENABLE
    383             stop_all_notes();
    384             shutdown_user();
    385 #else
    386             wait_ms(1000);
    387 #endif
    388             bootloader_jump();  // not return
    389             break;
    390 
    391         // debug toggle
    392         case MAGIC_KC(MAGIC_KEY_DEBUG):
    393             debug_enable = !debug_enable;
    394             if (debug_enable) {
    395                 print("\ndebug: on\n");
    396             } else {
    397                 print("\ndebug: off\n");
    398                 debug_matrix   = false;
    399                 debug_keyboard = false;
    400                 debug_mouse    = false;
    401             }
    402             break;
    403 
    404         // debug matrix toggle
    405         case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
    406             debug_matrix = !debug_matrix;
    407             if (debug_matrix) {
    408                 print("\nmatrix: on\n");
    409                 debug_enable = true;
    410             } else {
    411                 print("\nmatrix: off\n");
    412             }
    413             break;
    414 
    415         // debug keyboard toggle
    416         case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
    417             debug_keyboard = !debug_keyboard;
    418             if (debug_keyboard) {
    419                 print("\nkeyboard: on\n");
    420                 debug_enable = true;
    421             } else {
    422                 print("\nkeyboard: off\n");
    423             }
    424             break;
    425 
    426         // debug mouse toggle
    427         case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
    428             debug_mouse = !debug_mouse;
    429             if (debug_mouse) {
    430                 print("\nmouse: on\n");
    431                 debug_enable = true;
    432             } else {
    433                 print("\nmouse: off\n");
    434             }
    435             break;
    436 
    437         // print version
    438         case MAGIC_KC(MAGIC_KEY_VERSION):
    439             print_version();
    440             break;
    441 
    442         // print status
    443         case MAGIC_KC(MAGIC_KEY_STATUS):
    444             print_status();
    445             break;
    446 
    447 #ifdef NKRO_ENABLE
    448 
    449         // NKRO toggle
    450         case MAGIC_KC(MAGIC_KEY_NKRO):
    451             clear_keyboard();  // clear to prevent stuck keys
    452             keymap_config.nkro = !keymap_config.nkro;
    453             if (keymap_config.nkro) {
    454                 print("NKRO: on\n");
    455             } else {
    456                 print("NKRO: off\n");
    457             }
    458             break;
    459 #endif
    460 
    461             // switch layers
    462 
    463         case MAGIC_KC(MAGIC_KEY_LAYER0_ALT):
    464             switch_default_layer(0);
    465             break;
    466 
    467 #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
    468 
    469         case MAGIC_KC(MAGIC_KEY_LAYER0):
    470             switch_default_layer(0);
    471             break;
    472 
    473         case MAGIC_KC(MAGIC_KEY_LAYER1):
    474             switch_default_layer(1);
    475             break;
    476 
    477         case MAGIC_KC(MAGIC_KEY_LAYER2):
    478             switch_default_layer(2);
    479             break;
    480 
    481         case MAGIC_KC(MAGIC_KEY_LAYER3):
    482             switch_default_layer(3);
    483             break;
    484 
    485         case MAGIC_KC(MAGIC_KEY_LAYER4):
    486             switch_default_layer(4);
    487             break;
    488 
    489         case MAGIC_KC(MAGIC_KEY_LAYER5):
    490             switch_default_layer(5);
    491             break;
    492 
    493         case MAGIC_KC(MAGIC_KEY_LAYER6):
    494             switch_default_layer(6);
    495             break;
    496 
    497         case MAGIC_KC(MAGIC_KEY_LAYER7):
    498             switch_default_layer(7);
    499             break;
    500 
    501         case MAGIC_KC(MAGIC_KEY_LAYER8):
    502             switch_default_layer(8);
    503             break;
    504 
    505         case MAGIC_KC(MAGIC_KEY_LAYER9):
    506             switch_default_layer(9);
    507             break;
    508 #endif
    509 
    510 #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
    511 
    512         case KC_F1 ... KC_F9:
    513             switch_default_layer((code - KC_F1) + 1);
    514             break;
    515         case KC_F10:
    516             switch_default_layer(0);
    517             break;
    518 #endif
    519 
    520 #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
    521 
    522         case KC_1 ... KC_9:
    523             switch_default_layer((code - KC_1) + 1);
    524             break;
    525         case KC_0:
    526             switch_default_layer(0);
    527             break;
    528 #endif
    529 
    530         default:
    531             print("?");
    532             return false;
    533     }
    534     return true;
    535 }
    536 
    537 /***********************************************************
    538  * Command console
    539  ***********************************************************/
    540 static void command_console_help(void) {
    541     print("\n\t- Console -\n"
    542           "ESC/q:	quit\n"
    543 #ifdef MOUSEKEY_ENABLE
    544           "m:	mousekey\n"
    545 #endif
    546     );
    547 }
    548 
    549 static bool command_console(uint8_t code) {
    550     switch (code) {
    551         case KC_H:
    552         case KC_SLASH: /* ? */
    553             command_console_help();
    554             break;
    555         case KC_Q:
    556         case KC_ESC:
    557             command_state = ONESHOT;
    558             return false;
    559 #ifdef MOUSEKEY_ENABLE
    560         case KC_M:
    561             mousekey_console_help();
    562             print("M> ");
    563             command_state = MOUSEKEY;
    564             return true;
    565 #endif
    566         default:
    567             print("?");
    568             return false;
    569     }
    570     print("C> ");
    571     return true;
    572 }
    573 
    574 #ifdef MOUSEKEY_ENABLE
    575 /***********************************************************
    576  * Mousekey console
    577  ***********************************************************/
    578 static uint8_t mousekey_param = 0;
    579 
    580 static void mousekey_param_print(void) {
    581 // Print these variables if NO_PRINT or USER_PRINT are not defined.
    582 #    if !defined(NO_PRINT) && !defined(USER_PRINT)
    583     print("\n\t- Values -\n");
    584     print("1: delay(*10ms): ");
    585     pdec(mk_delay);
    586     print("\n");
    587     print("2: interval(ms): ");
    588     pdec(mk_interval);
    589     print("\n");
    590     print("3: max_speed: ");
    591     pdec(mk_max_speed);
    592     print("\n");
    593     print("4: time_to_max: ");
    594     pdec(mk_time_to_max);
    595     print("\n");
    596     print("5: wheel_max_speed: ");
    597     pdec(mk_wheel_max_speed);
    598     print("\n");
    599     print("6: wheel_time_to_max: ");
    600     pdec(mk_wheel_time_to_max);
    601     print("\n");
    602 #    endif /* !NO_PRINT */
    603 }
    604 
    605 //#define PRINT_SET_VAL(v)  print(#v " = "); print_dec(v); print("\n");
    606 #    define PRINT_SET_VAL(v) xprintf(#    v " = %d\n", (v))
    607 static void mousekey_param_inc(uint8_t param, uint8_t inc) {
    608     switch (param) {
    609         case 1:
    610             if (mk_delay + inc < UINT8_MAX)
    611                 mk_delay += inc;
    612             else
    613                 mk_delay = UINT8_MAX;
    614             PRINT_SET_VAL(mk_delay);
    615             break;
    616         case 2:
    617             if (mk_interval + inc < UINT8_MAX)
    618                 mk_interval += inc;
    619             else
    620                 mk_interval = UINT8_MAX;
    621             PRINT_SET_VAL(mk_interval);
    622             break;
    623         case 3:
    624             if (mk_max_speed + inc < UINT8_MAX)
    625                 mk_max_speed += inc;
    626             else
    627                 mk_max_speed = UINT8_MAX;
    628             PRINT_SET_VAL(mk_max_speed);
    629             break;
    630         case 4:
    631             if (mk_time_to_max + inc < UINT8_MAX)
    632                 mk_time_to_max += inc;
    633             else
    634                 mk_time_to_max = UINT8_MAX;
    635             PRINT_SET_VAL(mk_time_to_max);
    636             break;
    637         case 5:
    638             if (mk_wheel_max_speed + inc < UINT8_MAX)
    639                 mk_wheel_max_speed += inc;
    640             else
    641                 mk_wheel_max_speed = UINT8_MAX;
    642             PRINT_SET_VAL(mk_wheel_max_speed);
    643             break;
    644         case 6:
    645             if (mk_wheel_time_to_max + inc < UINT8_MAX)
    646                 mk_wheel_time_to_max += inc;
    647             else
    648                 mk_wheel_time_to_max = UINT8_MAX;
    649             PRINT_SET_VAL(mk_wheel_time_to_max);
    650             break;
    651     }
    652 }
    653 
    654 static void mousekey_param_dec(uint8_t param, uint8_t dec) {
    655     switch (param) {
    656         case 1:
    657             if (mk_delay > dec)
    658                 mk_delay -= dec;
    659             else
    660                 mk_delay = 0;
    661             PRINT_SET_VAL(mk_delay);
    662             break;
    663         case 2:
    664             if (mk_interval > dec)
    665                 mk_interval -= dec;
    666             else
    667                 mk_interval = 0;
    668             PRINT_SET_VAL(mk_interval);
    669             break;
    670         case 3:
    671             if (mk_max_speed > dec)
    672                 mk_max_speed -= dec;
    673             else
    674                 mk_max_speed = 0;
    675             PRINT_SET_VAL(mk_max_speed);
    676             break;
    677         case 4:
    678             if (mk_time_to_max > dec)
    679                 mk_time_to_max -= dec;
    680             else
    681                 mk_time_to_max = 0;
    682             PRINT_SET_VAL(mk_time_to_max);
    683             break;
    684         case 5:
    685             if (mk_wheel_max_speed > dec)
    686                 mk_wheel_max_speed -= dec;
    687             else
    688                 mk_wheel_max_speed = 0;
    689             PRINT_SET_VAL(mk_wheel_max_speed);
    690             break;
    691         case 6:
    692             if (mk_wheel_time_to_max > dec)
    693                 mk_wheel_time_to_max -= dec;
    694             else
    695                 mk_wheel_time_to_max = 0;
    696             PRINT_SET_VAL(mk_wheel_time_to_max);
    697             break;
    698     }
    699 }
    700 
    701 static void mousekey_console_help(void) {
    702     print("\n\t- Mousekey -\n"
    703           "ESC/q:	quit\n"
    704           "1:	delay(*10ms)\n"
    705           "2:	interval(ms)\n"
    706           "3:	max_speed\n"
    707           "4:	time_to_max\n"
    708           "5:	wheel_max_speed\n"
    709           "6:	wheel_time_to_max\n"
    710           "\n"
    711           "p:	print values\n"
    712           "d:	set defaults\n"
    713           "up:	+1\n"
    714           "down:	-1\n"
    715           "pgup:	+10\n"
    716           "pgdown:	-10\n"
    717           "\n"
    718           "speed = delta * max_speed * (repeat / time_to_max)\n");
    719     xprintf("where delta: cursor=%d, wheel=%d\n"
    720             "See http://en.wikipedia.org/wiki/Mouse_keys\n",
    721             MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
    722 }
    723 
    724 static bool mousekey_console(uint8_t code) {
    725     switch (code) {
    726         case KC_H:
    727         case KC_SLASH: /* ? */
    728             mousekey_console_help();
    729             break;
    730         case KC_Q:
    731         case KC_ESC:
    732             if (mousekey_param) {
    733                 mousekey_param = 0;
    734             } else {
    735                 print("C> ");
    736                 command_state = CONSOLE;
    737                 return false;
    738             }
    739             break;
    740         case KC_P:
    741             mousekey_param_print();
    742             break;
    743         case KC_1:
    744         case KC_2:
    745         case KC_3:
    746         case KC_4:
    747         case KC_5:
    748         case KC_6:
    749             mousekey_param = numkey2num(code);
    750             break;
    751         case KC_UP:
    752             mousekey_param_inc(mousekey_param, 1);
    753             break;
    754         case KC_DOWN:
    755             mousekey_param_dec(mousekey_param, 1);
    756             break;
    757         case KC_PGUP:
    758             mousekey_param_inc(mousekey_param, 10);
    759             break;
    760         case KC_PGDN:
    761             mousekey_param_dec(mousekey_param, 10);
    762             break;
    763         case KC_D:
    764             mk_delay             = MOUSEKEY_DELAY / 10;
    765             mk_interval          = MOUSEKEY_INTERVAL;
    766             mk_max_speed         = MOUSEKEY_MAX_SPEED;
    767             mk_time_to_max       = MOUSEKEY_TIME_TO_MAX;
    768             mk_wheel_max_speed   = MOUSEKEY_WHEEL_MAX_SPEED;
    769             mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
    770             print("set default\n");
    771             break;
    772         default:
    773             print("?");
    774             return false;
    775     }
    776     if (mousekey_param) {
    777         xprintf("M%d> ", mousekey_param);
    778     } else {
    779         print("M>");
    780     }
    781     return true;
    782 }
    783 #endif
    784 
    785 /***********************************************************
    786  * Utilities
    787  ***********************************************************/
    788 uint8_t numkey2num(uint8_t code) {
    789     switch (code) {
    790         case KC_1:
    791             return 1;
    792         case KC_2:
    793             return 2;
    794         case KC_3:
    795             return 3;
    796         case KC_4:
    797             return 4;
    798         case KC_5:
    799             return 5;
    800         case KC_6:
    801             return 6;
    802         case KC_7:
    803             return 7;
    804         case KC_8:
    805             return 8;
    806         case KC_9:
    807             return 9;
    808         case KC_0:
    809             return 0;
    810     }
    811     return 0;
    812 }
    813 
    814 static void switch_default_layer(uint8_t layer) {
    815     xprintf("L%d\n", layer);
    816     default_layer_set(1UL << layer);
    817     clear_keyboard();
    818 }