sym_defer_pr.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2017 Alex Ong<the.onga@gmail.com>
  2. // Copyright 2020 Andrei Purdea<andrei@purdea.ro>
  3. // Copyright 2021 Simon Arlott
  4. // Copyright @filterpaper
  5. // SPDX-License-Identifier: GPL-2.0-or-later
  6. //
  7. // Basic symmetric per-row algorithm. Uses an 8-bit counter per row.
  8. // When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  9. #include "debounce.h"
  10. #include "timer.h"
  11. #include "util.h"
  12. #ifndef DEBOUNCE
  13. # define DEBOUNCE 5
  14. #endif
  15. // Maximum debounce: 255ms
  16. #if DEBOUNCE > UINT8_MAX
  17. # undef DEBOUNCE
  18. # define DEBOUNCE UINT8_MAX
  19. #endif
  20. #define DEBOUNCE_ELAPSED 0
  21. #if DEBOUNCE > 0
  22. typedef uint8_t debounce_counter_t;
  23. // Uses MATRIX_ROWS_PER_HAND instead of MATRIX_ROWS to support split keyboards
  24. static debounce_counter_t debounce_counters[MATRIX_ROWS_PER_HAND] = {DEBOUNCE_ELAPSED};
  25. static bool counters_need_update;
  26. static bool cooked_changed;
  27. static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
  28. static inline void start_debounce_counters(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_and_transfer_if_expired(raw, cooked, MIN(elapsed_time, UINT8_MAX));
  42. }
  43. }
  44. if (changed) {
  45. if (!updated_last) {
  46. last_time = timer_read_fast();
  47. }
  48. start_debounce_counters(raw, cooked);
  49. }
  50. return cooked_changed;
  51. }
  52. /**
  53. * @brief Updates debounce counters and transfers debounced row states if the debounce period has expired.
  54. *
  55. * Iterates through each row in the matrix and checks its debounce counter. If the debounce period has expired
  56. * for a row, the debounced state is updated to match the raw state. Otherwise, the debounce counter is decremented
  57. * by the elapsed time and marked for further updates.
  58. *
  59. * @param raw The current raw key state matrix.
  60. * @param cooked The debounced key state matrix to be updated.
  61. * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
  62. */
  63. static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time) {
  64. counters_need_update = false;
  65. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  66. if (debounce_counters[row] != DEBOUNCE_ELAPSED) {
  67. if (debounce_counters[row] <= elapsed_time) {
  68. debounce_counters[row] = DEBOUNCE_ELAPSED;
  69. cooked_changed |= cooked[row] ^ raw[row];
  70. cooked[row] = raw[row];
  71. } else {
  72. debounce_counters[row] -= elapsed_time;
  73. counters_need_update = true;
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * @brief Initializes debounce counters for rows with changed states.
  80. *
  81. * For each row in the matrix, this function checks if the raw state differs from the debounced state.
  82. * If a change is detected and the debounce counter has elapsed, the counter is set to the debounce period
  83. * and marked for update. Otherwise, the counter is cleared.
  84. *
  85. * @param raw The current raw key state matrix.
  86. * @param cooked The debounced key state matrix.
  87. */
  88. static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]) {
  89. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  90. if (raw[row] != cooked[row]) {
  91. debounce_counters[row] = DEBOUNCE;
  92. counters_need_update = true;
  93. } else {
  94. debounce_counters[row] = DEBOUNCE_ELAPSED;
  95. }
  96. }
  97. }
  98. #else
  99. # include "none.c"
  100. #endif