diff options
| author | Simon Arlott <70171+nomis@users.noreply.github.com> | 2021-06-09 08:23:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-09 17:23:21 +1000 |
| commit | b829a1d2648abd0751e9d3505554547faf643425 (patch) | |
| tree | 35ba811a62feffd88759d0a53c1d6353d5d6d88a | |
| parent | f287597c19dc926c7c28cefa32ffc3f0b4fdba7f (diff) | |
| download | qmk_firmware-b829a1d2648abd0751e9d3505554547faf643425.tar.gz qmk_firmware-b829a1d2648abd0751e9d3505554547faf643425.zip | |
Avoid 8-bit timer overflows in debounce algorithms (#12240)
* Add fast_timer_t that is 16-bit or 32-bit based on architecture
A 16-bit timer will overflow sooner but be faster to compare on AVR.
* Avoid 8-bit timer overflows in debounce algorithms
Count down remaining elapsed time instead of trying to do 8-bit timer
comparisons.
Add a "none" implementation that is automatically used if DEBOUNCE is
0 otherwise it will break the _pk/_pr count down.
* Avoid unnecessary polling of the entire matrix in sym_eager_pk
The matrix only needs to be updated when a debounce timer expires.
* Avoid unnecessary polling of the entire matrix in sym_eager_pr
The matrix only needs to be updated when a debounce timer expires.
The use of the "needed_update" variable is trying to do what
"matrix_need_update" was added to fix but didn't work because it only
applied when all keys finished debouncing.
* Fix sym_defer_g timing inconsistency compared to other debounce algorithms
DEBOUNCE=5 should process the key after 5ms, not 6ms
* Add debounce tests
| -rw-r--r-- | build_test.mk | 1 | ||||
| -rw-r--r-- | quantum/debounce.h | 2 | ||||
| -rw-r--r-- | quantum/debounce/none.c | 31 | ||||
| -rw-r--r-- | quantum/debounce/sym_defer_g.c | 26 | ||||
| -rw-r--r-- | quantum/debounce/sym_defer_pk.c | 67 | ||||
| -rw-r--r-- | quantum/debounce/sym_eager_pk.c | 72 | ||||
| -rw-r--r-- | quantum/debounce/sym_eager_pr.c | 76 | ||||
| -rw-r--r-- | quantum/debounce/tests/debounce_test_common.cpp | 229 | ||||
| -rw-r--r-- | quantum/debounce/tests/debounce_test_common.h | 83 | ||||
| -rw-r--r-- | quantum/debounce/tests/rules.mk | 39 | ||||
| -rw-r--r-- | quantum/debounce/tests/sym_defer_g_tests.cpp | 223 | ||||
| -rw-r--r-- | quantum/debounce/tests/sym_defer_pk_tests.cpp | 225 | ||||
| -rw-r--r-- | quantum/debounce/tests/sym_eager_pk_tests.cpp | 237 | ||||
| -rw-r--r-- | quantum/debounce/tests/sym_eager_pr_tests.cpp | 280 | ||||
| -rw-r--r-- | quantum/debounce/tests/testlist.mk | 5 | ||||
| -rw-r--r-- | testlist.mk | 1 | ||||
| -rw-r--r-- | tmk_core/common/arm_atsam/_timer.h | 19 | ||||
| -rw-r--r-- | tmk_core/common/avr/_timer.h | 19 | ||||
| -rw-r--r-- | tmk_core/common/chibios/_timer.h | 19 | ||||
| -rw-r--r-- | tmk_core/common/timer.h | 24 |
20 files changed, 1587 insertions, 91 deletions
diff --git a/build_test.mk b/build_test.mk index 77c4265f9..5171c58c3 100644 --- a/build_test.mk +++ b/build_test.mk | |||
| @@ -49,6 +49,7 @@ endif | |||
| 49 | 49 | ||
| 50 | include common_features.mk | 50 | include common_features.mk |
| 51 | include $(TMK_PATH)/common.mk | 51 | include $(TMK_PATH)/common.mk |
| 52 | include $(QUANTUM_PATH)/debounce/tests/rules.mk | ||
| 52 | include $(QUANTUM_PATH)/sequencer/tests/rules.mk | 53 | include $(QUANTUM_PATH)/sequencer/tests/rules.mk |
| 53 | include $(QUANTUM_PATH)/serial_link/tests/rules.mk | 54 | include $(QUANTUM_PATH)/serial_link/tests/rules.mk |
| 54 | ifneq ($(filter $(FULL_TESTS),$(TEST)),) | 55 | ifneq ($(filter $(FULL_TESTS),$(TEST)),) |
diff --git a/quantum/debounce.h b/quantum/debounce.h index 9ca05c682..504386828 100644 --- a/quantum/debounce.h +++ b/quantum/debounce.h | |||
| @@ -9,3 +9,5 @@ void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool | |||
| 9 | bool debounce_active(void); | 9 | bool debounce_active(void); |
| 10 | 10 | ||
| 11 | void debounce_init(uint8_t num_rows); | 11 | void debounce_init(uint8_t num_rows); |
| 12 | |||
| 13 | void debounce_free(void); | ||
diff --git a/quantum/debounce/none.c b/quantum/debounce/none.c new file mode 100644 index 000000000..b03892bc5 --- /dev/null +++ b/quantum/debounce/none.c | |||
| @@ -0,0 +1,31 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "matrix.h" | ||
| 18 | #include "quantum.h" | ||
| 19 | #include <stdlib.h> | ||
| 20 | |||
| 21 | void debounce_init(uint8_t num_rows) {} | ||
| 22 | |||
| 23 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | ||
| 24 | for (int i = 0; i < num_rows; i++) { | ||
| 25 | cooked[i] = raw[i]; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | bool debounce_active(void) { return false; } | ||
| 30 | |||
| 31 | void debounce_free(void) {} | ||
diff --git a/quantum/debounce/sym_defer_g.c b/quantum/debounce/sym_defer_g.c index 3ed9055d2..fbefd55ed 100644 --- a/quantum/debounce/sym_defer_g.c +++ b/quantum/debounce/sym_defer_g.c | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> |
| 3 | Copyright 2021 Simon Arlott | ||
| 3 | This program is free software: you can redistribute it and/or modify | 4 | 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 | 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 | the Free Software Foundation, either version 2 of the License, or |
| @@ -23,30 +24,29 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state. | |||
| 23 | # define DEBOUNCE 5 | 24 | # define DEBOUNCE 5 |
| 24 | #endif | 25 | #endif |
| 25 | 26 | ||
| 26 | void debounce_init(uint8_t num_rows) {} | 27 | #if DEBOUNCE > 0 |
| 27 | static bool debouncing = false; | 28 | static bool debouncing = false; |
| 29 | static fast_timer_t debouncing_time; | ||
| 28 | 30 | ||
| 29 | #if DEBOUNCE > 0 | 31 | void debounce_init(uint8_t num_rows) {} |
| 30 | static uint16_t debouncing_time; | 32 | |
| 31 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | 33 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { |
| 32 | if (changed) { | 34 | if (changed) { |
| 33 | debouncing = true; | 35 | debouncing = true; |
| 34 | debouncing_time = timer_read(); | 36 | debouncing_time = timer_read_fast(); |
| 35 | } | 37 | } |
| 36 | 38 | ||
| 37 | if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCE) { | 39 | if (debouncing && timer_elapsed_fast(debouncing_time) >= DEBOUNCE) { |
| 38 | for (int i = 0; i < num_rows; i++) { | 40 | for (int i = 0; i < num_rows; i++) { |
| 39 | cooked[i] = raw[i]; | 41 | cooked[i] = raw[i]; |
| 40 | } | 42 | } |
| 41 | debouncing = false; | 43 | debouncing = false; |
| 42 | } | 44 | } |
| 43 | } | 45 | } |
| 44 | #else // no debouncing. | ||
| 45 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | ||
| 46 | for (int i = 0; i < num_rows; i++) { | ||
| 47 | cooked[i] = raw[i]; | ||
| 48 | } | ||
| 49 | } | ||
| 50 | #endif | ||
| 51 | 46 | ||
| 52 | bool debounce_active(void) { return debouncing; } | 47 | bool debounce_active(void) { return debouncing; } |
| 48 | |||
| 49 | void debounce_free(void) {} | ||
| 50 | #else // no debouncing. | ||
| 51 | # include "none.c" | ||
| 52 | #endif | ||
diff --git a/quantum/debounce/sym_defer_pk.c b/quantum/debounce/sym_defer_pk.c index 60513f98e..626a9be84 100644 --- a/quantum/debounce/sym_defer_pk.c +++ b/quantum/debounce/sym_defer_pk.c | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> |
| 3 | Copyright 2020 Andrei Purdea<andrei@purdea.ro> | 3 | Copyright 2020 Andrei Purdea<andrei@purdea.ro> |
| 4 | Copyright 2021 Simon Arlott | ||
| 4 | This program is free software: you can redistribute it and/or modify | 5 | 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 | 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 | the Free Software Foundation, either version 2 of the License, or |
| @@ -33,28 +34,25 @@ When no state changes have occured for DEBOUNCE milliseconds, we push the state. | |||
| 33 | # define DEBOUNCE 5 | 34 | # define DEBOUNCE 5 |
| 34 | #endif | 35 | #endif |
| 35 | 36 | ||
| 37 | // Maximum debounce: 255ms | ||
| 38 | #if DEBOUNCE > UINT8_MAX | ||
| 39 | # undef DEBOUNCE | ||
| 40 | # define DEBOUNCE UINT8_MAX | ||
| 41 | #endif | ||
| 42 | |||
| 36 | #define ROW_SHIFTER ((matrix_row_t)1) | 43 | #define ROW_SHIFTER ((matrix_row_t)1) |
| 37 | 44 | ||
| 38 | #define debounce_counter_t uint8_t | 45 | typedef uint8_t debounce_counter_t; |
| 39 | 46 | ||
| 47 | #if DEBOUNCE > 0 | ||
| 40 | static debounce_counter_t *debounce_counters; | 48 | static debounce_counter_t *debounce_counters; |
| 49 | static fast_timer_t last_time; | ||
| 41 | static bool counters_need_update; | 50 | static bool counters_need_update; |
| 42 | 51 | ||
| 43 | #define DEBOUNCE_ELAPSED 251 | 52 | #define DEBOUNCE_ELAPSED 0 |
| 44 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 45 | |||
| 46 | static uint8_t wrapping_timer_read(void) { | ||
| 47 | static uint16_t time = 0; | ||
| 48 | static uint8_t last_result = 0; | ||
| 49 | uint16_t new_time = timer_read(); | ||
| 50 | uint16_t diff = new_time - time; | ||
| 51 | time = new_time; | ||
| 52 | last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); | ||
| 53 | return last_result; | ||
| 54 | } | ||
| 55 | 53 | ||
| 56 | void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | 54 | static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time); |
| 57 | void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | 55 | static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows); |
| 58 | 56 | ||
| 59 | // we use num_rows rather than MATRIX_ROWS to support split keyboards | 57 | // we use num_rows rather than MATRIX_ROWS to support split keyboards |
| 60 | void debounce_init(uint8_t num_rows) { | 58 | void debounce_init(uint8_t num_rows) { |
| @@ -67,27 +65,49 @@ void debounce_init(uint8_t num_rows) { | |||
| 67 | } | 65 | } |
| 68 | } | 66 | } |
| 69 | 67 | ||
| 68 | void debounce_free(void) { | ||
| 69 | free(debounce_counters); | ||
| 70 | debounce_counters = NULL; | ||
| 71 | } | ||
| 72 | |||
| 70 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | 73 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { |
| 71 | uint8_t current_time = wrapping_timer_read(); | 74 | bool updated_last = false; |
| 75 | |||
| 72 | if (counters_need_update) { | 76 | if (counters_need_update) { |
| 73 | update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, current_time); | 77 | fast_timer_t now = timer_read_fast(); |
| 78 | fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time); | ||
| 79 | |||
| 80 | last_time = now; | ||
| 81 | updated_last = true; | ||
| 82 | if (elapsed_time > UINT8_MAX) { | ||
| 83 | elapsed_time = UINT8_MAX; | ||
| 84 | } | ||
| 85 | |||
| 86 | if (elapsed_time > 0) { | ||
| 87 | update_debounce_counters_and_transfer_if_expired(raw, cooked, num_rows, elapsed_time); | ||
| 88 | } | ||
| 74 | } | 89 | } |
| 75 | 90 | ||
| 76 | if (changed) { | 91 | if (changed) { |
| 77 | start_debounce_counters(raw, cooked, num_rows, current_time); | 92 | if (!updated_last) { |
| 93 | last_time = timer_read_fast(); | ||
| 94 | } | ||
| 95 | |||
| 96 | start_debounce_counters(raw, cooked, num_rows); | ||
| 78 | } | 97 | } |
| 79 | } | 98 | } |
| 80 | 99 | ||
| 81 | void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | 100 | static void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t elapsed_time) { |
| 82 | counters_need_update = false; | 101 | counters_need_update = false; |
| 83 | debounce_counter_t *debounce_pointer = debounce_counters; | 102 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 84 | for (uint8_t row = 0; row < num_rows; row++) { | 103 | for (uint8_t row = 0; row < num_rows; row++) { |
| 85 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | 104 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
| 86 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { | 105 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { |
| 87 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { | 106 | if (*debounce_pointer <= elapsed_time) { |
| 88 | *debounce_pointer = DEBOUNCE_ELAPSED; | 107 | *debounce_pointer = DEBOUNCE_ELAPSED; |
| 89 | cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col)); | 108 | cooked[row] = (cooked[row] & ~(ROW_SHIFTER << col)) | (raw[row] & (ROW_SHIFTER << col)); |
| 90 | } else { | 109 | } else { |
| 110 | *debounce_pointer -= elapsed_time; | ||
| 91 | counters_need_update = true; | 111 | counters_need_update = true; |
| 92 | } | 112 | } |
| 93 | } | 113 | } |
| @@ -96,14 +116,14 @@ void update_debounce_counters_and_transfer_if_expired(matrix_row_t raw[], matrix | |||
| 96 | } | 116 | } |
| 97 | } | 117 | } |
| 98 | 118 | ||
| 99 | void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | 119 | static void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { |
| 100 | debounce_counter_t *debounce_pointer = debounce_counters; | 120 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 101 | for (uint8_t row = 0; row < num_rows; row++) { | 121 | for (uint8_t row = 0; row < num_rows; row++) { |
| 102 | matrix_row_t delta = raw[row] ^ cooked[row]; | 122 | matrix_row_t delta = raw[row] ^ cooked[row]; |
| 103 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | 123 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
| 104 | if (delta & (ROW_SHIFTER << col)) { | 124 | if (delta & (ROW_SHIFTER << col)) { |
| 105 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { | 125 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { |
| 106 | *debounce_pointer = current_time; | 126 | *debounce_pointer = DEBOUNCE; |
| 107 | counters_need_update = true; | 127 | counters_need_update = true; |
| 108 | } | 128 | } |
| 109 | } else { | 129 | } else { |
| @@ -115,3 +135,6 @@ void start_debounce_counters(matrix_row_t raw[], matrix_row_t cooked[], uint8_t | |||
| 115 | } | 135 | } |
| 116 | 136 | ||
| 117 | bool debounce_active(void) { return true; } | 137 | bool debounce_active(void) { return true; } |
| 138 | #else | ||
| 139 | # include "none.c" | ||
| 140 | #endif | ||
diff --git a/quantum/debounce/sym_eager_pk.c b/quantum/debounce/sym_eager_pk.c index e66cf92d7..15a3242e6 100644 --- a/quantum/debounce/sym_eager_pk.c +++ b/quantum/debounce/sym_eager_pk.c | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> | 2 | Copyright 2017 Alex Ong<the.onga@gmail.com> |
| 3 | Copyright 2021 Simon Arlott | ||
| 3 | This program is free software: you can redistribute it and/or modify | 4 | 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 | 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 | the Free Software Foundation, either version 2 of the License, or |
| @@ -33,29 +34,26 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred. | |||
| 33 | # define DEBOUNCE 5 | 34 | # define DEBOUNCE 5 |
| 34 | #endif | 35 | #endif |
| 35 | 36 | ||
| 37 | // Maximum debounce: 255ms | ||
| 38 | #if DEBOUNCE > UINT8_MAX | ||
| 39 | # undef DEBOUNCE | ||
| 40 | # define DEBOUNCE UINT8_MAX | ||
| 41 | #endif | ||
| 42 | |||
| 36 | #define ROW_SHIFTER ((matrix_row_t)1) | 43 | #define ROW_SHIFTER ((matrix_row_t)1) |
| 37 | 44 | ||
| 38 | #define debounce_counter_t uint8_t | 45 | typedef uint8_t debounce_counter_t; |
| 39 | 46 | ||
| 47 | #if DEBOUNCE > 0 | ||
| 40 | static debounce_counter_t *debounce_counters; | 48 | static debounce_counter_t *debounce_counters; |
| 49 | static fast_timer_t last_time; | ||
| 41 | static bool counters_need_update; | 50 | static bool counters_need_update; |
| 42 | static bool matrix_need_update; | 51 | static bool matrix_need_update; |
| 43 | 52 | ||
| 44 | #define DEBOUNCE_ELAPSED 251 | 53 | #define DEBOUNCE_ELAPSED 0 |
| 45 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 46 | |||
| 47 | static uint8_t wrapping_timer_read(void) { | ||
| 48 | static uint16_t time = 0; | ||
| 49 | static uint8_t last_result = 0; | ||
| 50 | uint16_t new_time = timer_read(); | ||
| 51 | uint16_t diff = new_time - time; | ||
| 52 | time = new_time; | ||
| 53 | last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); | ||
| 54 | return last_result; | ||
| 55 | } | ||
| 56 | 54 | ||
| 57 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time); | 55 | static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time); |
| 58 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | 56 | static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows); |
| 59 | 57 | ||
| 60 | // we use num_rows rather than MATRIX_ROWS to support split keyboards | 58 | // we use num_rows rather than MATRIX_ROWS to support split keyboards |
| 61 | void debounce_init(uint8_t num_rows) { | 59 | void debounce_init(uint8_t num_rows) { |
| @@ -68,27 +66,51 @@ void debounce_init(uint8_t num_rows) { | |||
| 68 | } | 66 | } |
| 69 | } | 67 | } |
| 70 | 68 | ||
| 69 | void debounce_free(void) { | ||
| 70 | free(debounce_counters); | ||
| 71 | debounce_counters = NULL; | ||
| 72 | } | ||
| 73 | |||
| 71 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | 74 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { |
| 72 | uint8_t current_time = wrapping_timer_read(); | 75 | bool updated_last = false; |
| 76 | |||
| 73 | if (counters_need_update) { | 77 | if (counters_need_update) { |
| 74 | update_debounce_counters(num_rows, current_time); | 78 | fast_timer_t now = timer_read_fast(); |
| 79 | fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time); | ||
| 80 | |||
| 81 | last_time = now; | ||
| 82 | updated_last = true; | ||
| 83 | if (elapsed_time > UINT8_MAX) { | ||
| 84 | elapsed_time = UINT8_MAX; | ||
| 85 | } | ||
| 86 | |||
| 87 | if (elapsed_time > 0) { | ||
| 88 | update_debounce_counters(num_rows, elapsed_time); | ||
| 89 | } | ||
| 75 | } | 90 | } |
| 76 | 91 | ||
| 77 | if (changed || matrix_need_update) { | 92 | if (changed || matrix_need_update) { |
| 78 | transfer_matrix_values(raw, cooked, num_rows, current_time); | 93 | if (!updated_last) { |
| 94 | last_time = timer_read_fast(); | ||
| 95 | } | ||
| 96 | |||
| 97 | transfer_matrix_values(raw, cooked, num_rows); | ||
| 79 | } | 98 | } |
| 80 | } | 99 | } |
| 81 | 100 | ||
| 82 | // If the current time is > debounce counter, set the counter to enable input. | 101 | // If the current time is > debounce counter, set the counter to enable input. |
| 83 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { | 102 | static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) { |
| 84 | counters_need_update = false; | 103 | counters_need_update = false; |
| 104 | matrix_need_update = false; | ||
| 85 | debounce_counter_t *debounce_pointer = debounce_counters; | 105 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 86 | for (uint8_t row = 0; row < num_rows; row++) { | 106 | for (uint8_t row = 0; row < num_rows; row++) { |
| 87 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { | 107 | for (uint8_t col = 0; col < MATRIX_COLS; col++) { |
| 88 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { | 108 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { |
| 89 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { | 109 | if (*debounce_pointer <= elapsed_time) { |
| 90 | *debounce_pointer = DEBOUNCE_ELAPSED; | 110 | *debounce_pointer = DEBOUNCE_ELAPSED; |
| 111 | matrix_need_update = true; | ||
| 91 | } else { | 112 | } else { |
| 113 | *debounce_pointer -= elapsed_time; | ||
| 92 | counters_need_update = true; | 114 | counters_need_update = true; |
| 93 | } | 115 | } |
| 94 | } | 116 | } |
| @@ -98,8 +120,7 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { | |||
| 98 | } | 120 | } |
| 99 | 121 | ||
| 100 | // upload from raw_matrix to final matrix; | 122 | // upload from raw_matrix to final matrix; |
| 101 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | 123 | static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { |
| 102 | matrix_need_update = false; | ||
| 103 | debounce_counter_t *debounce_pointer = debounce_counters; | 124 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 104 | for (uint8_t row = 0; row < num_rows; row++) { | 125 | for (uint8_t row = 0; row < num_rows; row++) { |
| 105 | matrix_row_t delta = raw[row] ^ cooked[row]; | 126 | matrix_row_t delta = raw[row] ^ cooked[row]; |
| @@ -108,11 +129,9 @@ void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t n | |||
| 108 | matrix_row_t col_mask = (ROW_SHIFTER << col); | 129 | matrix_row_t col_mask = (ROW_SHIFTER << col); |
| 109 | if (delta & col_mask) { | 130 | if (delta & col_mask) { |
| 110 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { | 131 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { |
| 111 | *debounce_pointer = current_time; | 132 | *debounce_pointer = DEBOUNCE; |
| 112 | counters_need_update = true; | 133 | counters_need_update = true; |
| 113 | existing_row ^= col_mask; // flip the bit. | 134 | existing_row ^= col_mask; // flip the bit. |
| 114 | } else { | ||
| 115 | matrix_need_update = true; | ||
| 116 | } | 135 | } |
| 117 | } | 136 | } |
| 118 | debounce_pointer++; | 137 | debounce_pointer++; |
| @@ -122,3 +141,6 @@ void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t n | |||
| 122 | } | 141 | } |
| 123 | 142 | ||
| 124 | bool debounce_active(void) { return true; } | 143 | bool debounce_active(void) { return true; } |
| 144 | #else | ||
| 145 | # include "none.c" | ||
| 146 | #endif | ||
diff --git a/quantum/debounce/sym_eager_pr.c b/quantum/debounce/sym_eager_pr.c index 20ccb46f1..2ad592c5a 100644 --- a/quantum/debounce/sym_eager_pr.c +++ b/quantum/debounce/sym_eager_pr.c | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2019 Alex Ong<the.onga@gmail.com> | 2 | Copyright 2019 Alex Ong<the.onga@gmail.com> |
| 3 | Copyright 2021 Simon Arlott | ||
| 3 | This program is free software: you can redistribute it and/or modify | 4 | 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 | 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 | the Free Software Foundation, either version 2 of the License, or |
| @@ -33,27 +34,25 @@ No further inputs are accepted until DEBOUNCE milliseconds have occurred. | |||
| 33 | # define DEBOUNCE 5 | 34 | # define DEBOUNCE 5 |
| 34 | #endif | 35 | #endif |
| 35 | 36 | ||
| 36 | #define debounce_counter_t uint8_t | 37 | // Maximum debounce: 255ms |
| 38 | #if DEBOUNCE > UINT8_MAX | ||
| 39 | # undef DEBOUNCE | ||
| 40 | # define DEBOUNCE UINT8_MAX | ||
| 41 | #endif | ||
| 42 | |||
| 43 | typedef uint8_t debounce_counter_t; | ||
| 44 | |||
| 45 | #if DEBOUNCE > 0 | ||
| 37 | static bool matrix_need_update; | 46 | static bool matrix_need_update; |
| 38 | 47 | ||
| 39 | static debounce_counter_t *debounce_counters; | 48 | static debounce_counter_t *debounce_counters; |
| 49 | static fast_timer_t last_time; | ||
| 40 | static bool counters_need_update; | 50 | static bool counters_need_update; |
| 41 | 51 | ||
| 42 | #define DEBOUNCE_ELAPSED 251 | 52 | #define DEBOUNCE_ELAPSED 0 |
| 43 | #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) | ||
| 44 | |||
| 45 | static uint8_t wrapping_timer_read(void) { | ||
| 46 | static uint16_t time = 0; | ||
| 47 | static uint8_t last_result = 0; | ||
| 48 | uint16_t new_time = timer_read(); | ||
| 49 | uint16_t diff = new_time - time; | ||
| 50 | time = new_time; | ||
| 51 | last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); | ||
| 52 | return last_result; | ||
| 53 | } | ||
| 54 | 53 | ||
| 55 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time); | 54 | static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time); |
| 56 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); | 55 | static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows); |
| 57 | 56 | ||
| 58 | // we use num_rows rather than MATRIX_ROWS to support split keyboards | 57 | // we use num_rows rather than MATRIX_ROWS to support split keyboards |
| 59 | void debounce_init(uint8_t num_rows) { | 58 | void debounce_init(uint8_t num_rows) { |
| @@ -63,27 +62,50 @@ void debounce_init(uint8_t num_rows) { | |||
| 63 | } | 62 | } |
| 64 | } | 63 | } |
| 65 | 64 | ||
| 65 | void debounce_free(void) { | ||
| 66 | free(debounce_counters); | ||
| 67 | debounce_counters = NULL; | ||
| 68 | } | ||
| 69 | |||
| 66 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { | 70 | void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { |
| 67 | uint8_t current_time = wrapping_timer_read(); | 71 | bool updated_last = false; |
| 68 | bool needed_update = counters_need_update; | 72 | |
| 69 | if (counters_need_update) { | 73 | if (counters_need_update) { |
| 70 | update_debounce_counters(num_rows, current_time); | 74 | fast_timer_t now = timer_read_fast(); |
| 75 | fast_timer_t elapsed_time = TIMER_DIFF_FAST(now, last_time); | ||
| 76 | |||
| 77 | last_time = now; | ||
| 78 | updated_last = true; | ||
| 79 | if (elapsed_time > UINT8_MAX) { | ||
| 80 | elapsed_time = UINT8_MAX; | ||
| 81 | } | ||
| 82 | |||
| 83 | if (elapsed_time > 0) { | ||
| 84 | update_debounce_counters(num_rows, elapsed_time); | ||
| 85 | } | ||
| 71 | } | 86 | } |
| 72 | 87 | ||
| 73 | if (changed || (needed_update && !counters_need_update) || matrix_need_update) { | 88 | if (changed || matrix_need_update) { |
| 74 | transfer_matrix_values(raw, cooked, num_rows, current_time); | 89 | if (!updated_last) { |
| 90 | last_time = timer_read_fast(); | ||
| 91 | } | ||
| 92 | |||
| 93 | transfer_matrix_values(raw, cooked, num_rows); | ||
| 75 | } | 94 | } |
| 76 | } | 95 | } |
| 77 | 96 | ||
| 78 | // If the current time is > debounce counter, set the counter to enable input. | 97 | // If the current time is > debounce counter, set the counter to enable input. |
| 79 | void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { | 98 | static void update_debounce_counters(uint8_t num_rows, uint8_t elapsed_time) { |
| 80 | counters_need_update = false; | 99 | counters_need_update = false; |
| 100 | matrix_need_update = false; | ||
| 81 | debounce_counter_t *debounce_pointer = debounce_counters; | 101 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 82 | for (uint8_t row = 0; row < num_rows; row++) { | 102 | for (uint8_t row = 0; row < num_rows; row++) { |
| 83 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { | 103 | if (*debounce_pointer != DEBOUNCE_ELAPSED) { |
| 84 | if (TIMER_DIFF(current_time, *debounce_pointer, MAX_DEBOUNCE) >= DEBOUNCE) { | 104 | if (*debounce_pointer <= elapsed_time) { |
| 85 | *debounce_pointer = DEBOUNCE_ELAPSED; | 105 | *debounce_pointer = DEBOUNCE_ELAPSED; |
| 106 | matrix_need_update = true; | ||
| 86 | } else { | 107 | } else { |
| 108 | *debounce_pointer -= elapsed_time; | ||
| 87 | counters_need_update = true; | 109 | counters_need_update = true; |
| 88 | } | 110 | } |
| 89 | } | 111 | } |
| @@ -92,8 +114,7 @@ void update_debounce_counters(uint8_t num_rows, uint8_t current_time) { | |||
| 92 | } | 114 | } |
| 93 | 115 | ||
| 94 | // upload from raw_matrix to final matrix; | 116 | // upload from raw_matrix to final matrix; |
| 95 | void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time) { | 117 | static void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows) { |
| 96 | matrix_need_update = false; | ||
| 97 | debounce_counter_t *debounce_pointer = debounce_counters; | 118 | debounce_counter_t *debounce_pointer = debounce_counters; |
| 98 | for (uint8_t row = 0; row < num_rows; row++) { | 119 | for (uint8_t row = 0; row < num_rows; row++) { |
| 99 | matrix_row_t existing_row = cooked[row]; | 120 | matrix_row_t existing_row = cooked[row]; |
| @@ -102,11 +123,9 @@ void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t n | |||
| 102 | // determine new value basd on debounce pointer + raw value | 123 | // determine new value basd on debounce pointer + raw value |
| 103 | if (existing_row != raw_row) { | 124 | if (existing_row != raw_row) { |
| 104 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { | 125 | if (*debounce_pointer == DEBOUNCE_ELAPSED) { |
| 105 | *debounce_pointer = current_time; | 126 | *debounce_pointer = DEBOUNCE; |
| 106 | cooked[row] = raw_row; | 127 | cooked[row] = raw_row; |
| 107 | counters_need_update = true; | 128 | counters_need_update = true; |
| 108 | } else { | ||
| 109 | matrix_need_update = true; | ||
| 110 | } | 129 | } |
| 111 | } | 130 | } |
| 112 | debounce_pointer++; | 131 | debounce_pointer++; |
| @@ -114,3 +133,6 @@ void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t n | |||
| 114 | } | 133 | } |
| 115 | 134 | ||
| 116 | bool debounce_active(void) { return true; } | 135 | bool debounce_active(void) { return true; } |
| 136 | #else | ||
| 137 | # include "none.c" | ||
| 138 | #endif | ||
diff --git a/quantum/debounce/tests/debounce_test_common.cpp b/quantum/debounce/tests/debounce_test_common.cpp new file mode 100644 index 000000000..1c5e7c9f4 --- /dev/null +++ b/quantum/debounce/tests/debounce_test_common.cpp | |||
| @@ -0,0 +1,229 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include "debounce_test_common.h" | ||
| 20 | |||
| 21 | #include <algorithm> | ||
| 22 | #include <iomanip> | ||
| 23 | #include <sstream> | ||
| 24 | |||
| 25 | extern "C" { | ||
| 26 | #include "quantum.h" | ||
| 27 | #include "timer.h" | ||
| 28 | #include "debounce.h" | ||
| 29 | |||
| 30 | void set_time(uint32_t t); | ||
| 31 | void advance_time(uint32_t ms); | ||
| 32 | } | ||
| 33 | |||
| 34 | void DebounceTest::addEvents(std::initializer_list<DebounceTestEvent> events) { | ||
| 35 | events_.insert(events_.end(), events.begin(), events.end()); | ||
| 36 | } | ||
| 37 | |||
| 38 | void DebounceTest::runEvents() { | ||
| 39 | /* Run the test multiple times, from 1kHz to 10kHz scan rate */ | ||
| 40 | for (extra_iterations_ = 0; extra_iterations_ < 10; extra_iterations_++) { | ||
| 41 | if (time_jumps_) { | ||
| 42 | /* Don't advance time smoothly, jump to the next event (some tests require this) */ | ||
| 43 | auto_advance_time_ = false; | ||
| 44 | runEventsInternal(); | ||
| 45 | } else { | ||
| 46 | /* Run the test with both smooth and irregular time; it must produce the same result */ | ||
| 47 | auto_advance_time_ = true; | ||
| 48 | runEventsInternal(); | ||
| 49 | auto_advance_time_ = false; | ||
| 50 | runEventsInternal(); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | void DebounceTest::runEventsInternal() { | ||
| 56 | fast_timer_t previous = 0; | ||
| 57 | bool first = true; | ||
| 58 | |||
| 59 | /* Initialise keyboard with start time (offset to avoid testing at 0) and all keys UP */ | ||
| 60 | debounce_init(MATRIX_ROWS); | ||
| 61 | set_time(time_offset_); | ||
| 62 | std::fill(std::begin(input_matrix_), std::end(input_matrix_), 0); | ||
| 63 | std::fill(std::begin(output_matrix_), std::end(output_matrix_), 0); | ||
| 64 | |||
| 65 | for (auto &event : events_) { | ||
| 66 | if (!auto_advance_time_) { | ||
| 67 | /* Jump to the next event */ | ||
| 68 | set_time(time_offset_ + event.time_); | ||
| 69 | } else if (!first && event.time_ == previous + 1) { | ||
| 70 | /* This event immediately follows the previous one, don't make extra debounce() calls */ | ||
| 71 | advance_time(1); | ||
| 72 | } else { | ||
| 73 | /* Fast forward to the time for this event, calling debounce() with no changes */ | ||
| 74 | ASSERT_LT((time_offset_ + event.time_) - timer_read_fast(), 60000) << "Test tries to advance more than 1 minute of time"; | ||
| 75 | |||
| 76 | while (timer_read_fast() != time_offset_ + event.time_) { | ||
| 77 | runDebounce(false); | ||
| 78 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
| 79 | advance_time(1); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | first = false; | ||
| 84 | previous = event.time_; | ||
| 85 | |||
| 86 | /* Prepare input matrix */ | ||
| 87 | for (auto &input : event.inputs_) { | ||
| 88 | matrixUpdate(input_matrix_, "input", input); | ||
| 89 | } | ||
| 90 | |||
| 91 | /* Call debounce */ | ||
| 92 | runDebounce(!event.inputs_.empty()); | ||
| 93 | |||
| 94 | /* Prepare output matrix */ | ||
| 95 | for (auto &output : event.outputs_) { | ||
| 96 | matrixUpdate(output_matrix_, "output", output); | ||
| 97 | } | ||
| 98 | |||
| 99 | /* Check output matrix has expected change events */ | ||
| 100 | for (auto &output : event.outputs_) { | ||
| 101 | EXPECT_EQ(!!(cooked_matrix_[output.row_] & (1U << output.col_)), directionValue(output.direction_)) | ||
| 102 | << "Missing event at " << strTime() | ||
| 103 | << " expected key " << output.row_ << "," << output.col_ << " " << directionLabel(output.direction_) | ||
| 104 | << "\ninput_matrix: changed=" << !event.inputs_.empty() << "\n" << strMatrix(input_matrix_) | ||
| 105 | << "\nexpected_matrix:\n" << strMatrix(output_matrix_) | ||
| 106 | << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); | ||
| 107 | } | ||
| 108 | |||
| 109 | /* Check output matrix has no other changes */ | ||
| 110 | checkCookedMatrix(!event.inputs_.empty(), "debounce() cooked matrix does not match expected output matrix"); | ||
| 111 | |||
| 112 | /* Perform some extra iterations of the matrix scan with no changes */ | ||
| 113 | for (int i = 0; i < extra_iterations_; i++) { | ||
| 114 | runDebounce(false); | ||
| 115 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | /* Check that no further changes happen for 1 minute */ | ||
| 120 | for (int i = 0; i < 60000; i++) { | ||
| 121 | runDebounce(false); | ||
| 122 | checkCookedMatrix(false, "debounce() modified cooked matrix"); | ||
| 123 | advance_time(1); | ||
| 124 | } | ||
| 125 | |||
| 126 | debounce_free(); | ||
| 127 | } | ||
| 128 | |||
| 129 | void DebounceTest::runDebounce(bool changed) { | ||
| 130 | std::copy(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_)); | ||
| 131 | std::copy(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_)); | ||
| 132 | |||
| 133 | debounce(raw_matrix_, cooked_matrix_, MATRIX_ROWS, changed); | ||
| 134 | |||
| 135 | if (!std::equal(std::begin(input_matrix_), std::end(input_matrix_), std::begin(raw_matrix_))) { | ||
| 136 | FAIL() << "Fatal error: debounce() modified raw matrix at " << strTime() | ||
| 137 | << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) | ||
| 138 | << "\nraw_matrix:\n" << strMatrix(raw_matrix_); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | |||
| 142 | void DebounceTest::checkCookedMatrix(bool changed, const std::string &error_message) { | ||
| 143 | if (!std::equal(std::begin(output_matrix_), std::end(output_matrix_), std::begin(cooked_matrix_))) { | ||
| 144 | FAIL() << "Unexpected event: " << error_message << " at " << strTime() | ||
| 145 | << "\ninput_matrix: changed=" << changed << "\n" << strMatrix(input_matrix_) | ||
| 146 | << "\nexpected_matrix:\n" << strMatrix(output_matrix_) | ||
| 147 | << "\nactual_matrix:\n" << strMatrix(cooked_matrix_); | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | std::string DebounceTest::strTime() { | ||
| 152 | std::stringstream text; | ||
| 153 | |||
| 154 | text << "time " << (timer_read_fast() - time_offset_) | ||
| 155 | << " (extra_iterations=" << extra_iterations_ | ||
| 156 | << ", auto_advance_time=" << auto_advance_time_ << ")"; | ||
| 157 | |||
| 158 | return text.str(); | ||
| 159 | } | ||
| 160 | |||
| 161 | std::string DebounceTest::strMatrix(matrix_row_t matrix[]) { | ||
| 162 | std::stringstream text; | ||
| 163 | |||
| 164 | text << "\t" << std::setw(3) << ""; | ||
| 165 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 166 | text << " " << std::setw(2) << col; | ||
| 167 | } | ||
| 168 | text << "\n"; | ||
| 169 | |||
| 170 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
| 171 | text << "\t" << std::setw(2) << row << ":"; | ||
| 172 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 173 | text << ((matrix[row] & (1U << col)) ? " XX" : " __"); | ||
| 174 | } | ||
| 175 | |||
| 176 | text << "\n"; | ||
| 177 | } | ||
| 178 | |||
| 179 | return text.str(); | ||
| 180 | } | ||
| 181 | |||
| 182 | bool DebounceTest::directionValue(Direction direction) { | ||
| 183 | switch (direction) { | ||
| 184 | case DOWN: | ||
| 185 | return true; | ||
| 186 | |||
| 187 | case UP: | ||
| 188 | return false; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | std::string DebounceTest::directionLabel(Direction direction) { | ||
| 193 | switch (direction) { | ||
| 194 | case DOWN: | ||
| 195 | return "DOWN"; | ||
| 196 | |||
| 197 | case UP: | ||
| 198 | return "UP"; | ||
| 199 | } | ||
| 200 | } | ||
| 201 | |||
| 202 | /* Modify a matrix and verify that events always specify a change */ | ||
| 203 | void DebounceTest::matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event) { | ||
| 204 | ASSERT_NE(!!(matrix[event.row_] & (1U << event.col_)), directionValue(event.direction_)) | ||
| 205 | << "Test " << name << " at " << strTime() | ||
| 206 | << " sets key " << event.row_ << "," << event.col_ << " " << directionLabel(event.direction_) | ||
| 207 | << " but it is already " << directionLabel(event.direction_) | ||
| 208 | << "\n" << name << "_matrix:\n" << strMatrix(matrix); | ||
| 209 | |||
| 210 | switch (event.direction_) { | ||
| 211 | case DOWN: | ||
| 212 | matrix[event.row_] |= (1U << event.col_); | ||
| 213 | break; | ||
| 214 | |||
| 215 | case UP: | ||
| 216 | matrix[event.row_] &= ~(1U << event.col_); | ||
| 217 | break; | ||
| 218 | } | ||
| 219 | } | ||
| 220 | |||
| 221 | DebounceTestEvent::DebounceTestEvent(fast_timer_t time, | ||
| 222 | std::initializer_list<MatrixTestEvent> inputs, | ||
| 223 | std::initializer_list<MatrixTestEvent> outputs) | ||
| 224 | : time_(time), inputs_(inputs), outputs_(outputs) { | ||
| 225 | } | ||
| 226 | |||
| 227 | MatrixTestEvent::MatrixTestEvent(int row, int col, Direction direction) | ||
| 228 | : row_(row), col_(col), direction_(direction) { | ||
| 229 | } | ||
diff --git a/quantum/debounce/tests/debounce_test_common.h b/quantum/debounce/tests/debounce_test_common.h new file mode 100644 index 000000000..d87e31059 --- /dev/null +++ b/quantum/debounce/tests/debounce_test_common.h | |||
| @@ -0,0 +1,83 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include <initializer_list> | ||
| 20 | #include <list> | ||
| 21 | #include <string> | ||
| 22 | |||
| 23 | extern "C" { | ||
| 24 | #include "quantum.h" | ||
| 25 | #include "timer.h" | ||
| 26 | } | ||
| 27 | |||
| 28 | enum Direction { | ||
| 29 | DOWN, | ||
| 30 | UP, | ||
| 31 | }; | ||
| 32 | |||
| 33 | class MatrixTestEvent { | ||
| 34 | public: | ||
| 35 | MatrixTestEvent(int row, int col, Direction direction); | ||
| 36 | |||
| 37 | const int row_; | ||
| 38 | const int col_; | ||
| 39 | const Direction direction_; | ||
| 40 | }; | ||
| 41 | |||
| 42 | class DebounceTestEvent { | ||
| 43 | public: | ||
| 44 | // 0, {{0, 1, DOWN}}, {{0, 1, DOWN}}) | ||
| 45 | DebounceTestEvent(fast_timer_t time, | ||
| 46 | std::initializer_list<MatrixTestEvent> inputs, | ||
| 47 | std::initializer_list<MatrixTestEvent> outputs); | ||
| 48 | |||
| 49 | const fast_timer_t time_; | ||
| 50 | const std::list<MatrixTestEvent> inputs_; | ||
| 51 | const std::list<MatrixTestEvent> outputs_; | ||
| 52 | }; | ||
| 53 | |||
| 54 | class DebounceTest : public ::testing::Test { | ||
| 55 | protected: | ||
| 56 | void addEvents(std::initializer_list<DebounceTestEvent> events); | ||
| 57 | void runEvents(); | ||
| 58 | |||
| 59 | fast_timer_t time_offset_ = 7777; | ||
| 60 | bool time_jumps_ = false; | ||
| 61 | |||
| 62 | private: | ||
| 63 | static bool directionValue(Direction direction); | ||
| 64 | static std::string directionLabel(Direction direction); | ||
| 65 | |||
| 66 | void runEventsInternal(); | ||
| 67 | void runDebounce(bool changed); | ||
| 68 | void checkCookedMatrix(bool changed, const std::string &error_message); | ||
| 69 | void matrixUpdate(matrix_row_t matrix[], const std::string &name, const MatrixTestEvent &event); | ||
| 70 | |||
| 71 | std::string strTime(); | ||
| 72 | std::string strMatrix(matrix_row_t matrix[]); | ||
| 73 | |||
| 74 | std::list<DebounceTestEvent> events_; | ||
| 75 | |||
| 76 | matrix_row_t input_matrix_[MATRIX_ROWS]; | ||
| 77 | matrix_row_t raw_matrix_[MATRIX_ROWS]; | ||
| 78 | matrix_row_t cooked_matrix_[MATRIX_ROWS]; | ||
| 79 | matrix_row_t output_matrix_[MATRIX_ROWS]; | ||
| 80 | |||
| 81 | int extra_iterations_; | ||
| 82 | bool auto_advance_time_; | ||
| 83 | }; | ||
diff --git a/quantum/debounce/tests/rules.mk b/quantum/debounce/tests/rules.mk new file mode 100644 index 000000000..29fda7889 --- /dev/null +++ b/quantum/debounce/tests/rules.mk | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | # Copyright 2021 Simon Arlott | ||
| 2 | # | ||
| 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 | # | ||
| 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 | # | ||
| 13 | # You should have received a copy of the GNU General Public License | ||
| 14 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | |||
| 16 | DEBOUNCE_COMMON_DEFS := -DMATRIX_ROWS=4 -DMATRIX_COLS=10 -DDEBOUNCE=5 | ||
| 17 | |||
| 18 | DEBOUNCE_COMMON_SRC := $(QUANTUM_PATH)/debounce/tests/debounce_test_common.cpp \ | ||
| 19 | $(TMK_PATH)/common/test/timer.c | ||
| 20 | |||
| 21 | debounce_sym_defer_g_DEFS := $(DEBOUNCE_COMMON_DEFS) | ||
| 22 | debounce_sym_defer_g_SRC := $(DEBOUNCE_COMMON_SRC) \ | ||
| 23 | $(QUANTUM_PATH)/debounce/sym_defer_g.c \ | ||
| 24 | $(QUANTUM_PATH)/debounce/tests/sym_defer_g_tests.cpp | ||
| 25 | |||
| 26 | debounce_sym_defer_pk_DEFS := $(DEBOUNCE_COMMON_DEFS) | ||
| 27 | debounce_sym_defer_pk_SRC := $(DEBOUNCE_COMMON_SRC) \ | ||
| 28 | $(QUANTUM_PATH)/debounce/sym_defer_pk.c \ | ||
| 29 | $(QUANTUM_PATH)/debounce/tests/sym_defer_pk_tests.cpp | ||
| 30 | |||
| 31 | debounce_sym_eager_pk_DEFS := $(DEBOUNCE_COMMON_DEFS) | ||
| 32 | debounce_sym_eager_pk_SRC := $(DEBOUNCE_COMMON_SRC) \ | ||
| 33 | $(QUANTUM_PATH)/debounce/sym_eager_pk.c \ | ||
| 34 | $(QUANTUM_PATH)/debounce/tests/sym_eager_pk_tests.cpp | ||
| 35 | |||
| 36 | debounce_sym_eager_pr_DEFS := $(DEBOUNCE_COMMON_DEFS) | ||
| 37 | debounce_sym_eager_pr_SRC := $(DEBOUNCE_COMMON_SRC) \ | ||
| 38 | $(QUANTUM_PATH)/debounce/sym_eager_pr.c \ | ||
| 39 | $(QUANTUM_PATH)/debounce/tests/sym_eager_pr_tests.cpp | ||
diff --git a/quantum/debounce/tests/sym_defer_g_tests.cpp b/quantum/debounce/tests/sym_defer_g_tests.cpp new file mode 100644 index 000000000..a56aecd8f --- /dev/null +++ b/quantum/debounce/tests/sym_defer_g_tests.cpp | |||
| @@ -0,0 +1,223 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include "debounce_test_common.h" | ||
| 20 | |||
| 21 | TEST_F(DebounceTest, OneKeyShort1) { | ||
| 22 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 23 | {0, {{0, 1, DOWN}}, {}}, | ||
| 24 | |||
| 25 | {5, {}, {{0, 1, DOWN}}}, | ||
| 26 | /* 0ms delay (fast scan rate) */ | ||
| 27 | {5, {{0, 1, UP}}, {}}, | ||
| 28 | |||
| 29 | {10, {}, {{0, 1, UP}}}, | ||
| 30 | }); | ||
| 31 | runEvents(); | ||
| 32 | } | ||
| 33 | |||
| 34 | TEST_F(DebounceTest, OneKeyShort2) { | ||
| 35 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 36 | {0, {{0, 1, DOWN}}, {}}, | ||
| 37 | |||
| 38 | {5, {}, {{0, 1, DOWN}}}, | ||
| 39 | /* 1ms delay */ | ||
| 40 | {6, {{0, 1, UP}}, {}}, | ||
| 41 | |||
| 42 | {11, {}, {{0, 1, UP}}}, | ||
| 43 | }); | ||
| 44 | runEvents(); | ||
| 45 | } | ||
| 46 | |||
| 47 | TEST_F(DebounceTest, OneKeyShort3) { | ||
| 48 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 49 | {0, {{0, 1, DOWN}}, {}}, | ||
| 50 | |||
| 51 | {5, {}, {{0, 1, DOWN}}}, | ||
| 52 | /* 2ms delay */ | ||
| 53 | {7, {{0, 1, UP}}, {}}, | ||
| 54 | |||
| 55 | {12, {}, {{0, 1, UP}}}, | ||
| 56 | }); | ||
| 57 | runEvents(); | ||
| 58 | } | ||
| 59 | |||
| 60 | TEST_F(DebounceTest, OneKeyTooQuick1) { | ||
| 61 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 62 | {0, {{0, 1, DOWN}}, {}}, | ||
| 63 | /* Release key exactly on the debounce time */ | ||
| 64 | {5, {{0, 1, UP}}, {}}, | ||
| 65 | }); | ||
| 66 | runEvents(); | ||
| 67 | } | ||
| 68 | |||
| 69 | TEST_F(DebounceTest, OneKeyTooQuick2) { | ||
| 70 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 71 | {0, {{0, 1, DOWN}}, {}}, | ||
| 72 | |||
| 73 | {5, {}, {{0, 1, DOWN}}}, | ||
| 74 | {6, {{0, 1, UP}}, {}}, | ||
| 75 | |||
| 76 | /* Press key exactly on the debounce time */ | ||
| 77 | {11, {{0, 1, DOWN}}, {}}, | ||
| 78 | }); | ||
| 79 | runEvents(); | ||
| 80 | } | ||
| 81 | |||
| 82 | TEST_F(DebounceTest, OneKeyBouncing1) { | ||
| 83 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 84 | {0, {{0, 1, DOWN}}, {}}, | ||
| 85 | {1, {{0, 1, UP}}, {}}, | ||
| 86 | {2, {{0, 1, DOWN}}, {}}, | ||
| 87 | {3, {{0, 1, UP}}, {}}, | ||
| 88 | {4, {{0, 1, DOWN}}, {}}, | ||
| 89 | {5, {{0, 1, UP}}, {}}, | ||
| 90 | {6, {{0, 1, DOWN}}, {}}, | ||
| 91 | {11, {}, {{0, 1, DOWN}}}, /* 5ms after DOWN at time 7 */ | ||
| 92 | }); | ||
| 93 | runEvents(); | ||
| 94 | } | ||
| 95 | |||
| 96 | TEST_F(DebounceTest, OneKeyBouncing2) { | ||
| 97 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 98 | {0, {{0, 1, DOWN}}, {}}, | ||
| 99 | {5, {}, {{0, 1, DOWN}}}, | ||
| 100 | {6, {{0, 1, UP}}, {}}, | ||
| 101 | {7, {{0, 1, DOWN}}, {}}, | ||
| 102 | {8, {{0, 1, UP}}, {}}, | ||
| 103 | {9, {{0, 1, DOWN}}, {}}, | ||
| 104 | {10, {{0, 1, UP}}, {}}, | ||
| 105 | {15, {}, {{0, 1, UP}}}, /* 5ms after UP at time 10 */ | ||
| 106 | }); | ||
| 107 | runEvents(); | ||
| 108 | } | ||
| 109 | |||
| 110 | TEST_F(DebounceTest, OneKeyLong) { | ||
| 111 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 112 | {0, {{0, 1, DOWN}}, {}}, | ||
| 113 | |||
| 114 | {5, {}, {{0, 1, DOWN}}}, | ||
| 115 | |||
| 116 | {25, {{0, 1, UP}}, {}}, | ||
| 117 | |||
| 118 | {30, {}, {{0, 1, UP}}}, | ||
| 119 | |||
| 120 | {50, {{0, 1, DOWN}}, {}}, | ||
| 121 | |||
| 122 | {55, {}, {{0, 1, DOWN}}}, | ||
| 123 | }); | ||
| 124 | runEvents(); | ||
| 125 | } | ||
| 126 | |||
| 127 | TEST_F(DebounceTest, TwoKeysShort) { | ||
| 128 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 129 | {0, {{0, 1, DOWN}}, {}}, | ||
| 130 | {1, {{0, 2, DOWN}}, {}}, | ||
| 131 | |||
| 132 | {6, {}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 133 | |||
| 134 | {7, {{0, 1, UP}}, {}}, | ||
| 135 | {8, {{0, 2, UP}}, {}}, | ||
| 136 | |||
| 137 | {13, {}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 138 | }); | ||
| 139 | runEvents(); | ||
| 140 | } | ||
| 141 | |||
| 142 | TEST_F(DebounceTest, TwoKeysSimultaneous1) { | ||
| 143 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 144 | {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {}}, | ||
| 145 | |||
| 146 | {5, {}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 147 | {6, {{0, 1, UP}, {0, 2, UP}}, {}}, | ||
| 148 | |||
| 149 | {11, {}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 150 | }); | ||
| 151 | runEvents(); | ||
| 152 | } | ||
| 153 | |||
| 154 | TEST_F(DebounceTest, TwoKeysSimultaneous2) { | ||
| 155 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 156 | {0, {{0, 1, DOWN}}, {}}, | ||
| 157 | {1, {{0, 2, DOWN}}, {}}, | ||
| 158 | |||
| 159 | {5, {}, {}}, | ||
| 160 | {6, {}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 161 | {7, {{0, 1, UP}}, {}}, | ||
| 162 | {8, {{0, 2, UP}}, {}}, | ||
| 163 | |||
| 164 | {13, {}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 165 | }); | ||
| 166 | runEvents(); | ||
| 167 | } | ||
| 168 | |||
| 169 | TEST_F(DebounceTest, OneKeyDelayedScan1) { | ||
| 170 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 171 | {0, {{0, 1, DOWN}}, {}}, | ||
| 172 | |||
| 173 | /* Processing is very late */ | ||
| 174 | {300, {}, {{0, 1, DOWN}}}, | ||
| 175 | /* Immediately release key */ | ||
| 176 | {300, {{0, 1, UP}}, {}}, | ||
| 177 | |||
| 178 | {305, {}, {{0, 1, UP}}}, | ||
| 179 | }); | ||
| 180 | time_jumps_ = true; | ||
| 181 | runEvents(); | ||
| 182 | } | ||
| 183 | |||
| 184 | TEST_F(DebounceTest, OneKeyDelayedScan2) { | ||
| 185 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 186 | {0, {{0, 1, DOWN}}, {}}, | ||
| 187 | |||
| 188 | /* Processing is very late */ | ||
| 189 | {300, {}, {{0, 1, DOWN}}}, | ||
| 190 | /* Release key after 1ms */ | ||
| 191 | {301, {{0, 1, UP}}, {}}, | ||
| 192 | |||
| 193 | {306, {}, {{0, 1, UP}}}, | ||
| 194 | }); | ||
| 195 | time_jumps_ = true; | ||
| 196 | runEvents(); | ||
| 197 | } | ||
| 198 | |||
| 199 | TEST_F(DebounceTest, OneKeyDelayedScan3) { | ||
| 200 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 201 | {0, {{0, 1, DOWN}}, {}}, | ||
| 202 | |||
| 203 | /* Release key before debounce expires */ | ||
| 204 | {300, {{0, 1, UP}}, {}}, | ||
| 205 | }); | ||
| 206 | time_jumps_ = true; | ||
| 207 | runEvents(); | ||
| 208 | } | ||
| 209 | |||
| 210 | TEST_F(DebounceTest, OneKeyDelayedScan4) { | ||
| 211 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 212 | {0, {{0, 1, DOWN}}, {}}, | ||
| 213 | |||
| 214 | /* Processing is a bit late */ | ||
| 215 | {50, {}, {{0, 1, DOWN}}}, | ||
| 216 | /* Release key after 1ms */ | ||
| 217 | {51, {{0, 1, UP}}, {}}, | ||
| 218 | |||
| 219 | {56, {}, {{0, 1, UP}}}, | ||
| 220 | }); | ||
| 221 | time_jumps_ = true; | ||
| 222 | runEvents(); | ||
| 223 | } | ||
diff --git a/quantum/debounce/tests/sym_defer_pk_tests.cpp b/quantum/debounce/tests/sym_defer_pk_tests.cpp new file mode 100644 index 000000000..1f3061e59 --- /dev/null +++ b/quantum/debounce/tests/sym_defer_pk_tests.cpp | |||
| @@ -0,0 +1,225 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include "debounce_test_common.h" | ||
| 20 | |||
| 21 | TEST_F(DebounceTest, OneKeyShort1) { | ||
| 22 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 23 | {0, {{0, 1, DOWN}}, {}}, | ||
| 24 | |||
| 25 | {5, {}, {{0, 1, DOWN}}}, | ||
| 26 | /* 0ms delay (fast scan rate) */ | ||
| 27 | {5, {{0, 1, UP}}, {}}, | ||
| 28 | |||
| 29 | {10, {}, {{0, 1, UP}}}, | ||
| 30 | }); | ||
| 31 | runEvents(); | ||
| 32 | } | ||
| 33 | |||
| 34 | TEST_F(DebounceTest, OneKeyShort2) { | ||
| 35 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 36 | {0, {{0, 1, DOWN}}, {}}, | ||
| 37 | |||
| 38 | {5, {}, {{0, 1, DOWN}}}, | ||
| 39 | /* 1ms delay */ | ||
| 40 | {6, {{0, 1, UP}}, {}}, | ||
| 41 | |||
| 42 | {11, {}, {{0, 1, UP}}}, | ||
| 43 | }); | ||
| 44 | runEvents(); | ||
| 45 | } | ||
| 46 | |||
| 47 | TEST_F(DebounceTest, OneKeyShort3) { | ||
| 48 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 49 | {0, {{0, 1, DOWN}}, {}}, | ||
| 50 | |||
| 51 | {5, {}, {{0, 1, DOWN}}}, | ||
| 52 | /* 2ms delay */ | ||
| 53 | {7, {{0, 1, UP}}, {}}, | ||
| 54 | |||
| 55 | {12, {}, {{0, 1, UP}}}, | ||
| 56 | }); | ||
| 57 | runEvents(); | ||
| 58 | } | ||
| 59 | |||
| 60 | TEST_F(DebounceTest, OneKeyTooQuick1) { | ||
| 61 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 62 | {0, {{0, 1, DOWN}}, {}}, | ||
| 63 | /* Release key exactly on the debounce time */ | ||
| 64 | {5, {{0, 1, UP}}, {}}, | ||
| 65 | }); | ||
| 66 | runEvents(); | ||
| 67 | } | ||
| 68 | |||
| 69 | TEST_F(DebounceTest, OneKeyTooQuick2) { | ||
| 70 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 71 | {0, {{0, 1, DOWN}}, {}}, | ||
| 72 | |||
| 73 | {5, {}, {{0, 1, DOWN}}}, | ||
| 74 | {6, {{0, 1, UP}}, {}}, | ||
| 75 | |||
| 76 | /* Press key exactly on the debounce time */ | ||
| 77 | {11, {{0, 1, DOWN}}, {}}, | ||
| 78 | }); | ||
| 79 | runEvents(); | ||
| 80 | } | ||
| 81 | |||
| 82 | TEST_F(DebounceTest, OneKeyBouncing1) { | ||
| 83 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 84 | {0, {{0, 1, DOWN}}, {}}, | ||
| 85 | {1, {{0, 1, UP}}, {}}, | ||
| 86 | {2, {{0, 1, DOWN}}, {}}, | ||
| 87 | {3, {{0, 1, UP}}, {}}, | ||
| 88 | {4, {{0, 1, DOWN}}, {}}, | ||
| 89 | {5, {{0, 1, UP}}, {}}, | ||
| 90 | {6, {{0, 1, DOWN}}, {}}, | ||
| 91 | {11, {}, {{0, 1, DOWN}}}, /* 5ms after DOWN at time 7 */ | ||
| 92 | }); | ||
| 93 | runEvents(); | ||
| 94 | } | ||
| 95 | |||
| 96 | TEST_F(DebounceTest, OneKeyBouncing2) { | ||
| 97 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 98 | {0, {{0, 1, DOWN}}, {}}, | ||
| 99 | {5, {}, {{0, 1, DOWN}}}, | ||
| 100 | {6, {{0, 1, UP}}, {}}, | ||
| 101 | {7, {{0, 1, DOWN}}, {}}, | ||
| 102 | {8, {{0, 1, UP}}, {}}, | ||
| 103 | {9, {{0, 1, DOWN}}, {}}, | ||
| 104 | {10, {{0, 1, UP}}, {}}, | ||
| 105 | {15, {}, {{0, 1, UP}}}, /* 5ms after UP at time 10 */ | ||
| 106 | }); | ||
| 107 | runEvents(); | ||
| 108 | } | ||
| 109 | |||
| 110 | TEST_F(DebounceTest, OneKeyLong) { | ||
| 111 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 112 | {0, {{0, 1, DOWN}}, {}}, | ||
| 113 | |||
| 114 | {5, {}, {{0, 1, DOWN}}}, | ||
| 115 | |||
| 116 | {25, {{0, 1, UP}}, {}}, | ||
| 117 | |||
| 118 | {30, {}, {{0, 1, UP}}}, | ||
| 119 | |||
| 120 | {50, {{0, 1, DOWN}}, {}}, | ||
| 121 | |||
| 122 | {55, {}, {{0, 1, DOWN}}}, | ||
| 123 | }); | ||
| 124 | runEvents(); | ||
| 125 | } | ||
| 126 | |||
| 127 | TEST_F(DebounceTest, TwoKeysShort) { | ||
| 128 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 129 | {0, {{0, 1, DOWN}}, {}}, | ||
| 130 | {1, {{0, 2, DOWN}}, {}}, | ||
| 131 | |||
| 132 | {5, {}, {{0, 1, DOWN}}}, | ||
| 133 | {6, {}, {{0, 2, DOWN}}}, | ||
| 134 | |||
| 135 | {7, {{0, 1, UP}}, {}}, | ||
| 136 | {8, {{0, 2, UP}}, {}}, | ||
| 137 | |||
| 138 | {12, {}, {{0, 1, UP}}}, | ||
| 139 | {13, {}, {{0, 2, UP}}}, | ||
| 140 | }); | ||
| 141 | runEvents(); | ||
| 142 | } | ||
| 143 | |||
| 144 | TEST_F(DebounceTest, TwoKeysSimultaneous1) { | ||
| 145 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 146 | {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {}}, | ||
| 147 | |||
| 148 | {5, {}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 149 | {6, {{0, 1, UP}, {0, 2, UP}}, {}}, | ||
| 150 | |||
| 151 | {11, {}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 152 | }); | ||
| 153 | runEvents(); | ||
| 154 | } | ||
| 155 | |||
| 156 | TEST_F(DebounceTest, TwoKeysSimultaneous2) { | ||
| 157 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 158 | {0, {{0, 1, DOWN}}, {}}, | ||
| 159 | {1, {{0, 2, DOWN}}, {}}, | ||
| 160 | |||
| 161 | {5, {}, {{0, 1, DOWN}}}, | ||
| 162 | {6, {{0, 1, UP}}, {{0, 2, DOWN}}}, | ||
| 163 | {7, {{0, 2, UP}}, {}}, | ||
| 164 | |||
| 165 | {11, {}, {{0, 1, UP}}}, | ||
| 166 | {12, {}, {{0, 2, UP}}}, | ||
| 167 | }); | ||
| 168 | runEvents(); | ||
| 169 | } | ||
| 170 | |||
| 171 | TEST_F(DebounceTest, OneKeyDelayedScan1) { | ||
| 172 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 173 | {0, {{0, 1, DOWN}}, {}}, | ||
| 174 | |||
| 175 | /* Processing is very late */ | ||
| 176 | {300, {}, {{0, 1, DOWN}}}, | ||
| 177 | /* Immediately release key */ | ||
| 178 | {300, {{0, 1, UP}}, {}}, | ||
| 179 | |||
| 180 | {305, {}, {{0, 1, UP}}}, | ||
| 181 | }); | ||
| 182 | time_jumps_ = true; | ||
| 183 | runEvents(); | ||
| 184 | } | ||
| 185 | |||
| 186 | TEST_F(DebounceTest, OneKeyDelayedScan2) { | ||
| 187 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 188 | {0, {{0, 1, DOWN}}, {}}, | ||
| 189 | |||
| 190 | /* Processing is very late */ | ||
| 191 | {300, {}, {{0, 1, DOWN}}}, | ||
| 192 | /* Release key after 1ms */ | ||
| 193 | {301, {{0, 1, UP}}, {}}, | ||
| 194 | |||
| 195 | {306, {}, {{0, 1, UP}}}, | ||
| 196 | }); | ||
| 197 | time_jumps_ = true; | ||
| 198 | runEvents(); | ||
| 199 | } | ||
| 200 | |||
| 201 | TEST_F(DebounceTest, OneKeyDelayedScan3) { | ||
| 202 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 203 | {0, {{0, 1, DOWN}}, {}}, | ||
| 204 | |||
| 205 | /* Release key before debounce expires */ | ||
| 206 | {300, {{0, 1, UP}}, {}}, | ||
| 207 | }); | ||
| 208 | time_jumps_ = true; | ||
| 209 | runEvents(); | ||
| 210 | } | ||
| 211 | |||
| 212 | TEST_F(DebounceTest, OneKeyDelayedScan4) { | ||
| 213 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 214 | {0, {{0, 1, DOWN}}, {}}, | ||
| 215 | |||
| 216 | /* Processing is a bit late */ | ||
| 217 | {50, {}, {{0, 1, DOWN}}}, | ||
| 218 | /* Release key after 1ms */ | ||
| 219 | {51, {{0, 1, UP}}, {}}, | ||
| 220 | |||
| 221 | {56, {}, {{0, 1, UP}}}, | ||
| 222 | }); | ||
| 223 | time_jumps_ = true; | ||
| 224 | runEvents(); | ||
| 225 | } | ||
diff --git a/quantum/debounce/tests/sym_eager_pk_tests.cpp b/quantum/debounce/tests/sym_eager_pk_tests.cpp new file mode 100644 index 000000000..e0fc205e3 --- /dev/null +++ b/quantum/debounce/tests/sym_eager_pk_tests.cpp | |||
| @@ -0,0 +1,237 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include "debounce_test_common.h" | ||
| 20 | |||
| 21 | TEST_F(DebounceTest, OneKeyShort1) { | ||
| 22 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 23 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 24 | {1, {{0, 1, UP}}, {}}, | ||
| 25 | |||
| 26 | {5, {}, {{0, 1, UP}}}, | ||
| 27 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 28 | {6, {{0, 1, DOWN}}, {}}, | ||
| 29 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 30 | }); | ||
| 31 | runEvents(); | ||
| 32 | } | ||
| 33 | |||
| 34 | TEST_F(DebounceTest, OneKeyShort2) { | ||
| 35 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 36 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 37 | {1, {{0, 1, UP}}, {}}, | ||
| 38 | |||
| 39 | {5, {}, {{0, 1, UP}}}, | ||
| 40 | /* Press key again after 2ms delay (debounce has not yet finished) */ | ||
| 41 | {7, {{0, 1, DOWN}}, {}}, | ||
| 42 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 43 | }); | ||
| 44 | runEvents(); | ||
| 45 | } | ||
| 46 | |||
| 47 | TEST_F(DebounceTest, OneKeyShort3) { | ||
| 48 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 49 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 50 | {1, {{0, 1, UP}}, {}}, | ||
| 51 | |||
| 52 | {5, {}, {{0, 1, UP}}}, | ||
| 53 | /* Press key again after 3ms delay (debounce has not yet finished) */ | ||
| 54 | {8, {{0, 1, DOWN}}, {}}, | ||
| 55 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 56 | }); | ||
| 57 | runEvents(); | ||
| 58 | } | ||
| 59 | |||
| 60 | TEST_F(DebounceTest, OneKeyShort4) { | ||
| 61 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 62 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 63 | {1, {{0, 1, UP}}, {}}, | ||
| 64 | |||
| 65 | {5, {}, {{0, 1, UP}}}, | ||
| 66 | /* Press key again after 4ms delay (debounce has not yet finished) */ | ||
| 67 | {9, {{0, 1, DOWN}}, {}}, | ||
| 68 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 69 | }); | ||
| 70 | runEvents(); | ||
| 71 | } | ||
| 72 | |||
| 73 | TEST_F(DebounceTest, OneKeyShort5) { | ||
| 74 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 75 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 76 | {1, {{0, 1, UP}}, {}}, | ||
| 77 | |||
| 78 | {5, {}, {{0, 1, UP}}}, | ||
| 79 | /* Press key again after 5ms delay (debounce has finished) */ | ||
| 80 | {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 81 | }); | ||
| 82 | runEvents(); | ||
| 83 | } | ||
| 84 | |||
| 85 | TEST_F(DebounceTest, OneKeyShort6) { | ||
| 86 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 87 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 88 | {1, {{0, 1, UP}}, {}}, | ||
| 89 | |||
| 90 | {5, {}, {{0, 1, UP}}}, | ||
| 91 | /* Press key after after 6ms delay (debounce has finished) */ | ||
| 92 | {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 93 | }); | ||
| 94 | runEvents(); | ||
| 95 | } | ||
| 96 | |||
| 97 | TEST_F(DebounceTest, OneKeyBouncing1) { | ||
| 98 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 99 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 100 | {1, {{0, 1, UP}}, {}}, | ||
| 101 | {2, {{0, 1, DOWN}}, {}}, | ||
| 102 | {3, {{0, 1, UP}}, {}}, | ||
| 103 | {4, {{0, 1, DOWN}}, {}}, | ||
| 104 | {5, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 105 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 106 | {6, {{0, 1, DOWN}}, {}}, | ||
| 107 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 108 | }); | ||
| 109 | runEvents(); | ||
| 110 | } | ||
| 111 | |||
| 112 | TEST_F(DebounceTest, OneKeyBouncing2) { | ||
| 113 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 114 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 115 | /* Change twice in the same time period */ | ||
| 116 | {1, {{0, 1, UP}}, {}}, | ||
| 117 | {1, {{0, 1, DOWN}}, {}}, | ||
| 118 | /* Change three times in the same time period */ | ||
| 119 | {2, {{0, 1, UP}}, {}}, | ||
| 120 | {2, {{0, 1, DOWN}}, {}}, | ||
| 121 | {2, {{0, 1, UP}}, {}}, | ||
| 122 | /* Change three times in the same time period */ | ||
| 123 | {3, {{0, 1, DOWN}}, {}}, | ||
| 124 | {3, {{0, 1, UP}}, {}}, | ||
| 125 | {3, {{0, 1, DOWN}}, {}}, | ||
| 126 | /* Change twice in the same time period */ | ||
| 127 | {4, {{0, 1, UP}}, {}}, | ||
| 128 | {4, {{0, 1, DOWN}}, {}}, | ||
| 129 | {5, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 130 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 131 | {6, {{0, 1, DOWN}}, {}}, | ||
| 132 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 133 | }); | ||
| 134 | runEvents(); | ||
| 135 | } | ||
| 136 | |||
| 137 | TEST_F(DebounceTest, OneKeyLong) { | ||
| 138 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 139 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 140 | |||
| 141 | {25, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 142 | |||
| 143 | {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 144 | }); | ||
| 145 | runEvents(); | ||
| 146 | } | ||
| 147 | |||
| 148 | TEST_F(DebounceTest, TwoKeysShort) { | ||
| 149 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 150 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 151 | {1, {{0, 1, UP}}, {}}, | ||
| 152 | {2, {{0, 2, DOWN}}, {{0, 2, DOWN}}}, | ||
| 153 | {3, {{0, 2, UP}}, {}}, | ||
| 154 | |||
| 155 | {5, {}, {{0, 1, UP}}}, | ||
| 156 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 157 | {6, {{0, 1, DOWN}}, {}}, | ||
| 158 | {7, {}, {{0, 2, UP}}}, | ||
| 159 | |||
| 160 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 161 | {9, {{0, 2, DOWN}}, {}}, | ||
| 162 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 163 | |||
| 164 | {12, {}, {{0, 2, DOWN}}}, /* 5ms after UP at time 7 */ | ||
| 165 | }); | ||
| 166 | runEvents(); | ||
| 167 | } | ||
| 168 | |||
| 169 | TEST_F(DebounceTest, OneKeyDelayedScan1) { | ||
| 170 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 171 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 172 | |||
| 173 | /* Processing is very late but the change will now be accepted */ | ||
| 174 | {300, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 175 | }); | ||
| 176 | time_jumps_ = true; | ||
| 177 | runEvents(); | ||
| 178 | } | ||
| 179 | |||
| 180 | TEST_F(DebounceTest, OneKeyDelayedScan2) { | ||
| 181 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 182 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 183 | |||
| 184 | /* Processing is very late but the change will now be accepted even with a 1 scan delay */ | ||
| 185 | {300, {}, {}}, | ||
| 186 | {300, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 187 | }); | ||
| 188 | time_jumps_ = true; | ||
| 189 | runEvents(); | ||
| 190 | } | ||
| 191 | |||
| 192 | TEST_F(DebounceTest, OneKeyDelayedScan3) { | ||
| 193 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 194 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 195 | |||
| 196 | /* Processing is very late but the change will now be accepted even with a 1ms delay */ | ||
| 197 | {300, {}, {}}, | ||
| 198 | {301, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 199 | }); | ||
| 200 | time_jumps_ = true; | ||
| 201 | runEvents(); | ||
| 202 | } | ||
| 203 | |||
| 204 | TEST_F(DebounceTest, OneKeyDelayedScan4) { | ||
| 205 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 206 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 207 | |||
| 208 | /* Processing is a bit late but the change will now be accepted */ | ||
| 209 | {50, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 210 | }); | ||
| 211 | time_jumps_ = true; | ||
| 212 | runEvents(); | ||
| 213 | } | ||
| 214 | |||
| 215 | TEST_F(DebounceTest, OneKeyDelayedScan5) { | ||
| 216 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 217 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 218 | |||
| 219 | /* Processing is very late but the change will now be accepted even with a 1 scan delay */ | ||
| 220 | {50, {}, {}}, | ||
| 221 | {50, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 222 | }); | ||
| 223 | time_jumps_ = true; | ||
| 224 | runEvents(); | ||
| 225 | } | ||
| 226 | |||
| 227 | TEST_F(DebounceTest, OneKeyDelayedScan6) { | ||
| 228 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 229 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 230 | |||
| 231 | /* Processing is very late but the change will now be accepted even with a 1ms delay */ | ||
| 232 | {50, {}, {}}, | ||
| 233 | {51, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 234 | }); | ||
| 235 | time_jumps_ = true; | ||
| 236 | runEvents(); | ||
| 237 | } | ||
diff --git a/quantum/debounce/tests/sym_eager_pr_tests.cpp b/quantum/debounce/tests/sym_eager_pr_tests.cpp new file mode 100644 index 000000000..2c4bca127 --- /dev/null +++ b/quantum/debounce/tests/sym_eager_pr_tests.cpp | |||
| @@ -0,0 +1,280 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | |||
| 17 | #include "gtest/gtest.h" | ||
| 18 | |||
| 19 | #include "debounce_test_common.h" | ||
| 20 | |||
| 21 | TEST_F(DebounceTest, OneKeyShort1) { | ||
| 22 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 23 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 24 | {1, {{0, 1, UP}}, {}}, | ||
| 25 | |||
| 26 | {5, {}, {{0, 1, UP}}}, | ||
| 27 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 28 | {6, {{0, 1, DOWN}}, {}}, | ||
| 29 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 30 | }); | ||
| 31 | runEvents(); | ||
| 32 | } | ||
| 33 | |||
| 34 | TEST_F(DebounceTest, OneKeyShort2) { | ||
| 35 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 36 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 37 | {1, {{0, 1, UP}}, {}}, | ||
| 38 | |||
| 39 | {5, {}, {{0, 1, UP}}}, | ||
| 40 | /* Press key again after 2ms delay (debounce has not yet finished) */ | ||
| 41 | {7, {{0, 1, DOWN}}, {}}, | ||
| 42 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 43 | }); | ||
| 44 | runEvents(); | ||
| 45 | } | ||
| 46 | |||
| 47 | TEST_F(DebounceTest, OneKeyShort3) { | ||
| 48 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 49 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 50 | {1, {{0, 1, UP}}, {}}, | ||
| 51 | |||
| 52 | {5, {}, {{0, 1, UP}}}, | ||
| 53 | /* Press key again after 3ms delay (debounce has not yet finished) */ | ||
| 54 | {8, {{0, 1, DOWN}}, {}}, | ||
| 55 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 56 | }); | ||
| 57 | runEvents(); | ||
| 58 | } | ||
| 59 | |||
| 60 | TEST_F(DebounceTest, OneKeyShort4) { | ||
| 61 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 62 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 63 | {1, {{0, 1, UP}}, {}}, | ||
| 64 | |||
| 65 | {5, {}, {{0, 1, UP}}}, | ||
| 66 | /* Press key again after 4ms delay (debounce has not yet finished) */ | ||
| 67 | {9, {{0, 1, DOWN}}, {}}, | ||
| 68 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 69 | }); | ||
| 70 | runEvents(); | ||
| 71 | } | ||
| 72 | |||
| 73 | TEST_F(DebounceTest, OneKeyShort5) { | ||
| 74 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 75 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 76 | {1, {{0, 1, UP}}, {}}, | ||
| 77 | |||
| 78 | {5, {}, {{0, 1, UP}}}, | ||
| 79 | /* Press key again after 5ms delay (debounce has finished) */ | ||
| 80 | {10, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 81 | }); | ||
| 82 | runEvents(); | ||
| 83 | } | ||
| 84 | |||
| 85 | TEST_F(DebounceTest, OneKeyShort6) { | ||
| 86 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 87 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 88 | {1, {{0, 1, UP}}, {}}, | ||
| 89 | |||
| 90 | {5, {}, {{0, 1, UP}}}, | ||
| 91 | /* Press key after after 6ms delay (debounce has finished) */ | ||
| 92 | {11, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 93 | }); | ||
| 94 | runEvents(); | ||
| 95 | } | ||
| 96 | |||
| 97 | TEST_F(DebounceTest, OneKeyBouncing1) { | ||
| 98 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 99 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 100 | {1, {{0, 1, UP}}, {}}, | ||
| 101 | {2, {{0, 1, DOWN}}, {}}, | ||
| 102 | {3, {{0, 1, UP}}, {}}, | ||
| 103 | {4, {{0, 1, DOWN}}, {}}, | ||
| 104 | {5, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 105 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 106 | {6, {{0, 1, DOWN}}, {}}, | ||
| 107 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 108 | }); | ||
| 109 | runEvents(); | ||
| 110 | } | ||
| 111 | |||
| 112 | TEST_F(DebounceTest, OneKeyBouncing2) { | ||
| 113 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 114 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 115 | /* Change twice in the same time period */ | ||
| 116 | {1, {{0, 1, UP}}, {}}, | ||
| 117 | {1, {{0, 1, DOWN}}, {}}, | ||
| 118 | /* Change three times in the same time period */ | ||
| 119 | {2, {{0, 1, UP}}, {}}, | ||
| 120 | {2, {{0, 1, DOWN}}, {}}, | ||
| 121 | {2, {{0, 1, UP}}, {}}, | ||
| 122 | /* Change three times in the same time period */ | ||
| 123 | {3, {{0, 1, DOWN}}, {}}, | ||
| 124 | {3, {{0, 1, UP}}, {}}, | ||
| 125 | {3, {{0, 1, DOWN}}, {}}, | ||
| 126 | /* Change twice in the same time period */ | ||
| 127 | {4, {{0, 1, UP}}, {}}, | ||
| 128 | {4, {{0, 1, DOWN}}, {}}, | ||
| 129 | {5, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 130 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 131 | {6, {{0, 1, DOWN}}, {}}, | ||
| 132 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 133 | }); | ||
| 134 | runEvents(); | ||
| 135 | } | ||
| 136 | |||
| 137 | TEST_F(DebounceTest, OneKeyLong) { | ||
| 138 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 139 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 140 | |||
| 141 | {25, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 142 | |||
| 143 | {50, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 144 | }); | ||
| 145 | runEvents(); | ||
| 146 | } | ||
| 147 | |||
| 148 | TEST_F(DebounceTest, TwoRowsShort) { | ||
| 149 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 150 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 151 | {1, {{0, 1, UP}}, {}}, | ||
| 152 | {2, {{2, 0, DOWN}}, {{2, 0, DOWN}}}, | ||
| 153 | {3, {{2, 0, UP}}, {}}, | ||
| 154 | |||
| 155 | {5, {}, {{0, 1, UP}}}, | ||
| 156 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 157 | {6, {{0, 1, DOWN}}, {}}, | ||
| 158 | {7, {}, {{2, 0, UP}}}, | ||
| 159 | |||
| 160 | /* Press key again after 1ms delay (debounce has not yet finished) */ | ||
| 161 | {9, {{2, 0, DOWN}}, {}}, | ||
| 162 | {10, {}, {{0, 1, DOWN}}}, /* 5ms after UP at time 5 */ | ||
| 163 | |||
| 164 | {12, {}, {{2, 0, DOWN}}}, /* 5ms after UP at time 7 */ | ||
| 165 | }); | ||
| 166 | runEvents(); | ||
| 167 | } | ||
| 168 | |||
| 169 | TEST_F(DebounceTest, TwoKeysOverlap) { | ||
| 170 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 171 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 172 | {1, {{0, 1, UP}}, {}}, | ||
| 173 | /* Press a second key during the first debounce */ | ||
| 174 | {2, {{0, 2, DOWN}}, {}}, | ||
| 175 | |||
| 176 | /* Key registers as soon as debounce finishes, 5ms after time 0 */ | ||
| 177 | {5, {}, {{0, 1, UP}, {0, 2, DOWN}}}, | ||
| 178 | {6, {{0, 1, DOWN}}, {}}, | ||
| 179 | |||
| 180 | /* Key registers as soon as debounce finishes, 5ms after time 5 */ | ||
| 181 | {10, {}, {{0, 1, DOWN}}}, | ||
| 182 | /* Release both keys */ | ||
| 183 | {11, {{0, 1, UP}}, {}}, | ||
| 184 | {12, {{0, 2, UP}}, {}}, | ||
| 185 | |||
| 186 | /* Keys register as soon as debounce finishes, 5ms after time 10 */ | ||
| 187 | {15, {}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 188 | }); | ||
| 189 | runEvents(); | ||
| 190 | } | ||
| 191 | |||
| 192 | TEST_F(DebounceTest, TwoKeysSimultaneous1) { | ||
| 193 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 194 | {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 195 | {20, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 196 | {21, {{0, 2, UP}}, {}}, | ||
| 197 | |||
| 198 | /* Key registers as soon as debounce finishes, 5ms after time 20 */ | ||
| 199 | {25, {}, {{0, 2, UP}}}, | ||
| 200 | }); | ||
| 201 | runEvents(); | ||
| 202 | } | ||
| 203 | |||
| 204 | TEST_F(DebounceTest, TwoKeysSimultaneous2) { | ||
| 205 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 206 | {0, {{0, 1, DOWN}, {0, 2, DOWN}}, {{0, 1, DOWN}, {0, 2, DOWN}}}, | ||
| 207 | {20, {{0, 1, UP}, {0, 2, UP}}, {{0, 1, UP}, {0, 2, UP}}}, | ||
| 208 | }); | ||
| 209 | runEvents(); | ||
| 210 | } | ||
| 211 | |||
| 212 | TEST_F(DebounceTest, OneKeyDelayedScan1) { | ||
| 213 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 214 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 215 | |||
| 216 | /* Processing is very late but the change will now be accepted */ | ||
| 217 | {300, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 218 | }); | ||
| 219 | time_jumps_ = true; | ||
| 220 | runEvents(); | ||
| 221 | } | ||
| 222 | |||
| 223 | TEST_F(DebounceTest, OneKeyDelayedScan2) { | ||
| 224 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 225 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 226 | |||
| 227 | /* Processing is very late but the change will now be accepted even with a 1 scan delay */ | ||
| 228 | {300, {}, {}}, | ||
| 229 | {300, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 230 | }); | ||
| 231 | time_jumps_ = true; | ||
| 232 | runEvents(); | ||
| 233 | } | ||
| 234 | |||
| 235 | TEST_F(DebounceTest, OneKeyDelayedScan3) { | ||
| 236 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 237 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 238 | |||
| 239 | /* Processing is very late but the change will now be accepted even with a 1ms delay */ | ||
| 240 | {300, {}, {}}, | ||
| 241 | {301, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 242 | }); | ||
| 243 | time_jumps_ = true; | ||
| 244 | runEvents(); | ||
| 245 | } | ||
| 246 | |||
| 247 | TEST_F(DebounceTest, OneKeyDelayedScan4) { | ||
| 248 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 249 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 250 | |||
| 251 | /* Processing is a bit late but the change will now be accepted */ | ||
| 252 | {50, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 253 | }); | ||
| 254 | time_jumps_ = true; | ||
| 255 | runEvents(); | ||
| 256 | } | ||
| 257 | |||
| 258 | TEST_F(DebounceTest, OneKeyDelayedScan5) { | ||
| 259 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 260 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 261 | |||
| 262 | /* Processing is very late but the change will now be accepted even with a 1 scan delay */ | ||
| 263 | {50, {}, {}}, | ||
| 264 | {50, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 265 | }); | ||
| 266 | time_jumps_ = true; | ||
| 267 | runEvents(); | ||
| 268 | } | ||
| 269 | |||
| 270 | TEST_F(DebounceTest, OneKeyDelayedScan6) { | ||
| 271 | addEvents({ /* Time, Inputs, Outputs */ | ||
| 272 | {0, {{0, 1, DOWN}}, {{0, 1, DOWN}}}, | ||
| 273 | |||
| 274 | /* Processing is very late but the change will now be accepted even with a 1ms delay */ | ||
| 275 | {50, {}, {}}, | ||
| 276 | {51, {{0, 1, UP}}, {{0, 1, UP}}}, | ||
| 277 | }); | ||
| 278 | time_jumps_ = true; | ||
| 279 | runEvents(); | ||
| 280 | } | ||
diff --git a/quantum/debounce/tests/testlist.mk b/quantum/debounce/tests/testlist.mk new file mode 100644 index 000000000..16ce8a0a8 --- /dev/null +++ b/quantum/debounce/tests/testlist.mk | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | TEST_LIST += \ | ||
| 2 | debounce_sym_defer_g \ | ||
| 3 | debounce_sym_defer_pk \ | ||
| 4 | debounce_sym_eager_pk \ | ||
| 5 | debounce_sym_eager_pr | ||
diff --git a/testlist.mk b/testlist.mk index 0d7609b9f..d256f4c81 100644 --- a/testlist.mk +++ b/testlist.mk | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | TEST_LIST = $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/tests/*/rules.mk))) | 1 | TEST_LIST = $(notdir $(patsubst %/rules.mk,%,$(wildcard $(ROOT_DIR)/tests/*/rules.mk))) |
| 2 | FULL_TESTS := $(TEST_LIST) | 2 | FULL_TESTS := $(TEST_LIST) |
| 3 | 3 | ||
| 4 | include $(ROOT_DIR)/quantum/debounce/tests/testlist.mk | ||
| 4 | include $(ROOT_DIR)/quantum/sequencer/tests/testlist.mk | 5 | include $(ROOT_DIR)/quantum/sequencer/tests/testlist.mk |
| 5 | include $(ROOT_DIR)/quantum/serial_link/tests/testlist.mk | 6 | include $(ROOT_DIR)/quantum/serial_link/tests/testlist.mk |
| 6 | 7 | ||
diff --git a/tmk_core/common/arm_atsam/_timer.h b/tmk_core/common/arm_atsam/_timer.h new file mode 100644 index 000000000..77402b612 --- /dev/null +++ b/tmk_core/common/arm_atsam/_timer.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | #pragma once | ||
| 17 | |||
| 18 | // The platform is 32-bit, so prefer 32-bit timers to avoid overflow | ||
| 19 | #define FAST_TIMER_T_SIZE 32 | ||
diff --git a/tmk_core/common/avr/_timer.h b/tmk_core/common/avr/_timer.h new file mode 100644 index 000000000..b81e0f68b --- /dev/null +++ b/tmk_core/common/avr/_timer.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | #pragma once | ||
| 17 | |||
| 18 | // The platform is 8-bit, so prefer 16-bit timers to reduce code size | ||
| 19 | #define FAST_TIMER_T_SIZE 16 | ||
diff --git a/tmk_core/common/chibios/_timer.h b/tmk_core/common/chibios/_timer.h new file mode 100644 index 000000000..77402b612 --- /dev/null +++ b/tmk_core/common/chibios/_timer.h | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | /* Copyright 2021 Simon Arlott | ||
| 2 | * | ||
| 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 | * | ||
| 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 | * | ||
| 13 | * You should have received a copy of the GNU General Public License | ||
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 15 | */ | ||
| 16 | #pragma once | ||
| 17 | |||
| 18 | // The platform is 32-bit, so prefer 32-bit timers to avoid overflow | ||
| 19 | #define FAST_TIMER_T_SIZE 32 | ||
diff --git a/tmk_core/common/timer.h b/tmk_core/common/timer.h index 58f637dd9..928811a2b 100644 --- a/tmk_core/common/timer.h +++ b/tmk_core/common/timer.h | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | /* | 1 | /* |
| 2 | Copyright 2011 Jun Wako <wakojun@gmail.com> | 2 | Copyright 2011 Jun Wako <wakojun@gmail.com> |
| 3 | Copyright 2021 Simon Arlott | ||
| 3 | 4 | ||
| 4 | This program is free software: you can redistribute it and/or modify | 5 | 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 | it under the terms of the GNU General Public License as published by |
| @@ -17,13 +18,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 17 | 18 | ||
| 18 | #pragma once | 19 | #pragma once |
| 19 | 20 | ||
| 21 | #if __has_include_next("_timer.h") | ||
| 22 | # include_next "_timer.h" /* Include the platform's _timer.h */ | ||
| 23 | #endif | ||
| 24 | |||
| 20 | #include <stdint.h> | 25 | #include <stdint.h> |
| 21 | #include <stdbool.h> | 26 | #include <stdbool.h> |
| 22 | 27 | ||
| 23 | #if defined(__AVR__) | ||
| 24 | # include "avr/timer_avr.h" | ||
| 25 | #endif | ||
| 26 | |||
| 27 | #define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a))))) | 28 | #define TIMER_DIFF(a, b, max) ((max == UINT8_MAX) ? ((uint8_t)((a) - (b))) : ((max == UINT16_MAX) ? ((uint16_t)((a) - (b))) : ((max == UINT32_MAX) ? ((uint32_t)((a) - (b))) : ((a) >= (b) ? (a) - (b) : (max) + 1 - (b) + (a))))) |
| 28 | #define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) | 29 | #define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) |
| 29 | #define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) | 30 | #define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) |
| @@ -47,6 +48,21 @@ uint32_t timer_elapsed32(uint32_t last); | |||
| 47 | #define timer_expired(current, future) ((uint16_t)(current - future) < UINT16_MAX / 2) | 48 | #define timer_expired(current, future) ((uint16_t)(current - future) < UINT16_MAX / 2) |
| 48 | #define timer_expired32(current, future) ((uint32_t)(current - future) < UINT32_MAX / 2) | 49 | #define timer_expired32(current, future) ((uint32_t)(current - future) < UINT32_MAX / 2) |
| 49 | 50 | ||
| 51 | // Use an appropriate timer integer size based on architecture (16-bit will overflow sooner) | ||
| 52 | #if FAST_TIMER_T_SIZE < 32 | ||
| 53 | # define TIMER_DIFF_FAST(a, b) TIMER_DIFF_16(a, b) | ||
| 54 | # define timer_expired_fast(current, future) timer_expired(current, future) | ||
| 55 | typedef uint16_t fast_timer_t; | ||
| 56 | fast_timer_t inline timer_read_fast(void) { return timer_read(); } | ||
| 57 | fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed(last); } | ||
| 58 | #else | ||
| 59 | # define TIMER_DIFF_FAST(a, b) TIMER_DIFF_32(a, b) | ||
| 60 | # define timer_expired_fast(current, future) timer_expired32(current, future) | ||
| 61 | typedef uint32_t fast_timer_t; | ||
| 62 | fast_timer_t inline timer_read_fast(void) { return timer_read32(); } | ||
| 63 | fast_timer_t inline timer_elapsed_fast(fast_timer_t last) { return timer_elapsed32(last); } | ||
| 64 | #endif | ||
| 65 | |||
| 50 | #ifdef __cplusplus | 66 | #ifdef __cplusplus |
| 51 | } | 67 | } |
| 52 | #endif | 68 | #endif |
