您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
此仓库已存档。您可以查看文件和克隆,但不能推送或创建工单/合并请求。

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