X-Git-Url: https://git.friedersdorff.com/?a=blobdiff_plain;f=macway%2Fmatrix.c;h=56fb85896025311ad85cfac7aa485d94a623a239;hb=e157aa7f91814864a75ecdb84188a443d63acc86;hp=20cfd9dd60f610131976eabfaf5ccad494e36290;hpb=0dde25e81cc06ed6331c030839b7048c69fc9c84;p=max%2Ftmk_keyboard.git diff --git a/macway/matrix.c b/macway/matrix.c index 20cfd9dd..56fb8589 100644 --- a/macway/matrix.c +++ b/macway/matrix.c @@ -23,6 +23,7 @@ along with this program. If not, see . #include #include #include "print.h" +#include "debug.h" #include "util.h" #include "matrix.h" @@ -35,6 +36,11 @@ along with this program. If not, see . #endif +#ifndef DEBOUNCE +# define DEBOUNCE 0 +#endif +static uint8_t debouncing = DEBOUNCE; + // matrix state buffer(1:on, 0:off) #if (MATRIX_COLS <= 8) static uint8_t *matrix; @@ -85,27 +91,40 @@ void matrix_init(void) uint8_t matrix_scan(void) { - uint8_t *tmp; - - tmp = matrix_prev; - matrix_prev = matrix; - matrix = tmp; + if (!debouncing) { + uint8_t *tmp = matrix_prev; + matrix_prev = matrix; + matrix = tmp; + } for (uint8_t i = 0; i < MATRIX_ROWS; i++) { unselect_rows(); select_row(i); _delay_us(30); // without this wait read unstable value. - matrix[i] = ~read_col(); + if (matrix[i] != (uint8_t)~read_col()) { + matrix[i] = (uint8_t)~read_col(); + if (debouncing) { + debug("bounce!: "); debug_hex(debouncing); print("\n"); + } + debouncing = DEBOUNCE; + } } unselect_rows(); + + if (debouncing) { + debouncing--; + } + return 1; } bool matrix_is_modified(void) { + if (debouncing) return false; for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - if (matrix[i] != matrix_prev[i]) + if (matrix[i] != matrix_prev[i]) { return true; + } } return false; }