command.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. #ifdef MOUSEKEY_ENABLE
  35. #include "mousekey.h"
  36. #endif
  37. #ifdef PROTOCOL_PJRC
  38. #include "usb_keyboard.h"
  39. #ifdef EXTRAKEY_ENABLE
  40. #include "usb_extra.h"
  41. #endif
  42. #endif
  43. #ifdef PROTOCOL_VUSB
  44. #include "usbdrv.h"
  45. #endif
  46. #ifdef AUDIO_ENABLE
  47. #include "audio.h"
  48. #endif /* AUDIO_ENABLE */
  49. static bool command_common(uint8_t code);
  50. static void command_common_help(void);
  51. static void print_version(void);
  52. static void print_status(void);
  53. static bool command_console(uint8_t code);
  54. static void command_console_help(void);
  55. #ifdef MOUSEKEY_ENABLE
  56. static bool mousekey_console(uint8_t code);
  57. static void mousekey_console_help(void);
  58. #endif
  59. static void switch_default_layer(uint8_t layer);
  60. command_state_t command_state = ONESHOT;
  61. bool command_proc(uint8_t code)
  62. {
  63. switch (command_state) {
  64. case ONESHOT:
  65. if (!IS_COMMAND())
  66. return false;
  67. return (command_extra(code) || command_common(code));
  68. break;
  69. case CONSOLE:
  70. if (IS_COMMAND())
  71. return (command_extra(code) || command_common(code));
  72. else
  73. return (command_console_extra(code) || command_console(code));
  74. break;
  75. #ifdef MOUSEKEY_ENABLE
  76. case MOUSEKEY:
  77. mousekey_console(code);
  78. break;
  79. #endif
  80. default:
  81. command_state = ONESHOT;
  82. return false;
  83. }
  84. return true;
  85. }
  86. /* TODO: Refactoring is needed. */
  87. /* This allows to define extra commands. return false when not processed. */
  88. bool command_extra(uint8_t code) __attribute__ ((weak));
  89. bool command_extra(uint8_t code)
  90. {
  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. {
  97. (void)code;
  98. return false;
  99. }
  100. /***********************************************************
  101. * Command common
  102. ***********************************************************/
  103. static void command_common_help(void)
  104. {
  105. print( "\n\t- Magic -\n"
  106. STR(MAGIC_KEY_DEBUG ) ": Debug Message Toggle\n"
  107. STR(MAGIC_KEY_DEBUG_MATRIX) ": Matrix Debug Mode Toggle - Show keypresses in matrix grid\n"
  108. STR(MAGIC_KEY_DEBUG_KBD ) ": Keyboard Debug Toggle - Show keypress report\n"
  109. STR(MAGIC_KEY_DEBUG_MOUSE ) ": Debug Mouse Toggle\n"
  110. STR(MAGIC_KEY_VERSION ) ": Version\n"
  111. STR(MAGIC_KEY_STATUS ) ": Status\n"
  112. STR(MAGIC_KEY_CONSOLE ) ": Activate Console Mode\n"
  113. #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
  114. STR(MAGIC_KEY_LAYER0 ) ": Switch to Layer 0\n"
  115. STR(MAGIC_KEY_LAYER1 ) ": Switch to Layer 1\n"
  116. STR(MAGIC_KEY_LAYER2 ) ": Switch to Layer 2\n"
  117. STR(MAGIC_KEY_LAYER3 ) ": Switch to Layer 3\n"
  118. STR(MAGIC_KEY_LAYER4 ) ": Switch to Layer 4\n"
  119. STR(MAGIC_KEY_LAYER5 ) ": Switch to Layer 5\n"
  120. STR(MAGIC_KEY_LAYER6 ) ": Switch to Layer 6\n"
  121. STR(MAGIC_KEY_LAYER7 ) ": Switch to Layer 7\n"
  122. STR(MAGIC_KEY_LAYER8 ) ": Switch to Layer 8\n"
  123. STR(MAGIC_KEY_LAYER9 ) ": Switch to Layer 9\n"
  124. #endif
  125. #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
  126. "F1-F10: Switch to Layer 0-9 (F10 = L0)\n"
  127. #endif
  128. #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
  129. "0-9: Switch to Layer 0-9\n"
  130. #endif
  131. STR(MAGIC_KEY_LAYER0_ALT1 ) ": Switch to Layer 0 (alternate key 1)\n"
  132. STR(MAGIC_KEY_LAYER0_ALT2 ) ": Switch to Layer 0 (alternate key 2)\n"
  133. STR(MAGIC_KEY_BOOTLOADER ) ": Jump to Bootloader (Reset)\n"
  134. #ifdef KEYBOARD_LOCK_ENABLE
  135. STR(MAGIC_KEY_LOCK ) ": Lock\n"
  136. #endif
  137. #ifdef BOOTMAGIC_ENABLE
  138. STR(MAGIC_KEY_EEPROM ) ": Print EEPROM Settings\n"
  139. #endif
  140. #ifdef NKRO_ENABLE
  141. STR(MAGIC_KEY_NKRO ) ": NKRO Toggle\n"
  142. #endif
  143. #ifdef SLEEP_LED_ENABLE
  144. STR(MAGIC_KEY_SLEEP_LED ) ": Sleep LED Test\n"
  145. #endif
  146. );
  147. }
  148. static void print_version(void)
  149. {
  150. // print version & information
  151. print("\n\t- Version -\n");
  152. print("DESC: " STR(DESCRIPTION) "\n");
  153. print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
  154. "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
  155. "VER: " STR(DEVICE_VER) "\n");
  156. print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
  157. /* build options */
  158. print("OPTIONS:"
  159. #ifdef PROTOCOL_PJRC
  160. " PJRC"
  161. #endif
  162. #ifdef PROTOCOL_LUFA
  163. " LUFA"
  164. #endif
  165. #ifdef PROTOCOL_VUSB
  166. " VUSB"
  167. #endif
  168. #ifdef BOOTMAGIC_ENABLE
  169. " BOOTMAGIC"
  170. #endif
  171. #ifdef MOUSEKEY_ENABLE
  172. " MOUSEKEY"
  173. #endif
  174. #ifdef EXTRAKEY_ENABLE
  175. " EXTRAKEY"
  176. #endif
  177. #ifdef CONSOLE_ENABLE
  178. " CONSOLE"
  179. #endif
  180. #ifdef COMMAND_ENABLE
  181. " COMMAND"
  182. #endif
  183. #ifdef NKRO_ENABLE
  184. " NKRO"
  185. #endif
  186. #ifdef KEYMAP_SECTION_ENABLE
  187. " KEYMAP_SECTION"
  188. #endif
  189. " " STR(BOOTLOADER_SIZE) "\n");
  190. print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
  191. #if defined(__AVR__)
  192. " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
  193. " AVR_ARCH: avr" STR(__AVR_ARCH__)
  194. #endif
  195. "\n");
  196. return;
  197. }
  198. static void print_status(void)
  199. {
  200. print("\n\t- Status -\n");
  201. print_val_hex8(host_keyboard_leds());
  202. print_val_hex8(keyboard_protocol);
  203. print_val_hex8(keyboard_idle);
  204. #ifdef NKRO_ENABLE
  205. print_val_hex8(keyboard_nkro);
  206. #endif
  207. print_val_hex32(timer_read32());
  208. #ifdef PROTOCOL_PJRC
  209. print_val_hex8(UDCON);
  210. print_val_hex8(UDIEN);
  211. print_val_hex8(UDINT);
  212. print_val_hex8(usb_keyboard_leds);
  213. print_val_hex8(usb_keyboard_idle_count);
  214. #endif
  215. #ifdef PROTOCOL_PJRC
  216. # if USB_COUNT_SOF
  217. print_val_hex8(usbSofCount);
  218. # endif
  219. #endif
  220. return;
  221. }
  222. #ifdef BOOTMAGIC_ENABLE
  223. static void print_eeconfig(void)
  224. {
  225. #ifndef NO_PRINT
  226. print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
  227. debug_config_t dc;
  228. dc.raw = eeconfig_read_debug();
  229. print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
  230. print(".enable: "); print_dec(dc.enable); print("\n");
  231. print(".matrix: "); print_dec(dc.matrix); print("\n");
  232. print(".keyboard: "); print_dec(dc.keyboard); print("\n");
  233. print(".mouse: "); print_dec(dc.mouse); print("\n");
  234. keymap_config_t kc;
  235. kc.raw = eeconfig_read_keymap();
  236. print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
  237. print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
  238. print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
  239. print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
  240. print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
  241. print(".no_gui: "); print_dec(kc.no_gui); print("\n");
  242. print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
  243. print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
  244. print(".nkro: "); print_dec(kc.nkro); print("\n");
  245. #ifdef BACKLIGHT_ENABLE
  246. backlight_config_t bc;
  247. bc.raw = eeconfig_read_backlight();
  248. print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
  249. print(".enable: "); print_dec(bc.enable); print("\n");
  250. print(".level: "); print_dec(bc.level); print("\n");
  251. #endif /* BACKLIGHT_ENABLE */
  252. #endif /* !NO_PRINT */
  253. }
  254. #endif /* BOOTMAGIC_ENABLE */
  255. static bool command_common(uint8_t code)
  256. {
  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. #ifdef BOOTMAGIC_ENABLE
  270. // print stored eeprom config
  271. case MAGIC_KC(MAGIC_KEY_EEPROM):
  272. print("eeconfig:\n");
  273. print_eeconfig();
  274. break;
  275. #endif
  276. #ifdef KEYBOARD_LOCK_ENABLE
  277. // lock/unlock keyboard
  278. case MAGIC_KC(MAGIC_KEY_LOCK):
  279. if (host_get_driver()) {
  280. host_driver = host_get_driver();
  281. clear_keyboard();
  282. host_set_driver(0);
  283. print("Locked.\n");
  284. } else {
  285. host_set_driver(host_driver);
  286. print("Unlocked.\n");
  287. }
  288. break;
  289. #endif
  290. // print help
  291. case MAGIC_KC(MAGIC_KEY_HELP1):
  292. case MAGIC_KC(MAGIC_KEY_HELP2):
  293. command_common_help();
  294. break;
  295. // activate console
  296. case MAGIC_KC(MAGIC_KEY_CONSOLE):
  297. debug_matrix = false;
  298. debug_keyboard = false;
  299. debug_mouse = false;
  300. debug_enable = false;
  301. command_console_help();
  302. print("C> ");
  303. command_state = CONSOLE;
  304. break;
  305. // jump to bootloader
  306. case MAGIC_KC(MAGIC_KEY_BOOTLOADER):
  307. clear_keyboard(); // clear to prevent stuck keys
  308. print("\n\nJumping to bootloader... ");
  309. #ifdef AUDIO_ENABLE
  310. stop_all_notes();
  311. shutdown_user();
  312. #else
  313. wait_ms(1000);
  314. #endif
  315. bootloader_jump(); // not return
  316. break;
  317. // debug toggle
  318. case MAGIC_KC(MAGIC_KEY_DEBUG):
  319. debug_enable = !debug_enable;
  320. if (debug_enable) {
  321. print("\ndebug: on\n");
  322. debug_matrix = true;
  323. debug_keyboard = true;
  324. debug_mouse = true;
  325. } else {
  326. print("\ndebug: off\n");
  327. debug_matrix = false;
  328. debug_keyboard = false;
  329. debug_mouse = false;
  330. }
  331. break;
  332. // debug matrix toggle
  333. case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
  334. debug_matrix = !debug_matrix;
  335. if (debug_matrix) {
  336. print("\nmatrix: on\n");
  337. debug_enable = true;
  338. } else {
  339. print("\nmatrix: off\n");
  340. }
  341. break;
  342. // debug keyboard toggle
  343. case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
  344. debug_keyboard = !debug_keyboard;
  345. if (debug_keyboard) {
  346. print("\nkeyboard: on\n");
  347. debug_enable = true;
  348. } else {
  349. print("\nkeyboard: off\n");
  350. }
  351. break;
  352. // debug mouse toggle
  353. case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
  354. debug_mouse = !debug_mouse;
  355. if (debug_mouse) {
  356. print("\nmouse: on\n");
  357. debug_enable = true;
  358. } else {
  359. print("\nmouse: off\n");
  360. }
  361. break;
  362. // print version
  363. case MAGIC_KC(MAGIC_KEY_VERSION):
  364. print_version();
  365. break;
  366. // print status
  367. case MAGIC_KC(MAGIC_KEY_STATUS):
  368. print_status();
  369. break;
  370. #ifdef NKRO_ENABLE
  371. // NKRO toggle
  372. case MAGIC_KC(MAGIC_KEY_NKRO):
  373. clear_keyboard(); // clear to prevent stuck keys
  374. keyboard_nkro = !keyboard_nkro;
  375. if (keyboard_nkro) {
  376. print("NKRO: on\n");
  377. } else {
  378. print("NKRO: off\n");
  379. }
  380. break;
  381. #endif
  382. // switch layers
  383. case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
  384. case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
  385. switch_default_layer(0);
  386. break;
  387. #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
  388. case MAGIC_KC(MAGIC_KEY_LAYER0):
  389. switch_default_layer(0);
  390. break;
  391. case MAGIC_KC(MAGIC_KEY_LAYER1):
  392. switch_default_layer(1);
  393. break;
  394. case MAGIC_KC(MAGIC_KEY_LAYER2):
  395. switch_default_layer(2);
  396. break;
  397. case MAGIC_KC(MAGIC_KEY_LAYER3):
  398. switch_default_layer(3);
  399. break;
  400. case MAGIC_KC(MAGIC_KEY_LAYER4):
  401. switch_default_layer(4);
  402. break;
  403. case MAGIC_KC(MAGIC_KEY_LAYER5):
  404. switch_default_layer(5);
  405. break;
  406. case MAGIC_KC(MAGIC_KEY_LAYER6):
  407. switch_default_layer(6);
  408. break;
  409. case MAGIC_KC(MAGIC_KEY_LAYER7):
  410. switch_default_layer(7);
  411. break;
  412. case MAGIC_KC(MAGIC_KEY_LAYER8):
  413. switch_default_layer(8);
  414. break;
  415. case MAGIC_KC(MAGIC_KEY_LAYER9):
  416. switch_default_layer(9);
  417. break;
  418. #endif
  419. #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
  420. case KC_F1 ... KC_F9:
  421. switch_default_layer((code - KC_F1) + 1);
  422. break;
  423. case KC_F10:
  424. switch_default_layer(0);
  425. break;
  426. #endif
  427. #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
  428. case KC_1 ... KC_9:
  429. switch_default_layer((code - KC_1) + 1);
  430. break;
  431. case KC_0:
  432. switch_default_layer(0);
  433. break;
  434. #endif
  435. default:
  436. print("?");
  437. return false;
  438. }
  439. return true;
  440. }
  441. /***********************************************************
  442. * Command console
  443. ***********************************************************/
  444. static void command_console_help(void)
  445. {
  446. print("\n\t- Console -\n"
  447. "ESC/q: quit\n"
  448. #ifdef MOUSEKEY_ENABLE
  449. "m: mousekey\n"
  450. #endif
  451. );
  452. }
  453. static bool command_console(uint8_t code)
  454. {
  455. switch (code) {
  456. case KC_H:
  457. case KC_SLASH: /* ? */
  458. command_console_help();
  459. break;
  460. case KC_Q:
  461. case KC_ESC:
  462. command_state = ONESHOT;
  463. return false;
  464. #ifdef MOUSEKEY_ENABLE
  465. case KC_M:
  466. mousekey_console_help();
  467. print("M> ");
  468. command_state = MOUSEKEY;
  469. return true;
  470. #endif
  471. default:
  472. print("?");
  473. return false;
  474. }
  475. print("C> ");
  476. return true;
  477. }
  478. #ifdef MOUSEKEY_ENABLE
  479. /***********************************************************
  480. * Mousekey console
  481. ***********************************************************/
  482. static uint8_t mousekey_param = 0;
  483. static void mousekey_param_print(void)
  484. {
  485. #ifndef NO_PRINT
  486. print("\n\t- Values -\n");
  487. print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
  488. print("2: interval(ms): "); pdec(mk_interval); print("\n");
  489. print("3: max_speed: "); pdec(mk_max_speed); print("\n");
  490. print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
  491. print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  492. print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  493. #endif /* !NO_PRINT */
  494. }
  495. //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  496. #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
  497. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  498. {
  499. switch (param) {
  500. case 1:
  501. if (mk_delay + inc < UINT8_MAX)
  502. mk_delay += inc;
  503. else
  504. mk_delay = UINT8_MAX;
  505. PRINT_SET_VAL(mk_delay);
  506. break;
  507. case 2:
  508. if (mk_interval + inc < UINT8_MAX)
  509. mk_interval += inc;
  510. else
  511. mk_interval = UINT8_MAX;
  512. PRINT_SET_VAL(mk_interval);
  513. break;
  514. case 3:
  515. if (mk_max_speed + inc < UINT8_MAX)
  516. mk_max_speed += inc;
  517. else
  518. mk_max_speed = UINT8_MAX;
  519. PRINT_SET_VAL(mk_max_speed);
  520. break;
  521. case 4:
  522. if (mk_time_to_max + inc < UINT8_MAX)
  523. mk_time_to_max += inc;
  524. else
  525. mk_time_to_max = UINT8_MAX;
  526. PRINT_SET_VAL(mk_time_to_max);
  527. break;
  528. case 5:
  529. if (mk_wheel_max_speed + inc < UINT8_MAX)
  530. mk_wheel_max_speed += inc;
  531. else
  532. mk_wheel_max_speed = UINT8_MAX;
  533. PRINT_SET_VAL(mk_wheel_max_speed);
  534. break;
  535. case 6:
  536. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  537. mk_wheel_time_to_max += inc;
  538. else
  539. mk_wheel_time_to_max = UINT8_MAX;
  540. PRINT_SET_VAL(mk_wheel_time_to_max);
  541. break;
  542. }
  543. }
  544. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  545. {
  546. switch (param) {
  547. case 1:
  548. if (mk_delay > dec)
  549. mk_delay -= dec;
  550. else
  551. mk_delay = 0;
  552. PRINT_SET_VAL(mk_delay);
  553. break;
  554. case 2:
  555. if (mk_interval > dec)
  556. mk_interval -= dec;
  557. else
  558. mk_interval = 0;
  559. PRINT_SET_VAL(mk_interval);
  560. break;
  561. case 3:
  562. if (mk_max_speed > dec)
  563. mk_max_speed -= dec;
  564. else
  565. mk_max_speed = 0;
  566. PRINT_SET_VAL(mk_max_speed);
  567. break;
  568. case 4:
  569. if (mk_time_to_max > dec)
  570. mk_time_to_max -= dec;
  571. else
  572. mk_time_to_max = 0;
  573. PRINT_SET_VAL(mk_time_to_max);
  574. break;
  575. case 5:
  576. if (mk_wheel_max_speed > dec)
  577. mk_wheel_max_speed -= dec;
  578. else
  579. mk_wheel_max_speed = 0;
  580. PRINT_SET_VAL(mk_wheel_max_speed);
  581. break;
  582. case 6:
  583. if (mk_wheel_time_to_max > dec)
  584. mk_wheel_time_to_max -= dec;
  585. else
  586. mk_wheel_time_to_max = 0;
  587. PRINT_SET_VAL(mk_wheel_time_to_max);
  588. break;
  589. }
  590. }
  591. static void mousekey_console_help(void)
  592. {
  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", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
  612. }
  613. static bool mousekey_console(uint8_t code)
  614. {
  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. {
  679. switch (code) {
  680. case KC_1: return 1;
  681. case KC_2: return 2;
  682. case KC_3: return 3;
  683. case KC_4: return 4;
  684. case KC_5: return 5;
  685. case KC_6: return 6;
  686. case KC_7: return 7;
  687. case KC_8: return 8;
  688. case KC_9: return 9;
  689. case KC_0: return 0;
  690. }
  691. return 0;
  692. }
  693. static void switch_default_layer(uint8_t layer)
  694. {
  695. xprintf("L%d\n", layer);
  696. default_layer_set(1UL<<layer);
  697. clear_keyboard();
  698. }