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

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