You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.5 KiB

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