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.

keyboard.c 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. Copyright 2011,2012,2013 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. #include "bootloader.h"
  27. #ifdef MOUSEKEY_ENABLE
  28. #include "mousekey.h"
  29. #endif
  30. void keyboard_init(void)
  31. {
  32. // TODO: configuration of sendchar impl
  33. print_sendchar_func = sendchar;
  34. timer_init();
  35. matrix_init();
  36. /* boot magic keys goes here */
  37. matrix_scan();
  38. #ifdef IS_BOOTMAGIC_BOOTLOADER
  39. /* kick up bootloader */
  40. if (IS_BOOTMAGIC_BOOTLOADER()) bootloader_jump();
  41. #endif
  42. #ifdef IS_BOOTMAGIC_DEBUG
  43. if (IS_BOOTMAGIC_DEBUG()) debug_enable = true;
  44. #endif
  45. #ifdef PS2_MOUSE_ENABLE
  46. ps2_mouse_init();
  47. #endif
  48. }
  49. /*
  50. * Do keyboard routine jobs: scan mantrix, light LEDs, ...
  51. * This is repeatedly called as fast as possible.
  52. */
  53. void keyboard_task(void)
  54. {
  55. static matrix_row_t matrix_prev[MATRIX_ROWS];
  56. static uint8_t led_status = 0;
  57. matrix_row_t matrix_row = 0;
  58. matrix_row_t matrix_change = 0;
  59. matrix_scan();
  60. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  61. matrix_row = matrix_get_row(r);
  62. matrix_change = matrix_row ^ matrix_prev[r];
  63. if (matrix_change) {
  64. if (debug_matrix) matrix_print();
  65. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  66. if (matrix_change & ((matrix_row_t)1<<c)) {
  67. action_exec((keyevent_t){
  68. .key.pos = (keypos_t){ .row = r, .col = c },
  69. .pressed = (matrix_row & (1<<c)),
  70. .time = (timer_read() | 1) /* time should not be 0 */
  71. });
  72. // record a processed key
  73. matrix_prev[r] ^= ((matrix_row_t)1<<c);
  74. // process a key per task call
  75. goto MATRIX_LOOP_END;
  76. }
  77. }
  78. }
  79. }
  80. // call with pseudo tick event when no real key event.
  81. action_exec(TICK);
  82. MATRIX_LOOP_END:
  83. #ifdef MOUSEKEY_ENABLE
  84. // mousekey repeat & acceleration
  85. mousekey_task();
  86. #endif
  87. // update LED
  88. if (led_status != host_keyboard_leds()) {
  89. led_status = host_keyboard_leds();
  90. keyboard_set_leds(led_status);
  91. }
  92. }
  93. void keyboard_set_leds(uint8_t leds)
  94. {
  95. led_set(leds);
  96. }