sym_defer_pk.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. Copyright 2017 Alex Ong<the.onga@gmail.com>
  3. Copyright 2020 Andrei Purdea<andrei@purdea.ro>
  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 symmetric per-key algorithm. Uses an 8-bit counter per key.
  17. When no state changes have occured for DEBOUNCE milliseconds, we push the state.
  18. */
  19. #include "matrix.h"
  20. #include "timer.h"
  21. #include "quantum.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. #define ROW_SHIFTER ((matrix_row_t)1)
  32. #define debounce_counter_t uint8_t
  33. static debounce_counter_t *debounce_counters;
  34. static bool counters_need_update;
  35. #define DEBOUNCE_ELAPSED 251
  36. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  37. static uint8_t wrapping_timer_read(void) {
  38. static uint16_t time = 0;
  39. static uint8_t last_result = 0;
  40. uint16_t new_time = timer_read();
  41. uint16_t diff = new_time - time;
  42. time = new_time;
  43. last_result = (last_result + diff) % (MAX_DEBOUNCE + 1);
  44. return last_result;
  45. }
  46. void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  47. void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  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(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  59. uint8_t current_time = wrapping_timer_read();
  60. if (counters_need_update) {
  61. update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, current_time);
  62. }
  63. if (changed) {
  64. start_debounce_counters(raw, cooked, num_rows, current_time);
  65. }
  66. }
  67. void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  68. counters_need_update = false;
  69. debounce_counter_t *debounce_pointer = debounce_counters;
  70. for (uint8_t row = 0; row < num_rows; row++) {
  71. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  72. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  73. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
  74. *debounce_pointer = DEBOUNCE_ELAPSED;
  75. cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
  76. } else {
  77. counters_need_update = true;
  78. }
  79. }
  80. debounce_pointer++;
  81. }
  82. }
  83. }
  84. void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  85. debounce_counter_t *debounce_pointer = debounce_counters;
  86. for (uint8_t row = 0; row < num_rows; row++) {
  87. matrix_row_t delta = raw[row] ^ cooked[row];
  88. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  89. if (delta & (ROW_SHIFTER << col)) {
  90. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  91. *debounce_pointer = current_time;
  92. counters_need_update = true;
  93. }
  94. } else {
  95. *debounce_pointer = DEBOUNCE_ELAPSED;
  96. }
  97. debounce_pointer++;
  98. }
  99. }
  100. }
  101. bool debounce_active(void) { return true; }