diff options
-rw-r--r-- | quantum/debounce/eager_pk.c | 12 | ||||
-rw-r--r-- | quantum/debounce/eager_pr.c | 12 | ||||
-rw-r--r-- | tmk_core/common/timer.h | 5 |
3 files changed, 26 insertions, 3 deletions
diff --git a/quantum/debounce/eager_pk.c b/quantum/debounce/eager_pk.c index 76b978d059..4676892243 100644 --- a/quantum/debounce/eager_pk.c +++ b/quantum/debounce/eager_pk.c @@ -44,6 +44,16 @@ static bool matrix_need_update; #define DEBOUNCE_ELAPSED 251 #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) +static uint8_t wrapping_timer_read(void) { + static uint16_t time = 0; + static uint8_t last_result = 0; + uint16_t new_time = timer_read(); + uint16_t diff = new_time - time; + time = new_time; + last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); + return last_result; +} + 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 +69,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 = wrapping_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 173ad15ee9..41843aedbc 100644 --- a/quantum/debounce/eager_pr.c +++ b/quantum/debounce/eager_pr.c @@ -36,6 +36,16 @@ static bool counters_need_update; #define DEBOUNCE_ELAPSED 251 #define MAX_DEBOUNCE (DEBOUNCE_ELAPSED - 1) +static uint8_t wrapping_timer_read(void) { + static uint16_t time = 0; + static uint8_t last_result = 0; + uint16_t new_time = timer_read(); + uint16_t diff = new_time - time; + time = new_time; + last_result = (last_result + diff) % (MAX_DEBOUNCE + 1); + return last_result; +} + 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 +58,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 = wrapping_timer_read(); bool needed_update = counters_need_update; if (counters_need_update) { update_debounce_counters(num_rows, current_time); diff --git a/tmk_core/common/timer.h b/tmk_core/common/timer.h index bbaae109d0..117b41d1be 100644 --- a/tmk_core/common/timer.h +++ b/tmk_core/common/timer.h @@ -25,7 +25,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. # include "avr/timer_avr.h" #endif -#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a)) +#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) )))) #define TIMER_DIFF_8(a, b) TIMER_DIFF(a, b, UINT8_MAX) #define TIMER_DIFF_16(a, b) TIMER_DIFF(a, b, UINT16_MAX) #define TIMER_DIFF_32(a, b) TIMER_DIFF(a, b, UINT32_MAX) |