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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 "eeconfig.h"
  28. #include "sleep_led.h"
  29. #include "led.h"
  30. #include "command.h"
  31. #include "backlight.h"
  32. #ifdef MOUSEKEY_ENABLE
  33. #include "mousekey.h"
  34. #endif
  35. #ifdef PROTOCOL_PJRC
  36. # include "usb_keyboard.h"
  37. # ifdef EXTRAKEY_ENABLE
  38. # include "usb_extra.h"
  39. # endif
  40. #endif
  41. #ifdef PROTOCOL_VUSB
  42. # include "usbdrv.h"
  43. #endif
  44. static bool command_common(uint8_t code);
  45. static void command_common_help(void);
  46. static bool command_console(uint8_t code);
  47. static void command_console_help(void);
  48. #ifdef MOUSEKEY_ENABLE
  49. static bool mousekey_console(uint8_t code);
  50. static void mousekey_console_help(void);
  51. #endif
  52. static uint8_t numkey2num(uint8_t code);
  53. static void switch_default_layer(uint8_t layer);
  54. typedef enum { ONESHOT, CONSOLE, MOUSEKEY } cmdstate_t;
  55. static cmdstate_t state = ONESHOT;
  56. bool command_proc(uint8_t code)
  57. {
  58. switch (state) {
  59. case ONESHOT:
  60. if (!IS_COMMAND())
  61. return false;
  62. return (command_extra(code) || command_common(code));
  63. case CONSOLE:
  64. command_console(code);
  65. break;
  66. #ifdef MOUSEKEY_ENABLE
  67. case MOUSEKEY:
  68. mousekey_console(code);
  69. break;
  70. #endif
  71. default:
  72. state = ONESHOT;
  73. return false;
  74. }
  75. return true;
  76. }
  77. /* This allows to define extra commands. return false when not processed. */
  78. bool command_extra(uint8_t code) __attribute__ ((weak));
  79. bool command_extra(uint8_t code)
  80. {
  81. return false;
  82. }
  83. /***********************************************************
  84. * Command common
  85. ***********************************************************/
  86. static void command_common_help(void)
  87. {
  88. print("\n\n----- Command Help -----\n");
  89. print("c: enter console mode\n");
  90. print("d: toggle debug enable\n");
  91. print("x: toggle matrix debug\n");
  92. print("k: toggle keyboard debug\n");
  93. print("m: toggle mouse debug\n");
  94. #ifdef SLEEP_LED_ENABLE
  95. print("z: toggle sleep LED test\n");
  96. #endif
  97. print("v: print device version & info\n");
  98. print("t: print timer count\n");
  99. print("s: print status\n");
  100. print("e: print eeprom config\n");
  101. #ifdef NKRO_ENABLE
  102. print("n: toggle NKRO\n");
  103. #endif
  104. print("0/F10: switch to Layer0 \n");
  105. print("1/F1: switch to Layer1 \n");
  106. print("2/F2: switch to Layer2 \n");
  107. print("3/F3: switch to Layer3 \n");
  108. print("4/F4: switch to Layer4 \n");
  109. print("PScr: power down/remote wake-up\n");
  110. print("Caps: Lock Keyboard(Child Proof)\n");
  111. print("Paus: jump to bootloader\n");
  112. }
  113. #ifdef BOOTMAGIC_ENABLE
  114. static void print_eeconfig(void)
  115. {
  116. print("default_layer: "); print_dec(eeconfig_read_default_layer()); print("\n");
  117. debug_config_t dc;
  118. dc.raw = eeconfig_read_debug();
  119. print("debug_config.raw: "); print_hex8(dc.raw); print("\n");
  120. print(".enable: "); print_dec(dc.enable); print("\n");
  121. print(".matrix: "); print_dec(dc.matrix); print("\n");
  122. print(".keyboard: "); print_dec(dc.keyboard); print("\n");
  123. print(".mouse: "); print_dec(dc.mouse); print("\n");
  124. keymap_config_t kc;
  125. kc.raw = eeconfig_read_keymap();
  126. print("keymap_config.raw: "); print_hex8(kc.raw); print("\n");
  127. print(".swap_control_capslock: "); print_dec(kc.swap_control_capslock); print("\n");
  128. print(".capslock_to_control: "); print_dec(kc.capslock_to_control); print("\n");
  129. print(".swap_lalt_lgui: "); print_dec(kc.swap_lalt_lgui); print("\n");
  130. print(".swap_ralt_rgui: "); print_dec(kc.swap_ralt_rgui); print("\n");
  131. print(".no_gui: "); print_dec(kc.no_gui); print("\n");
  132. print(".swap_grave_esc: "); print_dec(kc.swap_grave_esc); print("\n");
  133. print(".swap_backslash_backspace: "); print_dec(kc.swap_backslash_backspace); print("\n");
  134. #ifdef BACKLIGHT_ENABLE
  135. backlight_config_t bc;
  136. bc.raw = eeconfig_read_backlight();
  137. print("backlight_config.raw: "); print_hex8(bc.raw); print("\n");
  138. print(".enable: "); print_dec(bc.enable); print("\n");
  139. print(".level: "); print_dec(bc.level); print("\n");
  140. #endif
  141. }
  142. #endif
  143. static bool command_common(uint8_t code)
  144. {
  145. static host_driver_t *host_driver = 0;
  146. switch (code) {
  147. #ifdef SLEEP_LED_ENABLE
  148. case KC_Z:
  149. // test breathing sleep LED
  150. print("Sleep LED test\n");
  151. sleep_led_toggle();
  152. led_set(host_keyboard_leds());
  153. break;
  154. #endif
  155. #ifdef BOOTMAGIC_ENABLE
  156. case KC_E:
  157. print("eeconfig:\n");
  158. print_eeconfig();
  159. break;
  160. #endif
  161. case KC_CAPSLOCK:
  162. if (host_get_driver()) {
  163. host_driver = host_get_driver();
  164. host_set_driver(0);
  165. print("Locked.\n");
  166. } else {
  167. host_set_driver(host_driver);
  168. print("Unlocked.\n");
  169. }
  170. break;
  171. case KC_H:
  172. case KC_SLASH: /* ? */
  173. command_common_help();
  174. break;
  175. case KC_C:
  176. debug_matrix = false;
  177. debug_keyboard = false;
  178. debug_mouse = false;
  179. debug_enable = false;
  180. command_console_help();
  181. print("\nEnter Console Mode\n");
  182. print("C> ");
  183. state = CONSOLE;
  184. break;
  185. case KC_PAUSE:
  186. clear_keyboard();
  187. print("\n\nJump to bootloader... ");
  188. _delay_ms(1000);
  189. bootloader_jump(); // not return
  190. print("not supported.\n");
  191. break;
  192. case KC_D:
  193. if (debug_enable) {
  194. print("\nDEBUG: disabled.\n");
  195. debug_matrix = false;
  196. debug_keyboard = false;
  197. debug_mouse = false;
  198. debug_enable = false;
  199. } else {
  200. print("\nDEBUG: enabled.\n");
  201. debug_enable = true;
  202. }
  203. break;
  204. case KC_X: // debug matrix toggle
  205. debug_matrix = !debug_matrix;
  206. if (debug_matrix) {
  207. print("\nDEBUG: matrix enabled.\n");
  208. debug_enable = true;
  209. } else {
  210. print("\nDEBUG: matrix disabled.\n");
  211. }
  212. break;
  213. case KC_K: // debug keyboard toggle
  214. debug_keyboard = !debug_keyboard;
  215. if (debug_keyboard) {
  216. print("\nDEBUG: keyboard enabled.\n");
  217. debug_enable = true;
  218. } else {
  219. print("\nDEBUG: keyboard disabled.\n");
  220. }
  221. break;
  222. case KC_M: // debug mouse toggle
  223. debug_mouse = !debug_mouse;
  224. if (debug_mouse) {
  225. print("\nDEBUG: mouse enabled.\n");
  226. debug_enable = true;
  227. } else {
  228. print("\nDEBUG: mouse disabled.\n");
  229. }
  230. break;
  231. case KC_V: // print version & information
  232. print("\n\n----- Version -----\n");
  233. print(STR(DESCRIPTION) "\n");
  234. print("VID: " STR(VENDOR_ID) "(" STR(MANUFACTURER) ") PID: " STR(PRODUCT_ID) "(" STR(PRODUCT) ") VER: " STR(DEVICE_VER) "\n");
  235. print("SRC: " STR(VERSION) "\n");
  236. print("GCC: " STR(__GNUC__) "." STR(__GNUC_MINOR__) "." STR(__GNUC_PATCHLEVEL__) "\n");
  237. print("AVR-LIBC: " __AVR_LIBC_VERSION_STRING__ "\n");
  238. break;
  239. case KC_T: // print timer
  240. print_val_hex32(timer_count);
  241. break;
  242. case KC_S:
  243. print("\n\n----- Status -----\n");
  244. print_val_hex8(host_keyboard_leds());
  245. #ifdef PROTOCOL_PJRC
  246. print_val_hex8(UDCON);
  247. print_val_hex8(UDIEN);
  248. print_val_hex8(UDINT);
  249. print_val_hex8(usb_keyboard_leds);
  250. print_val_hex8(usb_keyboard_protocol);
  251. print_val_hex8(usb_keyboard_idle_config);
  252. print_val_hex8(usb_keyboard_idle_count);
  253. #endif
  254. #ifdef PROTOCOL_PJRC
  255. # if USB_COUNT_SOF
  256. print_val_hex8(usbSofCount);
  257. # endif
  258. #endif
  259. break;
  260. #ifdef NKRO_ENABLE
  261. case KC_N:
  262. clear_keyboard(); //Prevents stuck keys.
  263. keyboard_nkro = !keyboard_nkro;
  264. if (keyboard_nkro)
  265. print("NKRO: enabled\n");
  266. else
  267. print("NKRO: disabled\n");
  268. break;
  269. #endif
  270. #ifdef EXTRAKEY_ENABLE
  271. case KC_PSCREEN:
  272. // TODO: Power key should take this feature? otherwise any key during suspend.
  273. #ifdef PROTOCOL_PJRC
  274. if (suspend && remote_wakeup) {
  275. usb_remote_wakeup();
  276. } else {
  277. host_system_send(SYSTEM_POWER_DOWN);
  278. host_system_send(0);
  279. _delay_ms(500);
  280. }
  281. #else
  282. host_system_send(SYSTEM_POWER_DOWN);
  283. _delay_ms(100);
  284. host_system_send(0);
  285. _delay_ms(500);
  286. #endif
  287. break;
  288. #endif
  289. case KC_ESC:
  290. case KC_GRV:
  291. case KC_0:
  292. switch_default_layer(0);
  293. break;
  294. case KC_1 ... KC_9:
  295. switch_default_layer((code - KC_1) + 1);
  296. break;
  297. case KC_F1 ... KC_F12:
  298. switch_default_layer((code - KC_F1) + 1);
  299. break;
  300. default:
  301. print("?");
  302. return false;
  303. }
  304. return true;
  305. }
  306. /***********************************************************
  307. * Command console
  308. ***********************************************************/
  309. static void command_console_help(void)
  310. {
  311. print("\n\n----- Console Help -----\n");
  312. print("ESC/q: quit\n");
  313. #ifdef MOUSEKEY_ENABLE
  314. print("m: mousekey\n");
  315. #endif
  316. }
  317. static bool command_console(uint8_t code)
  318. {
  319. switch (code) {
  320. case KC_H:
  321. case KC_SLASH: /* ? */
  322. command_console_help();
  323. break;
  324. case KC_Q:
  325. case KC_ESC:
  326. print("\nQuit Console Mode\n");
  327. state = ONESHOT;
  328. return false;
  329. #ifdef MOUSEKEY_ENABLE
  330. case KC_M:
  331. mousekey_console_help();
  332. print("\nEnter Mousekey Console\n");
  333. print("M0>");
  334. state = MOUSEKEY;
  335. return true;
  336. #endif
  337. default:
  338. print("?");
  339. return false;
  340. }
  341. print("C> ");
  342. return true;
  343. }
  344. #ifdef MOUSEKEY_ENABLE
  345. /***********************************************************
  346. * Mousekey console
  347. ***********************************************************/
  348. static uint8_t mousekey_param = 0;
  349. static void mousekey_param_print(void)
  350. {
  351. print("\n\n----- Mousekey Parameters -----\n");
  352. print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
  353. print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
  354. print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
  355. print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
  356. print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  357. print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  358. }
  359. #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  360. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  361. {
  362. switch (param) {
  363. case 1:
  364. if (mk_delay + inc < UINT8_MAX)
  365. mk_delay += inc;
  366. else
  367. mk_delay = UINT8_MAX;
  368. PRINT_SET_VAL(mk_delay);
  369. break;
  370. case 2:
  371. if (mk_interval + inc < UINT8_MAX)
  372. mk_interval += inc;
  373. else
  374. mk_interval = UINT8_MAX;
  375. PRINT_SET_VAL(mk_interval);
  376. break;
  377. case 3:
  378. if (mk_max_speed + inc < UINT8_MAX)
  379. mk_max_speed += inc;
  380. else
  381. mk_max_speed = UINT8_MAX;
  382. PRINT_SET_VAL(mk_max_speed);
  383. break;
  384. case 4:
  385. if (mk_time_to_max + inc < UINT8_MAX)
  386. mk_time_to_max += inc;
  387. else
  388. mk_time_to_max = UINT8_MAX;
  389. PRINT_SET_VAL(mk_time_to_max);
  390. break;
  391. case 5:
  392. if (mk_wheel_max_speed + inc < UINT8_MAX)
  393. mk_wheel_max_speed += inc;
  394. else
  395. mk_wheel_max_speed = UINT8_MAX;
  396. PRINT_SET_VAL(mk_wheel_max_speed);
  397. break;
  398. case 6:
  399. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  400. mk_wheel_time_to_max += inc;
  401. else
  402. mk_wheel_time_to_max = UINT8_MAX;
  403. PRINT_SET_VAL(mk_wheel_time_to_max);
  404. break;
  405. }
  406. }
  407. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  408. {
  409. switch (param) {
  410. case 1:
  411. if (mk_delay > dec)
  412. mk_delay -= dec;
  413. else
  414. mk_delay = 0;
  415. PRINT_SET_VAL(mk_delay);
  416. break;
  417. case 2:
  418. if (mk_interval > dec)
  419. mk_interval -= dec;
  420. else
  421. mk_interval = 0;
  422. PRINT_SET_VAL(mk_interval);
  423. break;
  424. case 3:
  425. if (mk_max_speed > dec)
  426. mk_max_speed -= dec;
  427. else
  428. mk_max_speed = 0;
  429. PRINT_SET_VAL(mk_max_speed);
  430. break;
  431. case 4:
  432. if (mk_time_to_max > dec)
  433. mk_time_to_max -= dec;
  434. else
  435. mk_time_to_max = 0;
  436. PRINT_SET_VAL(mk_time_to_max);
  437. break;
  438. case 5:
  439. if (mk_wheel_max_speed > dec)
  440. mk_wheel_max_speed -= dec;
  441. else
  442. mk_wheel_max_speed = 0;
  443. PRINT_SET_VAL(mk_wheel_max_speed);
  444. break;
  445. case 6:
  446. if (mk_wheel_time_to_max > dec)
  447. mk_wheel_time_to_max -= dec;
  448. else
  449. mk_wheel_time_to_max = 0;
  450. PRINT_SET_VAL(mk_wheel_time_to_max);
  451. break;
  452. }
  453. }
  454. static void mousekey_console_help(void)
  455. {
  456. print("\n\n----- Mousekey Parameters Help -----\n");
  457. print("ESC/q: quit\n");
  458. print("1: select mk_delay(*10ms)\n");
  459. print("2: select mk_interval(ms)\n");
  460. print("3: select mk_max_speed\n");
  461. print("4: select mk_time_to_max\n");
  462. print("5: select mk_wheel_max_speed\n");
  463. print("6: select mk_wheel_time_to_max\n");
  464. print("p: print prameters\n");
  465. print("d: set default values\n");
  466. print("up: increase prameters(+1)\n");
  467. print("down: decrease prameters(-1)\n");
  468. print("pgup: increase prameters(+10)\n");
  469. print("pgdown: decrease prameters(-10)\n");
  470. print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
  471. print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
  472. print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
  473. print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
  474. }
  475. static bool mousekey_console(uint8_t code)
  476. {
  477. switch (code) {
  478. case KC_H:
  479. case KC_SLASH: /* ? */
  480. mousekey_console_help();
  481. break;
  482. case KC_Q:
  483. case KC_ESC:
  484. mousekey_param = 0;
  485. print("\nQuit Mousekey Console\n");
  486. print("C> ");
  487. state = CONSOLE;
  488. return false;
  489. case KC_P:
  490. mousekey_param_print();
  491. break;
  492. case KC_1:
  493. case KC_2:
  494. case KC_3:
  495. case KC_4:
  496. case KC_5:
  497. case KC_6:
  498. case KC_7:
  499. case KC_8:
  500. case KC_9:
  501. case KC_0:
  502. mousekey_param = numkey2num(code);
  503. print("selected parameter: "); pdec(mousekey_param); print("\n");
  504. break;
  505. case KC_UP:
  506. mousekey_param_inc(mousekey_param, 1);
  507. break;
  508. case KC_DOWN:
  509. mousekey_param_dec(mousekey_param, 1);
  510. break;
  511. case KC_PGUP:
  512. mousekey_param_inc(mousekey_param, 10);
  513. break;
  514. case KC_PGDN:
  515. mousekey_param_dec(mousekey_param, 10);
  516. break;
  517. case KC_D:
  518. mk_delay = MOUSEKEY_DELAY/10;
  519. mk_interval = MOUSEKEY_INTERVAL;
  520. mk_max_speed = MOUSEKEY_MAX_SPEED;
  521. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  522. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  523. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  524. print("set default values.\n");
  525. break;
  526. default:
  527. print("?");
  528. return false;
  529. }
  530. print("M"); pdec(mousekey_param); print("> ");
  531. return true;
  532. }
  533. #endif
  534. /***********************************************************
  535. * Utilities
  536. ***********************************************************/
  537. static uint8_t numkey2num(uint8_t code)
  538. {
  539. switch (code) {
  540. case KC_1: return 1;
  541. case KC_2: return 2;
  542. case KC_3: return 3;
  543. case KC_4: return 4;
  544. case KC_5: return 5;
  545. case KC_6: return 6;
  546. case KC_7: return 7;
  547. case KC_8: return 8;
  548. case KC_9: return 9;
  549. case KC_0: return 0;
  550. }
  551. return 0;
  552. }
  553. static void switch_default_layer(uint8_t layer)
  554. {
  555. print("switch_default_layer: "); print_dec(biton32(default_layer_state));
  556. print(" to "); print_dec(layer); print("\n");
  557. default_layer_set(1UL<<layer);
  558. clear_keyboard();
  559. }