From 84eb44d968dd0f594f5c8aeae1c9b5b5866037c0 Mon Sep 17 00:00:00 2001 From: Purdea Andrei Date: Sat, 28 Mar 2020 06:35:24 +0200 Subject: [PATCH] quantum/debounce: Fix custom wrapping timers in eager_pr and eager_pk debounce algorithms Please see the below simulated sequence of events: Column A is the 16-bit value returned by read_timer(); Column B is the value returned by custom_wrap_timer_read(); Column C is the original code: (timer_read() % MAX_DEBOUNCE) A, B, C 65530, 19, 30 65531, 20, 31 65532, 21, 32 65533, 22, 33 65534, 23, 34 65535, 24, 35 0 25, 0 1, 26, 1 2, 27, 2 3, 28, 3 4, 29, 4 5, 30, 5 read_timer() wraps about every 1.09 seconds, and so debouncing might fail at these times without this commit. --- quantum/debounce/eager_pk.c | 13 ++++++++++++- quantum/debounce/eager_pr.c | 13 ++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/quantum/debounce/eager_pk.c b/quantum/debounce/eager_pk.c index 76b978d059e8..9d227aa708dc 100644 --- a/quantum/debounce/eager_pk.c +++ b/quantum/debounce/eager_pk.c @@ -44,6 +44,17 @@ static bool matrix_need_update; #define DEBOUNCE_ELAPSED 251 #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) +static uint16_t custom_wrap_timer_reference = 0; +static uint8_t custom_wrap_timer_last_value = 0; + +static uint8_t custom_wrap_timer_read(void) { + uint16_t custom_wrap_timer_new_reference = timer_read(); + uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference; + custom_wrap_timer_reference = custom_wrap_timer_new_reference; + custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1); + return custom_wrap_timer_last_value; +} + void update_debounce_counters(uint8_t num_rows, uint8_t current_time); void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); @@ -59,7 +70,7 @@ void debounce_init(uint8_t num_rows) { } void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { - uint8_t current_time = timer_read() % MAX_DEBOUNCE; + uint8_t current_time = custom_wrap_timer_read(); if (counters_need_update) { update_debounce_counters(num_rows, current_time); } diff --git a/quantum/debounce/eager_pr.c b/quantum/debounce/eager_pr.c index 173ad15ee92b..88cdbe951155 100644 --- a/quantum/debounce/eager_pr.c +++ b/quantum/debounce/eager_pr.c @@ -36,6 +36,17 @@ static bool counters_need_update; #define DEBOUNCE_ELAPSED 251 #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) +static uint16_t custom_wrap_timer_reference = 0; +static uint8_t custom_wrap_timer_last_value = 0; + +static uint8_t custom_wrap_timer_read(void) { + uint16_t custom_wrap_timer_new_reference = timer_read(); + uint16_t diff = custom_wrap_timer_new_reference - custom_wrap_timer_reference; + custom_wrap_timer_reference = custom_wrap_timer_new_reference; + custom_wrap_timer_last_value = (custom_wrap_timer_last_value + diff) % (MAX_DEBOUNCE + 1); + return custom_wrap_timer_last_value; +} + void update_debounce_counters(uint8_t num_rows, uint8_t current_time); void transfer_matrix_values(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, uint8_t current_time); @@ -48,7 +59,7 @@ void debounce_init(uint8_t num_rows) { } void debounce(matrix_row_t raw[], matrix_row_t cooked[], uint8_t num_rows, bool changed) { - uint8_t current_time = timer_read() % MAX_DEBOUNCE; + uint8_t current_time = custom_wrap_timer_read(); bool needed_update = counters_need_update; if (counters_need_update) { update_debounce_counters(num_rows, current_time);