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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. #ifdef NKRO_ENABLE
  292. print_val_hex8(keyboard_nkro);
  293. #endif
  294. print_val_hex32(timer_count);
  295. #ifdef PROTOCOL_PJRC
  296. print_val_hex8(UDCON);
  297. print_val_hex8(UDIEN);
  298. print_val_hex8(UDINT);
  299. print_val_hex8(usb_keyboard_leds);
  300. print_val_hex8(usb_keyboard_idle_count);
  301. #endif
  302. #ifdef PROTOCOL_PJRC
  303. # if USB_COUNT_SOF
  304. print_val_hex8(usbSofCount);
  305. # endif
  306. #endif
  307. break;
  308. #ifdef NKRO_ENABLE
  309. case KC_N:
  310. clear_keyboard(); //Prevents stuck keys.
  311. keyboard_nkro = !keyboard_nkro;
  312. if (keyboard_nkro)
  313. print("NKRO: on\n");
  314. else
  315. print("NKRO: off\n");
  316. break;
  317. #endif
  318. case KC_ESC:
  319. case KC_GRV:
  320. case KC_0:
  321. case KC_F10:
  322. switch_default_layer(0);
  323. break;
  324. case KC_1 ... KC_9:
  325. switch_default_layer((code - KC_1) + 1);
  326. break;
  327. case KC_F1 ... KC_F9:
  328. switch_default_layer((code - KC_F1) + 1);
  329. break;
  330. default:
  331. print("?");
  332. return false;
  333. }
  334. return true;
  335. }
  336. /***********************************************************
  337. * Command console
  338. ***********************************************************/
  339. static void command_console_help(void)
  340. {
  341. print("\n\t- Console -\n"
  342. "ESC/q: quit\n"
  343. #ifdef MOUSEKEY_ENABLE
  344. "m: mousekey\n"
  345. #endif
  346. );
  347. }
  348. static bool command_console(uint8_t code)
  349. {
  350. switch (code) {
  351. case KC_H:
  352. case KC_SLASH: /* ? */
  353. command_console_help();
  354. break;
  355. case KC_Q:
  356. case KC_ESC:
  357. command_state = ONESHOT;
  358. return false;
  359. #ifdef MOUSEKEY_ENABLE
  360. case KC_M:
  361. mousekey_console_help();
  362. print("M> ");
  363. command_state = MOUSEKEY;
  364. return true;
  365. #endif
  366. default:
  367. print("?");
  368. return false;
  369. }
  370. print("C> ");
  371. return true;
  372. }
  373. #ifdef MOUSEKEY_ENABLE
  374. /***********************************************************
  375. * Mousekey console
  376. ***********************************************************/
  377. static uint8_t mousekey_param = 0;
  378. static void mousekey_param_print(void)
  379. {
  380. print("\n\t- Values -\n");
  381. print("1: delay(*10ms): "); pdec(mk_delay); print("\n");
  382. print("2: interval(ms): "); pdec(mk_interval); print("\n");
  383. print("3: max_speed: "); pdec(mk_max_speed); print("\n");
  384. print("4: time_to_max: "); pdec(mk_time_to_max); print("\n");
  385. print("5: wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  386. print("6: wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  387. }
  388. //#define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  389. #define PRINT_SET_VAL(v) xprintf(#v " = %d\n", (v))
  390. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  391. {
  392. switch (param) {
  393. case 1:
  394. if (mk_delay + inc < UINT8_MAX)
  395. mk_delay += inc;
  396. else
  397. mk_delay = UINT8_MAX;
  398. PRINT_SET_VAL(mk_delay);
  399. break;
  400. case 2:
  401. if (mk_interval + inc < UINT8_MAX)
  402. mk_interval += inc;
  403. else
  404. mk_interval = UINT8_MAX;
  405. PRINT_SET_VAL(mk_interval);
  406. break;
  407. case 3:
  408. if (mk_max_speed + inc < UINT8_MAX)
  409. mk_max_speed += inc;
  410. else
  411. mk_max_speed = UINT8_MAX;
  412. PRINT_SET_VAL(mk_max_speed);
  413. break;
  414. case 4:
  415. if (mk_time_to_max + inc < UINT8_MAX)
  416. mk_time_to_max += inc;
  417. else
  418. mk_time_to_max = UINT8_MAX;
  419. PRINT_SET_VAL(mk_time_to_max);
  420. break;
  421. case 5:
  422. if (mk_wheel_max_speed + inc < UINT8_MAX)
  423. mk_wheel_max_speed += inc;
  424. else
  425. mk_wheel_max_speed = UINT8_MAX;
  426. PRINT_SET_VAL(mk_wheel_max_speed);
  427. break;
  428. case 6:
  429. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  430. mk_wheel_time_to_max += inc;
  431. else
  432. mk_wheel_time_to_max = UINT8_MAX;
  433. PRINT_SET_VAL(mk_wheel_time_to_max);
  434. break;
  435. }
  436. }
  437. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  438. {
  439. switch (param) {
  440. case 1:
  441. if (mk_delay > dec)
  442. mk_delay -= dec;
  443. else
  444. mk_delay = 0;
  445. PRINT_SET_VAL(mk_delay);
  446. break;
  447. case 2:
  448. if (mk_interval > dec)
  449. mk_interval -= dec;
  450. else
  451. mk_interval = 0;
  452. PRINT_SET_VAL(mk_interval);
  453. break;
  454. case 3:
  455. if (mk_max_speed > dec)
  456. mk_max_speed -= dec;
  457. else
  458. mk_max_speed = 0;
  459. PRINT_SET_VAL(mk_max_speed);
  460. break;
  461. case 4:
  462. if (mk_time_to_max > dec)
  463. mk_time_to_max -= dec;
  464. else
  465. mk_time_to_max = 0;
  466. PRINT_SET_VAL(mk_time_to_max);
  467. break;
  468. case 5:
  469. if (mk_wheel_max_speed > dec)
  470. mk_wheel_max_speed -= dec;
  471. else
  472. mk_wheel_max_speed = 0;
  473. PRINT_SET_VAL(mk_wheel_max_speed);
  474. break;
  475. case 6:
  476. if (mk_wheel_time_to_max > dec)
  477. mk_wheel_time_to_max -= dec;
  478. else
  479. mk_wheel_time_to_max = 0;
  480. PRINT_SET_VAL(mk_wheel_time_to_max);
  481. break;
  482. }
  483. }
  484. static void mousekey_console_help(void)
  485. {
  486. print("\n\t- Mousekey -\n"
  487. "ESC/q: quit\n"
  488. "1: delay(*10ms)\n"
  489. "2: interval(ms)\n"
  490. "3: max_speed\n"
  491. "4: time_to_max\n"
  492. "5: wheel_max_speed\n"
  493. "6: wheel_time_to_max\n"
  494. "\n"
  495. "p: print values\n"
  496. "d: set defaults\n"
  497. "up: +1\n"
  498. "down: -1\n"
  499. "pgup: +10\n"
  500. "pgdown: -10\n"
  501. "\n"
  502. "speed = delta * max_speed * (repeat / time_to_max)\n");
  503. xprintf("where delta: cursor=%d, wheel=%d\n"
  504. "See http://en.wikipedia.org/wiki/Mouse_keys\n", MOUSEKEY_MOVE_DELTA, MOUSEKEY_WHEEL_DELTA);
  505. }
  506. static bool mousekey_console(uint8_t code)
  507. {
  508. switch (code) {
  509. case KC_H:
  510. case KC_SLASH: /* ? */
  511. mousekey_console_help();
  512. break;
  513. case KC_Q:
  514. case KC_ESC:
  515. if (mousekey_param) {
  516. mousekey_param = 0;
  517. } else {
  518. print("C> ");
  519. command_state = CONSOLE;
  520. return false;
  521. }
  522. break;
  523. case KC_P:
  524. mousekey_param_print();
  525. break;
  526. case KC_1:
  527. case KC_2:
  528. case KC_3:
  529. case KC_4:
  530. case KC_5:
  531. case KC_6:
  532. mousekey_param = numkey2num(code);
  533. break;
  534. case KC_UP:
  535. mousekey_param_inc(mousekey_param, 1);
  536. break;
  537. case KC_DOWN:
  538. mousekey_param_dec(mousekey_param, 1);
  539. break;
  540. case KC_PGUP:
  541. mousekey_param_inc(mousekey_param, 10);
  542. break;
  543. case KC_PGDN:
  544. mousekey_param_dec(mousekey_param, 10);
  545. break;
  546. case KC_D:
  547. mk_delay = MOUSEKEY_DELAY/10;
  548. mk_interval = MOUSEKEY_INTERVAL;
  549. mk_max_speed = MOUSEKEY_MAX_SPEED;
  550. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  551. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  552. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  553. print("set default\n");
  554. break;
  555. default:
  556. print("?");
  557. return false;
  558. }
  559. if (mousekey_param)
  560. xprintf("M%d> ", mousekey_param);
  561. else
  562. print("M>" );
  563. return true;
  564. }
  565. #endif
  566. /***********************************************************
  567. * Utilities
  568. ***********************************************************/
  569. static uint8_t numkey2num(uint8_t code)
  570. {
  571. switch (code) {
  572. case KC_1: return 1;
  573. case KC_2: return 2;
  574. case KC_3: return 3;
  575. case KC_4: return 4;
  576. case KC_5: return 5;
  577. case KC_6: return 6;
  578. case KC_7: return 7;
  579. case KC_8: return 8;
  580. case KC_9: return 9;
  581. case KC_0: return 0;
  582. }
  583. return 0;
  584. }
  585. static void switch_default_layer(uint8_t layer)
  586. {
  587. xprintf("L%d\n", layer);
  588. default_layer_set(1UL<<layer);
  589. clear_keyboard();
  590. }