Keyboard firmwares for Atmel AVR and Cortex-M
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

command.c 16KB

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