pana_m8: Add debouncing

This commit is contained in:
tmk 2017-08-21 23:23:34 +09:00
parent 9ac2580687
commit 894e393c67
2 changed files with 19 additions and 5 deletions

View file

@ -14,6 +14,9 @@
#define MATRIX_ROWS 8 #define MATRIX_ROWS 8
#define MATRIX_COLS 8 #define MATRIX_COLS 8
/* matrix debounce time in ms */
#define DEBOUNCE 10
/* key combination for command */ /* key combination for command */
#define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT))) #define IS_COMMAND() (keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)))

View file

@ -3,16 +3,19 @@
#include "matrix.h" #include "matrix.h"
#include "led.h" #include "led.h"
#include "wait.h" #include "wait.h"
#include "timer.h"
#include "debug.h" #include "debug.h"
#define CLK_HI() (PORTD |= (1<<0)) #define CLK_HI() (PORTD |= (1<<0))
#define CLK_LO() (PORTD &= ~(1<<0)) #define CLK_LO() (PORTD &= ~(1<<0))
#define STATE() (PIND & (1<<1)) #define STATE() (!!(PIND & (1<<1)))
#define RST_HI() (PORTD |= (1<<3)) #define RST_HI() (PORTD |= (1<<3))
#define RST_LO() (PORTD &= ~(1<<3)) #define RST_LO() (PORTD &= ~(1<<3))
#define SENSE() (PIND & (1<<2)) #define SENSE() (PIND & (1<<2))
static matrix_row_t matrix[8] = {}; static matrix_row_t matrix[8] = {};
static matrix_row_t matrix_debouncing[8] = {};
static uint16_t debouncing_time = 0;
void matrix_init(void) void matrix_init(void)
@ -48,10 +51,10 @@ uint8_t matrix_scan(void)
CLK_HI(); CLK_HI();
wait_us(10); wait_us(10);
if (STATE()) { // detect state change and start debounce
matrix[row] |= (1<<col); if ((matrix_debouncing[row] & (1<<col)) ^ (STATE()<<col)) {
} else { matrix_debouncing[row] ^= (1<<col);
matrix[row] &= ~(1<<col); debouncing_time = timer_read() || 1;
} }
// proceed counter - next row // proceed counter - next row
@ -59,6 +62,14 @@ uint8_t matrix_scan(void)
wait_us(10); wait_us(10);
} }
} }
// debounced
if (debouncing_time && timer_elapsed(debouncing_time) > DEBOUNCE) {
for (int row = 0; row < MATRIX_ROWS; row++) {
matrix[row] = matrix_debouncing[row];
}
debouncing_time = 0;
}
return 1; return 1;
} }