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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // TODO: clean unused headers
  2. #include <stdbool.h>
  3. #include <avr/io.h>
  4. #include <avr/pgmspace.h>
  5. #include <avr/interrupt.h>
  6. #include <util/delay.h>
  7. #include "usb.h"
  8. #include "usb_keyboard.h"
  9. #include "usb_mouse.h"
  10. #include "print.h"
  11. #include "matrix.h"
  12. #include "keymap.h"
  13. #include "jump_bootloader.h"
  14. #include "key_process.h"
  15. // for Teensy/Teensy++ 2.0
  16. #define LED_CONFIG (DDRD |= (1<<6))
  17. #define LED_ON (PORTD |= (1<<6))
  18. #define LED_OFF (PORTD &= ~(1<<6))
  19. #define MOUSE_MOVE_UNIT 10
  20. #define MOUSE_DELAY_MS 200
  21. #define MOUSE_DELAY_ACC 5
  22. static void print_matrix(void);
  23. static void print_keys(void);
  24. static void print_mouse(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h);
  25. void proc_matrix(void) {
  26. static int mouse_repeat = 0;
  27. bool modified = false;
  28. bool has_ghost = false;
  29. int layer = 0;
  30. int key_index = 0;
  31. uint8_t mouse_btn = 0;
  32. int8_t mouse_x = 0;
  33. int8_t mouse_y = 0;
  34. int8_t mouse_wheel = 0;
  35. int8_t mouse_hwheel = 0;
  36. matrix_scan();
  37. modified = matrix_is_modified();
  38. has_ghost = matrix_has_ghost();
  39. layer = get_layer();
  40. // print matrix state for debug
  41. if (modified) {
  42. print_matrix();
  43. // LED flash for debug
  44. LED_CONFIG;
  45. LED_ON;
  46. }
  47. keyboard_modifier_keys = 0;
  48. for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
  49. key_index = 0;
  50. mouse_btn = 0;
  51. mouse_x = 0;
  52. mouse_y = 0;
  53. mouse_wheel = 0;
  54. mouse_hwheel = 0;
  55. // convert matrix state to HID report
  56. for (int row = 0; row < MATRIX_ROWS; row++) {
  57. for (int col = 0; col < MATRIX_COLS; col++) {
  58. if (matrix[row] & 1<<col) continue;
  59. uint8_t code = get_keycode(layer, row, col);
  60. if (code == KB_NO) {
  61. continue;
  62. } else if (KB_LCTRL <= code && code <= KB_RGUI) {
  63. // modifier keys(0xE0-0xE7)
  64. keyboard_modifier_keys |= 1<<(code & 0x07);
  65. } else if (code >= MS_UP) {
  66. // mouse
  67. if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  68. if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  69. if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  70. if (code == MS_RIGHT) mouse_x += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  71. if (code == MS_BTN1) mouse_btn |= 1<<0;
  72. if (code == MS_BTN2) mouse_btn |= 1<<1;
  73. if (code == MS_BTN3) mouse_btn |= 1<<2;
  74. if (code == MS_BTN4) mouse_btn |= 1<<3;
  75. if (code == MS_BTN5) mouse_btn |= 1<<4;
  76. if (code == MS_WH_UP) mouse_wheel += 1;
  77. if (code == MS_WH_DOWN) mouse_wheel -= 1;
  78. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  79. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  80. } else {
  81. // normal keys
  82. if (key_index < 6)
  83. keyboard_keys[key_index] = code;
  84. key_index++;
  85. }
  86. }
  87. }
  88. if (!has_ghost) {
  89. // when 4 left modifier keys down
  90. if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
  91. // cancel all keys
  92. keyboard_modifier_keys = 0;
  93. for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
  94. usb_keyboard_send();
  95. print("jump to bootloader...\n");
  96. _delay_ms(100);
  97. jump_bootloader(); // not return
  98. }
  99. if (mouse_x || mouse_y || mouse_wheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  100. mouse_buttons = mouse_btn;
  101. usb_mouse_move(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  102. print_mouse(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  103. key_sent = true;
  104. // acceleration
  105. _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
  106. mouse_repeat++;
  107. } else {
  108. mouse_repeat = 0;
  109. }
  110. // send keys to host
  111. if (modified) {
  112. if (key_index > 6) {
  113. //Rollover
  114. }
  115. usb_keyboard_send();
  116. if (keyboard_keys[0])
  117. key_sent = true;
  118. print_keys();
  119. // LED flash for debug
  120. LED_CONFIG;
  121. LED_OFF;
  122. }
  123. }
  124. }
  125. static void print_matrix(void) {
  126. print("\nr/c 01234567\n");
  127. for (int row = 0; row < MATRIX_ROWS; row++) {
  128. phex(row); print(": ");
  129. pbin_reverse(matrix[row]);
  130. if (matrix_has_ghost_in_row(row)) {
  131. print(" <ghost");
  132. }
  133. print("\n");
  134. }
  135. }
  136. static void print_keys(void) {
  137. print("\nkeys: ");
  138. for (int i = 0; i < 6; i++) { phex(keyboard_keys[i]); print(" "); }
  139. print("\n");
  140. print("mods: "); phex(keyboard_modifier_keys); print("\n");
  141. }
  142. static void print_mouse(int8_t mouse_x, int8_t mouse_y, int8_t wheel_v, int8_t wheel_h) {
  143. print("\nmouse_x y v h: ");
  144. phex(mouse_x); print(" ");
  145. phex(mouse_y); print(" ");
  146. phex(wheel_v); print(" ");
  147. phex(wheel_h); print("\n");
  148. print("buttons: "); phex(mouse_buttons); print("\n");
  149. }