logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

sym_defer_pr.c (2299B)


  1. /*
  2. Copyright 2021 Chad Austin <chad@chadaustin.me>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. Symmetric per-row debounce algorithm. Changes only apply when
  16. DEBOUNCE milliseconds have elapsed since the last change.
  17. */
  18. #include "debounce.h"
  19. #include "timer.h"
  20. #include <stdlib.h>
  21. #ifndef DEBOUNCE
  22. # define DEBOUNCE 5
  23. #endif
  24. static uint16_t last_time;
  25. // [row] milliseconds until key's state is considered debounced.
  26. static uint8_t* countdowns;
  27. // [row]
  28. static matrix_row_t* last_raw;
  29. void debounce_init(uint8_t num_rows) {
  30. countdowns = (uint8_t*)calloc(num_rows, sizeof(uint8_t));
  31. last_raw = (matrix_row_t*)calloc(num_rows, sizeof(matrix_row_t));
  32. last_time = timer_read();
  33. }
  34. void debounce_free(void) {
  35. free(countdowns);
  36. countdowns = NULL;
  37. free(last_raw);
  38. last_raw = NULL;
  39. }
  40. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  41. uint16_t now = timer_read();
  42. uint16_t elapsed16 = TIMER_DIFF_16(now, last_time);
  43. last_time = now;
  44. uint8_t elapsed = (elapsed16 > 255) ? 255 : elapsed16;
  45. bool cooked_changed = false;
  46. uint8_t* countdown = countdowns;
  47. for (uint8_t row = 0; row < num_rows; ++row, ++countdown) {
  48. matrix_row_t raw_row = raw[row];
  49. if (raw_row != last_raw[row]) {
  50. *countdown = DEBOUNCE;
  51. last_raw[row] = raw_row;
  52. } else if (*countdown > elapsed) {
  53. *countdown -= elapsed;
  54. } else if (*countdown) {
  55. cooked_changed |= cooked[row] ^ raw_row;
  56. cooked[row] = raw_row;
  57. *countdown = 0;
  58. }
  59. }
  60. return cooked_changed;
  61. }
  62. bool debounce_active(void) {
  63. return true;
  64. }