command.c 23 KB

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