debounce.c 937 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "matrix.h"
  2. #include "timer.h"
  3. #include "quantum.h"
  4. #ifndef DEBOUNCING_DELAY
  5. # define DEBOUNCING_DELAY 5
  6. #endif
  7. void debounce_init(uint8_t num_rows) {
  8. }
  9. #if DEBOUNCING_DELAY > 0
  10. static bool debouncing = false;
  11. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  12. static uint16_t debouncing_time;
  13. if (changed) {
  14. debouncing = true;
  15. debouncing_time = timer_read();
  16. }
  17. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  18. for (uint8_t i = 0; i < num_rows; i++) {
  19. cooked[i] = raw[i];
  20. }
  21. debouncing = false;
  22. }
  23. }
  24. bool debounce_active(void) {
  25. return debouncing;
  26. }
  27. #else
  28. // no debounce
  29. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  30. if (changed)
  31. {
  32. for (uint8_t i = 0; i < num_rows; i++) {
  33. cooked[i] = raw[i];
  34. }
  35. }
  36. }
  37. bool debounce_active(void) {
  38. return false;
  39. }
  40. #endif