sym_eager_pr.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2017 Alex Ong<the.onga@gmail.com>
  2. // Copyright 2021 Simon Arlott
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. //
  5. // Basic per-row algorithm. Uses an 8-bit counter per key.
  6. // After pressing a key, it immediately changes state, and sets a counter.
  7. // No further inputs are accepted until DEBOUNCE milliseconds have occurred.
  8. #include "debounce.h"
  9. #include "timer.h"
  10. #include "util.h"
  11. #ifndef DEBOUNCE
  12. # define DEBOUNCE 5
  13. #endif
  14. // Maximum debounce: 255ms
  15. #if DEBOUNCE > UINT8_MAX
  16. # undef DEBOUNCE
  17. # define DEBOUNCE UINT8_MAX
  18. #endif
  19. #define DEBOUNCE_ELAPSED 0
  20. #if DEBOUNCE > 0
  21. typedef uint8_t debounce_counter_t;
  22. // Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
  23. static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND] = {DEBOUNCE_ELAPSED};
  24. static bool counters_need_update;
  25. static bool matrix_need_update;
  26. static bool cooked_changed;
  27. static inline void update_debounce_counters(uint8_t elapsed_time);
  28. static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]);
  29. void debounce_init(void) {}
  30. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
  31. static fast_timer_t last_time;
  32. bool updated_last = false;
  33. cooked_changed = false;
  34. if (counters_need_update) {
  35. fast_timer_t now = timer_read_fast();
  36. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  37. last_time = now;
  38. updated_last = true;
  39. if (elapsed_time > 0) {
  40. // Update debounce counters with elapsed timer clamped to UINT8_MAX
  41. update_debounce_counters(MIN(elapsed_time, UINT8_MAX));
  42. }
  43. }
  44. if (changed || matrix_need_update) {
  45. if (!updated_last) {
  46. last_time = timer_read_fast();
  47. }
  48. transfer_matrix_values(raw, cooked);
  49. }
  50. return cooked_changed;
  51. }
  52. /**
  53. * @brief Updates per-row debounce counters and determines if matrix needs updating.
  54. *
  55. * Iterates through each row in the matrix and checks its debounce counter. If the debounce
  56. * period has elapsed, the counter is reset and the matrix is marked for update. Otherwise,
  57. * the counter is decremented by the elapsed time and marked for further updates if needed.
  58. *
  59. * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
  60. */
  61. static inline void update_debounce_counters(uint8_t elapsed_time) {
  62. counters_need_update = false;
  63. matrix_need_update = false;
  64. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  65. if (debounce_counters[row] != DEBOUNCE_ELAPSED) {
  66. if (debounce_counters[row] <= elapsed_time) {
  67. debounce_counters[row] = DEBOUNCE_ELAPSED;
  68. matrix_need_update = true;
  69. } else {
  70. debounce_counters[row] -= elapsed_time;
  71. counters_need_update = true;
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * @brief Transfers debounced key states from the raw matrix to the cooked matrix.
  78. *
  79. * For each row in the matrix, this function checks if its state has changed and if its
  80. * debounce counter has elapsed. If so, the debounce counter is reset, the cooked matrix
  81. * is updated to reflect the new state, and the matrix is marked for further updates.
  82. *
  83. * @param raw The current raw key state matrix.
  84. * @param cooked The debounced key state matrix
  85. */
  86. static inline void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[]) {
  87. matrix_need_update = false;
  88. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  89. matrix_row_t existing_row = cooked[row];
  90. matrix_row_t raw_row = raw[row];
  91. // determine new value basd on debounce pointer + raw value
  92. if (existing_row != raw_row) {
  93. if (debounce_counters[row] == DEBOUNCE_ELAPSED) {
  94. debounce_counters[row] = DEBOUNCE;
  95. cooked_changed |= cooked[row] ^ raw_row;
  96. cooked[row] = raw_row;
  97. counters_need_update = true;
  98. }
  99. }
  100. }
  101. }
  102. #else
  103. # include "none.c"
  104. #endif