diff options
Diffstat (limited to 'keyboard/planck/matrix.c')
-rw-r--r-- | keyboard/planck/matrix.c | 65 |
1 files changed, 64 insertions, 1 deletions
diff --git a/keyboard/planck/matrix.c b/keyboard/planck/matrix.c index 98ef55ed68..8a021719ce 100644 --- a/keyboard/planck/matrix.c +++ b/keyboard/planck/matrix.c @@ -37,6 +37,11 @@ static uint8_t debouncing = DEBOUNCE; static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix_debouncing[MATRIX_ROWS]; +#if DIODE_DIRECTION == ROW2COL +static matrix_row_t matrix_reversed[MATRIX_COLS]; +static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS]; +#endif + static matrix_row_t read_cols(void); static void init_cols(void); static void unselect_rows(void); @@ -80,6 +85,7 @@ void matrix_init(void) uint8_t matrix_scan(void) { +#if DIODE_DIRECTION == COL2ROW for (uint8_t i = 0; i < MATRIX_ROWS; i++) { select_row(i); _delay_us(30); // without this wait read unstable value. @@ -103,6 +109,38 @@ uint8_t matrix_scan(void) } } } +#else + for (uint8_t i = 0; i < MATRIX_COLS; i++) { + select_row(i); + _delay_us(30); // without this wait read unstable value. + matrix_row_t rows = read_cols(); + if (matrix_reversed_debouncing[i] != rows) { + matrix_reversed_debouncing[i] = rows; + if (debouncing) { + debug("bounce!: "); debug_hex(debouncing); debug("\n"); + } + debouncing = DEBOUNCE; + } + unselect_rows(); + } + + if (debouncing) { + if (--debouncing) { + _delay_ms(1); + } else { + for (uint8_t i = 0; i < MATRIX_COLS; i++) { + matrix_reversed[i] = matrix_reversed_debouncing[i]; + } + } + } + for (uint8_t y = 0; y < MATRIX_ROWS; y++) { + matrix_row_t row = 0; + for (uint8_t x = 0; x < MATRIX_COLS; x++) { + row |= ((matrix_reversed[x] & (1<<y)) >> y) << x; + } + matrix[y] = row; + } +#endif return 1; } @@ -147,8 +185,14 @@ uint8_t matrix_key_count(void) static void init_cols(void) { int B = 0, C = 0, D = 0, E = 0, F = 0; - for(int x = 0; x < MATRIX_COLS; x++) { + +#if DIODE_DIRECTION == COL2ROW + for(int x = 0; x < MATRIX_COLS; x++) { int col = COLS[x]; +#else + for(int x = 0; x < MATRIX_ROWS; x++) { + int col = ROWS[x]; +#endif if ((col & 0xF0) == 0x20) { B |= (1<<(col & 0x0F)); } else if ((col & 0xF0) == 0x30) { @@ -171,8 +215,15 @@ static void init_cols(void) static matrix_row_t read_cols(void) { matrix_row_t result = 0; + +#if DIODE_DIRECTION == COL2ROW for(int x = 0; x < MATRIX_COLS; x++) { int col = COLS[x]; +#else + for(int x = 0; x < MATRIX_ROWS; x++) { + int col = ROWS[x]; +#endif + if ((col & 0xF0) == 0x20) { result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x)); } else if ((col & 0xF0) == 0x30) { @@ -191,8 +242,14 @@ static matrix_row_t read_cols(void) static void unselect_rows(void) { int B = 0, C = 0, D = 0, E = 0, F = 0; + +#if DIODE_DIRECTION == COL2ROW for(int x = 0; x < MATRIX_ROWS; x++) { int row = ROWS[x]; +#else + for(int x = 0; x < MATRIX_COLS; x++) { + int row = COLS[x]; +#endif if ((row & 0xF0) == 0x20) { B |= (1<<(row & 0x0F)); } else if ((row & 0xF0) == 0x30) { @@ -214,7 +271,13 @@ static void unselect_rows(void) static void select_row(uint8_t row) { + +#if DIODE_DIRECTION == COL2ROW int row_pin = ROWS[row]; +#else + int row_pin = COLS[row]; +#endif + if ((row_pin & 0xF0) == 0x20) { DDRB |= (1<<(row_pin & 0x0F)); PORTB &= ~(1<<(row_pin & 0x0F)); |