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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. uint8_t code = layer_get_keycode(row, col);
  54. if (code == KB_NO) {
  55. // do nothing
  56. } else if (IS_MOD(code)) {
  57. usb_keyboard_add_mod(code);
  58. } else if (IS_FN(code)) {
  59. fn_bits |= FN_BIT(code);
  60. }
  61. #ifdef MOUSEKEY_ENABLE
  62. else if (IS_MOUSEKEY(code)) {
  63. mousekey_decode(code);
  64. }
  65. #endif
  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 key
  90. else if (IS_KEY(code)) {
  91. usb_keyboard_add_key(code);
  92. } else {
  93. debug("ignore keycode: "); debug_hex(code); debug("\n");
  94. }
  95. }
  96. }
  97. if (modified) {
  98. #ifdef DEBUG_LED
  99. // LED flash for debug
  100. DEBUG_LED_CONFIG;
  101. DEBUG_LED_OFF;
  102. #endif
  103. }
  104. layer_switching(fn_bits);
  105. // TODO: clean code
  106. // special mode for control, develop and debug
  107. if (keymap_is_special_mode(fn_bits)) {
  108. switch (usb_keyboard_get_key()) {
  109. case KB_H: // help
  110. print_enable = true;
  111. print("b: jump to bootloader\n");
  112. print("d: toggle debug enable\n");
  113. print("x: toggle matrix debug\n");
  114. print("k: toggle keyboard debug\n");
  115. print("m: toggle mouse debug\n");
  116. print("p: toggle print enable\n");
  117. print("v: print version\n");
  118. print("t: print timer count\n");
  119. print("s: print status\n");
  120. print("`: toggle protcol(boot/report)\n");
  121. #ifdef USB_NKRO_ENABLE
  122. print("n: toggle USB_NKRO\n");
  123. #endif
  124. print("ESC: power down/wake up\n");
  125. #ifdef PS2_MOUSE_ENABLE
  126. print("1: ps2_mouse_init \n");
  127. print("2: ps2_mouse_read \n");
  128. print("3: ps2_mouse: on/off toggle \n");
  129. #endif
  130. _delay_ms(500);
  131. print_enable = false;
  132. break;
  133. #ifdef PS2_MOUSE_ENABLE
  134. case KB_1:
  135. usb_keyboard_clear_report();
  136. usb_keyboard_send();
  137. print_enable = true;
  138. print("ps2_mouse_init...\n");
  139. _delay_ms(500);
  140. ps2_mouse_init();
  141. break;
  142. case KB_2:
  143. usb_keyboard_clear_report();
  144. usb_keyboard_send();
  145. print_enable = true;
  146. print("ps2_mouse_read[btn x y]: ");
  147. _delay_ms(100);
  148. ps2_mouse_read();
  149. phex(ps2_mouse_btn); print(" ");
  150. phex(ps2_mouse_x); print(" ");
  151. phex(ps2_mouse_y); print("\n");
  152. print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
  153. break;
  154. case KB_3:
  155. ps2_mouse_enable = !ps2_mouse_enable;
  156. print("ps2_mouse: ");
  157. if (ps2_mouse_enable)
  158. print("on");
  159. else
  160. print("off");
  161. print("\n");
  162. _delay_ms(500);
  163. break;
  164. #endif
  165. case KB_B: // bootloader
  166. usb_keyboard_clear_report();
  167. usb_keyboard_send();
  168. print_enable = true;
  169. print("jump to bootloader...\n");
  170. _delay_ms(1000);
  171. jump_bootloader(); // not return
  172. break;
  173. case KB_D: // debug all toggle
  174. usb_keyboard_clear_report();
  175. usb_keyboard_send();
  176. debug_enable = !debug_enable;
  177. if (debug_enable) {
  178. print_enable = true;
  179. print("debug enabled.\n");
  180. debug_matrix = true;
  181. debug_keyboard = true;
  182. debug_mouse = true;
  183. } else {
  184. print("debug disabled.\n");
  185. print_enable = false;
  186. debug_matrix = false;
  187. debug_keyboard = false;
  188. debug_mouse = false;
  189. }
  190. _delay_ms(1000);
  191. break;
  192. case KB_X: // debug matrix toggle
  193. usb_keyboard_clear_report();
  194. usb_keyboard_send();
  195. debug_matrix = !debug_matrix;
  196. if (debug_matrix)
  197. print("debug matrix enabled.\n");
  198. else
  199. print("debug matrix disabled.\n");
  200. _delay_ms(1000);
  201. break;
  202. case KB_K: // debug keyboard toggle
  203. usb_keyboard_clear_report();
  204. usb_keyboard_send();
  205. debug_keyboard = !debug_keyboard;
  206. if (debug_keyboard)
  207. print("debug keyboard enabled.\n");
  208. else
  209. print("debug keyboard disabled.\n");
  210. _delay_ms(1000);
  211. break;
  212. case KB_M: // debug mouse toggle
  213. usb_keyboard_clear_report();
  214. usb_keyboard_send();
  215. debug_mouse = !debug_mouse;
  216. if (debug_mouse)
  217. print("debug mouse enabled.\n");
  218. else
  219. print("debug mouse disabled.\n");
  220. _delay_ms(1000);
  221. break;
  222. case KB_V: // print version & information
  223. usb_keyboard_clear_report();
  224. usb_keyboard_send();
  225. print_enable = true;
  226. print(STR(DESCRIPTION) "\n");
  227. _delay_ms(1000);
  228. break;
  229. case KB_T: // print timer
  230. usb_keyboard_clear_report();
  231. usb_keyboard_send();
  232. print_enable = true;
  233. print("timer: "); phex16(timer_count); print("\n");
  234. _delay_ms(500);
  235. break;
  236. case KB_P: // print toggle
  237. usb_keyboard_clear_report();
  238. usb_keyboard_send();
  239. if (print_enable) {
  240. print("print disabled.\n");
  241. print_enable = false;
  242. } else {
  243. print_enable = true;
  244. print("print enabled.\n");
  245. }
  246. _delay_ms(1000);
  247. break;
  248. case KB_S:
  249. usb_keyboard_clear_report();
  250. usb_keyboard_send();
  251. print("UDCON: "); phex(UDCON); print("\n");
  252. print("UDIEN: "); phex(UDIEN); print("\n");
  253. print("UDINT: "); phex(UDINT); print("\n");
  254. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  255. print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
  256. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  257. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  258. #ifdef USB_MOUSE_ENABLE
  259. print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
  260. #endif
  261. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  262. _delay_ms(500);
  263. break;
  264. case KB_GRV:
  265. usb_keyboard_clear_report();
  266. usb_keyboard_send();
  267. usb_keyboard_protocol = !usb_keyboard_protocol;
  268. print("keyboard protcol: ");
  269. if (usb_keyboard_protocol) print("report"); else print("boot");
  270. print("\n");
  271. #ifdef USB_MOUSE_ENABLE
  272. usb_mouse_protocol = !usb_mouse_protocol;
  273. print("mouse protcol: ");
  274. if (usb_mouse_protocol) print("report"); else print("boot");
  275. print("\n");
  276. #endif
  277. _delay_ms(1000);
  278. break;
  279. #ifdef USB_NKRO_ENABLE
  280. case KB_N:
  281. usb_keyboard_clear_report();
  282. usb_keyboard_send();
  283. usb_keyboard_nkro = !usb_keyboard_nkro;
  284. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  285. _delay_ms(1000);
  286. break;
  287. #endif
  288. #ifdef USB_EXTRA_ENABLE
  289. case KB_ESC:
  290. usb_keyboard_clear_report();
  291. usb_keyboard_send();
  292. if (suspend && remote_wakeup) {
  293. usb_remote_wakeup();
  294. } else {
  295. usb_extra_system_send(SYSTEM_POWER_DOWN);
  296. }
  297. _delay_ms(1000);
  298. break;
  299. #endif
  300. }
  301. }
  302. if (modified) {
  303. usb_keyboard_send();
  304. }
  305. #ifdef MOUSEKEY_ENABLE
  306. mousekey_usb_send();
  307. #endif
  308. #ifdef PS2_MOUSE_ENABLE
  309. if (ps2_mouse_read() == 0)
  310. ps2_mouse_usb_send();
  311. #endif
  312. }