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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 EXTRAKEY_ENABLE
  30. # include "usb_extra.h"
  31. # endif
  32. #endif
  33. #ifdef HOST_VUSB
  34. # include "usbdrv.h"
  35. #endif
  36. static uint8_t command_common(void);
  37. static void help(void);
  38. static void switch_layer(uint8_t layer);
  39. static bool last_print_enable;
  40. uint8_t command_proc(void)
  41. {
  42. uint8_t processed = 0;
  43. last_print_enable = print_enable;
  44. if (!IS_COMMAND())
  45. return 0;
  46. print_enable = true;
  47. if (command_extra() || command_common()) {
  48. processed = 1;
  49. _delay_ms(500);
  50. }
  51. print_enable = last_print_enable;
  52. return processed;
  53. }
  54. /* This allows to define extra commands. return 0 when not processed. */
  55. uint8_t command_extra(void) __attribute__ ((weak));
  56. uint8_t command_extra(void)
  57. {
  58. return 0;
  59. }
  60. static uint8_t command_common(void)
  61. {
  62. switch (host_get_first_key()) {
  63. case KB_H:
  64. help();
  65. break;
  66. case KB_B:
  67. #ifdef HOST_PJRC
  68. host_clear_keyboard_report();
  69. host_send_keyboard_report();
  70. print("jump to bootloader...\n");
  71. _delay_ms(1000);
  72. jump_bootloader(); // not return
  73. #endif
  74. break;
  75. case KB_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 KB_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 KB_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 KB_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 KB_V: // print version & information
  113. print(STR(DESCRIPTION) "\n");
  114. break;
  115. case KB_T: // print timer
  116. print("timer: "); phex16(timer_count); print("\n");
  117. break;
  118. case KB_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 KB_S:
  128. #ifdef HOST_PJRC
  129. print("UDCON: "); phex(UDCON); print("\n");
  130. print("UDIEN: "); phex(UDIEN); print("\n");
  131. print("UDINT: "); phex(UDINT); print("\n");
  132. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  133. print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
  134. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  135. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  136. #endif
  137. #ifdef HOST_VUSB
  138. # if USB_COUNT_SOF
  139. print("usbSofCount: "); phex(usbSofCount); print("\n");
  140. # endif
  141. #endif
  142. break;
  143. #ifdef NKRO_ENABLE
  144. case KB_N:
  145. // send empty report before change
  146. host_clear_keyboard_report();
  147. host_send_keyboard_report();
  148. keyboard_nkro = !keyboard_nkro;
  149. if (keyboard_nkro)
  150. print("NKRO: enabled\n");
  151. else
  152. print("NKRO: disabled\n");
  153. break;
  154. #endif
  155. #ifdef EXTRAKEY_ENABLE
  156. case KB_ESC:
  157. host_clear_keyboard_report();
  158. host_send_keyboard_report();
  159. #ifdef HOST_PJRC
  160. if (suspend && remote_wakeup) {
  161. usb_remote_wakeup();
  162. } else {
  163. host_system_send(SYSTEM_POWER_DOWN);
  164. host_system_send(0);
  165. _delay_ms(500);
  166. }
  167. #else
  168. host_system_send(SYSTEM_POWER_DOWN);
  169. host_system_send(0);
  170. _delay_ms(500);
  171. #endif
  172. break;
  173. #endif
  174. case KB_BSPC:
  175. matrix_init();
  176. print("clear matrix\n");
  177. break;
  178. case KB_0:
  179. switch_layer(0);
  180. break;
  181. case KB_1:
  182. switch_layer(1);
  183. break;
  184. case KB_2:
  185. switch_layer(2);
  186. break;
  187. case KB_3:
  188. switch_layer(3);
  189. break;
  190. case KB_4:
  191. switch_layer(4);
  192. break;
  193. default:
  194. return 0;
  195. }
  196. return 1;
  197. }
  198. static void help(void)
  199. {
  200. print("b: jump to bootloader\n");
  201. print("d: toggle debug enable\n");
  202. print("x: toggle matrix debug\n");
  203. print("k: toggle keyboard debug\n");
  204. print("m: toggle mouse debug\n");
  205. print("p: toggle print enable\n");
  206. print("v: print version\n");
  207. print("t: print timer count\n");
  208. print("s: print status\n");
  209. #ifdef NKRO_ENABLE
  210. print("n: toggle NKRO\n");
  211. #endif
  212. print("Backspace: clear matrix\n");
  213. print("ESC: power down/wake up\n");
  214. print("0: switch to Layer0 \n");
  215. print("1: switch to Layer1 \n");
  216. print("2: switch to Layer2 \n");
  217. print("3: switch to Layer3 \n");
  218. print("4: switch to Layer4 \n");
  219. }
  220. static void switch_layer(uint8_t layer)
  221. {
  222. print("current_layer: "); phex(current_layer); print("\n");
  223. print("default_layer: "); phex(default_layer); print("\n");
  224. current_layer = layer;
  225. default_layer = layer;
  226. print("switch to Layer: "); phex(layer); print("\n");
  227. }