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

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