1
0

Improve debouncing algorithm

This commit is contained in:
Kai Ryu 2014-08-11 17:55:04 +09:00
parent 74929866fa
commit 1f125e1c9b

View File

@ -92,42 +92,24 @@ uint8_t matrix_scan(void)
{ {
matrix_row_t cols = read_cols(); matrix_row_t cols = read_cols();
for (uint8_t col = 0; col < MATRIX_COLS; col++) { for (uint8_t col = 0; col < MATRIX_COLS; col++) {
if ((cols & (1<<col)) != (matrix_debouncing & (1<<col))) {
// state changed
if (debouncing & (1<<col)) { if (debouncing & (1<<col)) {
// debouncing
if (timer_elapsed(debouncing_last[col]) > DEBOUNCE) { if (timer_elapsed(debouncing_last[col]) > DEBOUNCE) {
// released
matrix &= ~(1<<col);
debouncing &= ~(1<<col); debouncing &= ~(1<<col);
} if (!(cols & (1<<col))) {
else { matrix &= ~(1<<col);
// reset debounce time
debouncing_last[col] = timer_read();
} }
} }
else { }
if (cols & (1<<col)) { if ((cols & (1<<col)) != (matrix_debouncing & (1<<col))) {
// pressed if (!(debouncing & (1<<col)) && (cols & (1<<col))) {
matrix |= (1<<col); matrix |= (1<<col);
} }
else { else {
// start debounce
debouncing_last[col] = timer_read(); debouncing_last[col] = timer_read();
debouncing |= (1<<col); debouncing |= (1<<col);
} }
} }
} }
else {
if (debouncing & (1<<col)) {
if (timer_elapsed(debouncing_last[col]) > DEBOUNCE) {
// released
matrix &= ~(1<<col);
debouncing &= ~(1<<col);
}
}
}
}
matrix_debouncing = cols; matrix_debouncing = cols;
/* /*