command.c 23 KB

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