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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <stdbool.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include "print.h"
  5. #include "debug.h"
  6. #include "timer.h"
  7. #include "util.h"
  8. #include "jump_bootloader.h"
  9. #include "usb_keyboard.h"
  10. #include "usb_mouse.h"
  11. #include "usb_keycodes.h"
  12. #include "layer.h"
  13. #include "matrix_skel.h"
  14. #include "keymap_skel.h"
  15. #include "controller.h"
  16. #include "key_process.h"
  17. #define MOUSE_MOVE_UNIT 10
  18. #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
  19. #ifndef MOUSE_DELAY_TIME
  20. # define MOUSE_DELAY_TIME 255
  21. #endif
  22. #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
  23. // TODO: refactoring
  24. void proc_matrix(void) {
  25. static int mouse_repeat = 0;
  26. bool modified = false;
  27. //bool has_ghost = false;
  28. int key_index = 0;
  29. uint8_t mouse_btn = 0;
  30. int8_t mouse_x = 0;
  31. int8_t mouse_y = 0;
  32. int8_t mouse_vwheel = 0;
  33. int8_t mouse_hwheel = 0;
  34. uint8_t fn_bits = 0;
  35. matrix_scan();
  36. modified = matrix_is_modified();
  37. if (modified) {
  38. if (debug_matrix) matrix_print();
  39. #ifdef DEBUG_LED
  40. // LED flash for debug
  41. DEBUG_LED_CONFIG;
  42. DEBUG_LED_ON;
  43. #endif
  44. }
  45. if (matrix_has_ghost()) {
  46. // should send error?
  47. debug("matrix has ghost!!\n");
  48. return;
  49. }
  50. usb_keyboard_clear();
  51. for (int row = 0; row < matrix_rows(); row++) {
  52. for (int col = 0; col < matrix_cols(); col++) {
  53. if (!matrix_is_on(row, col)) continue;
  54. uint8_t code = layer_get_keycode(row, col);
  55. if (code == KB_NO) {
  56. // do nothing
  57. } else if (IS_MOD(code)) {
  58. keyboard_modifier_keys |= MOD_BIT(code);
  59. } else if (IS_MOUSE(code)) {
  60. // mouse
  61. if (code == MS_UP)
  62. mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  63. if (code == MS_DOWN)
  64. mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  65. if (code == MS_LEFT)
  66. mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  67. if (code == MS_RIGHT)
  68. 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_UP) mouse_vwheel += 1;
  75. if (code == MS_WH_DOWN) mouse_vwheel -= 1;
  76. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  77. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  78. } else if (IS_FN(code)) {
  79. fn_bits |= FN_BIT(code);
  80. } else {
  81. // normal keys
  82. if (key_index < 6)
  83. keyboard_keys[key_index] = code;
  84. key_index++;
  85. }
  86. }
  87. }
  88. layer_switching(fn_bits);
  89. // when 4 left modifier keys down
  90. if (keymap_is_special_mode(fn_bits)) {
  91. switch (keyboard_keys[0]) {
  92. case KB_H: // help
  93. print_enable = true;
  94. print("b: jump to bootloader\n");
  95. print("d: debug print toggle\n");
  96. print("k: keyboard debug toggle\n");
  97. print("m: mouse debug toggle\n");
  98. print("x: matrix debug toggle\n");
  99. print("v: print version\n");
  100. print("t: print timer count\n");
  101. print("p: print enable toggle\n");
  102. _delay_ms(500);
  103. print_enable = false;
  104. break;
  105. case KB_B: // bootloader
  106. usb_keyboard_clear();
  107. usb_keyboard_send();
  108. print_enable = true;
  109. print("jump to bootloader...\n");
  110. _delay_ms(1000);
  111. jump_bootloader(); // not return
  112. break;
  113. case KB_D: // debug all toggle
  114. usb_keyboard_clear();
  115. usb_keyboard_send();
  116. debug_enable = !debug_enable;
  117. if (debug_enable) {
  118. print_enable = true;
  119. print("debug enabled.\n");
  120. debug_matrix = true;
  121. debug_keyboard = true;
  122. debug_mouse = true;
  123. } else {
  124. print("debug disabled.\n");
  125. print_enable = false;
  126. debug_matrix = false;
  127. debug_keyboard = false;
  128. debug_mouse = false;
  129. }
  130. _delay_ms(1000);
  131. break;
  132. case KB_X: // debug matrix toggle
  133. usb_keyboard_clear();
  134. usb_keyboard_send();
  135. debug_matrix = !debug_matrix;
  136. if (debug_matrix)
  137. print("debug matrix enabled.\n");
  138. else
  139. print("debug matrix disabled.\n");
  140. _delay_ms(1000);
  141. break;
  142. case KB_K: // debug keyboard toggle
  143. usb_keyboard_clear();
  144. usb_keyboard_send();
  145. debug_keyboard = !debug_keyboard;
  146. if (debug_keyboard)
  147. print("debug keyboard enabled.\n");
  148. else
  149. print("debug keyboard disabled.\n");
  150. _delay_ms(1000);
  151. break;
  152. case KB_M: // debug mouse toggle
  153. usb_keyboard_clear();
  154. usb_keyboard_send();
  155. debug_mouse = !debug_mouse;
  156. if (debug_mouse)
  157. print("debug mouse enabled.\n");
  158. else
  159. print("debug mouse disabled.\n");
  160. _delay_ms(1000);
  161. break;
  162. case KB_V: // print version & information
  163. usb_keyboard_clear();
  164. usb_keyboard_send();
  165. print_enable = true;
  166. print(STR(DESCRIPTION) "\n");
  167. _delay_ms(1000);
  168. break;
  169. case KB_T: // print timer
  170. usb_keyboard_clear();
  171. usb_keyboard_send();
  172. print_enable = true;
  173. print("timer: "); phex16(timer_count); print("\n");
  174. _delay_ms(500);
  175. break;
  176. case KB_P: // print toggle
  177. usb_keyboard_clear();
  178. usb_keyboard_send();
  179. if (print_enable) {
  180. print("print disabled.\n");
  181. print_enable = false;
  182. } else {
  183. print_enable = true;
  184. print("print enabled.\n");
  185. }
  186. _delay_ms(1000);
  187. break;
  188. }
  189. }
  190. // send mouse packet to host
  191. if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  192. mouse_buttons = mouse_btn;
  193. if (mouse_x && mouse_y)
  194. usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
  195. else
  196. usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  197. usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  198. // acceleration
  199. _delay_ms(MOUSE_DELAY_MS);
  200. mouse_repeat++;
  201. } else {
  202. mouse_repeat = 0;
  203. }
  204. // send key packet to host
  205. if (modified) {
  206. if (key_index > 6) {
  207. //Rollover
  208. }
  209. usb_keyboard_send();
  210. usb_keyboard_print();
  211. #ifdef DEBUG_LED
  212. // LED flash for debug
  213. DEBUG_LED_CONFIG;
  214. DEBUG_LED_OFF;
  215. #endif
  216. }
  217. }