sym_eager_pk.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 per-key algorithm. Uses an 8-bit counter per key.
  17. After pressing a key, it immediately changes state, and sets a counter.
  18. No further inputs are accepted until DEBOUNCE milliseconds have occurred.
  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 matrix_need_update;
  44. static bool cooked_changed;
  45. # define DEBOUNCE_ELAPSED 0
  46. static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time);
  47. static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows);
  48. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  49. void debounce_init(uint8_t num_rows) {
  50. debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
  51. int i = 0;
  52. for (uint8_t r = 0; r < num_rows; r++) {
  53. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  54. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  55. }
  56. }
  57. }
  58. void debounce_free(void) {
  59. free(debounce_counters);
  60. debounce_counters = NULL;
  61. }
  62. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  63. bool updated_last = false;
  64. cooked_changed = false;
  65. if (counters_need_update) {
  66. fast_timer_t now = timer_read_fast();
  67. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  68. last_time = now;
  69. updated_last = true;
  70. if (elapsed_time > UINT8_MAX) {
  71. elapsed_time = UINT8_MAX;
  72. }
  73. if (elapsed_time > 0) {
  74. update_debounce_counters(num_rows, elapsed_time);
  75. }
  76. }
  77. if (changed || matrix_need_update) {
  78. if (!updated_last) {
  79. last_time = timer_read_fast();
  80. }
  81. transfer_matrix_values(raw, cooked, num_rows);
  82. }
  83. return cooked_changed;
  84. }
  85. // If the current time is > debounce counter, set the counter to enable input.
  86. static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) {
  87. counters_need_update = false;
  88. matrix_need_update = false;
  89. debounce_counter_t *debounce_pointer = debounce_counters;
  90. for (uint8_t row = 0; row < num_rows; row++) {
  91. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  92. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  93. if (*debounce_pointer <= elapsed_time) {
  94. *debounce_pointer = DEBOUNCE_ELAPSED;
  95. matrix_need_update = true;
  96. } else {
  97. *debounce_pointer -= elapsed_time;
  98. counters_need_update = true;
  99. }
  100. }
  101. debounce_pointer++;
  102. }
  103. }
  104. }
  105. // upload from raw_matrix to final matrix;
  106. static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
  107. debounce_counter_t *debounce_pointer = debounce_counters;
  108. for (uint8_t row = 0; row < num_rows; row++) {
  109. matrix_row_t delta = raw[row] ^ cooked[row];
  110. matrix_row_t existing_row = cooked[row];
  111. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  112. matrix_row_t col_mask = (ROW_SHIFTER << col);
  113. if (delta & col_mask) {
  114. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  115. *debounce_pointer = DEBOUNCE;
  116. counters_need_update = true;
  117. existing_row ^= col_mask; // flip the bit.
  118. cooked_changed = true;
  119. }
  120. }
  121. debounce_pointer++;
  122. }
  123. cooked[row] = existing_row;
  124. }
  125. }
  126. #else
  127. # include "none.c"
  128. #endif