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.

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