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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. #include <stdbool.h>
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include "usb_keyboard.h"
  5. #include "usb_mouse.h"
  6. #include "usb_keycodes.h"
  7. #include "print.h"
  8. #include "debug.h"
  9. #include "util.h"
  10. #include "jump_bootloader.h"
  11. #include "matrix_skel.h"
  12. #include "keymap_skel.h"
  13. #include "controller.h"
  14. #include "key_process.h"
  15. #define MOUSE_MOVE_UNIT 10
  16. #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
  17. #ifndef MOUSE_DELAY_TIME
  18. # define MOUSE_DELAY_TIME 255
  19. #endif
  20. #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
  21. // TODO: refactoring
  22. void proc_matrix(void) {
  23. static int mouse_repeat = 0;
  24. bool modified = false;
  25. //bool has_ghost = false;
  26. int key_index = 0;
  27. uint8_t mouse_btn = 0;
  28. int8_t mouse_x = 0;
  29. int8_t mouse_y = 0;
  30. int8_t mouse_vwheel = 0;
  31. int8_t mouse_hwheel = 0;
  32. int 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_clear();
  49. for (int row = 0; row < matrix_rows(); row++) {
  50. for (int col = 0; col < matrix_cols(); col++) {
  51. if (!matrix_is_on(row, col)) continue;
  52. uint8_t code = keymap_get_keycode(row, col);
  53. if (code == KB_NO) {
  54. // do nothing
  55. } else if (IS_MOD(code)) {
  56. keyboard_modifier_keys |= MOD_BIT(code);
  57. } else if (IS_MOUSE(code)) {
  58. // mouse
  59. if (code == MS_UP)
  60. mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  61. if (code == MS_DOWN)
  62. mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  63. if (code == MS_LEFT)
  64. mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  65. if (code == MS_RIGHT)
  66. mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  67. if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
  68. if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
  69. if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
  70. if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
  71. if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
  72. if (code == MS_WH_UP) mouse_vwheel += 1;
  73. if (code == MS_WH_DOWN) mouse_vwheel -= 1;
  74. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  75. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  76. } else if (IS_FN(code)) {
  77. fn_bits |= FN_BIT(code);
  78. } else {
  79. // normal keys
  80. if (key_index < 6)
  81. keyboard_keys[key_index] = code;
  82. key_index++;
  83. }
  84. }
  85. }
  86. keymap_fn_proc(fn_bits);
  87. // when 4 left modifier keys down
  88. if (keymap_is_special_mode(fn_bits)) {
  89. switch (keyboard_keys[0]) {
  90. case KB_B: // bootloader
  91. usb_keyboard_clear();
  92. usb_keyboard_send();
  93. print_enable = true;
  94. print("jump to bootloader...\n");
  95. _delay_ms(1000);
  96. jump_bootloader(); // not return
  97. break;
  98. case KB_D: // debug all toggle
  99. usb_keyboard_clear();
  100. usb_keyboard_send();
  101. debug_enable = !debug_enable;
  102. if (debug_enable) {
  103. print("debug enabled.\n");
  104. print_enable = true;
  105. debug_matrix = true;
  106. debug_keyboard = true;
  107. debug_mouse = true;
  108. } else {
  109. print("debug disabled.\n");
  110. print_enable = false;
  111. debug_matrix = false;
  112. debug_keyboard = false;
  113. debug_mouse = false;
  114. }
  115. _delay_ms(1000);
  116. break;
  117. case KB_X: // debug matrix toggle
  118. usb_keyboard_clear();
  119. usb_keyboard_send();
  120. debug_matrix = !debug_matrix;
  121. if (debug_matrix)
  122. print("debug matrix enabled.\n");
  123. else
  124. print("debug matrix disabled.\n");
  125. _delay_ms(1000);
  126. break;
  127. case KB_K: // debug keyboard toggle
  128. usb_keyboard_clear();
  129. usb_keyboard_send();
  130. debug_keyboard = !debug_keyboard;
  131. if (debug_keyboard)
  132. print("debug keyboard enabled.\n");
  133. else
  134. print("debug keyboard disabled.\n");
  135. _delay_ms(1000);
  136. break;
  137. case KB_M: // debug mouse toggle
  138. usb_keyboard_clear();
  139. usb_keyboard_send();
  140. debug_mouse = !debug_mouse;
  141. if (debug_mouse)
  142. print("debug mouse enabled.\n");
  143. else
  144. print("debug mouse disabled.\n");
  145. _delay_ms(1000);
  146. break;
  147. case KB_V: // print version & information
  148. usb_keyboard_clear();
  149. usb_keyboard_send();
  150. print(STR(DESCRIPTION) "\n");
  151. _delay_ms(1000);
  152. break;
  153. }
  154. }
  155. // send mouse packet to host
  156. if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  157. mouse_buttons = mouse_btn;
  158. if (mouse_x && mouse_y)
  159. usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
  160. else
  161. usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  162. usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  163. // acceleration
  164. _delay_ms(MOUSE_DELAY_MS);
  165. mouse_repeat++;
  166. } else {
  167. mouse_repeat = 0;
  168. }
  169. // send key packet to host
  170. if (modified) {
  171. if (key_index > 6) {
  172. //Rollover
  173. }
  174. usb_keyboard_send();
  175. usb_keyboard_print();
  176. #ifdef DEBUG_LED
  177. // LED flash for debug
  178. DEBUG_LED_CONFIG;
  179. DEBUG_LED_OFF;
  180. #endif
  181. }
  182. }