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.

key_process.c 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #include <stdbool.h>
  2. #include <avr/io.h>
  3. #include <avr/interrupt.h>
  4. #include <util/delay.h>
  5. #include "print.h"
  6. #include "debug.h"
  7. #include "timer.h"
  8. #include "util.h"
  9. #include "jump_bootloader.h"
  10. #include "usb_keyboard.h"
  11. #include "usb_mouse.h"
  12. #include "usb_extra.h"
  13. #include "usb_keycodes.h"
  14. #include "usb.h"
  15. #include "layer.h"
  16. #include "matrix_skel.h"
  17. #include "keymap_skel.h"
  18. #include "key_process.h"
  19. #ifdef MOUSEKEY_ENABLE
  20. # include "mousekey.h"
  21. #endif
  22. #ifdef PS2_MOUSE_ENABLE
  23. # include "ps2_mouse.h"
  24. #endif
  25. // TODO: refactoring
  26. void proc_matrix(void) {
  27. bool modified = false;
  28. uint8_t fn_bits = 0;
  29. matrix_scan();
  30. modified = matrix_is_modified();
  31. if (modified) {
  32. if (debug_matrix) matrix_print();
  33. #ifdef DEBUG_LED
  34. // LED flash for debug
  35. DEBUG_LED_CONFIG;
  36. DEBUG_LED_ON;
  37. #endif
  38. }
  39. if (matrix_has_ghost()) {
  40. // should send error?
  41. debug("matrix has ghost!!\n");
  42. return;
  43. }
  44. usb_keyboard_swap_report();
  45. usb_keyboard_clear_report();
  46. for (int row = 0; row < matrix_rows(); row++) {
  47. for (int col = 0; col < matrix_cols(); col++) {
  48. if (!matrix_is_on(row, col)) continue;
  49. // TODO: clean code
  50. uint8_t code = layer_get_keycode(row, col);
  51. if (code == KB_NO) {
  52. // do nothing
  53. } else if (IS_MOD(code)) {
  54. usb_keyboard_add_mod(code);
  55. } else if (IS_FN(code)) {
  56. fn_bits |= FN_BIT(code);
  57. } else if (IS_MOUSE(code)) {
  58. #ifdef MOUSEKEY_ENABLE
  59. mousekey_decode(code);
  60. #endif
  61. }
  62. // audio control & system control
  63. else if (code == KB_MUTE) {
  64. usb_extra_audio_send(AUDIO_MUTE);
  65. usb_extra_audio_send(0);
  66. _delay_ms(500);
  67. } else if (code == KB_VOLU) {
  68. usb_extra_audio_send(AUDIO_VOL_UP);
  69. usb_extra_audio_send(0);
  70. _delay_ms(100);
  71. } else if (code == KB_VOLD) {
  72. usb_extra_audio_send(AUDIO_VOL_DOWN);
  73. usb_extra_audio_send(0);
  74. _delay_ms(100);
  75. } else if (code == KB_PWR) {
  76. if (suspend && remote_wakeup) {
  77. usb_remote_wakeup();
  78. } else {
  79. usb_extra_system_send(SYSTEM_POWER_DOWN);
  80. }
  81. _delay_ms(1000);
  82. }
  83. // normal keys
  84. else {
  85. usb_keyboard_add_key(code);
  86. }
  87. }
  88. }
  89. if (modified) {
  90. #ifdef DEBUG_LED
  91. // LED flash for debug
  92. DEBUG_LED_CONFIG;
  93. DEBUG_LED_OFF;
  94. #endif
  95. }
  96. layer_switching(fn_bits);
  97. // TODO: clean code
  98. // special mode for control, develop and debug
  99. if (keymap_is_special_mode(fn_bits)) {
  100. switch (usb_keyboard_get_key()) {
  101. case KB_H: // help
  102. print_enable = true;
  103. print("b: jump to bootloader\n");
  104. print("d: toggle debug enable\n");
  105. print("x: toggle matrix debug\n");
  106. print("k: toggle keyboard debug\n");
  107. print("m: toggle mouse debug\n");
  108. print("p: toggle print enable\n");
  109. print("v: print version\n");
  110. print("t: print timer count\n");
  111. print("s: print status\n");
  112. print("`: toggle protcol(boot/report)\n");
  113. #ifdef USB_NKRO_ENABLE
  114. print("n: toggle USB_NKRO\n");
  115. #endif
  116. print("ESC: power down/wake up\n");
  117. #ifdef PS2_MOUSE_ENABLE
  118. print("1: ps2_mouse_init \n");
  119. print("2: ps2_mouse_read \n");
  120. print("3: ps2_mouse: on/off toggle \n");
  121. #endif
  122. _delay_ms(500);
  123. print_enable = false;
  124. break;
  125. #ifdef PS2_MOUSE_ENABLE
  126. case KB_1:
  127. usb_keyboard_clear_report();
  128. usb_keyboard_send();
  129. print_enable = true;
  130. print("ps2_mouse_init...\n");
  131. _delay_ms(500);
  132. ps2_mouse_init();
  133. break;
  134. case KB_2:
  135. usb_keyboard_clear_report();
  136. usb_keyboard_send();
  137. print_enable = true;
  138. print("ps2_mouse_read[btn x y]: ");
  139. _delay_ms(100);
  140. ps2_mouse_read();
  141. phex(ps2_mouse_btn); print(" ");
  142. phex(ps2_mouse_x); print(" ");
  143. phex(ps2_mouse_y); print("\n");
  144. print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
  145. break;
  146. case KB_3:
  147. ps2_mouse_enable = !ps2_mouse_enable;
  148. print("ps2_mouse: ");
  149. if (ps2_mouse_enable)
  150. print("on");
  151. else
  152. print("off");
  153. print("\n");
  154. _delay_ms(500);
  155. break;
  156. #endif
  157. case KB_B: // bootloader
  158. usb_keyboard_clear_report();
  159. usb_keyboard_send();
  160. print_enable = true;
  161. print("jump to bootloader...\n");
  162. _delay_ms(1000);
  163. jump_bootloader(); // not return
  164. break;
  165. case KB_D: // debug all toggle
  166. usb_keyboard_clear_report();
  167. usb_keyboard_send();
  168. debug_enable = !debug_enable;
  169. if (debug_enable) {
  170. print_enable = true;
  171. print("debug enabled.\n");
  172. debug_matrix = true;
  173. debug_keyboard = true;
  174. debug_mouse = true;
  175. } else {
  176. print("debug disabled.\n");
  177. print_enable = false;
  178. debug_matrix = false;
  179. debug_keyboard = false;
  180. debug_mouse = false;
  181. }
  182. _delay_ms(1000);
  183. break;
  184. case KB_X: // debug matrix toggle
  185. usb_keyboard_clear_report();
  186. usb_keyboard_send();
  187. debug_matrix = !debug_matrix;
  188. if (debug_matrix)
  189. print("debug matrix enabled.\n");
  190. else
  191. print("debug matrix disabled.\n");
  192. _delay_ms(1000);
  193. break;
  194. case KB_K: // debug keyboard toggle
  195. usb_keyboard_clear_report();
  196. usb_keyboard_send();
  197. debug_keyboard = !debug_keyboard;
  198. if (debug_keyboard)
  199. print("debug keyboard enabled.\n");
  200. else
  201. print("debug keyboard disabled.\n");
  202. _delay_ms(1000);
  203. break;
  204. case KB_M: // debug mouse toggle
  205. usb_keyboard_clear_report();
  206. usb_keyboard_send();
  207. debug_mouse = !debug_mouse;
  208. if (debug_mouse)
  209. print("debug mouse enabled.\n");
  210. else
  211. print("debug mouse disabled.\n");
  212. _delay_ms(1000);
  213. break;
  214. case KB_V: // print version & information
  215. usb_keyboard_clear_report();
  216. usb_keyboard_send();
  217. print_enable = true;
  218. print(STR(DESCRIPTION) "\n");
  219. _delay_ms(1000);
  220. break;
  221. case KB_T: // print timer
  222. usb_keyboard_clear_report();
  223. usb_keyboard_send();
  224. print_enable = true;
  225. print("timer: "); phex16(timer_count); print("\n");
  226. _delay_ms(500);
  227. break;
  228. case KB_P: // print toggle
  229. usb_keyboard_clear_report();
  230. usb_keyboard_send();
  231. if (print_enable) {
  232. print("print disabled.\n");
  233. print_enable = false;
  234. } else {
  235. print_enable = true;
  236. print("print enabled.\n");
  237. }
  238. _delay_ms(1000);
  239. break;
  240. case KB_S:
  241. usb_keyboard_clear_report();
  242. usb_keyboard_send();
  243. print("UDCON: "); phex(UDCON); print("\n");
  244. print("UDIEN: "); phex(UDIEN); print("\n");
  245. print("UDINT: "); phex(UDINT); print("\n");
  246. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  247. print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
  248. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  249. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  250. print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
  251. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  252. _delay_ms(500);
  253. break;
  254. case KB_GRV:
  255. usb_keyboard_clear_report();
  256. usb_keyboard_send();
  257. usb_keyboard_protocol = !usb_keyboard_protocol;
  258. usb_mouse_protocol = !usb_mouse_protocol;
  259. print("keyboard protcol: ");
  260. if (usb_keyboard_protocol) print("report"); else print("boot");
  261. print("\n");
  262. print("mouse protcol: ");
  263. if (usb_mouse_protocol) print("report"); else print("boot");
  264. print("\n");
  265. _delay_ms(1000);
  266. break;
  267. #ifdef USB_NKRO_ENABLE
  268. case KB_N:
  269. usb_keyboard_clear_report();
  270. usb_keyboard_send();
  271. usb_keyboard_nkro = !usb_keyboard_nkro;
  272. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  273. _delay_ms(1000);
  274. break;
  275. #endif
  276. case KB_ESC:
  277. usb_keyboard_clear_report();
  278. usb_keyboard_send();
  279. if (suspend && remote_wakeup) {
  280. usb_remote_wakeup();
  281. } else {
  282. usb_extra_system_send(SYSTEM_POWER_DOWN);
  283. }
  284. _delay_ms(1000);
  285. break;
  286. }
  287. }
  288. if (modified) {
  289. usb_keyboard_send();
  290. }
  291. #ifdef MOUSEKEY_ENABLE
  292. // mouse keys
  293. mousekey_usb_send();
  294. #endif
  295. #ifdef PS2_MOUSE_ENABLE
  296. // ps2 mouse
  297. if (ps2_mouse_read() == 0)
  298. ps2_mouse_usb_send();
  299. #endif
  300. }