sym_eager_pk.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. Copyright 2017 Alex Ong<the.onga@gmail.com>
  3. Copyright 2021 Simon Arlott
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Basic per-key algorithm. Uses an 8-bit counter per key.
  17. After pressing a key, it immediately changes state, and sets a counter.
  18. No further inputs are accepted until DEBOUNCE milliseconds have occurred.
  19. */
  20. #include "debounce.h"
  21. #include "timer.h"
  22. #include "util.h"
  23. #ifndef DEBOUNCE
  24. # define DEBOUNCE 5
  25. #endif
  26. // Maximum debounce: 255ms
  27. #if DEBOUNCE > UINT8_MAX
  28. # undef DEBOUNCE
  29. # define DEBOUNCE UINT8_MAX
  30. #endif
  31. #define DEBOUNCE_ELAPSED 0
  32. #if DEBOUNCE > 0
  33. typedef uint8_t debounce_counter_t;
  34. // Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
  35. static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND * MATRIX_COLS] = {DEBOUNCE_ELAPSED};
  36. static bool counters_need_update;
  37. static bool matrix_need_update;
  38. static bool cooked_changed;
  39. static inline void update_debounce_counters(uint8_t elapsed_time);
  40. static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
  41. void debounce_init(void) {}
  42. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
  43. static fast_timer_t last_time;
  44. bool updated_last = false;
  45. cooked_changed = false;
  46. if (counters_need_update) {
  47. fast_timer_t now = timer_read_fast();
  48. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  49. last_time = now;
  50. updated_last = true;
  51. if (elapsed_time > 0) {
  52. // Update debounce counters with elapsed timer clamped to UINT8_MAX
  53. update_debounce_counters(MIN(elapsed_time, UINT8_MAX));
  54. }
  55. }
  56. if (changed || matrix_need_update) {
  57. if (!updated_last) {
  58. last_time = timer_read_fast();
  59. }
  60. transfer_matrix_values(raw, cooked);
  61. }
  62. return cooked_changed;
  63. }
  64. /**
  65. * @brief Updates per-key debounce counters and determines if matrix needs updating.
  66. *
  67. * Iterates through each key in the matrix and checks its debounce counter. If the debounce
  68. * period has elapsed, the counter is reset and the matrix is marked for update. Otherwise,
  69. * the counter is decremented by the elapsed time and marked for further updates if needed.
  70. *
  71. * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
  72. */
  73. static inline void update_debounce_counters(uint8_t elapsed_time) {
  74. counters_need_update = false;
  75. matrix_need_update = false;
  76. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  77. uint16_t row_offset = row * MATRIX_COLS;
  78. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  79. uint16_t index = row_offset + col;
  80. if (debounce_counters[index] != DEBOUNCE_ELAPSED) {
  81. if (debounce_counters[index] <= elapsed_time) {
  82. debounce_counters[index] = DEBOUNCE_ELAPSED;
  83. matrix_need_update = true;
  84. } else {
  85. debounce_counters[index] -= elapsed_time;
  86. counters_need_update = true;
  87. }
  88. }
  89. }
  90. }
  91. }
  92. /**
  93. * @brief Transfers debounced key states from the raw matrix to the cooked matrix.
  94. *
  95. * For each key in the matrix, this function checks if its state has changed and if its
  96. * debounce counter has elapsed. If so, the debounce counter is reset, the cooked matrix
  97. * is updated to reflect the new state, and the matrix is marked for further updates.
  98. *
  99. * @param raw The current raw key state matrix.
  100. * @param cooked The debounced key state matrix to be updated.
  101. */
  102. static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
  103. matrix_need_update = false;
  104. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  105. uint16_t row_offset = row * MATRIX_COLS;
  106. matrix_row_t delta = raw[row] ^ cooked[row];
  107. matrix_row_t existing_row = cooked[row];
  108. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  109. uint16_t index = row_offset + col;
  110. matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col);
  111. if (delta & col_mask) {
  112. if (debounce_counters[index] == DEBOUNCE_ELAPSED) {
  113. debounce_counters[index] = DEBOUNCE;
  114. counters_need_update = true;
  115. existing_row ^= col_mask; // flip the bit.
  116. cooked_changed = true;
  117. }
  118. }
  119. }
  120. cooked[row] = existing_row;
  121. }
  122. }
  123. #else
  124. # include "none.c"
  125. #endif