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

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
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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_enable = true;
  152. }
  153. break;
  154. case KC_X: // debug matrix toggle
  155. debug_matrix = !debug_matrix;
  156. if (debug_matrix) {
  157. print("\nDEBUG: matrix enabled.\n");
  158. debug_enable = true;
  159. } else {
  160. print("\nDEBUG: matrix disabled.\n");
  161. }
  162. break;
  163. case KC_K: // debug keyboard toggle
  164. debug_keyboard = !debug_keyboard;
  165. if (debug_keyboard) {
  166. print("\nDEBUG: keyboard enabled.\n");
  167. debug_enable = true;
  168. } else {
  169. print("\nDEBUG: keyboard disabled.\n");
  170. }
  171. break;
  172. case KC_M: // debug mouse toggle
  173. debug_mouse = !debug_mouse;
  174. if (debug_mouse) {
  175. print("\nDEBUG: mouse enabled.\n");
  176. debug_enable = true;
  177. } else {
  178. print("\nDEBUG: mouse disabled.\n");
  179. }
  180. break;
  181. case KC_V: // print version & information
  182. print("\n\n----- Version -----\n");
  183. print(STR(DESCRIPTION) "\n");
  184. print(STR(MANUFACTURER) "(" STR(VENDOR_ID) ")/");
  185. print(STR(PRODUCT) "(" STR(PRODUCT_ID) ") ");
  186. print("VERSION: " STR(DEVICE_VER) "\n");
  187. break;
  188. case KC_T: // print timer
  189. print_val_hex32(timer_count);
  190. break;
  191. case KC_P: // print toggle
  192. if (print_enable) {
  193. print("print disabled.\n");
  194. print_enable = false;
  195. } else {
  196. print_enable = true;
  197. print("print enabled.\n");
  198. }
  199. break;
  200. case KC_S:
  201. print("\n\n----- Status -----\n");
  202. print_val_hex8(host_keyboard_leds());
  203. #ifdef HOST_PJRC
  204. print_val_hex8(UDCON);
  205. print_val_hex8(UDIEN);
  206. print_val_hex8(UDINT);
  207. print_val_hex8(usb_keyboard_leds);
  208. print_val_hex8(usb_keyboard_protocol);
  209. print_val_hex8(usb_keyboard_idle_config);
  210. print_val_hex8(usb_keyboard_idle_count);
  211. #endif
  212. #ifdef HOST_VUSB
  213. # if USB_COUNT_SOF
  214. print_val_hex8(usbSofCount);
  215. # endif
  216. #endif
  217. break;
  218. #ifdef NKRO_ENABLE
  219. case KC_N:
  220. clear_keyboard(); //Prevents stuck keys.
  221. keyboard_nkro = !keyboard_nkro;
  222. if (keyboard_nkro)
  223. print("NKRO: enabled\n");
  224. else
  225. print("NKRO: disabled\n");
  226. break;
  227. #endif
  228. #ifdef EXTRAKEY_ENABLE
  229. case KC_PSCREEN:
  230. // TODO: Power key should take this feature? otherwise any key during suspend.
  231. #ifdef HOST_PJRC
  232. if (suspend && remote_wakeup) {
  233. usb_remote_wakeup();
  234. } else {
  235. host_system_send(SYSTEM_POWER_DOWN);
  236. host_system_send(0);
  237. _delay_ms(500);
  238. }
  239. #else
  240. host_system_send(SYSTEM_POWER_DOWN);
  241. _delay_ms(100);
  242. host_system_send(0);
  243. _delay_ms(500);
  244. #endif
  245. break;
  246. #endif
  247. case KC_0:
  248. case KC_F10:
  249. switch_layer(0);
  250. break;
  251. case KC_1:
  252. case KC_F1:
  253. switch_layer(1);
  254. break;
  255. case KC_2:
  256. case KC_F2:
  257. switch_layer(2);
  258. break;
  259. case KC_3:
  260. case KC_F3:
  261. switch_layer(3);
  262. break;
  263. case KC_4:
  264. case KC_F4:
  265. switch_layer(4);
  266. break;
  267. default:
  268. print("?");
  269. return false;
  270. }
  271. return true;
  272. }
  273. /***********************************************************
  274. * Command console
  275. ***********************************************************/
  276. static void command_console_help(void)
  277. {
  278. print_enable = true;
  279. print("\n\n----- Console Help -----\n");
  280. print("ESC/q: quit\n");
  281. #ifdef MOUSEKEY_ENABLE
  282. print("m: mousekey\n");
  283. #endif
  284. }
  285. static bool command_console(uint8_t code)
  286. {
  287. switch (code) {
  288. case KC_H:
  289. case KC_SLASH: /* ? */
  290. command_console_help();
  291. break;
  292. case KC_Q:
  293. case KC_ESC:
  294. print("\nQuit Console Mode\n");
  295. state = ONESHOT;
  296. return false;
  297. #ifdef MOUSEKEY_ENABLE
  298. case KC_M:
  299. mousekey_console_help();
  300. print("\nEnter Mousekey Console\n");
  301. print("M0>");
  302. state = MOUSEKEY;
  303. return true;
  304. #endif
  305. default:
  306. print("?");
  307. return false;
  308. }
  309. print("C> ");
  310. return true;
  311. }
  312. #ifdef MOUSEKEY_ENABLE
  313. /***********************************************************
  314. * Mousekey console
  315. ***********************************************************/
  316. static uint8_t mousekey_param = 0;
  317. static void mousekey_param_print(void)
  318. {
  319. print("\n\n----- Mousekey Parameters -----\n");
  320. print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
  321. print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
  322. print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
  323. print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
  324. print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  325. print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  326. }
  327. #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  328. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  329. {
  330. switch (param) {
  331. case 1:
  332. if (mk_delay + inc < UINT8_MAX)
  333. mk_delay += inc;
  334. else
  335. mk_delay = UINT8_MAX;
  336. PRINT_SET_VAL(mk_delay);
  337. break;
  338. case 2:
  339. if (mk_interval + inc < UINT8_MAX)
  340. mk_interval += inc;
  341. else
  342. mk_interval = UINT8_MAX;
  343. PRINT_SET_VAL(mk_interval);
  344. break;
  345. case 3:
  346. if (mk_max_speed + inc < UINT8_MAX)
  347. mk_max_speed += inc;
  348. else
  349. mk_max_speed = UINT8_MAX;
  350. PRINT_SET_VAL(mk_max_speed);
  351. break;
  352. case 4:
  353. if (mk_time_to_max + inc < UINT8_MAX)
  354. mk_time_to_max += inc;
  355. else
  356. mk_time_to_max = UINT8_MAX;
  357. PRINT_SET_VAL(mk_time_to_max);
  358. break;
  359. case 5:
  360. if (mk_wheel_max_speed + inc < UINT8_MAX)
  361. mk_wheel_max_speed += inc;
  362. else
  363. mk_wheel_max_speed = UINT8_MAX;
  364. PRINT_SET_VAL(mk_wheel_max_speed);
  365. break;
  366. case 6:
  367. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  368. mk_wheel_time_to_max += inc;
  369. else
  370. mk_wheel_time_to_max = UINT8_MAX;
  371. PRINT_SET_VAL(mk_wheel_time_to_max);
  372. break;
  373. }
  374. }
  375. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  376. {
  377. switch (param) {
  378. case 1:
  379. if (mk_delay > dec)
  380. mk_delay -= dec;
  381. else
  382. mk_delay = 0;
  383. PRINT_SET_VAL(mk_delay);
  384. break;
  385. case 2:
  386. if (mk_interval > dec)
  387. mk_interval -= dec;
  388. else
  389. mk_interval = 0;
  390. PRINT_SET_VAL(mk_interval);
  391. break;
  392. case 3:
  393. if (mk_max_speed > dec)
  394. mk_max_speed -= dec;
  395. else
  396. mk_max_speed = 0;
  397. PRINT_SET_VAL(mk_max_speed);
  398. break;
  399. case 4:
  400. if (mk_time_to_max > dec)
  401. mk_time_to_max -= dec;
  402. else
  403. mk_time_to_max = 0;
  404. PRINT_SET_VAL(mk_time_to_max);
  405. break;
  406. case 5:
  407. if (mk_wheel_max_speed > dec)
  408. mk_wheel_max_speed -= dec;
  409. else
  410. mk_wheel_max_speed = 0;
  411. PRINT_SET_VAL(mk_wheel_max_speed);
  412. break;
  413. case 6:
  414. if (mk_wheel_time_to_max > dec)
  415. mk_wheel_time_to_max -= dec;
  416. else
  417. mk_wheel_time_to_max = 0;
  418. PRINT_SET_VAL(mk_wheel_time_to_max);
  419. break;
  420. }
  421. }
  422. static void mousekey_console_help(void)
  423. {
  424. print("\n\n----- Mousekey Parameters Help -----\n");
  425. print("ESC/q: quit\n");
  426. print("1: select mk_delay(*10ms)\n");
  427. print("2: select mk_interval(ms)\n");
  428. print("3: select mk_max_speed\n");
  429. print("4: select mk_time_to_max\n");
  430. print("5: select mk_wheel_max_speed\n");
  431. print("6: select mk_wheel_time_to_max\n");
  432. print("p: print prameters\n");
  433. print("d: set default values\n");
  434. print("up: increase prameters(+1)\n");
  435. print("down: decrease prameters(-1)\n");
  436. print("pgup: increase prameters(+10)\n");
  437. print("pgdown: decrease prameters(-10)\n");
  438. print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
  439. print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
  440. print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
  441. print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
  442. }
  443. static bool mousekey_console(uint8_t code)
  444. {
  445. switch (code) {
  446. case KC_H:
  447. case KC_SLASH: /* ? */
  448. mousekey_console_help();
  449. break;
  450. case KC_Q:
  451. case KC_ESC:
  452. mousekey_param = 0;
  453. print("\nQuit Mousekey Console\n");
  454. print("C> ");
  455. state = CONSOLE;
  456. return false;
  457. case KC_P:
  458. mousekey_param_print();
  459. break;
  460. case KC_1:
  461. case KC_2:
  462. case KC_3:
  463. case KC_4:
  464. case KC_5:
  465. case KC_6:
  466. case KC_7:
  467. case KC_8:
  468. case KC_9:
  469. case KC_0:
  470. mousekey_param = numkey2num(code);
  471. print("selected parameter: "); pdec(mousekey_param); print("\n");
  472. break;
  473. case KC_UP:
  474. mousekey_param_inc(mousekey_param, 1);
  475. break;
  476. case KC_DOWN:
  477. mousekey_param_dec(mousekey_param, 1);
  478. break;
  479. case KC_PGUP:
  480. mousekey_param_inc(mousekey_param, 10);
  481. break;
  482. case KC_PGDN:
  483. mousekey_param_dec(mousekey_param, 10);
  484. break;
  485. case KC_D:
  486. mk_delay = MOUSEKEY_DELAY/10;
  487. mk_interval = MOUSEKEY_INTERVAL;
  488. mk_max_speed = MOUSEKEY_MAX_SPEED;
  489. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  490. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  491. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  492. print("set default values.\n");
  493. break;
  494. default:
  495. print("?");
  496. return false;
  497. }
  498. print("M"); pdec(mousekey_param); print("> ");
  499. return true;
  500. }
  501. #endif
  502. /***********************************************************
  503. * Utilities
  504. ***********************************************************/
  505. static uint8_t numkey2num(uint8_t code)
  506. {
  507. switch (code) {
  508. case KC_1: return 1;
  509. case KC_2: return 2;
  510. case KC_3: return 3;
  511. case KC_4: return 4;
  512. case KC_5: return 5;
  513. case KC_6: return 6;
  514. case KC_7: return 7;
  515. case KC_8: return 8;
  516. case KC_9: return 9;
  517. case KC_0: return 0;
  518. }
  519. return 0;
  520. }
  521. static void switch_layer(uint8_t layer)
  522. {
  523. print_val_hex8(current_layer);
  524. print_val_hex8(default_layer);
  525. current_layer = layer;
  526. default_layer = layer;
  527. print("switch to "); print_val_hex8(layer);
  528. }
  529. static void clear_keyboard(void)
  530. {
  531. host_clear_keys();
  532. host_clear_mods();
  533. host_send_keyboard_report();
  534. host_system_send(0);
  535. host_consumer_send(0);
  536. #ifdef MOUSEKEY_ENABLE
  537. mousekey_clear();
  538. mousekey_send();
  539. #endif
  540. }