sym_defer_g.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 "matrix.h"
  20. #include "timer.h"
  21. #include "quantum.h"
  22. #include <string.h>
  23. #ifndef DEBOUNCE
  24. # define DEBOUNCE 5
  25. #endif
  26. #if DEBOUNCE > 0
  27. static bool debouncing = false;
  28. static fast_timer_t debouncing_time;
  29. void debounce_init(uint8_t num_rows) {}
  30. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  31. bool cooked_changed = false;
  32. if (changed) {
  33. debouncing = true;
  34. debouncing_time = timer_read_fast();
  35. }
  36. if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) {
  37. if (memcmp(cooked, raw, sizeof(matrix_row_t) * num_rows) != 0) {
  38. memcpy(cooked, raw, sizeof(matrix_row_t) * num_rows);
  39. cooked_changed = true;
  40. }
  41. debouncing = false;
  42. }
  43. return cooked_changed;
  44. }
  45. void debounce_free(void) {}
  46. #else // no debouncing.
  47. # include "none.c"
  48. #endif