sym_defer_pk.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2017 Alex Ong<the.onga@gmail.com>
  2. // Copyright 2020 Andrei Purdea<andrei@purdea.ro>
  3. // Copyright 2021 Simon Arlott
  4. // SPDX-License-Identifier: GPL-2.0-or-later
  5. //
  6. // Basic symmetric per-key algorithm. Uses an 8-bit counter per key.
  7. // When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  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 * MATRIX_COLS] = {DEBOUNCE_ELAPSED};
  24. static bool counters_need_update;
  25. static bool cooked_changed;
  26. static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time);
  27. static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]);
  28. void debounce_init(void) {}
  29. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
  30. static fast_timer_t last_time;
  31. bool updated_last = false;
  32. cooked_changed = false;
  33. if (counters_need_update) {
  34. fast_timer_t now = timer_read_fast();
  35. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  36. last_time = now;
  37. updated_last = true;
  38. if (elapsed_time > 0) {
  39. // Update debounce counters with elapsed timer clamped to UINT8_MAX
  40. update_debounce_counters_and_transfer_if_expired(raw, cooked, MIN(elapsed_time, UINT8_MAX));
  41. }
  42. }
  43. if (changed) {
  44. if (!updated_last) {
  45. last_time = timer_read_fast();
  46. }
  47. start_debounce_counters(raw, cooked);
  48. }
  49. return cooked_changed;
  50. }
  51. /**
  52. * @brief Updates debounce counters and transfers debounced key states if the debounce period has expired.
  53. *
  54. * Iterates through each key in the matrix and checks its debounce counter. If the debounce period has expired
  55. * for a key, the debounced state is updated to match the raw state. Otherwise, the debounce counter is decremented
  56. * by the elapsed time and marked for further updates.
  57. *
  58. * @param raw The current raw key state matrix.
  59. * @param cooked The debounced key state matrix to be updated.
  60. * @param elapsed_time The time elapsed since the last debounce update, in milliseconds.
  61. */
  62. static inline void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t elapsed_time) {
  63. counters_need_update = false;
  64. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  65. uint16_t row_offset = row * MATRIX_COLS;
  66. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  67. uint16_t index = row_offset + col;
  68. if (debounce_counters[index] != DEBOUNCE_ELAPSED) {
  69. if (debounce_counters[index] <= elapsed_time) {
  70. debounce_counters[index] = DEBOUNCE_ELAPSED;
  71. matrix_row_t col_mask = (MATRIX_ROW_SHIFTER << col);
  72. matrix_row_t cooked_next = (cooked[row] & ~col_mask) | (raw[row] & col_mask);
  73. cooked_changed |= cooked[row] ^ cooked_next;
  74. cooked[row] = cooked_next;
  75. } else {
  76. debounce_counters[index] -= elapsed_time;
  77. counters_need_update = true;
  78. }
  79. }
  80. }
  81. }
  82. }
  83. /**
  84. * @brief Initializes debounce counters for keys with changed states.
  85. *
  86. * For each key in the matrix, this function checks if the raw state differs from the debounced state.
  87. * If a change is detected and the debounce counter has elapsed, the counter is set to the debounce period
  88. * and marked for update. Otherwise, the counter is cleared.
  89. *
  90. * @param raw The current raw key state matrix.
  91. * @param cooked The debounced key state matrix.
  92. */
  93. static inline void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[]) {
  94. for (uint8_t row = 0; row < MATRIX_ROWS_PER_HAND; row++) {
  95. uint16_t row_offset = row * MATRIX_COLS;
  96. matrix_row_t delta = raw[row] ^ cooked[row];
  97. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  98. uint16_t index = row_offset + col;
  99. if (delta & (MATRIX_ROW_SHIFTER << col)) {
  100. if (debounce_counters[index] == DEBOUNCE_ELAPSED) {
  101. debounce_counters[index] = DEBOUNCE;
  102. counters_need_update = true;
  103. }
  104. } else {
  105. debounce_counters[index] = DEBOUNCE_ELAPSED;
  106. }
  107. }
  108. }
  109. }
  110. #else
  111. # include "none.c"
  112. #endif