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

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