eager_pr.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Copyright 2019 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-row algorithm. Uses an 8-bit counter per row.
  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. #define debounce_counter_t uint8_t
  27. static debounce_counter_t *debounce_counters;
  28. static bool counters_need_update;
  29. #define DEBOUNCE_ELAPSED 251
  30. #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1)
  31. void update_debounce_counters(uint8_t num_rows, uint8_t current_time);
  32. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time);
  33. // we use num_rows rather than MATRIX_ROWS to support split keyboards
  34. void debounce_init(uint8_t num_rows) {
  35. debounce_counters = (debounce_counter_t *)malloc(num_rows * sizeof(debounce_counter_t));
  36. for (uint8_t r = 0; r < num_rows; r++) {
  37. debounce_counters[r] = DEBOUNCE_ELAPSED;
  38. }
  39. }
  40. void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) {
  41. uint8_t current_time = timer_read() % MAX_DEBOUNCE;
  42. bool needed_update = counters_need_update;
  43. if (counters_need_update) {
  44. update_debounce_counters(num_rows, current_time);
  45. }
  46. if (changed || (needed_update && !counters_need_update)) {
  47. transfer_matrix_values(raw, cooked, num_rows, current_time);
  48. }
  49. }
  50. // If the current time is > debounce counter, set the counter to enable input.
  51. void update_debounce_counters(uint8_t num_rows, uint8_t current_time) {
  52. counters_need_update = false;
  53. debounce_counter_t *debounce_pointer = debounce_counters;
  54. for (uint8_t row = 0; row < num_rows; row++) {
  55. if (*debounce_pointer != DEBOUNCE_ELAPSED) {
  56. if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) {
  57. *debounce_pointer = DEBOUNCE_ELAPSED;
  58. } else {
  59. counters_need_update = true;
  60. }
  61. }
  62. debounce_pointer++;
  63. }
  64. }
  65. // upload from raw_matrix to final matrix;
  66. void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) {
  67. debounce_counter_t *debounce_pointer = debounce_counters;
  68. for (uint8_t row = 0; row < num_rows; row++) {
  69. matrix_row_t existing_row = cooked[row];
  70. matrix_row_t raw_row = raw[row];
  71. // determine new value basd on debounce pointer + raw value
  72. if (*debounce_pointer == DEBOUNCE_ELAPSED && (existing_row != raw_row)) {
  73. *debounce_pointer = current_time;
  74. cooked[row] = raw_row;
  75. counters_need_update = true;
  76. }
  77. debounce_pointer++;
  78. }
  79. }
  80. bool debounce_active(void) { return true; }