command.c 21 KB

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