sym_defer_g.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 global debounce algorithm. Used in 99% of keyboards at time of implementation
  6. // When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  7. #include "debounce.h"
  8. #include "timer.h"
  9. #include <string.h>
  10. #ifndef DEBOUNCE
  11. # define DEBOUNCE 5
  12. #endif
  13. // Maximum debounce: 255ms
  14. #if DEBOUNCE > UINT8_MAX
  15. # undef DEBOUNCE
  16. # define DEBOUNCE UINT8_MAX
  17. #endif
  18. #if DEBOUNCE > 0
  19. void debounce_init(void) {}
  20. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], bool changed) {
  21. static fast_timer_t debouncing_time;
  22. static bool debouncing = false;
  23. bool cooked_changed = false;
  24. if (changed) {
  25. debouncing = true;
  26. debouncing_time = timer_read_fast();
  27. } else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) {
  28. size_t matrix_size = MATRIX_ROWS_PER_HAND * sizeof(matrix_row_t);
  29. if (memcmp(cooked, raw, matrix_size) != 0) {
  30. memcpy(cooked, raw, matrix_size);
  31. cooked_changed = true;
  32. }
  33. debouncing = false;
  34. }
  35. return cooked_changed;
  36. }
  37. #else // no debouncing.
  38. # include "none.c"
  39. #endif