command.c 23 KB

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