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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

command.c 17KB

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