command.c 22 KB

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