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.

140 lines
4.7 KiB

  1. /*
  2. Copyright 2017 Alex Ong<the.onga@gmail.com>
  3. Copyright 2020 Andrei Purdea<andrei@purdea.ro>
  4. Copyright 2021 Simon Arlott
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. Basic symmetric per-key algorithm. Uses an 8-bit counter per key.
  18. When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  19. */
  20. #include "matrix.h"
  21. #include "timer.h"
  22. #include "quantum.h"
  23. #include <stdlib.h>
  24. #ifdef PROTOCOL_CHIBIOS
  25. # if CH_CFG_USE_MEMCORE == FALSE
  26. # error ChibiOS is configured without a memory allocator. Your keyboard may have set `#define CH_CFG_USE_MEMCORE FALSE`, which is incompatible with this debounce algorithm.
  27. # endif
  28. #endif
  29. #ifndef DEBOUNCE
  30. # define DEBOUNCE 5
  31. #endif
  32. // Maximum debounce: 255ms
  33. #if DEBOUNCE > UINT8_MAX
  34. # undef DEBOUNCE
  35. # define DEBOUNCE UINT8_MAX
  36. #endif
  37. #define ROW_SHIFTER ((matrix_row_t)1)
  38. typedef uint8_t debounce_counter_t;
  39. #if DEBOUNCE > 0
  40. static debounce_counter_t *debounce_counters;
  41. static fast_timer_t last_time;
  42. static bool counters_need_update;
  43. # define DEBOUNCE_ELAPSED 0
  44. static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time);
  45. static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
  46. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  47. void debounce_init(uint8_t num_rows) {
  48. debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
  49. int i = 0;
  50. for (uint8_t r = 0; r < num_rows; r++) {
  51. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  52. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  53. }
  54. }
  55. }
  56. void debounce_free(void) {
  57. free(debounce_counters);
  58. debounce_counters = NULL;
  59. }
  60. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  61. bool updated_last = false;
  62. if (counters_need_update) {
  63. fast_timer_t now = timer_read_fast();
  64. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  65. last_time = now;
  66. updated_last = true;
  67. if (elapsed_time > UINT8_MAX) {
  68. elapsed_time = UINT8_MAX;
  69. }
  70. if (elapsed_time > 0) {
  71. update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
  72. }
  73. }
  74. if (changed) {
  75. if (!updated_last) {
  76. last_time = timer_read_fast();
  77. }
  78. start_debounce_counters(raw, cooked, num_rows);
  79. }
  80. }
  81. static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) {
  82. counters_need_update = false;
  83. debounce_counter_t *debounce_pointer = debounce_counters;
  84. for (uint8_t row = 0; row < num_rows; row++) {
  85. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  86. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  87. if (*debounce_pointer <= elapsed_time) {
  88. *debounce_pointer = DEBOUNCE_ELAPSED;
  89. cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
  90. } else {
  91. *debounce_pointer -= elapsed_time;
  92. counters_need_update = true;
  93. }
  94. }
  95. debounce_pointer++;
  96. }
  97. }
  98. }
  99. static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
  100. debounce_counter_t *debounce_pointer = debounce_counters;
  101. for (uint8_t row = 0; row < num_rows; row++) {
  102. matrix_row_t delta = raw[row] ^ cooked[row];
  103. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  104. if (delta & (ROW_SHIFTER << col)) {
  105. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  106. *debounce_pointer = DEBOUNCE;
  107. counters_need_update = true;
  108. }
  109. } else {
  110. *debounce_pointer = DEBOUNCE_ELAPSED;
  111. }
  112. debounce_pointer++;
  113. }
  114. }
  115. }
  116. bool debounce_active(void) { return true; }
  117. #else
  118. # include "none.c"
  119. #endif