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

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