debounce_eager_pk.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "debounce.h"
  20. #include "matrix.h"
  21. #include "timer.h"
  22. #ifndef DEBOUNCE
  23. #define DEBOUNCE 5
  24. #endif
  25. #if (MATRIX_COLS <= 8)
  26. # define ROW_SHIFTER ((uint8_t)1)
  27. #elif (MATRIX_COLS <= 16)
  28. # define ROW_SHIFTER ((uint16_t)1)
  29. #elif (MATRIX_COLS <= 32)
  30. # define ROW_SHIFTER ((uint32_t)1)
  31. #endif
  32. #define debounce_counter_t uint8_t
  33. static debounce_counter_t debounce_counters[MATRIX_ROWS*MATRIX_COLS];
  34. #define DEBOUNCE_ELAPSED 251
  35. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  36. void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
  37. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  38. void debounce_init(uint8_t num_rows)
  39. {
  40. int i = 0;
  41. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  42. {
  43. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  44. {
  45. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  46. }
  47. }
  48. }
  49. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed)
  50. {
  51. uint8_t current_time = timer_read() % MAX_DEBOUNCE;
  52. update_debounce_counters(num_rows, current_time);
  53. transfer_matrix_values(raw, cooked, num_rows, current_time);
  54. }
  55. //If the current time is > debounce counter, set the counter to enable input.
  56. void update_debounce_counters(uint8_t num_rows, uint8_t current_time)
  57. {
  58. debounce_counter_t *debounce_pointer = debounce_counters;
  59. for (uint8_t row = 0; row < num_rows; row++)
  60. {
  61. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  62. {
  63. if (*debounce_pointer != DEBOUNCE_ELAPSED)
  64. {
  65. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
  66. *debounce_pointer = DEBOUNCE_ELAPSED;
  67. }
  68. }
  69. debounce_pointer++;
  70. }
  71. }
  72. }
  73. // upload from raw_matrix to final matrix;
  74. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time)
  75. {
  76. debounce_counter_t *debounce_pointer = debounce_counters;
  77. for (uint8_t row = 0; row < num_rows; row++)
  78. {
  79. matrix_row_t existing_row = cooked[row];
  80. matrix_row_t raw_row = raw[row];
  81. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  82. {
  83. matrix_row_t col_mask = (ROW_SHIFTER << col);
  84. bool final_value = raw_row & col_mask;
  85. bool existing_value = existing_row & col_mask;
  86. if (*debounce_pointer == DEBOUNCE_ELAPSED &&
  87. (existing_value != final_value))
  88. {
  89. *debounce_pointer = current_time;
  90. existing_row ^= col_mask; //flip the bit.
  91. }
  92. debounce_pointer++;
  93. }
  94. cooked[row] = existing_row;
  95. }
  96. }
  97. bool debounce_active()
  98. {
  99. return true;
  100. }