command.c 22 KB

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