diff options
| author | Purdea Andrei <andrei@purdea.ro> | 2020-04-11 14:29:48 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-11 21:29:48 +1000 |
| commit | b8c3f4c60b1d58cb8261b96c76e1f4cd03c1b0f8 (patch) | |
| tree | b9f11cd751d3537a653d4931955ca17e0460395f /quantum/debounce | |
| parent | 511fe643c2ac902e8d42f87bffccdf4aaab700d1 (diff) | |
| download | qmk_firmware-b8c3f4c60b1d58cb8261b96c76e1f4cd03c1b0f8.tar.gz qmk_firmware-b8c3f4c60b1d58cb8261b96c76e1f4cd03c1b0f8.zip | |
quantum/debounce: Added sym_pk debounce algorithm (#8587)
* quantum/debounce: Added sym_pk debounce algorithm
* Apply suggestions from code review
Co-Authored-By: Ryan <fauxpark@gmail.com>
* quantum/debounce/sym_pk: delete comments and rename functions following code review
* quantum/debounce/sym_pk: Modifications for code readability according to code review
* quantum/debounce/sym_pk: Modifications for code readability according to code review (2)
* quantum/debounce/sym_pk: code review: cleaner code
Co-Authored-By: Nick Brassel <nick@tzarc.org>
Co-authored-by: Ryan <fauxpark@gmail.com>
Co-authored-by: Nick Brassel <nick@tzarc.org>
Diffstat (limited to 'quantum/debounce')
| -rw-r--r-- | quantum/debounce/eager_pk.c | 8 | ||||
| -rw-r--r-- | quantum/debounce/sym_pk.c | 111 |
2 files changed, 112 insertions, 7 deletions
diff --git a/quantum/debounce/eager_pk.c b/quantum/debounce/eager_pk.c index 6fa956bf3..93a40ad44 100644 --- a/quantum/debounce/eager_pk.c +++ b/quantum/debounce/eager_pk.c | |||
| @@ -27,13 +27,7 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred. | |||
| 27 | # define DEBOUNCE 5 | 27 | # define DEBOUNCE 5 |
| 28 | #endif | 28 | #endif |
| 29 | 29 | ||
| 30 | #if (MATRIX_COLS <= 8) | 30 | #define ROW_SHIFTER ((matrix_row_t)1) |
| 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 | 31 | ||
| 38 | #define debounce_counter_t uint8_t | 32 | #define debounce_counter_t uint8_t |
| 39 | 33 | ||
diff --git a/quantum/debounce/sym_pk.c b/quantum/debounce/sym_pk.c new file mode 100644 index 000000000..f404cf9c4 --- /dev/null +++ b/quantum/debounce/sym_pk.c | |||
| @@ -0,0 +1,111 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | ||
| 3 | Copyright 2020 Andrei Purdea<andrei@purdea.ro> | ||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | This program is distributed in the hope that it will be useful, | ||
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 11 | GNU General Public License for more details. | ||
| 12 | You should have received a copy of the GNU General Public License | ||
| 13 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 14 | */ | ||
| 15 | |||
| 16 | /* | ||
| 17 | Basic symmetric per-key algorithm. Uses an 8-bit counter per key. | ||
| 18 | When no state changes have occured for DEBOUNCE milliseconds, we push the state. | ||
| 19 | */ | ||
| 20 | |||
| 21 | #include "matrix.h" | ||
| 22 | #include "timer.h" | ||
| 23 | #include "quantum.h" | ||
| 24 | #include <stdlib.h> | ||
| 25 | |||
| 26 | #ifndef DEBOUNCE | ||
| 27 | # define DEBOUNCE 5 | ||
| 28 | #endif | ||
| 29 | |||
| 30 | #define ROW_SHIFTER ((matrix_row_t)1) | ||
| 31 | |||
| 32 | #define debounce_counter_t uint8_t | ||
| 33 | |||
| 34 | static debounce_counter_t *debounce_counters; | ||
| 35 | static bool counters_need_update; | ||
| 36 | |||
| 37 | #define DEBOUNCE_ELAPSED 251 | ||
| 38 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 39 | |||
| 40 | static uint8_t wrapping_timer_read(void) { | ||
| 41 | static uint16_t time = 0; | ||
| 42 | static uint8_t last_result = 0; | ||
| 43 | uint16_t new_time = timer_read(); | ||
| 44 | uint16_t diff = new_time - time; | ||
| 45 | time = new_time; | ||
| 46 | last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); | ||
| 47 | return last_result; | ||
| 48 | } | ||
| 49 | |||
| 50 | void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | ||
| 51 | void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | ||
| 52 | |||
| 53 | // we use num_rows rather than MATRIX_ROWS to support split keyboards | ||
| 54 | void debounce_init(uint8_t num_rows) { | ||
| 55 | debounce_counters = (debounce_counter_t *)malloc(num_rows * MATRIX_COLS * sizeof(debounce_counter_t)); | ||
| 56 | int i = 0; | ||
| 57 | for (uint8_t r = 0; r < num_rows; r++) { | ||
| 58 | for (uint8_t c = 0; c < MATRIX_COLS; c++) { | ||
| 59 | debounce_counters[i++] = DEBOUNCE_ELAPSED; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | } | ||
| 63 | |||
| 64 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | ||
| 65 | uint8_t current_time = wrapping_timer_read(); | ||
| 66 | if (counters_need_update) { | ||
| 67 | update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, current_time); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (changed) { | ||
| 71 | start_debounce_counters(raw, cooked, num_rows, current_time); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | |||
| 75 | void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | ||
| 76 | counters_need_update = false; | ||
| 77 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 78 | for (uint8_t row = 0; row < num_rows; row++) { | ||
| 79 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
| 80 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { | ||
| 81 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { | ||
| 82 | *debounce_pointer = DEBOUNCE_ELAPSED; | ||
| 83 | cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col)); | ||
| 84 | } else { | ||
| 85 | counters_need_update = true; | ||
| 86 | } | ||
| 87 | } | ||
| 88 | debounce_pointer++; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | ||
| 94 | debounce_counter_t *debounce_pointer = debounce_counters; | ||
| 95 | for (uint8_t row = 0; row < num_rows; row++) { | ||
| 96 | matrix_row_t delta = raw[row] ^ cooked[row]; | ||
| 97 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | ||
| 98 | if (delta & (ROW_SHIFTER << col)) { | ||
| 99 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { | ||
| 100 | *debounce_pointer = current_time; | ||
| 101 | counters_need_update = true; | ||
| 102 | } | ||
| 103 | } else { | ||
| 104 | *debounce_pointer = DEBOUNCE_ELAPSED; | ||
| 105 | } | ||
| 106 | debounce_pointer++; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | bool debounce_active(void) { return true; } | ||
