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

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 "controller.h"
  19. #include "key_process.h"
  20. #define MOUSE_MOVE_UNIT 10
  21. #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
  22. #ifndef MOUSE_DELAY_TIME
  23. # define MOUSE_DELAY_TIME 255
  24. #endif
  25. #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
  26. // TODO: refactoring
  27. void proc_matrix(void) {
  28. static int mouse_repeat = 0;
  29. bool modified = false;
  30. uint8_t mouse_btn = 0;
  31. int8_t mouse_x = 0;
  32. int8_t mouse_y = 0;
  33. int8_t mouse_vwheel = 0;
  34. int8_t mouse_hwheel = 0;
  35. uint8_t fn_bits = 0;
  36. matrix_scan();
  37. modified = matrix_is_modified();
  38. if (modified) {
  39. if (debug_matrix) matrix_print();
  40. #ifdef DEBUG_LED
  41. // LED flash for debug
  42. DEBUG_LED_CONFIG;
  43. DEBUG_LED_ON;
  44. #endif
  45. }
  46. if (matrix_has_ghost()) {
  47. // should send error?
  48. debug("matrix has ghost!!\n");
  49. return;
  50. }
  51. usb_keyboard_swap_report();
  52. usb_keyboard_clear_report();
  53. for (int row = 0; row < matrix_rows(); row++) {
  54. for (int col = 0; col < matrix_cols(); col++) {
  55. if (!matrix_is_on(row, col)) continue;
  56. // TODO: clean code
  57. uint8_t code = layer_get_keycode(row, col);
  58. if (code == KB_NO) {
  59. // do nothing
  60. } else if (IS_MOD(code)) {
  61. usb_keyboard_add_mod(code);
  62. } else if (IS_FN(code)) {
  63. fn_bits |= FN_BIT(code);
  64. } else if (IS_MOUSE(code)) {
  65. if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  66. if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  67. if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  68. if (code == MS_RGHT) mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  69. if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
  70. if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
  71. if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
  72. if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
  73. if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
  74. if (code == MS_WH_U) mouse_vwheel += 1;
  75. if (code == MS_WH_D) mouse_vwheel -= 1;
  76. if (code == MS_WH_L) mouse_hwheel -= 1;
  77. if (code == MS_WH_R) mouse_hwheel += 1;
  78. }
  79. // audio control & system control
  80. else if (code == KB_MUTE) {
  81. usb_extra_audio_send(AUDIO_MUTE);
  82. usb_extra_audio_send(0);
  83. _delay_ms(500);
  84. } else if (code == KB_VOLU) {
  85. usb_extra_audio_send(AUDIO_VOL_UP);
  86. usb_extra_audio_send(0);
  87. _delay_ms(100);
  88. } else if (code == KB_VOLD) {
  89. usb_extra_audio_send(AUDIO_VOL_DOWN);
  90. usb_extra_audio_send(0);
  91. _delay_ms(100);
  92. } else if (code == KB_PWR) {
  93. if (suspend && remote_wakeup) {
  94. usb_remote_wakeup();
  95. } else {
  96. usb_extra_system_send(SYSTEM_POWER_DOWN);
  97. }
  98. _delay_ms(1000);
  99. }
  100. // normal keys
  101. else {
  102. usb_keyboard_add_key(code);
  103. }
  104. }
  105. }
  106. if (modified) {
  107. #ifdef DEBUG_LED
  108. // LED flash for debug
  109. DEBUG_LED_CONFIG;
  110. DEBUG_LED_OFF;
  111. #endif
  112. }
  113. layer_switching(fn_bits);
  114. // TODO: clean code
  115. // special mode for control, develop and debug
  116. if (keymap_is_special_mode(fn_bits)) {
  117. switch (usb_keyboard_get_key()) {
  118. case KB_H: // help
  119. print_enable = true;
  120. print("b: jump to bootloader\n");
  121. print("d: toggle debug enable\n");
  122. print("x: toggle matrix debug\n");
  123. print("k: toggle keyboard debug\n");
  124. print("m: toggle mouse debug\n");
  125. print("p: toggle print enable\n");
  126. print("v: print version\n");
  127. print("t: print timer count\n");
  128. print("s: print status\n");
  129. print("`: toggle protcol(boot/report)\n");
  130. #ifdef NKRO_ENABLE
  131. print("n: toggle NKRO\n");
  132. #endif
  133. print("ESC: power down/wake up\n");
  134. _delay_ms(500);
  135. print_enable = false;
  136. break;
  137. case KB_B: // bootloader
  138. usb_keyboard_clear_report();
  139. usb_keyboard_send();
  140. print_enable = true;
  141. print("jump to bootloader...\n");
  142. _delay_ms(1000);
  143. jump_bootloader(); // not return
  144. break;
  145. case KB_D: // debug all toggle
  146. usb_keyboard_clear_report();
  147. usb_keyboard_send();
  148. debug_enable = !debug_enable;
  149. if (debug_enable) {
  150. print_enable = true;
  151. print("debug enabled.\n");
  152. debug_matrix = true;
  153. debug_keyboard = true;
  154. debug_mouse = true;
  155. } else {
  156. print("debug disabled.\n");
  157. print_enable = false;
  158. debug_matrix = false;
  159. debug_keyboard = false;
  160. debug_mouse = false;
  161. }
  162. _delay_ms(1000);
  163. break;
  164. case KB_X: // debug matrix toggle
  165. usb_keyboard_clear_report();
  166. usb_keyboard_send();
  167. debug_matrix = !debug_matrix;
  168. if (debug_matrix)
  169. print("debug matrix enabled.\n");
  170. else
  171. print("debug matrix disabled.\n");
  172. _delay_ms(1000);
  173. break;
  174. case KB_K: // debug keyboard toggle
  175. usb_keyboard_clear_report();
  176. usb_keyboard_send();
  177. debug_keyboard = !debug_keyboard;
  178. if (debug_keyboard)
  179. print("debug keyboard enabled.\n");
  180. else
  181. print("debug keyboard disabled.\n");
  182. _delay_ms(1000);
  183. break;
  184. case KB_M: // debug mouse toggle
  185. usb_keyboard_clear_report();
  186. usb_keyboard_send();
  187. debug_mouse = !debug_mouse;
  188. if (debug_mouse)
  189. print("debug mouse enabled.\n");
  190. else
  191. print("debug mouse disabled.\n");
  192. _delay_ms(1000);
  193. break;
  194. case KB_V: // print version & information
  195. usb_keyboard_clear_report();
  196. usb_keyboard_send();
  197. print_enable = true;
  198. print(STR(DESCRIPTION) "\n");
  199. _delay_ms(1000);
  200. break;
  201. case KB_T: // print timer
  202. usb_keyboard_clear_report();
  203. usb_keyboard_send();
  204. print_enable = true;
  205. print("timer: "); phex16(timer_count); print("\n");
  206. _delay_ms(500);
  207. break;
  208. case KB_P: // print toggle
  209. usb_keyboard_clear_report();
  210. usb_keyboard_send();
  211. if (print_enable) {
  212. print("print disabled.\n");
  213. print_enable = false;
  214. } else {
  215. print_enable = true;
  216. print("print enabled.\n");
  217. }
  218. _delay_ms(1000);
  219. break;
  220. case KB_S:
  221. usb_keyboard_clear_report();
  222. usb_keyboard_send();
  223. print("UDCON: "); phex(UDCON); print("\n");
  224. print("UDIEN: "); phex(UDIEN); print("\n");
  225. print("UDINT: "); phex(UDINT); print("\n");
  226. print("usb_keyboard_leds:"); phex(usb_keyboard_leds); print("\n");
  227. print("usb_keyboard_protocol:"); phex(usb_keyboard_protocol); print("\n");
  228. print("usb_keyboard_idle_config:"); phex(usb_keyboard_idle_config); print("\n");
  229. print("usb_keyboard_idle_count:"); phex(usb_keyboard_idle_count); print("\n");
  230. print("mouse_protocol:"); phex(mouse_protocol); print("\n");
  231. if (usb_keyboard_nkro) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
  232. _delay_ms(500);
  233. break;
  234. case KB_GRV:
  235. usb_keyboard_clear_report();
  236. usb_keyboard_send();
  237. usb_keyboard_protocol = !usb_keyboard_protocol;
  238. mouse_protocol = !mouse_protocol;
  239. print("keyboard protcol: ");
  240. if (usb_keyboard_protocol) print("report"); else print("boot");
  241. print("\n");
  242. print("mouse protcol: ");
  243. if (mouse_protocol) print("report"); else print("boot");
  244. print("\n");
  245. _delay_ms(1000);
  246. break;
  247. #ifdef NKRO_ENABLE
  248. case KB_N:
  249. usb_keyboard_clear_report();
  250. usb_keyboard_send();
  251. usb_keyboard_nkro = !usb_keyboard_nkro;
  252. if (usb_keyboard_nkro) print("NKRO: enabled\n"); else print("NKRO: disabled\n");
  253. _delay_ms(1000);
  254. break;
  255. #endif
  256. case KB_ESC:
  257. usb_keyboard_clear_report();
  258. usb_keyboard_send();
  259. if (suspend && remote_wakeup) {
  260. usb_remote_wakeup();
  261. } else {
  262. usb_extra_system_send(SYSTEM_POWER_DOWN);
  263. }
  264. _delay_ms(1000);
  265. break;
  266. }
  267. }
  268. // send mouse packet to host
  269. if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  270. mouse_buttons = mouse_btn;
  271. if (mouse_x && mouse_y)
  272. usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
  273. else
  274. usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  275. usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  276. // acceleration
  277. _delay_ms(MOUSE_DELAY_MS);
  278. mouse_repeat++;
  279. } else {
  280. mouse_repeat = 0;
  281. }
  282. // send key packet to host
  283. if (modified) {
  284. usb_keyboard_send();
  285. }
  286. }