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

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