From 1b581bc173ef1a72852069c9452ee28adb2fbb1d Mon Sep 17 00:00:00 2001 From: tmk Date: Thu, 3 Oct 2019 13:41:48 +0900 Subject: [PATCH] alps64: Change debouncing using timer count --- keyboard/alps64/matrix.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/keyboard/alps64/matrix.c b/keyboard/alps64/matrix.c index 109bcf2c..4a1c84c6 100644 --- a/keyboard/alps64/matrix.c +++ b/keyboard/alps64/matrix.c @@ -25,13 +25,15 @@ along with this program. If not, see . #include "print.h" #include "debug.h" #include "util.h" +#include "timer.h" #include "matrix.h" #ifndef DEBOUNCE # define DEBOUNCE 5 #endif -static uint8_t debouncing = DEBOUNCE; +static bool debouncing = false; +static uint16_t debouncing_time = 0; /* matrix state(1:on, 0:off) */ static matrix_row_t matrix[MATRIX_ROWS]; @@ -70,26 +72,24 @@ uint8_t matrix_scan(void) { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { select_row(i); - _delay_us(30); // without this wait read unstable value. + _delay_us(1); // delay for settling matrix_row_t cols = read_cols(); if (matrix_debouncing[i] != cols) { - matrix_debouncing[i] = cols; if (debouncing) { - debug("bounce!: "); debug_hex(debouncing); debug("\n"); + dprintf("bounce: %d %d@%02X\n", timer_elapsed(debouncing_time), i, matrix_debouncing[i]^cols); } - debouncing = DEBOUNCE; + matrix_debouncing[i] = cols; + debouncing = true; + debouncing_time = timer_read(); } unselect_rows(); } - if (debouncing) { - if (--debouncing) { - _delay_ms(1); - } else { - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - matrix[i] = matrix_debouncing[i]; - } + if (debouncing && timer_elapsed(debouncing_time) >= DEBOUNCE) { + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + matrix[i] = matrix_debouncing[i]; } + debouncing = false; } return 1; -- 2.46.2