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

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