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

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