Keyboard firmwares for Atmel AVR and Cortex-M
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

key_process.c 4.2KB

13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
13 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 "jump_bootloader.h"
  12. #include "matrix_skel.h"
  13. #include "keymap_skel.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. // TODO: refactoring
  23. void proc_matrix(void) {
  24. static int mouse_repeat = 0;
  25. bool modified = false;
  26. //bool has_ghost = false;
  27. int key_index = 0;
  28. uint8_t mouse_btn = 0;
  29. int8_t mouse_x = 0;
  30. int8_t mouse_y = 0;
  31. int8_t mouse_wheel = 0;
  32. int8_t mouse_hwheel = 0;
  33. int fn_bits = 0;
  34. matrix_scan();
  35. modified = matrix_is_modified();
  36. if (modified) {
  37. matrix_print();
  38. // LED flash for debug
  39. LED_CONFIG;
  40. LED_ON;
  41. }
  42. if (matrix_has_ghost()) {
  43. // should send error?
  44. print("matrix has ghost!!\n");
  45. return;
  46. }
  47. usb_keyboard_clear();
  48. for (int row = 0; row < matrix_rows(); row++) {
  49. for (int col = 0; col < matrix_cols(); col++) {
  50. if (matrix_get_row(row) & 1<<col) continue;
  51. uint8_t code = keymap_get_keycode(row, col);
  52. if (code == KB_NO) {
  53. code = keymap_get_keycodel(0, row, col);
  54. if (FN_0 <= code && code <= FN_7) {
  55. fn_bits |= 1<<(code - FN_0);
  56. }
  57. } else if (KB_LCTRL <= code && code <= KB_RGUI) {
  58. // modifier keys(0xE0-0xE7)
  59. keyboard_modifier_keys |= 1<<(code & 0x07);
  60. } else if (code >= MS_UP) {
  61. // mouse
  62. if (code == MS_UP) mouse_y -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  63. if (code == MS_DOWN) mouse_y += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  64. if (code == MS_LEFT) mouse_x -= MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  65. if (code == MS_RIGHT) mouse_x += MOUSE_MOVE_UNIT + (mouse_repeat < 50 ? mouse_repeat/5 : 10);
  66. if (code == MS_BTN1) mouse_btn |= 1<<0;
  67. if (code == MS_BTN2) mouse_btn |= 1<<1;
  68. if (code == MS_BTN3) mouse_btn |= 1<<2;
  69. if (code == MS_BTN4) mouse_btn |= 1<<3;
  70. if (code == MS_BTN5) mouse_btn |= 1<<4;
  71. if (code == MS_WH_UP) mouse_wheel += 1;
  72. if (code == MS_WH_DOWN) mouse_wheel -= 1;
  73. if (code == MS_WH_LEFT) mouse_hwheel -= 1;
  74. if (code == MS_WH_RIGHT) mouse_hwheel += 1;
  75. } else if (FN_0 <= code && code <= FN_7) {
  76. fn_bits |= 1<<(code - FN_0);
  77. } else {
  78. // normal keys
  79. if (key_index < 6)
  80. keyboard_keys[key_index] = code;
  81. key_index++;
  82. }
  83. }
  84. }
  85. keymap_fn_proc(fn_bits);
  86. // when 4 left modifier keys down
  87. if (keyboard_modifier_keys == (MOD_LCTRL | MOD_LSHIFT | MOD_LALT | MOD_LGUI)) {
  88. // cancel all keys
  89. keyboard_modifier_keys = 0;
  90. for (int i = 0; i < 6; i++) keyboard_keys[i] = KB_NO;
  91. usb_keyboard_send();
  92. print("jump to bootloader...\n");
  93. _delay_ms(100);
  94. jump_bootloader(); // not return
  95. }
  96. if (mouse_x || mouse_y || mouse_wheel || mouse_hwheel || mouse_btn != mouse_buttons) {
  97. mouse_buttons = mouse_btn;
  98. usb_mouse_move(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  99. usb_mouse_print(mouse_x, mouse_y, mouse_wheel, mouse_hwheel);
  100. // acceleration
  101. _delay_ms(MOUSE_DELAY_MS >> (mouse_repeat < MOUSE_DELAY_ACC ? mouse_repeat : MOUSE_DELAY_ACC));
  102. mouse_repeat++;
  103. } else {
  104. mouse_repeat = 0;
  105. }
  106. // send keys to host
  107. if (modified) {
  108. if (key_index > 6) {
  109. //Rollover
  110. }
  111. usb_keyboard_send();
  112. usb_keyboard_print();
  113. // LED flash for debug
  114. LED_CONFIG;
  115. LED_OFF;
  116. }
  117. }