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.6KB

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
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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_report();
  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. usb_keyboard_mods |= 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. usb_keyboard_keys[key_index] = code;
  84. key_index++;
  85. }
  86. }
  87. }
  88. if (modified) {
  89. #ifdef DEBUG_LED
  90. // LED flash for debug
  91. DEBUG_LED_CONFIG;
  92. DEBUG_LED_OFF;
  93. #endif
  94. }
  95. layer_switching(fn_bits);
  96. // when 4 left modifier keys down
  97. if (keymap_is_special_mode(fn_bits)) {
  98. switch (usb_keyboard_keys[0]) {
  99. case KB_H: // help
  100. print_enable = true;
  101. print("b: jump to bootloader\n");
  102. print("d: debug print toggle\n");
  103. print("k: keyboard debug toggle\n");
  104. print("m: mouse debug toggle\n");
  105. print("x: matrix debug toggle\n");
  106. print("v: print version\n");
  107. print("t: print timer count\n");
  108. print("p: print enable toggle\n");
  109. _delay_ms(500);
  110. print_enable = false;
  111. break;
  112. case KB_B: // bootloader
  113. usb_keyboard_clear_report();
  114. usb_keyboard_send();
  115. print_enable = true;
  116. print("jump to bootloader...\n");
  117. _delay_ms(1000);
  118. jump_bootloader(); // not return
  119. break;
  120. case KB_D: // debug all toggle
  121. usb_keyboard_clear_report();
  122. usb_keyboard_send();
  123. debug_enable = !debug_enable;
  124. if (debug_enable) {
  125. print_enable = true;
  126. print("debug enabled.\n");
  127. debug_matrix = true;
  128. debug_keyboard = true;
  129. debug_mouse = true;
  130. } else {
  131. print("debug disabled.\n");
  132. print_enable = false;
  133. debug_matrix = false;
  134. debug_keyboard = false;
  135. debug_mouse = false;
  136. }
  137. _delay_ms(1000);
  138. break;
  139. case KB_X: // debug matrix toggle
  140. usb_keyboard_clear_report();
  141. usb_keyboard_send();
  142. debug_matrix = !debug_matrix;
  143. if (debug_matrix)
  144. print("debug matrix enabled.\n");
  145. else
  146. print("debug matrix disabled.\n");
  147. _delay_ms(1000);
  148. break;
  149. case KB_K: // debug keyboard toggle
  150. usb_keyboard_clear_report();
  151. usb_keyboard_send();
  152. debug_keyboard = !debug_keyboard;
  153. if (debug_keyboard)
  154. print("debug keyboard enabled.\n");
  155. else
  156. print("debug keyboard disabled.\n");
  157. _delay_ms(1000);
  158. break;
  159. case KB_M: // debug mouse toggle
  160. usb_keyboard_clear_report();
  161. usb_keyboard_send();
  162. debug_mouse = !debug_mouse;
  163. if (debug_mouse)
  164. print("debug mouse enabled.\n");
  165. else
  166. print("debug mouse disabled.\n");
  167. _delay_ms(1000);
  168. break;
  169. case KB_V: // print version & information
  170. usb_keyboard_clear_report();
  171. usb_keyboard_send();
  172. print_enable = true;
  173. print(STR(DESCRIPTION) "\n");
  174. _delay_ms(1000);
  175. break;
  176. case KB_T: // print timer
  177. usb_keyboard_clear_report();
  178. usb_keyboard_send();
  179. print_enable = true;
  180. print("timer: "); phex16(timer_count); print("\n");
  181. _delay_ms(500);
  182. break;
  183. case KB_P: // print toggle
  184. usb_keyboard_clear_report();
  185. usb_keyboard_send();
  186. if (print_enable) {
  187. print("print disabled.\n");
  188. print_enable = false;
  189. } else {
  190. print_enable = true;
  191. print("print enabled.\n");
  192. }
  193. _delay_ms(1000);
  194. break;
  195. }
  196. }
  197. // send mouse packet to host
  198. if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  199. mouse_buttons = mouse_btn;
  200. if (mouse_x && mouse_y)
  201. usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
  202. else
  203. usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  204. usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  205. // acceleration
  206. _delay_ms(MOUSE_DELAY_MS);
  207. mouse_repeat++;
  208. } else {
  209. mouse_repeat = 0;
  210. }
  211. // send key packet to host
  212. if (modified) {
  213. if (key_index > 6) {
  214. //Rollover
  215. }
  216. usb_keyboard_send();
  217. }
  218. }