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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_extra.h"
  12. #include "usb_keycodes.h"
  13. #include "layer.h"
  14. #include "matrix_skel.h"
  15. #include "keymap_skel.h"
  16. #include "controller.h"
  17. #include "key_process.h"
  18. #define MOUSE_MOVE_UNIT 10
  19. #define MOUSE_MOVE_ACCEL (mouse_repeat < 50 ? mouse_repeat/5 : 10)
  20. #ifndef MOUSE_DELAY_TIME
  21. # define MOUSE_DELAY_TIME 255
  22. #endif
  23. #define MOUSE_DELAY_MS (MOUSE_DELAY_TIME >> (mouse_repeat < 5 ? mouse_repeat : 4))
  24. // TODO: refactoring
  25. void proc_matrix(void) {
  26. static int mouse_repeat = 0;
  27. bool modified = false;
  28. //bool has_ghost = false;
  29. int key_index = 0;
  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_clear_report();
  52. for (int row = 0; row < matrix_rows(); row++) {
  53. for (int col = 0; col < matrix_cols(); col++) {
  54. if (!matrix_is_on(row, col)) continue;
  55. uint8_t code = layer_get_keycode(row, col);
  56. if (code == KB_NO) {
  57. // do nothing
  58. } else if (IS_MOD(code)) {
  59. usb_keyboard_mods |= MOD_BIT(code);
  60. } else if (IS_MOUSE(code)) {
  61. // mouse
  62. if (code == MS_UP)
  63. mouse_y -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  64. if (code == MS_DOWN)
  65. mouse_y += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  66. if (code == MS_LEFT)
  67. mouse_x -= MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  68. if (code == MS_RIGHT)
  69. mouse_x += MOUSE_MOVE_UNIT + MOUSE_MOVE_ACCEL;
  70. if (code == MS_BTN1) mouse_btn |= BIT_BTN1;
  71. if (code == MS_BTN2) mouse_btn |= BIT_BTN2;
  72. if (code == MS_BTN3) mouse_btn |= BIT_BTN3;
  73. if (code == MS_BTN4) mouse_btn |= BIT_BTN4;
  74. if (code == MS_BTN5) mouse_btn |= BIT_BTN5;
  75. if (code == MS_WH_UP) mouse_vwheel += 1;
  76. if (code == MS_WH_DOWN) mouse_vwheel -= 1;
  77. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  78. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  79. } else if (IS_FN(code)) {
  80. fn_bits |= FN_BIT(code);
  81. } else if (code == KB_MUTE) {
  82. usb_extra_send(AUDIO_MUTE);
  83. usb_extra_send(0);
  84. _delay_ms(500);
  85. } else if (code == KB_VOLU) {
  86. usb_extra_send(AUDIO_VOL_UP);
  87. usb_extra_send(0);
  88. _delay_ms(100);
  89. } else if (code == KB_VOLD) {
  90. usb_extra_send(AUDIO_VOL_DOWN);
  91. usb_extra_send(0);
  92. _delay_ms(100);
  93. } else {
  94. // normal keys
  95. if (key_index < 6)
  96. usb_keyboard_keys[key_index] = code;
  97. key_index++;
  98. }
  99. }
  100. }
  101. if (modified) {
  102. #ifdef DEBUG_LED
  103. // LED flash for debug
  104. DEBUG_LED_CONFIG;
  105. DEBUG_LED_OFF;
  106. #endif
  107. }
  108. layer_switching(fn_bits);
  109. // when 4 left modifier keys down
  110. if (keymap_is_special_mode(fn_bits)) {
  111. switch (usb_keyboard_keys[0]) {
  112. case KB_H: // help
  113. print_enable = true;
  114. print("b: jump to bootloader\n");
  115. print("d: debug print toggle\n");
  116. print("k: keyboard debug toggle\n");
  117. print("m: mouse debug toggle\n");
  118. print("x: matrix debug toggle\n");
  119. print("v: print version\n");
  120. print("t: print timer count\n");
  121. print("p: print enable toggle\n");
  122. _delay_ms(500);
  123. print_enable = false;
  124. break;
  125. case KB_B: // bootloader
  126. usb_keyboard_clear_report();
  127. usb_keyboard_send();
  128. print_enable = true;
  129. print("jump to bootloader...\n");
  130. _delay_ms(1000);
  131. jump_bootloader(); // not return
  132. break;
  133. case KB_D: // debug all toggle
  134. usb_keyboard_clear_report();
  135. usb_keyboard_send();
  136. debug_enable = !debug_enable;
  137. if (debug_enable) {
  138. print_enable = true;
  139. print("debug enabled.\n");
  140. debug_matrix = true;
  141. debug_keyboard = true;
  142. debug_mouse = true;
  143. } else {
  144. print("debug disabled.\n");
  145. print_enable = false;
  146. debug_matrix = false;
  147. debug_keyboard = false;
  148. debug_mouse = false;
  149. }
  150. _delay_ms(1000);
  151. break;
  152. case KB_X: // debug matrix toggle
  153. usb_keyboard_clear_report();
  154. usb_keyboard_send();
  155. debug_matrix = !debug_matrix;
  156. if (debug_matrix)
  157. print("debug matrix enabled.\n");
  158. else
  159. print("debug matrix disabled.\n");
  160. _delay_ms(1000);
  161. break;
  162. case KB_K: // debug keyboard toggle
  163. usb_keyboard_clear_report();
  164. usb_keyboard_send();
  165. debug_keyboard = !debug_keyboard;
  166. if (debug_keyboard)
  167. print("debug keyboard enabled.\n");
  168. else
  169. print("debug keyboard disabled.\n");
  170. _delay_ms(1000);
  171. break;
  172. case KB_M: // debug mouse toggle
  173. usb_keyboard_clear_report();
  174. usb_keyboard_send();
  175. debug_mouse = !debug_mouse;
  176. if (debug_mouse)
  177. print("debug mouse enabled.\n");
  178. else
  179. print("debug mouse disabled.\n");
  180. _delay_ms(1000);
  181. break;
  182. case KB_V: // print version & information
  183. usb_keyboard_clear_report();
  184. usb_keyboard_send();
  185. print_enable = true;
  186. print(STR(DESCRIPTION) "\n");
  187. _delay_ms(1000);
  188. break;
  189. case KB_T: // print timer
  190. usb_keyboard_clear_report();
  191. usb_keyboard_send();
  192. print_enable = true;
  193. print("timer: "); phex16(timer_count); print("\n");
  194. _delay_ms(500);
  195. break;
  196. case KB_P: // print toggle
  197. usb_keyboard_clear_report();
  198. usb_keyboard_send();
  199. if (print_enable) {
  200. print("print disabled.\n");
  201. print_enable = false;
  202. } else {
  203. print_enable = true;
  204. print("print enabled.\n");
  205. }
  206. _delay_ms(1000);
  207. break;
  208. }
  209. }
  210. // send mouse packet to host
  211. if (mouse_x || mouse_y || mouse_vwheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  212. mouse_buttons = mouse_btn;
  213. if (mouse_x && mouse_y)
  214. usb_mouse_move(mouse_x*0.7, mouse_y*0.7, mouse_vwheel, mouse_hwheel);
  215. else
  216. usb_mouse_move(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  217. usb_mouse_print(mouse_x, mouse_y, mouse_vwheel, mouse_hwheel);
  218. // acceleration
  219. _delay_ms(MOUSE_DELAY_MS);
  220. mouse_repeat++;
  221. } else {
  222. mouse_repeat = 0;
  223. }
  224. // send key packet to host
  225. if (modified) {
  226. if (key_index > 6) {
  227. //Rollover
  228. }
  229. usb_keyboard_send();
  230. }
  231. }