diff options
author | tmk <nobody@nowhere> | 2011-09-29 17:08:57 +0900 |
---|---|---|
committer | tmk <nobody@nowhere> | 2011-09-29 17:08:57 +0900 |
commit | 6284b147c23aa32a9e65138b1eb8ee908ece4941 (patch) | |
tree | a4770122b0c61f2c44ea4153628485515245b452 /macway | |
parent | e65575d4a5f03a90adfa9850cb71a5d0448098cd (diff) |
add debouncing into macway/matrix.c.
Diffstat (limited to 'macway')
-rw-r--r-- | macway/config.h | 2 | ||||
-rw-r--r-- | macway/matrix.c | 33 |
2 files changed, 28 insertions, 7 deletions
diff --git a/macway/config.h b/macway/config.h index 546067beb1..2e68bf252f 100644 --- a/macway/config.h +++ b/macway/config.h @@ -33,6 +33,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #define MATRIX_COLS 8 /* define if matrix has ghost */ #define MATRIX_HAS_GHOST +/* Set 0 if need no debouncing */ +#define DEBOUNCE 5 /* key combination for command */ diff --git a/macway/matrix.c b/macway/matrix.c index 20cfd9dd60..56fb858960 100644 --- a/macway/matrix.c +++ b/macway/matrix.c @@ -23,6 +23,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #include <avr/io.h> #include <util/delay.h> #include "print.h" +#include "debug.h" #include "util.h" #include "matrix.h" @@ -35,6 +36,11 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. #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; } |