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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

command.c 17KB

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