command.c 22 KB

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