Keyboard firmwares for Atmel AVR and Cortex-M
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

keyboard.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright 2011,2012 Jun Wako <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keyboard.h"
  15. #include "matrix.h"
  16. #include "keymap.h"
  17. #include "host.h"
  18. #include "led.h"
  19. #include "keycode.h"
  20. #include "timer.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "command.h"
  24. #include "util.h"
  25. #include "sendchar.h"
  26. #ifdef MOUSEKEY_ENABLE
  27. #include "mousekey.h"
  28. #endif
  29. void keyboard_init(void)
  30. {
  31. // TODO: to enable debug print magic key bind on boot time
  32. // TODO: configuration of sendchar impl
  33. print_sendchar_func = sendchar;
  34. timer_init();
  35. matrix_init();
  36. #ifdef PS2_MOUSE_ENABLE
  37. ps2_mouse_init();
  38. #endif
  39. }
  40. /*
  41. * Do keyboard routine jobs: scan mantrix, light LEDs, ...
  42. * This is repeatedly called as fast as possible.
  43. */
  44. void keyboard_task(void)
  45. {
  46. static matrix_row_t matrix_prev[MATRIX_ROWS];
  47. static uint8_t led_status = 0;
  48. matrix_row_t matrix_row = 0;
  49. matrix_row_t matrix_change = 0;
  50. matrix_scan();
  51. for (int r = 0; r < MATRIX_ROWS; r++) {
  52. matrix_row = matrix_get_row(r);
  53. matrix_change = matrix_row ^ matrix_prev[r];
  54. if (matrix_change) {
  55. if (debug_matrix) matrix_print();
  56. for (int c = 0; c < MATRIX_COLS; c++) {
  57. if (matrix_change & (1<<c)) {
  58. action_exec((keyevent_t){
  59. .key = (keypos_t){ .row = r, .col = c },
  60. .pressed = (matrix_row & (1<<c)),
  61. .time = (timer_read() | 1) /* NOTE: 0 means no event */
  62. });
  63. // record a processed key
  64. matrix_prev[r] ^= (1<<c);
  65. // process a key per task call
  66. goto MATRIX_LOOP_END;
  67. }
  68. }
  69. }
  70. }
  71. MATRIX_LOOP_END:
  72. #ifdef MOUSEKEY_ENABLE
  73. // mousekey repeat & acceleration
  74. mousekey_task();
  75. #endif
  76. // update LED
  77. if (led_status != host_keyboard_leds()) {
  78. led_status = host_keyboard_leds();
  79. keyboard_set_leds(led_status);
  80. }
  81. return;
  82. }
  83. void keyboard_set_leds(uint8_t leds)
  84. {
  85. led_set(leds);
  86. }