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

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