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

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