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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 "bootloader.h"
  26. #include "command.h"
  27. #ifdef HOST_PJRC
  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. host_clear_keyboard_report();
  68. host_send_keyboard_report();
  69. print("jump to bootloader... ");
  70. _delay_ms(1000);
  71. bootloader_jump(); // not return
  72. print("not supported.\n");
  73. break;
  74. case KB_D:
  75. debug_enable = !debug_enable;
  76. if (debug_enable) {
  77. last_print_enable = true;
  78. print("debug enabled.\n");
  79. debug_matrix = true;
  80. debug_keyboard = true;
  81. debug_mouse = true;
  82. } else {
  83. print("debug disabled.\n");
  84. last_print_enable = false;
  85. debug_matrix = false;
  86. debug_keyboard = false;
  87. debug_mouse = false;
  88. }
  89. break;
  90. case KB_X: // debug matrix toggle
  91. debug_matrix = !debug_matrix;
  92. if (debug_matrix)
  93. print("debug matrix enabled.\n");
  94. else
  95. print("debug matrix disabled.\n");
  96. break;
  97. case KB_K: // debug keyboard toggle
  98. debug_keyboard = !debug_keyboard;
  99. if (debug_keyboard)
  100. print("debug keyboard enabled.\n");
  101. else
  102. print("debug keyboard disabled.\n");
  103. break;
  104. case KB_M: // debug mouse toggle
  105. debug_mouse = !debug_mouse;
  106. if (debug_mouse)
  107. print("debug mouse enabled.\n");
  108. else
  109. print("debug mouse disabled.\n");
  110. break;
  111. case KB_V: // print version & information
  112. print(STR(DESCRIPTION) "\n");
  113. break;
  114. case KB_T: // print timer
  115. print("timer: "); phex16(timer_count); print("\n");
  116. break;
  117. case KB_P: // print toggle
  118. if (last_print_enable) {
  119. print("print disabled.\n");
  120. last_print_enable = false;
  121. } else {
  122. last_print_enable = true;
  123. print("print enabled.\n");
  124. }
  125. break;
  126. case KB_S:
  127. #ifdef HOST_PJRC
  128. print("UDCON: "); phex(UDCON); print("\n");
  129. print("UDIEN: "); phex(UDIEN); print("\n");
  130. print("UDINT: "); phex(UDINT); print("\n");
  131. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  132. print("usb_keyboard_protocol: "); phex(usb_keyboard_protocol); print("\n");
  133. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  134. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  135. #endif
  136. #ifdef HOST_VUSB
  137. # if USB_COUNT_SOF
  138. print("usbSofCount: "); phex(usbSofCount); print("\n");
  139. # endif
  140. #endif
  141. break;
  142. #ifdef NKRO_ENABLE
  143. case KB_N:
  144. // send empty report before change
  145. host_clear_keyboard_report();
  146. host_send_keyboard_report();
  147. keyboard_nkro = !keyboard_nkro;
  148. if (keyboard_nkro)
  149. print("NKRO: enabled\n");
  150. else
  151. print("NKRO: disabled\n");
  152. break;
  153. #endif
  154. #ifdef EXTRAKEY_ENABLE
  155. case KB_ESC:
  156. host_clear_keyboard_report();
  157. host_send_keyboard_report();
  158. #ifdef HOST_PJRC
  159. if (suspend && remote_wakeup) {
  160. usb_remote_wakeup();
  161. } else {
  162. host_system_send(SYSTEM_POWER_DOWN);
  163. host_system_send(0);
  164. _delay_ms(500);
  165. }
  166. #else
  167. host_system_send(SYSTEM_POWER_DOWN);
  168. host_system_send(0);
  169. _delay_ms(500);
  170. #endif
  171. break;
  172. #endif
  173. case KB_BSPC:
  174. matrix_init();
  175. print("clear matrix\n");
  176. break;
  177. case KB_0:
  178. switch_layer(0);
  179. break;
  180. case KB_1:
  181. switch_layer(1);
  182. break;
  183. case KB_2:
  184. switch_layer(2);
  185. break;
  186. case KB_3:
  187. switch_layer(3);
  188. break;
  189. case KB_4:
  190. switch_layer(4);
  191. break;
  192. default:
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. static void help(void)
  198. {
  199. print("b: jump to bootloader\n");
  200. print("d: toggle debug enable\n");
  201. print("x: toggle matrix debug\n");
  202. print("k: toggle keyboard debug\n");
  203. print("m: toggle mouse debug\n");
  204. print("p: toggle print enable\n");
  205. print("v: print version\n");
  206. print("t: print timer count\n");
  207. print("s: print status\n");
  208. #ifdef NKRO_ENABLE
  209. print("n: toggle NKRO\n");
  210. #endif
  211. print("Backspace: clear matrix\n");
  212. print("ESC: power down/wake up\n");
  213. print("0: switch to Layer0 \n");
  214. print("1: switch to Layer1 \n");
  215. print("2: switch to Layer2 \n");
  216. print("3: switch to Layer3 \n");
  217. print("4: switch to Layer4 \n");
  218. }
  219. static void switch_layer(uint8_t layer)
  220. {
  221. print("current_layer: "); phex(current_layer); print("\n");
  222. print("default_layer: "); phex(default_layer); print("\n");
  223. current_layer = layer;
  224. default_layer = layer;
  225. print("switch to Layer: "); phex(layer); print("\n");
  226. }