command.c 23 KB

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