Keyboard firmwares for Atmel AVR and Cortex-M
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.

command.c 18KB

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