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