sym_pk.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #ifndef DEBOUNCE
  24. # define DEBOUNCE 5
  25. #endif
  26. #define ROW_SHIFTER ((matrix_row_t)1)
  27. #define debounce_counter_t uint8_t
  28. static debounce_counter_t *debounce_counters;
  29. static bool counters_need_update;
  30. #define DEBOUNCE_ELAPSED 251
  31. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  32. static uint8_t wrapping_timer_read(void) {
  33. static uint16_t time = 0;
  34. static uint8_t last_result = 0;
  35. uint16_t new_time = timer_read();
  36. uint16_t diff = new_time - time;
  37. time = new_time;
  38. last_result = (last_result + diff) % (MAX_DEBOUNCE + 1);
  39. return last_result;
  40. }
  41. void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  42. void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  43. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  44. void debounce_init(uint8_t num_rows) {
  45. debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t));
  46. int i = 0;
  47. for (uint8_t r = 0; r < num_rows; r++) {
  48. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  49. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  50. }
  51. }
  52. }
  53. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  54. uint8_t current_time = wrapping_timer_read();
  55. if (counters_need_update) {
  56. update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, current_time);
  57. }
  58. if (changed) {
  59. start_debounce_counters(raw, cooked, num_rows, current_time);
  60. }
  61. }
  62. void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  63. counters_need_update = false;
  64. debounce_counter_t *debounce_pointer = debounce_counters;
  65. for (uint8_t row = 0; row < num_rows; row++) {
  66. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  67. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  68. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
  69. *debounce_pointer = DEBOUNCE_ELAPSED;
  70. cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col));
  71. } else {
  72. counters_need_update = true;
  73. }
  74. }
  75. debounce_pointer++;
  76. }
  77. }
  78. }
  79. void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  80. debounce_counter_t *debounce_pointer = debounce_counters;
  81. for (uint8_t row = 0; row < num_rows; row++) {
  82. matrix_row_t delta = raw[row] ^ cooked[row];
  83. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  84. if (delta & (ROW_SHIFTER << col)) {
  85. if (*debounce_pointer == DEBOUNCE_ELAPSED) {
  86. *debounce_pointer = current_time;
  87. counters_need_update = true;
  88. }
  89. } else {
  90. *debounce_pointer = DEBOUNCE_ELAPSED;
  91. }
  92. debounce_pointer++;
  93. }
  94. }
  95. }
  96. bool debounce_active(void) { return true; }