debounce_eager_pk.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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 matrix_row_t matrix_debounced[MATRIX_ROWS];
  34. static debounce_counter_t debounce_counters[MATRIX_ROWS*MATRIX_COLS];
  35. #define DEBOUNCE_ELAPSED 251
  36. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  37. void update_debounce_counters(uint8_t current_time);
  38. void transfer_matrix_values(uint8_t current_time);
  39. void matrix_debounce_init(void)
  40. {
  41. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  42. {
  43. matrix_debounced[r] = 0;
  44. }
  45. int i = 0;
  46. for (uint8_t r = 0; r < MATRIX_ROWS; r++)
  47. {
  48. for (uint8_t c = 0; c < MATRIX_COLS; c++)
  49. {
  50. debounce_counters[i++] = DEBOUNCE_ELAPSED;
  51. }
  52. }
  53. }
  54. void matrix_debounce(void)
  55. {
  56. uint8_t current_time = timer_read() % MAX_DEBOUNCE;
  57. update_debounce_counters(current_time);
  58. transfer_matrix_values(current_time);
  59. }
  60. //If the current time is > debounce counter, set the counter to enable input.
  61. void update_debounce_counters(uint8_t current_time)
  62. {
  63. debounce_counter_t *debounce_pointer = debounce_counters;
  64. for (uint8_t row = 0; row < MATRIX_ROWS; row++)
  65. {
  66. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  67. {
  68. if (*debounce_pointer != DEBOUNCE_ELAPSED)
  69. {
  70. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >=
  71. DEBOUNCING_DELAY) {
  72. *debounce_pointer = DEBOUNCE_ELAPSED;
  73. }
  74. }
  75. debounce_pointer++;
  76. }
  77. }
  78. }
  79. // upload from raw_matrix to final matrix;
  80. void transfer_matrix_values(uint8_t current_time)
  81. {
  82. debounce_counter_t *debounce_pointer = debounce_counters;
  83. for (uint8_t row = 0; row < MATRIX_ROWS; row++)
  84. {
  85. matrix_row_t existing_row = matrix_debounced[row];
  86. matrix_row_t raw_row = matrix_get_row(row);
  87. for (uint8_t col = 0; col < MATRIX_COLS; col++)
  88. {
  89. matrix_row_t col_mask = (ROW_SHIFTER << col);
  90. bool final_value = raw_row & col_mask;
  91. bool existing_value = existing_row & col_mask;
  92. if (*debounce_pointer == DEBOUNCE_ELAPSED &&
  93. (existing_value != final_value))
  94. {
  95. *debounce_pointer = current_time;
  96. existing_row ^= col_mask; //flip the bit.
  97. }
  98. debounce_pointer++;
  99. }
  100. matrix_debounced[row] = existing_row;
  101. }
  102. }
  103. //Implementation of no debounce.