logo

qmk_firmware

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

sym_defer_pk.c (4938B)


  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 "debounce.h"
  21. #include "timer.h"
  22. #include <stdlib.h>
  23. #ifdef PROTOCOL_CHIBIOS
  24. # if CH_CFG_USE_MEMCORE == FALSE
  25. # 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.
  26. # endif
  27. #endif
  28. #ifndef DEBOUNCE
  29. # define DEBOUNCE 5
  30. #endif
  31. // Maximum debounce: 255ms
  32. #if DEBOUNCE > UINT8_MAX
  33. # undef DEBOUNCE
  34. # define DEBOUNCE UINT8_MAX
  35. #endif
  36. #define ROW_SHIFTER ((matrix_row_t)1)
  37. typedef uint8_t debounce_counter_t;
  38. #if DEBOUNCE > 0
  39. static debounce_counter_t *debounce_counters;
  40. static fast_timer_t last_time;
  41. static bool counters_need_update;
  42. static bool cooked_changed;
  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. bool debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  61. bool updated_last = false;
  62. cooked_changed = false;
  63. if (counters_need_update) {
  64. fast_timer_t now = timer_read_fast();
  65. fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time);
  66. last_time = now;
  67. updated_last = true;
  68. if (elapsed_time > UINT8_MAX) {
  69. elapsed_time = UINT8_MAX;
  70. }
  71. if (elapsed_time > 0) {
  72. update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time);
  73. }
  74. }
  75. if (changed) {
  76. if (!updated_last) {
  77. last_time = timer_read_fast();
  78. }
  79. start_debounce_counters(raw, cooked, num_rows);
  80. }
  81. return cooked_changed;
  82. }
  83. 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) {
  84. counters_need_update = false;
  85. debounce_counter_t *debounce_pointer = debounce_counters;
  86. for (uint8_t row = 0; row < num_rows; row++) {
  87. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  88. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  89. if (*debounce_pointer <= elapsed_time) {
  90. *debounce_pointer = DEBOUNCE_ELAPSED;
  91. matrix_row_t cooked_next = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
  92. cooked_changed |= cooked[row] ^ cooked_next;
  93. cooked[row] = cooked_next;
  94. } else {
  95. *debounce_pointer -= elapsed_time;
  96. counters_need_update = true;
  97. }
  98. }
  99. debounce_pointer++;
  100. }
  101. }
  102. }
  103. static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) {
  104. debounce_counter_t *debounce_pointer = debounce_counters;
  105. for (uint8_t row = 0; row < num_rows; row++) {
  106. matrix_row_t delta = raw[row] ^ cooked[row];
  107. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  108. if (delta & (ROW_SHIFTER << col)) {
  109. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  110. *debounce_pointer = DEBOUNCE;
  111. counters_need_update = true;
  112. }
  113. } else {
  114. *debounce_pointer = DEBOUNCE_ELAPSED;
  115. }
  116. debounce_pointer++;
  117. }
  118. }
  119. }
  120. #else
  121. # include "none.c"
  122. #endif