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

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