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

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