sym_eager_pk.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2017 Alex Ong<the.onga@gmail.com>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /*
  15. Basic per-key algorithm. Uses an 8-bit counter per key.
  16. After pressing a key, it immediately changes state, and sets a counter.
  17. No further inputs are accepted until DEBOUNCE milliseconds have occurred.
  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. static bool matrix_need_update;
  36. #define DEBOUNCE_ELAPSED 251
  37. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  38. static uint8_t wrapping_timer_read(void) {
  39. static uint16_t time = 0;
  40. static uint8_t last_result = 0;
  41. uint16_t new_time = timer_read();
  42. uint16_t diff = new_time - time;
  43. time = new_time;
  44. last_result = (last_result + diff) % (MAX_DEBOUNCE + 1);
  45. return last_result;
  46. }
  47. void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
  48. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  49. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  50. void debounce_init(uint8_t num_rows) {
  51. debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
  52. int i = 0;
  53. for (uint8_t r = 0; r < num_rows; r++) {
  54. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  55. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  56. }
  57. }
  58. }
  59. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  60. uint8_t current_time = wrapping_timer_read();
  61. if (counters_need_update) {
  62. update_debounce_counters(num_rows, current_time);
  63. }
  64. if (changed || matrix_need_update) {
  65. transfer_matrix_values(raw, cooked, num_rows, current_time);
  66. }
  67. }
  68. // If the current time is > debounce counter, set the counter to enable input.
  69. void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
  70. counters_need_update = false;
  71. debounce_counter_t *debounce_pointer = debounce_counters;
  72. for (uint8_t row = 0; row < num_rows; row++) {
  73. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  74. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  75. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
  76. *debounce_pointer = DEBOUNCE_ELAPSED;
  77. } else {
  78. counters_need_update = true;
  79. }
  80. }
  81. debounce_pointer++;
  82. }
  83. }
  84. }
  85. // upload from raw_matrix to final matrix;
  86. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  87. matrix_need_update = false;
  88. debounce_counter_t *debounce_pointer = debounce_counters;
  89. for (uint8_t row = 0; row < num_rows; row++) {
  90. matrix_row_t delta = raw[row] ^ cooked[row];
  91. matrix_row_t existing_row = cooked[row];
  92. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  93. matrix_row_t col_mask = (ROW_SHIFTER << col);
  94. if (delta & col_mask) {
  95. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  96. *debounce_pointer = current_time;
  97. counters_need_update = true;
  98. existing_row ^= col_mask; // flip the bit.
  99. } else {
  100. matrix_need_update = true;
  101. }
  102. }
  103. debounce_pointer++;
  104. }
  105. cooked[row] = existing_row;
  106. }
  107. }
  108. bool debounce_active(void) { return true; }