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

13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #endif
  121. _delay_ms(500);
  122. print_enable = false;
  123. break;
  124. #ifdef PS2_MOUSE_ENABLE
  125. case KB_1:
  126. usb_keyboard_clear_report();
  127. usb_keyboard_send();
  128. print_enable = true;
  129. print("ps2_mouse_init...\n");
  130. _delay_ms(500);
  131. ps2_mouse_init();
  132. break;
  133. case KB_2:
  134. usb_keyboard_clear_report();
  135. usb_keyboard_send();
  136. print_enable = true;
  137. print("ps2_mouse_read[btn x y]: ");
  138. _delay_ms(100);
  139. ps2_mouse_read();
  140. phex(ps2_mouse_btn); print(" ");
  141. phex(ps2_mouse_x); print(" ");
  142. phex(ps2_mouse_y); print("\n");
  143. print("ps2_mouse_error_count: "); phex(ps2_mouse_error_count); print("\n");
  144. break;
  145. #endif
  146. case KB_B: // bootloader
  147. usb_keyboard_clear_report();
  148. usb_keyboard_send();
  149. print_enable = true;
  150. print("jump to bootloader...\n");
  151. _delay_ms(1000);
  152. jump_bootloader(); // not return
  153. break;
  154. case KB_D: // debug all toggle
  155. usb_keyboard_clear_report();
  156. usb_keyboard_send();
  157. debug_enable = !debug_enable;
  158. if (debug_enable) {
  159. print_enable = true;
  160. print("debug enabled.\n");
  161. debug_matrix = true;
  162. debug_keyboard = true;
  163. debug_mouse = true;
  164. } else {
  165. print("debug disabled.\n");
  166. print_enable = false;
  167. debug_matrix = false;
  168. debug_keyboard = false;
  169. debug_mouse = false;
  170. }
  171. _delay_ms(1000);
  172. break;
  173. case KB_X: // debug matrix toggle
  174. usb_keyboard_clear_report();
  175. usb_keyboard_send();
  176. debug_matrix = !debug_matrix;
  177. if (debug_matrix)
  178. print("debug matrix enabled.\n");
  179. else
  180. print("debug matrix disabled.\n");
  181. _delay_ms(1000);
  182. break;
  183. case KB_K: // debug keyboard toggle
  184. usb_keyboard_clear_report();
  185. usb_keyboard_send();
  186. debug_keyboard = !debug_keyboard;
  187. if (debug_keyboard)
  188. print("debug keyboard enabled.\n");
  189. else
  190. print("debug keyboard disabled.\n");
  191. _delay_ms(1000);
  192. break;
  193. case KB_M: // debug mouse toggle
  194. usb_keyboard_clear_report();
  195. usb_keyboard_send();
  196. debug_mouse = !debug_mouse;
  197. if (debug_mouse)
  198. print("debug mouse enabled.\n");
  199. else
  200. print("debug mouse disabled.\n");
  201. _delay_ms(1000);
  202. break;
  203. case KB_V: // print version & information
  204. usb_keyboard_clear_report();
  205. usb_keyboard_send();
  206. print_enable = true;
  207. print(STR(DESCRIPTION) "\n");
  208. _delay_ms(1000);
  209. break;
  210. case KB_T: // print timer
  211. usb_keyboard_clear_report();
  212. usb_keyboard_send();
  213. print_enable = true;
  214. print("timer: "); phex16(timer_count); print("\n");
  215. _delay_ms(500);
  216. break;
  217. case KB_P: // print toggle
  218. usb_keyboard_clear_report();
  219. usb_keyboard_send();
  220. if (print_enable) {
  221. print("print disabled.\n");
  222. print_enable = false;
  223. } else {
  224. print_enable = true;
  225. print("print enabled.\n");
  226. }
  227. _delay_ms(1000);
  228. break;
  229. case KB_S:
  230. usb_keyboard_clear_report();
  231. usb_keyboard_send();
  232. print("UDCON: "); phex(UDCON); print("\n");
  233. print("UDIEN: "); phex(UDIEN); print("\n");
  234. print("UDINT: "); phex(UDINT); print("\n");
  235. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  236. print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
  237. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  238. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  239. print("usb_mouse_protocol:"); phex(usb_mouse_protocol); print("\n");
  240. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  241. _delay_ms(500);
  242. break;
  243. case KB_GRV:
  244. usb_keyboard_clear_report();
  245. usb_keyboard_send();
  246. usb_keyboard_protocol = !usb_keyboard_protocol;
  247. usb_mouse_protocol = !usb_mouse_protocol;
  248. print("keyboard protcol: ");
  249. if (usb_keyboard_protocol) print("report"); else print("boot");
  250. print("\n");
  251. print("mouse protcol: ");
  252. if (usb_mouse_protocol) print("report"); else print("boot");
  253. print("\n");
  254. _delay_ms(1000);
  255. break;
  256. #ifdef USB_NKRO_ENABLE
  257. case KB_N:
  258. usb_keyboard_clear_report();
  259. usb_keyboard_send();
  260. usb_keyboard_nkro = !usb_keyboard_nkro;
  261. if (usb_keyboard_nkro) print("USB_NKRO: enabled\n"); else print("USB_NKRO: disabled\n");
  262. _delay_ms(1000);
  263. break;
  264. #endif
  265. case KB_ESC:
  266. usb_keyboard_clear_report();
  267. usb_keyboard_send();
  268. if (suspend && remote_wakeup) {
  269. usb_remote_wakeup();
  270. } else {
  271. usb_extra_system_send(SYSTEM_POWER_DOWN);
  272. }
  273. _delay_ms(1000);
  274. break;
  275. }
  276. }
  277. if (modified) {
  278. usb_keyboard_send();
  279. }
  280. #ifdef MOUSEKEY_ENABLE
  281. // mouse keys
  282. mousekey_usb_send();
  283. #endif
  284. #ifdef PS2_MOUSE_ENABLE
  285. // ps2 mouse
  286. //if (ps2_mouse_error_count > 10) {
  287. ps2_mouse_read();
  288. ps2_mouse_usb_send();
  289. //}
  290. #endif
  291. }