diff options
Diffstat (limited to 'quantum/debounce/debounce_eager_pk.c')
| -rw-r--r-- | quantum/debounce/debounce_eager_pk.c | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/quantum/debounce/debounce_eager_pk.c b/quantum/debounce/debounce_eager_pk.c new file mode 100644 index 000000000..e6e8bfd31 --- /dev/null +++ b/quantum/debounce/debounce_eager_pk.c | |||
| @@ -0,0 +1,123 @@ | |||
| 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 | /* | ||
| 16 | Basic per-key algorithm. Uses an 8-bit counter per key. | ||
| 17 | After pressing a key, it immediately changes state, and sets a counter. | ||
| 18 | No further inputs are accepted until DEBOUNCE milliseconds have occurred. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "debounce.h" | ||
| 22 | #include "matrix.h" | ||
| 23 | #include "timer.h" | ||
| 24 | |||
| 25 | #ifndef DEBOUNCE | ||
| 26 | #define DEBOUNCE 5 | ||
| 27 | #endif | ||
| 28 | |||
| 29 | |||
| 30 | #if (MATRIX_COLS <= 8) | ||
| 31 | # define ROW_SHIFTER ((uint8_t)1) | ||
| 32 | #elif (MATRIX_COLS <= 16) | ||
| 33 | # define ROW_SHIFTER ((uint16_t)1) | ||
| 34 | #elif (MATRIX_COLS <= 32) | ||
| 35 | # define ROW_SHIFTER ((uint32_t)1) | ||
| 36 | #endif | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | #define debounce_counter_t uint8_t | ||
| 41 | |||
| 42 | static matrix_row_t matrix_debounced[MATRIX_ROWS]; | ||
| 43 | static debounce_counter_t debounce_counters[MATRIX_ROWS*MATRIX_COLS]; | ||
| 44 | |||
| 45 | #define DEBOUNCE_ELAPSED 251 | ||
| 46 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 47 | |||
| 48 | void update_debounce_counters(uint8_t current_time); | ||
| 49 | void transfer_matrix_values(uint8_t current_time); | ||
| 50 | |||
| 51 | void matrix_debounce_init(void) | ||
| 52 | { | ||
| 53 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 54 | { | ||
| 55 | matrix_debounced[r] = 0; | ||
| 56 | } | ||
| 57 | |||
| 58 | int i = 0; | ||
| 59 | for (uint8_t r = 0; r < MATRIX_ROWS; r++) | ||
| 60 | { | ||
| 61 | for (uint8_t c = 0; c < MATRIX_COLS; c++) | ||
| 62 | { | ||
| 63 | debounce_counters[i++] = DEBOUNCE_ELAPSED; | ||
| 64 | } | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | void matrix_debounce(void) | ||
| 69 | { | ||
| 70 | uint8_t current_time = timer_read() % MAX_DEBOUNCE; | ||
| 71 | update_debounce_counters(current_time); | ||
| 72 | transfer_matrix_values(current_time); | ||
| 73 | } | ||
| 74 | |||
| 75 | //If the current time is > debounce counter, set the counter to enable input. | ||
| 76 | void update_debounce_counters(uint8_t current_time) | ||
| 77 | { | ||
| 78 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 79 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) | ||
| 80 | { | ||
| 81 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
| 82 | { | ||
| 83 | if (*debounce_pointer != DEBOUNCE_ELAPSED) | ||
| 84 | { | ||
| 85 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= | ||
| 86 | DEBOUNCING_DELAY) { | ||
| 87 | *debounce_pointer = DEBOUNCE_ELAPSED; | ||
| 88 | } | ||
| 89 | } | ||
| 90 | debounce_pointer++; | ||
| 91 | } | ||
| 92 | } | ||
| 93 | } | ||
| 94 | |||
| 95 | // upload from raw_matrix to final matrix; | ||
| 96 | void transfer_matrix_values(uint8_t current_time) | ||
| 97 | { | ||
| 98 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 99 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) | ||
| 100 | { | ||
| 101 | matrix_row_t existing_row = matrix_debounced[row]; | ||
| 102 | matrix_row_t raw_row = matrix_get_row(row); | ||
| 103 | |||
| 104 | for (uint8_t col = 0; col < MATRIX_COLS; col++) | ||
| 105 | { | ||
| 106 | matrix_row_t col_mask = (ROW_SHIFTER << col); | ||
| 107 | bool final_value = raw_row & col_mask; | ||
| 108 | bool existing_value = existing_row & col_mask; | ||
| 109 | if (*debounce_pointer == DEBOUNCE_ELAPSED && | ||
| 110 | (existing_value != final_value)) | ||
| 111 | { | ||
| 112 | *debounce_pointer = current_time; | ||
| 113 | existing_row ^= col_mask; //flip the bit. | ||
| 114 | } | ||
| 115 | debounce_pointer++; | ||
| 116 | } | ||
| 117 | matrix_debounced[row] = existing_row; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | |||
| 122 | |||
| 123 | //Implementation of no debounce. | ||
