command.c 22 KB

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