command.c 22 KB

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