command.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  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. #ifdef MOUSEKEY_ENABLE
  34. #include "mousekey.h"
  35. #endif
  36. #ifdef PROTOCOL_PJRC
  37. # include "usb_keyboard.h"
  38. # ifdef EXTRAKEY_ENABLE
  39. # include "usb_extra.h"
  40. # endif
  41. #endif
  42. #ifdef PROTOCOL_VUSB
  43. # include "usbdrv.h"
  44. #endif
  45. #ifdef AUDIO_ENABLE
  46. #include "audio.h"
  47. #endif /* AUDIO_ENABLE */
  48. static bool command_common(uint8_t code);
  49. static void command_common_help(void);
  50. static void print_version(void);
  51. static void print_status(void);
  52. static bool command_console(uint8_t code);
  53. static void command_console_help(void);
  54. #ifdef MOUSEKEY_ENABLE
  55. static bool mousekey_console(uint8_t code);
  56. static void mousekey_console_help(void);
  57. #endif
  58. static uint8_t numkey2num(uint8_t code);
  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. play_goodbye_tone();
  306. #endif
  307. _delay_ms(1000);
  308. bootloader_jump(); // not return
  309. break;
  310. // debug toggle
  311. case MAGIC_KC(MAGIC_KEY_DEBUG):
  312. debug_enable = !debug_enable;
  313. if (debug_enable) {
  314. print("\ndebug: on\n");
  315. debug_matrix = true;
  316. debug_keyboard = true;
  317. debug_mouse = true;
  318. } else {
  319. print("\ndebug: off\n");
  320. debug_matrix = false;
  321. debug_keyboard = false;
  322. debug_mouse = false;
  323. }
  324. break;
  325. // debug matrix toggle
  326. case MAGIC_KC(MAGIC_KEY_DEBUG_MATRIX):
  327. debug_matrix = !debug_matrix;
  328. if (debug_matrix) {
  329. print("\nmatrix: on\n");
  330. debug_enable = true;
  331. } else {
  332. print("\nmatrix: off\n");
  333. }
  334. break;
  335. // debug keyboard toggle
  336. case MAGIC_KC(MAGIC_KEY_DEBUG_KBD):
  337. debug_keyboard = !debug_keyboard;
  338. if (debug_keyboard) {
  339. print("\nkeyboard: on\n");
  340. debug_enable = true;
  341. } else {
  342. print("\nkeyboard: off\n");
  343. }
  344. break;
  345. // debug mouse toggle
  346. case MAGIC_KC(MAGIC_KEY_DEBUG_MOUSE):
  347. debug_mouse = !debug_mouse;
  348. if (debug_mouse) {
  349. print("\nmouse: on\n");
  350. debug_enable = true;
  351. } else {
  352. print("\nmouse: off\n");
  353. }
  354. break;
  355. // print version
  356. case MAGIC_KC(MAGIC_KEY_VERSION):
  357. print_version();
  358. break;
  359. // print status
  360. case MAGIC_KC(MAGIC_KEY_STATUS):
  361. print_status();
  362. break;
  363. #ifdef NKRO_ENABLE
  364. // NKRO toggle
  365. case MAGIC_KC(MAGIC_KEY_NKRO):
  366. clear_keyboard(); // clear to prevent stuck keys
  367. keyboard_nkro = !keyboard_nkro;
  368. if (keyboard_nkro)
  369. print("NKRO: on\n");
  370. else
  371. print("NKRO: off\n");
  372. break;
  373. #endif
  374. // switch layers
  375. case MAGIC_KC(MAGIC_KEY_LAYER0_ALT1):
  376. case MAGIC_KC(MAGIC_KEY_LAYER0_ALT2):
  377. switch_default_layer(0);
  378. break;
  379. #if MAGIC_KEY_SWITCH_LAYER_WITH_CUSTOM
  380. case MAGIC_KC(MAGIC_KEY_LAYER0):
  381. switch_default_layer(0);
  382. break;
  383. case MAGIC_KC(MAGIC_KEY_LAYER1):
  384. switch_default_layer(1);
  385. break;
  386. case MAGIC_KC(MAGIC_KEY_LAYER2):
  387. switch_default_layer(2);
  388. break;
  389. case MAGIC_KC(MAGIC_KEY_LAYER3):
  390. switch_default_layer(3);
  391. break;
  392. case MAGIC_KC(MAGIC_KEY_LAYER4):
  393. switch_default_layer(4);
  394. break;
  395. case MAGIC_KC(MAGIC_KEY_LAYER5):
  396. switch_default_layer(5);
  397. break;
  398. case MAGIC_KC(MAGIC_KEY_LAYER6):
  399. switch_default_layer(6);
  400. break;
  401. case MAGIC_KC(MAGIC_KEY_LAYER7):
  402. switch_default_layer(7);
  403. break;
  404. case MAGIC_KC(MAGIC_KEY_LAYER8):
  405. switch_default_layer(8);
  406. break;
  407. case MAGIC_KC(MAGIC_KEY_LAYER9):
  408. switch_default_layer(9);
  409. break;
  410. #endif
  411. #if MAGIC_KEY_SWITCH_LAYER_WITH_FKEYS
  412. case KC_F1 ... KC_F9:
  413. switch_default_layer((code - KC_F1) + 1);
  414. break;
  415. case KC_F10:
  416. switch_default_layer(0);
  417. break;
  418. #endif
  419. #if MAGIC_KEY_SWITCH_LAYER_WITH_NKEYS
  420. case KC_1 ... KC_9:
  421. switch_default_layer((code - KC_1) + 1);
  422. break;
  423. case KC_0:
  424. switch_default_layer(0);
  425. break;
  426. #endif
  427. default:
  428. print("?");
  429. return false;
  430. }
  431. return true;
  432. }
  433. /***********************************************************
  434. * Command console
  435. ***********************************************************/
  436. static void command_console_help(void)
  437. {
  438. print("\n\t- Console -\n"
  439. "ESC/q: quit\n"
  440. #ifdef MOUSEKEY_ENABLE
  441. "m: mousekey\n"
  442. #endif
  443. );
  444. }
  445. static bool command_console(uint8_t code)
  446. {
  447. switch (code) {
  448. case KC_H:
  449. case KC_SLASH: /* ? */
  450. command_console_help();
  451. break;
  452. case KC_Q:
  453. case KC_ESC:
  454. command_state = ONESHOT;
  455. return false;
  456. #ifdef MOUSEKEY_ENABLE
  457. case KC_M:
  458. mousekey_console_help();
  459. print("M> ");
  460. command_state = MOUSEKEY;
  461. return true;
  462. #endif
  463. default:
  464. print("?");
  465. return false;
  466. }
  467. print("C> ");
  468. return true;
  469. }
  470. #ifdef MOUSEKEY_ENABLE
  471. /***********************************************************
  472. * Mousekey console
  473. ***********************************************************/
  474. static uint8_t mousekey_param = 0;
  475. static void mousekey_param_print(void)
  476. {
  477. #ifndef NO_PRINT
  478. print("\n\t- Values -\n");
  479. print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
  480. print("2: interval(ms): "); pdec(mk_interval); print("\n");
  481. print("3: max_speed: "); pdec(mk_max_speed); print("\n");
  482. print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
  483. print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  484. print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  485. #endif /* !NO_PRINT */
  486. }
  487. //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  488. #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
  489. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  490. {
  491. switch (param) {
  492. case 1:
  493. if (mk_delay + inc < UINT8_MAX)
  494. mk_delay += inc;
  495. else
  496. mk_delay = UINT8_MAX;
  497. PRINT_SET_VAL(mk_delay);
  498. break;
  499. case 2:
  500. if (mk_interval + inc < UINT8_MAX)
  501. mk_interval += inc;
  502. else
  503. mk_interval = UINT8_MAX;
  504. PRINT_SET_VAL(mk_interval);
  505. break;
  506. case 3:
  507. if (mk_max_speed + inc < UINT8_MAX)
  508. mk_max_speed += inc;
  509. else
  510. mk_max_speed = UINT8_MAX;
  511. PRINT_SET_VAL(mk_max_speed);
  512. break;
  513. case 4:
  514. if (mk_time_to_max + inc < UINT8_MAX)
  515. mk_time_to_max += inc;
  516. else
  517. mk_time_to_max = UINT8_MAX;
  518. PRINT_SET_VAL(mk_time_to_max);
  519. break;
  520. case 5:
  521. if (mk_wheel_max_speed + inc < UINT8_MAX)
  522. mk_wheel_max_speed += inc;
  523. else
  524. mk_wheel_max_speed = UINT8_MAX;
  525. PRINT_SET_VAL(mk_wheel_max_speed);
  526. break;
  527. case 6:
  528. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  529. mk_wheel_time_to_max += inc;
  530. else
  531. mk_wheel_time_to_max = UINT8_MAX;
  532. PRINT_SET_VAL(mk_wheel_time_to_max);
  533. break;
  534. }
  535. }
  536. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  537. {
  538. switch (param) {
  539. case 1:
  540. if (mk_delay > dec)
  541. mk_delay -= dec;
  542. else
  543. mk_delay = 0;
  544. PRINT_SET_VAL(mk_delay);
  545. break;
  546. case 2:
  547. if (mk_interval > dec)
  548. mk_interval -= dec;
  549. else
  550. mk_interval = 0;
  551. PRINT_SET_VAL(mk_interval);
  552. break;
  553. case 3:
  554. if (mk_max_speed > dec)
  555. mk_max_speed -= dec;
  556. else
  557. mk_max_speed = 0;
  558. PRINT_SET_VAL(mk_max_speed);
  559. break;
  560. case 4:
  561. if (mk_time_to_max > dec)
  562. mk_time_to_max -= dec;
  563. else
  564. mk_time_to_max = 0;
  565. PRINT_SET_VAL(mk_time_to_max);
  566. break;
  567. case 5:
  568. if (mk_wheel_max_speed > dec)
  569. mk_wheel_max_speed -= dec;
  570. else
  571. mk_wheel_max_speed = 0;
  572. PRINT_SET_VAL(mk_wheel_max_speed);
  573. break;
  574. case 6:
  575. if (mk_wheel_time_to_max > dec)
  576. mk_wheel_time_to_max -= dec;
  577. else
  578. mk_wheel_time_to_max = 0;
  579. PRINT_SET_VAL(mk_wheel_time_to_max);
  580. break;
  581. }
  582. }
  583. static void mousekey_console_help(void)
  584. {
  585. print("\n\t- Mousekey -\n"
  586. "ESC/q: quit\n"
  587. "1: delay(*10ms)\n"
  588. "2: interval(ms)\n"
  589. "3: max_speed\n"
  590. "4: time_to_max\n"
  591. "5: wheel_max_speed\n"
  592. "6: wheel_time_to_max\n"
  593. "\n"
  594. "p: print values\n"
  595. "d: set defaults\n"
  596. "up: +1\n"
  597. "down: -1\n"
  598. "pgup: +10\n"
  599. "pgdown: -10\n"
  600. "\n"
  601. "speed = delta * max_speed * (repeat / time_to_max)\n");
  602. xprintf("where delta: cursor=%d, wheel=%d\n"
  603. "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
  604. }
  605. static bool mousekey_console(uint8_t code)
  606. {
  607. switch (code) {
  608. case KC_H:
  609. case KC_SLASH: /* ? */
  610. mousekey_console_help();
  611. break;
  612. case KC_Q:
  613. case KC_ESC:
  614. if (mousekey_param) {
  615. mousekey_param = 0;
  616. } else {
  617. print("C> ");
  618. command_state = CONSOLE;
  619. return false;
  620. }
  621. break;
  622. case KC_P:
  623. mousekey_param_print();
  624. break;
  625. case KC_1:
  626. case KC_2:
  627. case KC_3:
  628. case KC_4:
  629. case KC_5:
  630. case KC_6:
  631. mousekey_param = numkey2num(code);
  632. break;
  633. case KC_UP:
  634. mousekey_param_inc(mousekey_param, 1);
  635. break;
  636. case KC_DOWN:
  637. mousekey_param_dec(mousekey_param, 1);
  638. break;
  639. case KC_PGUP:
  640. mousekey_param_inc(mousekey_param, 10);
  641. break;
  642. case KC_PGDN:
  643. mousekey_param_dec(mousekey_param, 10);
  644. break;
  645. case KC_D:
  646. mk_delay = MOUSEKEY_DELAY/10;
  647. mk_interval = MOUSEKEY_INTERVAL;
  648. mk_max_speed = MOUSEKEY_MAX_SPEED;
  649. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  650. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  651. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  652. print("set default\n");
  653. break;
  654. default:
  655. print("?");
  656. return false;
  657. }
  658. if (mousekey_param)
  659. xprintf("M%d> ", mousekey_param);
  660. else
  661. print("M>" );
  662. return true;
  663. }
  664. #endif
  665. /***********************************************************
  666. * Utilities
  667. ***********************************************************/
  668. static uint8_t numkey2num(uint8_t code)
  669. {
  670. switch (code) {
  671. case KC_1: return 1;
  672. case KC_2: return 2;
  673. case KC_3: return 3;
  674. case KC_4: return 4;
  675. case KC_5: return 5;
  676. case KC_6: return 6;
  677. case KC_7: return 7;
  678. case KC_8: return 8;
  679. case KC_9: return 9;
  680. case KC_0: return 0;
  681. }
  682. return 0;
  683. }
  684. static void switch_default_layer(uint8_t layer)
  685. {
  686. xprintf("L%d\n", layer);
  687. default_layer_set(1UL<<layer);
  688. clear_keyboard();
  689. }