command.c 21 KB

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