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 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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 "eeconfig.h"
  28. #include "sleep_led.h"
  29. #include "led.h"
  30. #include "command.h"
  31. #include "backlight.h"
  32. #ifdef MOUSEKEY_ENABLE
  33. #include "mousekey.h"
  34. #endif
  35. #ifdef PROTOCOL_PJRC
  36. # include "usb_keyboard.h"
  37. # ifdef EXTRAKEY_ENABLE
  38. # include "usb_extra.h"
  39. # endif
  40. #endif
  41. #ifdef PROTOCOL_VUSB
  42. # include "usbdrv.h"
  43. #endif
  44. static bool command_common(uint8_t code);
  45. static void command_common_help(void);
  46. static bool command_console(uint8_t code);
  47. static void command_console_help(void);
  48. #ifdef MOUSEKEY_ENABLE
  49. static bool mousekey_console(uint8_t code);
  50. static void mousekey_console_help(void);
  51. #endif
  52. static uint8_t numkey2num(uint8_t code);
  53. static void switch_default_layer(uint8_t layer);
  54. typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
  55. static cmdstate_t state = ONESHOT;
  56. bool command_proc(uint8_t code)
  57. {
  58. switch (state) {
  59. case ONESHOT:
  60. if (!IS_COMMAND())
  61. return false;
  62. return (command_extra(code) || command_common(code));
  63. case CONSOLE:
  64. command_console(code);
  65. break;
  66. #ifdef MOUSEKEY_ENABLE
  67. case MOUSEKEY:
  68. mousekey_console(code);
  69. break;
  70. #endif
  71. default:
  72. state = ONESHOT;
  73. return false;
  74. }
  75. return true;
  76. }
  77. /* This allows to define extra commands. return false when not processed. */
  78. bool command_extra(uint8_t code) __attribute__ ((weak));
  79. bool command_extra(uint8_t code)
  80. {
  81. return false;
  82. }
  83. /***********************************************************
  84. * Command common
  85. ***********************************************************/
  86. static void command_common_help(void)
  87. {
  88. print("\n\n----- Command Help -----\n");
  89. print("c: enter console mode\n");
  90. print("d: toggle debug enable\n");
  91. print("x: toggle matrix debug\n");
  92. print("k: toggle keyboard debug\n");
  93. print("m: toggle mouse debug\n");
  94. #ifdef SLEEP_LED_ENABLE
  95. print("z: toggle sleep LED test\n");
  96. #endif
  97. print("v: print device version & info\n");
  98. print("t: print timer count\n");
  99. print("s: print status\n");
  100. print("e: print eeprom config\n");
  101. #ifdef NKRO_ENABLE
  102. print("n: toggle NKRO\n");
  103. #endif
  104. print("0/F10: switch to Layer0 \n");
  105. print("1/F1: switch to Layer1 \n");
  106. print("2/F2: switch to Layer2 \n");
  107. print("3/F3: switch to Layer3 \n");
  108. print("4/F4: switch to Layer4 \n");
  109. print("PScr: power down/remote wake-up\n");
  110. print("Caps: Lock Keyboard(Child Proof)\n");
  111. print("Paus: jump to bootloader\n");
  112. }
  113. #ifdef BOOTMAGIC_ENABLE
  114. static void print_eeconfig(void)
  115. {
  116. print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
  117. debug_config_t dc;
  118. dc.raw = eeconfig_read_debug();
  119. print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
  120. print(".enable: "); print_dec(dc.enable); print("\n");
  121. print(".matrix: "); print_dec(dc.matrix); print("\n");
  122. print(".keyboard: "); print_dec(dc.keyboard); print("\n");
  123. print(".mouse: "); print_dec(dc.mouse); print("\n");
  124. keymap_config_t kc;
  125. kc.raw = eeconfig_read_keymap();
  126. print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
  127. print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
  128. print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
  129. print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
  130. print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
  131. print(".no_gui: "); print_dec(kc.no_gui); print("\n");
  132. print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
  133. print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
  134. #ifdef BACKLIGHT_ENABLE
  135. backlight_config_t bc;
  136. bc.raw = eeconfig_read_backlight();
  137. print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
  138. print(".enable: "); print_dec(bc.enable); print("\n");
  139. print(".level: "); print_dec(bc.level); print("\n");
  140. #endif
  141. }
  142. #endif
  143. static bool command_common(uint8_t code)
  144. {
  145. static host_driver_t *host_driver = 0;
  146. switch (code) {
  147. #ifdef SLEEP_LED_ENABLE
  148. case KC_Z:
  149. // test breathing sleep LED
  150. print("Sleep LED test\n");
  151. sleep_led_toggle();
  152. led_set(host_keyboard_leds());
  153. break;
  154. #endif
  155. #ifdef BOOTMAGIC_ENABLE
  156. case KC_E:
  157. print("eeconfig:\n");
  158. print_eeconfig();
  159. break;
  160. #endif
  161. case KC_CAPSLOCK:
  162. if (host_get_driver()) {
  163. host_driver = host_get_driver();
  164. host_set_driver(0);
  165. print("Locked.\n");
  166. } else {
  167. host_set_driver(host_driver);
  168. print("Unlocked.\n");
  169. }
  170. break;
  171. case KC_H:
  172. case KC_SLASH: /* ? */
  173. command_common_help();
  174. break;
  175. case KC_C:
  176. debug_matrix = false;
  177. debug_keyboard = false;
  178. debug_mouse = false;
  179. debug_enable = false;
  180. command_console_help();
  181. print("\nEnter Console Mode\n");
  182. print("C> ");
  183. state = CONSOLE;
  184. break;
  185. case KC_PAUSE:
  186. clear_keyboard();
  187. print("\n\nJump to bootloader... ");
  188. _delay_ms(1000);
  189. bootloader_jump(); // not return
  190. print("not supported.\n");
  191. break;
  192. case KC_D:
  193. if (debug_enable) {
  194. print("\nDEBUG: disabled.\n");
  195. debug_matrix = false;
  196. debug_keyboard = false;
  197. debug_mouse = false;
  198. debug_enable = false;
  199. } else {
  200. print("\nDEBUG: enabled.\n");
  201. debug_enable = true;
  202. }
  203. break;
  204. case KC_X: // debug matrix toggle
  205. debug_matrix = !debug_matrix;
  206. if (debug_matrix) {
  207. print("\nDEBUG: matrix enabled.\n");
  208. debug_enable = true;
  209. } else {
  210. print("\nDEBUG: matrix disabled.\n");
  211. }
  212. break;
  213. case KC_K: // debug keyboard toggle
  214. debug_keyboard = !debug_keyboard;
  215. if (debug_keyboard) {
  216. print("\nDEBUG: keyboard enabled.\n");
  217. debug_enable = true;
  218. } else {
  219. print("\nDEBUG: keyboard disabled.\n");
  220. }
  221. break;
  222. case KC_M: // debug mouse toggle
  223. debug_mouse = !debug_mouse;
  224. if (debug_mouse) {
  225. print("\nDEBUG: mouse enabled.\n");
  226. debug_enable = true;
  227. } else {
  228. print("\nDEBUG: mouse disabled.\n");
  229. }
  230. break;
  231. case KC_V: // print version & information
  232. print("\n\n----- Version -----\n");
  233. print("DESC: " STR(DESCRIPTION) "\n");
  234. print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") "
  235. "PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") "
  236. "VER: " STR(DEVICE_VER) "\n");
  237. print("BUILD: " STR(VERSION) " (" __TIME__ " " __DATE__ ")\n");
  238. /* build options */
  239. print("OPTIONS:"
  240. #ifdef PROTOCOL_PJRC
  241. " PJRC"
  242. #endif
  243. #ifdef PROTOCOL_LUFA
  244. " LUFA"
  245. #endif
  246. #ifdef PROTOCOL_VUSB
  247. " VUSB"
  248. #endif
  249. #ifdef BOOTMAGIC_ENABLE
  250. " BOOTMAGIC"
  251. #endif
  252. #ifdef MOUSEKEY_ENABLE
  253. " MOUSEKEY"
  254. #endif
  255. #ifdef EXTRAKEY_ENABLE
  256. " EXTRAKEY"
  257. #endif
  258. #ifdef CONSOLE_ENABLE
  259. " CONSOLE"
  260. #endif
  261. #ifdef COMMAND_ENABLE
  262. " COMMAND"
  263. #endif
  264. #ifdef NKRO_ENABLE
  265. " NKRO"
  266. #endif
  267. #ifdef KEYMAP_SECTION_ENABLE
  268. " KEYMAP_SECTION"
  269. #endif
  270. " " STR(BOOTLOADER_SIZE) "\n");
  271. print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__)
  272. " AVR-LIBC: " __AVR_LIBC_VERSION_STRING__
  273. " AVR_ARCH: avr" STR(__AVR_ARCH__) "\n");
  274. break;
  275. case KC_T: // print timer
  276. print_val_hex32(timer_count);
  277. break;
  278. case KC_S:
  279. print("\n\n----- Status -----\n");
  280. print_val_hex8(host_keyboard_leds());
  281. #ifdef PROTOCOL_PJRC
  282. print_val_hex8(UDCON);
  283. print_val_hex8(UDIEN);
  284. print_val_hex8(UDINT);
  285. print_val_hex8(usb_keyboard_leds);
  286. print_val_hex8(usb_keyboard_protocol);
  287. print_val_hex8(usb_keyboard_idle_config);
  288. print_val_hex8(usb_keyboard_idle_count);
  289. #endif
  290. #ifdef PROTOCOL_PJRC
  291. # if USB_COUNT_SOF
  292. print_val_hex8(usbSofCount);
  293. # endif
  294. #endif
  295. break;
  296. #ifdef NKRO_ENABLE
  297. case KC_N:
  298. clear_keyboard(); //Prevents stuck keys.
  299. keyboard_nkro = !keyboard_nkro;
  300. if (keyboard_nkro)
  301. print("NKRO: enabled\n");
  302. else
  303. print("NKRO: disabled\n");
  304. break;
  305. #endif
  306. #ifdef EXTRAKEY_ENABLE
  307. case KC_PSCREEN:
  308. // TODO: Power key should take this feature? otherwise any key during suspend.
  309. #ifdef PROTOCOL_PJRC
  310. if (suspend && remote_wakeup) {
  311. usb_remote_wakeup();
  312. } else {
  313. host_system_send(SYSTEM_POWER_DOWN);
  314. host_system_send(0);
  315. _delay_ms(500);
  316. }
  317. #else
  318. host_system_send(SYSTEM_POWER_DOWN);
  319. _delay_ms(100);
  320. host_system_send(0);
  321. _delay_ms(500);
  322. #endif
  323. break;
  324. #endif
  325. case KC_ESC:
  326. case KC_GRV:
  327. case KC_0:
  328. switch_default_layer(0);
  329. break;
  330. case KC_1 ... KC_9:
  331. switch_default_layer((code - KC_1) + 1);
  332. break;
  333. case KC_F1 ... KC_F12:
  334. switch_default_layer((code - KC_F1) + 1);
  335. break;
  336. default:
  337. print("?");
  338. return false;
  339. }
  340. return true;
  341. }
  342. /***********************************************************
  343. * Command console
  344. ***********************************************************/
  345. static void command_console_help(void)
  346. {
  347. print("\n\n----- Console Help -----\n");
  348. print("ESC/q: quit\n");
  349. #ifdef MOUSEKEY_ENABLE
  350. print("m: mousekey\n");
  351. #endif
  352. }
  353. static bool command_console(uint8_t code)
  354. {
  355. switch (code) {
  356. case KC_H:
  357. case KC_SLASH: /* ? */
  358. command_console_help();
  359. break;
  360. case KC_Q:
  361. case KC_ESC:
  362. print("\nQuit Console Mode\n");
  363. state = ONESHOT;
  364. return false;
  365. #ifdef MOUSEKEY_ENABLE
  366. case KC_M:
  367. mousekey_console_help();
  368. print("\nEnter Mousekey Console\n");
  369. print("M0>");
  370. state = MOUSEKEY;
  371. return true;
  372. #endif
  373. default:
  374. print("?");
  375. return false;
  376. }
  377. print("C> ");
  378. return true;
  379. }
  380. #ifdef MOUSEKEY_ENABLE
  381. /***********************************************************
  382. * Mousekey console
  383. ***********************************************************/
  384. static uint8_t mousekey_param = 0;
  385. static void mousekey_param_print(void)
  386. {
  387. print("\n\n----- Mousekey Parameters -----\n");
  388. print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
  389. print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
  390. print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
  391. print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
  392. print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  393. print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  394. }
  395. #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  396. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  397. {
  398. switch (param) {
  399. case 1:
  400. if (mk_delay + inc < UINT8_MAX)
  401. mk_delay += inc;
  402. else
  403. mk_delay = UINT8_MAX;
  404. PRINT_SET_VAL(mk_delay);
  405. break;
  406. case 2:
  407. if (mk_interval + inc < UINT8_MAX)
  408. mk_interval += inc;
  409. else
  410. mk_interval = UINT8_MAX;
  411. PRINT_SET_VAL(mk_interval);
  412. break;
  413. case 3:
  414. if (mk_max_speed + inc < UINT8_MAX)
  415. mk_max_speed += inc;
  416. else
  417. mk_max_speed = UINT8_MAX;
  418. PRINT_SET_VAL(mk_max_speed);
  419. break;
  420. case 4:
  421. if (mk_time_to_max + inc < UINT8_MAX)
  422. mk_time_to_max += inc;
  423. else
  424. mk_time_to_max = UINT8_MAX;
  425. PRINT_SET_VAL(mk_time_to_max);
  426. break;
  427. case 5:
  428. if (mk_wheel_max_speed + inc < UINT8_MAX)
  429. mk_wheel_max_speed += inc;
  430. else
  431. mk_wheel_max_speed = UINT8_MAX;
  432. PRINT_SET_VAL(mk_wheel_max_speed);
  433. break;
  434. case 6:
  435. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  436. mk_wheel_time_to_max += inc;
  437. else
  438. mk_wheel_time_to_max = UINT8_MAX;
  439. PRINT_SET_VAL(mk_wheel_time_to_max);
  440. break;
  441. }
  442. }
  443. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  444. {
  445. switch (param) {
  446. case 1:
  447. if (mk_delay > dec)
  448. mk_delay -= dec;
  449. else
  450. mk_delay = 0;
  451. PRINT_SET_VAL(mk_delay);
  452. break;
  453. case 2:
  454. if (mk_interval > dec)
  455. mk_interval -= dec;
  456. else
  457. mk_interval = 0;
  458. PRINT_SET_VAL(mk_interval);
  459. break;
  460. case 3:
  461. if (mk_max_speed > dec)
  462. mk_max_speed -= dec;
  463. else
  464. mk_max_speed = 0;
  465. PRINT_SET_VAL(mk_max_speed);
  466. break;
  467. case 4:
  468. if (mk_time_to_max > dec)
  469. mk_time_to_max -= dec;
  470. else
  471. mk_time_to_max = 0;
  472. PRINT_SET_VAL(mk_time_to_max);
  473. break;
  474. case 5:
  475. if (mk_wheel_max_speed > dec)
  476. mk_wheel_max_speed -= dec;
  477. else
  478. mk_wheel_max_speed = 0;
  479. PRINT_SET_VAL(mk_wheel_max_speed);
  480. break;
  481. case 6:
  482. if (mk_wheel_time_to_max > dec)
  483. mk_wheel_time_to_max -= dec;
  484. else
  485. mk_wheel_time_to_max = 0;
  486. PRINT_SET_VAL(mk_wheel_time_to_max);
  487. break;
  488. }
  489. }
  490. static void mousekey_console_help(void)
  491. {
  492. print("\n\n----- Mousekey Parameters Help -----\n");
  493. print("ESC/q: quit\n");
  494. print("1: select mk_delay(*10ms)\n");
  495. print("2: select mk_interval(ms)\n");
  496. print("3: select mk_max_speed\n");
  497. print("4: select mk_time_to_max\n");
  498. print("5: select mk_wheel_max_speed\n");
  499. print("6: select mk_wheel_time_to_max\n");
  500. print("p: print prameters\n");
  501. print("d: set default values\n");
  502. print("up: increase prameters(+1)\n");
  503. print("down: decrease prameters(-1)\n");
  504. print("pgup: increase prameters(+10)\n");
  505. print("pgdown: decrease prameters(-10)\n");
  506. print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
  507. print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
  508. print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
  509. print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
  510. }
  511. static bool mousekey_console(uint8_t code)
  512. {
  513. switch (code) {
  514. case KC_H:
  515. case KC_SLASH: /* ? */
  516. mousekey_console_help();
  517. break;
  518. case KC_Q:
  519. case KC_ESC:
  520. mousekey_param = 0;
  521. print("\nQuit Mousekey Console\n");
  522. print("C> ");
  523. state = CONSOLE;
  524. return false;
  525. case KC_P:
  526. mousekey_param_print();
  527. break;
  528. case KC_1:
  529. case KC_2:
  530. case KC_3:
  531. case KC_4:
  532. case KC_5:
  533. case KC_6:
  534. case KC_7:
  535. case KC_8:
  536. case KC_9:
  537. case KC_0:
  538. mousekey_param = numkey2num(code);
  539. print("selected parameter: "); pdec(mousekey_param); print("\n");
  540. break;
  541. case KC_UP:
  542. mousekey_param_inc(mousekey_param, 1);
  543. break;
  544. case KC_DOWN:
  545. mousekey_param_dec(mousekey_param, 1);
  546. break;
  547. case KC_PGUP:
  548. mousekey_param_inc(mousekey_param, 10);
  549. break;
  550. case KC_PGDN:
  551. mousekey_param_dec(mousekey_param, 10);
  552. break;
  553. case KC_D:
  554. mk_delay = MOUSEKEY_DELAY/10;
  555. mk_interval = MOUSEKEY_INTERVAL;
  556. mk_max_speed = MOUSEKEY_MAX_SPEED;
  557. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  558. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  559. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  560. print("set default values.\n");
  561. break;
  562. default:
  563. print("?");
  564. return false;
  565. }
  566. print("M"); pdec(mousekey_param); print("> ");
  567. return true;
  568. }
  569. #endif
  570. /***********************************************************
  571. * Utilities
  572. ***********************************************************/
  573. static uint8_t numkey2num(uint8_t code)
  574. {
  575. switch (code) {
  576. case KC_1: return 1;
  577. case KC_2: return 2;
  578. case KC_3: return 3;
  579. case KC_4: return 4;
  580. case KC_5: return 5;
  581. case KC_6: return 6;
  582. case KC_7: return 7;
  583. case KC_8: return 8;
  584. case KC_9: return 9;
  585. case KC_0: return 0;
  586. }
  587. return 0;
  588. }
  589. static void switch_default_layer(uint8_t layer)
  590. {
  591. print("switch_default_layer: "); print_dec(biton32(default_layer_state));
  592. print(" to "); print_dec(layer); print("\n");
  593. default_layer_set(1UL<<layer);
  594. clear_keyboard();
  595. }