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

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