Keyboard firmwares for Atmel AVR and Cortex-M
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.

command.c 7.0KB

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
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "timer.h"
  23. #include "keyboard.h"
  24. #include "bootloader.h"
  25. #include "command.h"
  26. #ifdef MOUSEKEY_ENABLE
  27. #include "mousekey.h"
  28. #endif
  29. #ifdef HOST_PJRC
  30. # include "usb_keyboard.h"
  31. # ifdef EXTRAKEY_ENABLE
  32. # include "usb_extra.h"
  33. # endif
  34. #endif
  35. #ifdef HOST_VUSB
  36. # include "usbdrv.h"
  37. #endif
  38. static bool command_common(uint8_t code);
  39. static void help(void);
  40. static void switch_layer(uint8_t layer);
  41. static void clear_keyboard(void);
  42. static bool last_print_enable;
  43. bool command_proc(uint8_t code)
  44. {
  45. if (!IS_COMMAND())
  46. return false;
  47. last_print_enable = print_enable;
  48. print_enable = true;
  49. if (command_extra(code) || command_common(code)) {
  50. _delay_ms(500);
  51. return true;
  52. }
  53. print_enable = last_print_enable;
  54. return false;
  55. }
  56. /* This allows to define extra commands. return 0 when not processed. */
  57. bool command_extra(uint8_t code) __attribute__ ((weak));
  58. bool command_extra(uint8_t code)
  59. {
  60. return false;
  61. }
  62. static bool command_common(uint8_t code)
  63. {
  64. switch (code) {
  65. case KC_H:
  66. help();
  67. break;
  68. case KC_DEL:
  69. clear_keyboard();
  70. print("jump to bootloader... ");
  71. _delay_ms(1000);
  72. bootloader_jump(); // not return
  73. print("not supported.\n");
  74. break;
  75. case KC_D:
  76. debug_enable = !debug_enable;
  77. if (debug_enable) {
  78. last_print_enable = true;
  79. print("debug enabled.\n");
  80. debug_matrix = true;
  81. debug_keyboard = true;
  82. debug_mouse = true;
  83. } else {
  84. print("debug disabled.\n");
  85. last_print_enable = false;
  86. debug_matrix = false;
  87. debug_keyboard = false;
  88. debug_mouse = false;
  89. }
  90. break;
  91. case KC_X: // debug matrix toggle
  92. debug_matrix = !debug_matrix;
  93. if (debug_matrix)
  94. print("debug matrix enabled.\n");
  95. else
  96. print("debug matrix disabled.\n");
  97. break;
  98. case KC_K: // debug keyboard toggle
  99. debug_keyboard = !debug_keyboard;
  100. if (debug_keyboard)
  101. print("debug keyboard enabled.\n");
  102. else
  103. print("debug keyboard disabled.\n");
  104. break;
  105. case KC_M: // debug mouse toggle
  106. debug_mouse = !debug_mouse;
  107. if (debug_mouse)
  108. print("debug mouse enabled.\n");
  109. else
  110. print("debug mouse disabled.\n");
  111. break;
  112. case KC_V: // print version & information
  113. print(STR(DESCRIPTION) "\n");
  114. break;
  115. case KC_T: // print timer
  116. print("timer: "); phex16(timer_count); print("\n");
  117. break;
  118. case KC_P: // print toggle
  119. if (last_print_enable) {
  120. print("print disabled.\n");
  121. last_print_enable = false;
  122. } else {
  123. last_print_enable = true;
  124. print("print enabled.\n");
  125. }
  126. break;
  127. case KC_S:
  128. print("host_keyboard_leds:"); phex(host_keyboard_leds()); print("\n");
  129. #ifdef HOST_PJRC
  130. print("UDCON: "); phex(UDCON); print("\n");
  131. print("UDIEN: "); phex(UDIEN); print("\n");
  132. print("UDINT: "); phex(UDINT); print("\n");
  133. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  134. print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
  135. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  136. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  137. #endif
  138. #ifdef HOST_VUSB
  139. # if USB_COUNT_SOF
  140. print("usbSofCount: "); phex(usbSofCount); print("\n");
  141. # endif
  142. #endif
  143. break;
  144. #ifdef NKRO_ENABLE
  145. case KC_N:
  146. keyboard_nkro = !keyboard_nkro;
  147. if (keyboard_nkro)
  148. print("NKRO: enabled\n");
  149. else
  150. print("NKRO: disabled\n");
  151. break;
  152. #endif
  153. #ifdef EXTRAKEY_ENABLE
  154. case KC_ESC:
  155. #ifdef HOST_PJRC
  156. if (suspend && remote_wakeup) {
  157. usb_remote_wakeup();
  158. } else {
  159. host_system_send(SYSTEM_POWER_DOWN);
  160. host_system_send(0);
  161. _delay_ms(500);
  162. }
  163. #else
  164. host_system_send(SYSTEM_POWER_DOWN);
  165. host_system_send(0);
  166. _delay_ms(500);
  167. #endif
  168. break;
  169. #endif
  170. case KC_0:
  171. case KC_F10:
  172. switch_layer(0);
  173. break;
  174. case KC_1:
  175. case KC_F1:
  176. switch_layer(1);
  177. break;
  178. case KC_2:
  179. case KC_F2:
  180. switch_layer(2);
  181. break;
  182. case KC_3:
  183. case KC_F3:
  184. switch_layer(3);
  185. break;
  186. case KC_4:
  187. case KC_F4:
  188. switch_layer(4);
  189. break;
  190. default:
  191. return false;
  192. }
  193. return true;
  194. }
  195. static void help(void)
  196. {
  197. print("d: toggle debug enable\n");
  198. print("x: toggle matrix debug\n");
  199. print("k: toggle keyboard debug\n");
  200. print("m: toggle mouse debug\n");
  201. print("p: toggle print enable\n");
  202. print("v: print version\n");
  203. print("t: print timer count\n");
  204. print("s: print status\n");
  205. print("ESC: power down/wake up\n");
  206. print("0/F10: switch to Layer0 \n");
  207. print("1/F1: switch to Layer1 \n");
  208. print("2/F2: switch to Layer2 \n");
  209. print("3/F3: switch to Layer3 \n");
  210. print("4/F4: switch to Layer4 \n");
  211. #ifdef NKRO_ENABLE
  212. print("n: toggle NKRO\n");
  213. #endif
  214. print("DEL: jump to bootloader\n");
  215. }
  216. static void switch_layer(uint8_t layer)
  217. {
  218. print("current_layer: "); phex(current_layer); print("\n");
  219. print("default_layer: "); phex(default_layer); print("\n");
  220. current_layer = layer;
  221. default_layer = layer;
  222. print("switch to Layer: "); phex(layer); print("\n");
  223. }
  224. static void clear_keyboard(void)
  225. {
  226. host_clear_keys();
  227. host_send_keyboard_report();
  228. host_system_send(0);
  229. host_consumer_send(0);
  230. #ifdef MOUSEKEY_ENABLE
  231. mousekey_clear();
  232. mousekey_send();
  233. #endif
  234. }