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ů.
Tento repozitář je archivovaný. Můžete prohlížet soubory, klonovat, ale nemůžete nahrávat a vytvářet nové úkoly a požadavky na natažení.

command.c 17KB

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