command.c 21 KB

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