You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB

  1. /*
  2. Copyright 2017 Alex Ong<the.onga@gmail.com>
  3. Copyright 2021 Simon Arlott
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. /*
  16. Basic global debounce algorithm. Used in 99% of keyboards at time of implementation
  17. When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  18. */
  19. #include "debounce.h"
  20. #include "timer.h"
  21. #include <string.h>
  22. #ifndef DEBOUNCE
  23. # define DEBOUNCE 5
  24. #endif
  25. // Maximum debounce: 255ms
  26. #if DEBOUNCE > UINT8_MAX
  27. # undef DEBOUNCE
  28. # define DEBOUNCE UINT8_MAX
  29. #endif
  30. #if DEBOUNCE > 0
  31. static bool debouncing = false;
  32. static fast_timer_t debouncing_time;
  33. void debounce_init(uint8_t num_rows) {}
  34. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  35. bool cooked_changed = false;
  36. if (changed) {
  37. debouncing = true;
  38. debouncing_time = timer_read_fast();
  39. } else if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) {
  40. size_t matrix_size = num_rows * sizeof(matrix_row_t);
  41. if (memcmp(cooked, raw, matrix_size) != 0) {
  42. memcpy(cooked, raw, matrix_size);
  43. cooked_changed = true;
  44. }
  45. debouncing = false;
  46. }
  47. return cooked_changed;
  48. }
  49. void debounce_free(void) {}
  50. #else // no debouncing.
  51. # include "none.c"
  52. #endif