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.

644 lines
19 KiB

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