Keyboard firmwares for Atmel AVR and Cortex-M
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

keyboard.c 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. keymap_process_event((keyevent_t){
  59. .key = (keypos_t){ .row = r, .col = c },
  60. .pressed = (matrix_row & (1<<c))
  61. });
  62. // record a processed key
  63. matrix_prev[r] ^= (1<<c);
  64. // process a key per task call
  65. goto MATRIX_LOOP_END;
  66. }
  67. }
  68. }
  69. }
  70. MATRIX_LOOP_END:
  71. #ifdef MOUSEKEY_ENABLE
  72. // mousekey repeat & acceleration
  73. mousekey_task();
  74. #endif
  75. // update LED
  76. if (led_status != host_keyboard_leds()) {
  77. led_status = host_keyboard_leds();
  78. keyboard_set_leds(led_status);
  79. }
  80. return;
  81. }
  82. void keyboard_set_leds(uint8_t leds)
  83. {
  84. led_set(leds);
  85. }