upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

command.c 21KB

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