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

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