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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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 "command.h"
  27. #ifdef MOUSEKEY_ENABLE
  28. #include "mousekey.h"
  29. #endif
  30. #ifdef HOST_PJRC
  31. # include "usb_keyboard.h"
  32. # ifdef EXTRAKEY_ENABLE
  33. # include "usb_extra.h"
  34. # endif
  35. #endif
  36. #ifdef HOST_VUSB
  37. # include "usbdrv.h"
  38. #endif
  39. static bool command_common(uint8_t code);
  40. static void command_common_help(void);
  41. static bool command_console(uint8_t code);
  42. static void command_console_help(void);
  43. #ifdef MOUSEKEY_ENABLE
  44. static bool mousekey_console(uint8_t code);
  45. static void mousekey_console_help(void);
  46. #endif
  47. static uint8_t numkey2num(uint8_t code);
  48. static void switch_layer(uint8_t layer);
  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_ESC:
  248. case KC_GRV:
  249. case KC_0:
  250. clear_keyboard();
  251. switch_layer(0);
  252. break;
  253. case KC_1 ... KC_9:
  254. clear_keyboard();
  255. switch_layer((code - KC_1) + 1);
  256. break;
  257. case KC_F1 ... KC_F12:
  258. clear_keyboard();
  259. switch_layer((code - KC_F1) + 1);
  260. break;
  261. default:
  262. print("?");
  263. return false;
  264. }
  265. return true;
  266. }
  267. /***********************************************************
  268. * Command console
  269. ***********************************************************/
  270. static void command_console_help(void)
  271. {
  272. print_enable = true;
  273. print("\n\n----- Console Help -----\n");
  274. print("ESC/q: quit\n");
  275. #ifdef MOUSEKEY_ENABLE
  276. print("m: mousekey\n");
  277. #endif
  278. }
  279. static bool command_console(uint8_t code)
  280. {
  281. switch (code) {
  282. case KC_H:
  283. case KC_SLASH: /* ? */
  284. command_console_help();
  285. break;
  286. case KC_Q:
  287. case KC_ESC:
  288. print("\nQuit Console Mode\n");
  289. state = ONESHOT;
  290. return false;
  291. #ifdef MOUSEKEY_ENABLE
  292. case KC_M:
  293. mousekey_console_help();
  294. print("\nEnter Mousekey Console\n");
  295. print("M0>");
  296. state = MOUSEKEY;
  297. return true;
  298. #endif
  299. default:
  300. print("?");
  301. return false;
  302. }
  303. print("C> ");
  304. return true;
  305. }
  306. #ifdef MOUSEKEY_ENABLE
  307. /***********************************************************
  308. * Mousekey console
  309. ***********************************************************/
  310. static uint8_t mousekey_param = 0;
  311. static void mousekey_param_print(void)
  312. {
  313. print("\n\n----- Mousekey Parameters -----\n");
  314. print("1: mk_delay(*10ms): "); pdec(mk_delay); print("\n");
  315. print("2: mk_interval(ms): "); pdec(mk_interval); print("\n");
  316. print("3: mk_max_speed: "); pdec(mk_max_speed); print("\n");
  317. print("4: mk_time_to_max: "); pdec(mk_time_to_max); print("\n");
  318. print("5: mk_wheel_max_speed: "); pdec(mk_wheel_max_speed); print("\n");
  319. print("6: mk_wheel_time_to_max: "); pdec(mk_wheel_time_to_max); print("\n");
  320. }
  321. #define PRINT_SET_VAL(v) print(#v " = "); print_dec(v); print("\n");
  322. static void mousekey_param_inc(uint8_t param, uint8_t inc)
  323. {
  324. switch (param) {
  325. case 1:
  326. if (mk_delay + inc < UINT8_MAX)
  327. mk_delay += inc;
  328. else
  329. mk_delay = UINT8_MAX;
  330. PRINT_SET_VAL(mk_delay);
  331. break;
  332. case 2:
  333. if (mk_interval + inc < UINT8_MAX)
  334. mk_interval += inc;
  335. else
  336. mk_interval = UINT8_MAX;
  337. PRINT_SET_VAL(mk_interval);
  338. break;
  339. case 3:
  340. if (mk_max_speed + inc < UINT8_MAX)
  341. mk_max_speed += inc;
  342. else
  343. mk_max_speed = UINT8_MAX;
  344. PRINT_SET_VAL(mk_max_speed);
  345. break;
  346. case 4:
  347. if (mk_time_to_max + inc < UINT8_MAX)
  348. mk_time_to_max += inc;
  349. else
  350. mk_time_to_max = UINT8_MAX;
  351. PRINT_SET_VAL(mk_time_to_max);
  352. break;
  353. case 5:
  354. if (mk_wheel_max_speed + inc < UINT8_MAX)
  355. mk_wheel_max_speed += inc;
  356. else
  357. mk_wheel_max_speed = UINT8_MAX;
  358. PRINT_SET_VAL(mk_wheel_max_speed);
  359. break;
  360. case 6:
  361. if (mk_wheel_time_to_max + inc < UINT8_MAX)
  362. mk_wheel_time_to_max += inc;
  363. else
  364. mk_wheel_time_to_max = UINT8_MAX;
  365. PRINT_SET_VAL(mk_wheel_time_to_max);
  366. break;
  367. }
  368. }
  369. static void mousekey_param_dec(uint8_t param, uint8_t dec)
  370. {
  371. switch (param) {
  372. case 1:
  373. if (mk_delay > dec)
  374. mk_delay -= dec;
  375. else
  376. mk_delay = 0;
  377. PRINT_SET_VAL(mk_delay);
  378. break;
  379. case 2:
  380. if (mk_interval > dec)
  381. mk_interval -= dec;
  382. else
  383. mk_interval = 0;
  384. PRINT_SET_VAL(mk_interval);
  385. break;
  386. case 3:
  387. if (mk_max_speed > dec)
  388. mk_max_speed -= dec;
  389. else
  390. mk_max_speed = 0;
  391. PRINT_SET_VAL(mk_max_speed);
  392. break;
  393. case 4:
  394. if (mk_time_to_max > dec)
  395. mk_time_to_max -= dec;
  396. else
  397. mk_time_to_max = 0;
  398. PRINT_SET_VAL(mk_time_to_max);
  399. break;
  400. case 5:
  401. if (mk_wheel_max_speed > dec)
  402. mk_wheel_max_speed -= dec;
  403. else
  404. mk_wheel_max_speed = 0;
  405. PRINT_SET_VAL(mk_wheel_max_speed);
  406. break;
  407. case 6:
  408. if (mk_wheel_time_to_max > dec)
  409. mk_wheel_time_to_max -= dec;
  410. else
  411. mk_wheel_time_to_max = 0;
  412. PRINT_SET_VAL(mk_wheel_time_to_max);
  413. break;
  414. }
  415. }
  416. static void mousekey_console_help(void)
  417. {
  418. print("\n\n----- Mousekey Parameters Help -----\n");
  419. print("ESC/q: quit\n");
  420. print("1: select mk_delay(*10ms)\n");
  421. print("2: select mk_interval(ms)\n");
  422. print("3: select mk_max_speed\n");
  423. print("4: select mk_time_to_max\n");
  424. print("5: select mk_wheel_max_speed\n");
  425. print("6: select mk_wheel_time_to_max\n");
  426. print("p: print prameters\n");
  427. print("d: set default values\n");
  428. print("up: increase prameters(+1)\n");
  429. print("down: decrease prameters(-1)\n");
  430. print("pgup: increase prameters(+10)\n");
  431. print("pgdown: decrease prameters(-10)\n");
  432. print("\nspeed = delta * max_speed * (repeat / time_to_max)\n");
  433. print("where delta: cursor="); pdec(MOUSEKEY_MOVE_DELTA);
  434. print(", wheel="); pdec(MOUSEKEY_WHEEL_DELTA); print("\n");
  435. print("See http://en.wikipedia.org/wiki/Mouse_keys\n");
  436. }
  437. static bool mousekey_console(uint8_t code)
  438. {
  439. switch (code) {
  440. case KC_H:
  441. case KC_SLASH: /* ? */
  442. mousekey_console_help();
  443. break;
  444. case KC_Q:
  445. case KC_ESC:
  446. mousekey_param = 0;
  447. print("\nQuit Mousekey Console\n");
  448. print("C> ");
  449. state = CONSOLE;
  450. return false;
  451. case KC_P:
  452. mousekey_param_print();
  453. break;
  454. case KC_1:
  455. case KC_2:
  456. case KC_3:
  457. case KC_4:
  458. case KC_5:
  459. case KC_6:
  460. case KC_7:
  461. case KC_8:
  462. case KC_9:
  463. case KC_0:
  464. mousekey_param = numkey2num(code);
  465. print("selected parameter: "); pdec(mousekey_param); print("\n");
  466. break;
  467. case KC_UP:
  468. mousekey_param_inc(mousekey_param, 1);
  469. break;
  470. case KC_DOWN:
  471. mousekey_param_dec(mousekey_param, 1);
  472. break;
  473. case KC_PGUP:
  474. mousekey_param_inc(mousekey_param, 10);
  475. break;
  476. case KC_PGDN:
  477. mousekey_param_dec(mousekey_param, 10);
  478. break;
  479. case KC_D:
  480. mk_delay = MOUSEKEY_DELAY/10;
  481. mk_interval = MOUSEKEY_INTERVAL;
  482. mk_max_speed = MOUSEKEY_MAX_SPEED;
  483. mk_time_to_max = MOUSEKEY_TIME_TO_MAX;
  484. mk_wheel_max_speed = MOUSEKEY_WHEEL_MAX_SPEED;
  485. mk_wheel_time_to_max = MOUSEKEY_WHEEL_TIME_TO_MAX;
  486. print("set default values.\n");
  487. break;
  488. default:
  489. print("?");
  490. return false;
  491. }
  492. print("M"); pdec(mousekey_param); print("> ");
  493. return true;
  494. }
  495. #endif
  496. /***********************************************************
  497. * Utilities
  498. ***********************************************************/
  499. static uint8_t numkey2num(uint8_t code)
  500. {
  501. switch (code) {
  502. case KC_1: return 1;
  503. case KC_2: return 2;
  504. case KC_3: return 3;
  505. case KC_4: return 4;
  506. case KC_5: return 5;
  507. case KC_6: return 6;
  508. case KC_7: return 7;
  509. case KC_8: return 8;
  510. case KC_9: return 9;
  511. case KC_0: return 0;
  512. }
  513. return 0;
  514. }
  515. static void switch_layer(uint8_t layer)
  516. {
  517. print_val_hex8(current_layer);
  518. print_val_hex8(default_layer);
  519. default_layer = layer;
  520. current_layer = 0;
  521. print("switch to "); print_val_hex8(layer);
  522. }