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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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_skel.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. 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_wheel = 0;
  31. int8_t mouse_hwheel = 0;
  32. int fn_bits = 0;
  33. matrix_scan();
  34. modified = matrix_is_modified();
  35. if (modified) {
  36. matrix_print();
  37. // LED flash for debug
  38. LED_CONFIG;
  39. LED_ON;
  40. }
  41. if (matrix_has_ghost()) {
  42. // should send error?
  43. print("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_get_row(row) & 1<<col) continue;
  50. uint8_t code = keymap_get_keycode(row, col);
  51. if (code == KB_NO) {
  52. code = keymap_get_keycodel(0, row, col);
  53. if (FN_0 <= code && code <= FN_7) {
  54. fn_bits |= 1<<(code - FN_0);
  55. }
  56. } else if (KB_LCTRL <= code && code <= KB_RGUI) {
  57. // modifier keys(0xE0-0xE7)
  58. keyboard_modifier_keys |= 1<<(code & 0x07);
  59. } else if (code >= MS_UP) {
  60. // mouse
  61. if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  62. if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  63. if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  64. if (code == MS_RIGHT) mouse_x += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  65. if (code == MS_BTN1) mouse_btn |= 1<<0;
  66. if (code == MS_BTN2) mouse_btn |= 1<<1;
  67. if (code == MS_BTN3) mouse_btn |= 1<<2;
  68. if (code == MS_BTN4) mouse_btn |= 1<<3;
  69. if (code == MS_BTN5) mouse_btn |= 1<<4;
  70. if (code == MS_WH_UP) mouse_wheel += 1;
  71. if (code == MS_WH_DOWN) mouse_wheel -= 1;
  72. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  73. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  74. } else if (FN_0 <= code && code <= FN_7) {
  75. fn_bits |= 1<<(code - FN_0);
  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 (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
  87. // cancel all keys
  88. keyboard_modifier_keys = 0;
  89. for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
  90. usb_keyboard_send();
  91. print("jump to bootloader...\n");
  92. _delay_ms(100);
  93. jump_bootloader(); // not return
  94. }
  95. if (mouse_x || mouse_y || mouse_wheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  96. mouse_buttons = mouse_btn;
  97. usb_mouse_move(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  98. usb_mouse_print(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  99. // acceleration
  100. _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
  101. mouse_repeat++;
  102. } else {
  103. mouse_repeat = 0;
  104. }
  105. // send keys to host
  106. if (modified) {
  107. if (key_index > 6) {
  108. //Rollover
  109. }
  110. usb_keyboard_send();
  111. usb_keyboard_print();
  112. // LED flash for debug
  113. LED_CONFIG;
  114. LED_OFF;
  115. }
  116. }