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

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