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 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "usb_keycodes.h"
  18. #include "host.h"
  19. #include "print.h"
  20. #include "debug.h"
  21. #include "util.h"
  22. #include "timer.h"
  23. #include "layer.h"
  24. #include "matrix.h"
  25. #include "command.h"
  26. #ifdef HOST_PJRC
  27. # include "jump_bootloader.h"
  28. # include "usb_keyboard.h"
  29. # ifdef USB_EXTRA_ENABLE
  30. # include "usb_extra.h"
  31. # endif
  32. #endif
  33. static void help(void);
  34. static void switch_layer(uint8_t layer);
  35. uint8_t command_proc(void)
  36. {
  37. if (!IS_COMMAND())
  38. return 0;
  39. uint8_t processed = 1;
  40. bool last_print_enable = print_enable;
  41. print_enable = true;
  42. switch (host_get_first_key()) {
  43. case KB_H:
  44. help();
  45. break;
  46. case KB_B:
  47. #ifdef HOST_PJRC
  48. host_clear_keyboard_report();
  49. host_send_keyboard_report();
  50. print("jump to bootloader...\n");
  51. _delay_ms(1000);
  52. jump_bootloader(); // not return
  53. #endif
  54. break;
  55. case KB_D:
  56. debug_enable = !debug_enable;
  57. if (debug_enable) {
  58. last_print_enable = true;
  59. print("debug enabled.\n");
  60. debug_matrix = true;
  61. debug_keyboard = true;
  62. debug_mouse = true;
  63. } else {
  64. print("debug disabled.\n");
  65. last_print_enable = false;
  66. debug_matrix = false;
  67. debug_keyboard = false;
  68. debug_mouse = false;
  69. }
  70. break;
  71. case KB_X: // debug matrix toggle
  72. debug_matrix = !debug_matrix;
  73. if (debug_matrix)
  74. print("debug matrix enabled.\n");
  75. else
  76. print("debug matrix disabled.\n");
  77. break;
  78. case KB_K: // debug keyboard toggle
  79. debug_keyboard = !debug_keyboard;
  80. if (debug_keyboard)
  81. print("debug keyboard enabled.\n");
  82. else
  83. print("debug keyboard disabled.\n");
  84. break;
  85. case KB_M: // debug mouse toggle
  86. debug_mouse = !debug_mouse;
  87. if (debug_mouse)
  88. print("debug mouse enabled.\n");
  89. else
  90. print("debug mouse disabled.\n");
  91. break;
  92. case KB_V: // print version & information
  93. print(STR(DESCRIPTION) "\n");
  94. break;
  95. case KB_T: // print timer
  96. print("timer: "); phex16(timer_count); print("\n");
  97. break;
  98. case KB_P: // print toggle
  99. if (last_print_enable) {
  100. print("print disabled.\n");
  101. last_print_enable = false;
  102. } else {
  103. last_print_enable = true;
  104. print("print enabled.\n");
  105. }
  106. break;
  107. case KB_S:
  108. #ifdef HOST_PJRC
  109. print("UDCON: "); phex(UDCON); print("\n");
  110. print("UDIEN: "); phex(UDIEN); print("\n");
  111. print("UDINT: "); phex(UDINT); print("\n");
  112. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  113. print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
  114. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  115. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  116. #endif
  117. break;
  118. #ifdef USB_NKRO_ENABLE
  119. case KB_N:
  120. // send empty report before change
  121. host_clear_keyboard_report();
  122. host_send_keyboard_report();
  123. keyboard_nkro = !keyboard_nkro;
  124. if (keyboard_nkro)
  125. print("USB_NKRO: enabled\n");
  126. else
  127. print("USB_NKRO: disabled\n");
  128. break;
  129. #endif
  130. #ifdef USB_EXTRA_ENABLE
  131. case KB_ESC:
  132. host_clear_keyboard_report();
  133. host_send_keyboard_report();
  134. #ifdef HOST_PJRC
  135. if (suspend && remote_wakeup) {
  136. usb_remote_wakeup();
  137. } else {
  138. host_system_send(SYSTEM_POWER_DOWN);
  139. host_system_send(0);
  140. _delay_ms(500);
  141. }
  142. #else
  143. host_system_send(SYSTEM_POWER_DOWN);
  144. host_system_send(0);
  145. _delay_ms(500);
  146. #endif
  147. break;
  148. #endif
  149. case KB_BSPC:
  150. matrix_init();
  151. print("clear matrix\n");
  152. break;
  153. case KB_0:
  154. switch_layer(0);
  155. break;
  156. case KB_1:
  157. switch_layer(1);
  158. break;
  159. case KB_2:
  160. switch_layer(2);
  161. break;
  162. case KB_3:
  163. switch_layer(3);
  164. break;
  165. case KB_4:
  166. switch_layer(4);
  167. break;
  168. default:
  169. processed = 0;
  170. }
  171. if (processed)
  172. _delay_ms(500);
  173. print_enable = last_print_enable;
  174. return processed;
  175. }
  176. static void help(void)
  177. {
  178. print("b: jump to bootloader\n");
  179. print("d: toggle debug enable\n");
  180. print("x: toggle matrix debug\n");
  181. print("k: toggle keyboard debug\n");
  182. print("m: toggle mouse debug\n");
  183. print("p: toggle print enable\n");
  184. print("v: print version\n");
  185. print("t: print timer count\n");
  186. print("s: print status\n");
  187. #ifdef USB_NKRO_ENABLE
  188. print("n: toggle USB_NKRO\n");
  189. #endif
  190. print("Backspace: clear matrix\n");
  191. print("ESC: power down/wake up\n");
  192. print("0: switch to Layer0 \n");
  193. print("1: switch to Layer1 \n");
  194. print("2: switch to Layer2 \n");
  195. print("3: switch to Layer3 \n");
  196. print("4: switch to Layer4 \n");
  197. }
  198. static void switch_layer(uint8_t layer)
  199. {
  200. print("current_layer: "); phex(current_layer); print("\n");
  201. print("default_layer: "); phex(default_layer); print("\n");
  202. current_layer = layer;
  203. default_layer = layer;
  204. print("switch to Layer: "); phex(layer); print("\n");
  205. }