sym_defer_pk.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. static bool cooked_changed;
  44. # define DEBOUNCE_ELAPSED 0
  45. 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);
  46. static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
  47. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  48. void debounce_init(uint8_t num_rows) {
  49. debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
  50. int i = 0;
  51. for (uint8_t r = 0; r < num_rows; r++) {
  52. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  53. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  54. }
  55. }
  56. }
  57. void debounce_free(void) {
  58. free(debounce_counters);
  59. debounce_counters = NULL;
  60. }
  61. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  62. bool updated_last = false;
  63. cooked_changed = false;
  64. if (counters_need_update) {
  65. fast_timer_t now = timer_read_fast();
  66. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  67. last_time = now;
  68. updated_last = true;
  69. if (elapsed_time > UINT8_MAX) {
  70. elapsed_time = UINT8_MAX;
  71. }
  72. if (elapsed_time > 0) {
  73. update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
  74. }
  75. }
  76. if (changed) {
  77. if (!updated_last) {
  78. last_time = timer_read_fast();
  79. }
  80. start_debounce_counters(raw, cooked, num_rows);
  81. }
  82. return cooked_changed;
  83. }
  84. 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) {
  85. counters_need_update = false;
  86. debounce_counter_t *debounce_pointer = debounce_counters;
  87. for (uint8_t row = 0; row < num_rows; row++) {
  88. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  89. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  90. if (*debounce_pointer <= elapsed_time) {
  91. *debounce_pointer = DEBOUNCE_ELAPSED;
  92. matrix_row_t cooked_next = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
  93. cooked_changed |= cooked[row] ^ cooked_next;
  94. cooked[row] = cooked_next;
  95. } else {
  96. *debounce_pointer -= elapsed_time;
  97. counters_need_update = true;
  98. }
  99. }
  100. debounce_pointer++;
  101. }
  102. }
  103. }
  104. static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
  105. debounce_counter_t *debounce_pointer = debounce_counters;
  106. for (uint8_t row = 0; row < num_rows; row++) {
  107. matrix_row_t delta = raw[row] ^ cooked[row];
  108. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  109. if (delta & (ROW_SHIFTER << col)) {
  110. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  111. *debounce_pointer = DEBOUNCE;
  112. counters_need_update = true;
  113. }
  114. } else {
  115. *debounce_pointer = DEBOUNCE_ELAPSED;
  116. }
  117. debounce_pointer++;
  118. }
  119. }
  120. }
  121. #else
  122. # include "none.c"
  123. #endif