Refactoring Matrix scanning

master
IBNobody 2016-10-28 16:24:20 -05:00
parent 508eddf8ba
commit 4c6960835c
2 changed files with 95 additions and 88 deletions

View File

@ -43,16 +43,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define ROW_SHIFTER ((uint32_t)1) # define ROW_SHIFTER ((uint32_t)1)
#endif #endif
#if (MATRIX_ROWS <= 8)
# define COL_SHIFTER ((uint8_t)1)
#elif (MATRIX_ROWS <= 16)
# define COL_SHIFTER ((uint16_t)1)
#elif (MATRIX_ROWS <= 32)
# define COL_SHIFTER ((uint32_t)1)
#endif
#ifdef MATRIX_MASKED #ifdef MATRIX_MASKED
extern const matrix_row_t matrix_mask[]; extern const matrix_row_t matrix_mask[];
#endif #endif
@ -70,6 +60,9 @@ static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
/* matrix state(1:on, 0:off) */ /* matrix state(1:on, 0:off) */
static matrix_row_t matrix[MATRIX_ROWS]; static matrix_row_t matrix[MATRIX_ROWS];
static matrix_row_t matrix_raw[MATRIX_ROWS];
#if DIODE_DIRECTION == COL2ROW #if DIODE_DIRECTION == COL2ROW
static matrix_row_t matrix_debouncing[MATRIX_ROWS]; static matrix_row_t matrix_debouncing[MATRIX_ROWS];
#else // ROW2COL #else // ROW2COL
@ -79,13 +72,13 @@ static matrix_row_t matrix[MATRIX_ROWS];
#if (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == COL2ROW)
static void init_cols(void); static void init_cols(void);
static matrix_row_t read_cols(void); static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
static void unselect_rows(void); static void unselect_rows(void);
static void select_row(uint8_t row); static void select_row(uint8_t row);
static void unselect_row(uint8_t row); static void unselect_row(uint8_t row);
#else // ROW2COL #else // ROW2COL
static void init_rows(void); static void init_rows(void);
static matrix_col_t read_rows(void); static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
static void unselect_cols(void); static void unselect_cols(void);
static void unselect_col(uint8_t col); static void unselect_col(uint8_t col);
static void select_col(uint8_t col); static void select_col(uint8_t col);
@ -169,6 +162,7 @@ void matrix_init(void) {
// initialize matrix state: all keys off // initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) { for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix[i] = 0; matrix[i] = 0;
matrix_raw[i] = 0;
matrix_debouncing[i] = 0; matrix_debouncing[i] = 0;
} }
@ -178,6 +172,7 @@ void matrix_init(void) {
// initialize matrix state: all keys off // initialize matrix state: all keys off
for (uint8_t i=0; i < MATRIX_ROWS; i++) { for (uint8_t i=0; i < MATRIX_ROWS; i++) {
matrix_raw[i] = 0;
matrix[i] = 0; matrix[i] = 0;
} }
@ -196,67 +191,73 @@ uint8_t matrix_scan(void)
#if (DIODE_DIRECTION == COL2ROW) #if (DIODE_DIRECTION == COL2ROW)
// Set row, read cols // Set row, read cols
for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { read_cols_on_row(matrix, current_row);
select_row(i);
wait_us(30); // without this wait read unstable value.
matrix_row_t current_row = read_cols();
if (matrix_debouncing[i] != current_row) {
matrix_debouncing[i] = current_row;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCING_DELAY;
}
unselect_row(i);
} }
if (debouncing) { // select_row(i);
if (--debouncing) { // wait_us(30); // without this wait read unstable value.
wait_ms(1); // matrix_row_t current_row = read_cols();
} else { // if (matrix_debouncing[i] != current_row) {
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { // matrix_debouncing[i] = current_row;
matrix[i] = matrix_debouncing[i]; // if (debouncing) {
} // debug("bounce!: "); debug_hex(debouncing); debug("\n");
} // }
} // debouncing = DEBOUNCING_DELAY;
// }
// unselect_row(i);
// }
// if (debouncing) {
// if (--debouncing) {
// wait_ms(1);
// } else {
// for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
// matrix[i] = matrix_debouncing[i];
// }
// }
// }
#else // ROW2COL #else // ROW2COL
// Set col, read rows // Set col, read rows
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
for (uint8_t i = 0; i < MATRIX_COLS; i++) { read_rows_on_col(matrix, current_col);
select_col(i);
wait_us(30); // without this wait read unstable value.
matrix_col_t current_col = read_rows();
if (matrix_transposed_debouncing[i] != current_col) {
matrix_transposed_debouncing[i] = current_col;
if (debouncing) {
debug("bounce!: "); debug_hex(debouncing); debug("\n");
}
debouncing = DEBOUNCING_DELAY;
}
unselect_col(i);
} }
if (debouncing) {
if (--debouncing) {
wait_ms(1);
} else {
for (uint8_t i = 0; i < MATRIX_COLS; i++) {
matrix_transposed[i] = matrix_transposed_debouncing[i];
}
}
}
// Untranspose matrix // for (uint8_t i = 0; i < MATRIX_COLS; i++) {
for (uint8_t y = 0; y < MATRIX_ROWS; y++) { // select_col(i);
matrix_row_t row = 0; // wait_us(30); // without this wait read unstable value.
for (uint8_t x = 0; x < MATRIX_COLS; x++) { // matrix_col_t current_col = read_rows();
row |= ((matrix_transposed[x] & (1<<y)) >> y) << x; // if (matrix_transposed_debouncing[i] != current_col) {
} // matrix_transposed_debouncing[i] = current_col;
matrix[y] = row; // if (debouncing) {
} // debug("bounce!: "); debug_hex(debouncing); debug("\n");
// }
// debouncing = DEBOUNCING_DELAY;
// }
// unselect_col(i);
// }
// if (debouncing) {
// if (--debouncing) {
// wait_ms(1);
// } else {
// for (uint8_t i = 0; i < MATRIX_COLS; i++) {
// matrix_transposed[i] = matrix_transposed_debouncing[i];
// }
// }
// }
// // Untranspose matrix
// 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_transposed[x] & (1<<y)) >> y) << x;
// }
// matrix[y] = row;
// }
#endif #endif
@ -322,16 +323,25 @@ static void init_cols(void)
} }
} }
static matrix_row_t read_cols(void) static void read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
{ {
matrix_row_t result = 0; // Clear data in matrix row
current_matrix[current_row] = 0;
for(uint8_t x = 0; x < MATRIX_COLS; x++) { // Select row and wait for row selecton to stabilize
uint8_t pin = col_pins[x]; select_row(current_row);
result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (ROW_SHIFTER << x); wait_us(30);
// For each col...
for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
// Select the col pin to read (active low)
uint8_t pin = col_pins[col_index];
uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
// Populate the matrix row with the state of the col pin
current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
} }
return result;
} }
static void select_row(uint8_t row) static void select_row(uint8_t row)
@ -368,16 +378,23 @@ static void init_rows(void)
} }
} }
static matrix_col_t read_rows(void) static void read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
{ {
matrix_col_t result = 0;
for(uint8_t x = 0; x < MATRIX_ROWS; x++) { // Select col and wait for col selecton to stabilize
uint8_t pin = row_pins[x]; select_col(current_col);
result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (COL_SHIFTER << x); wait_us(30);
// For each row...
for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
// Select the row pin to read (active low)
uint8_t pin = row_pins[row_index];
uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
// Populate the matrix row with the state of the col pin
current_matrix[row_index] &= pin_state ? ~(ROW_SHIFTER << current_col) : 0;
} }
return result;
} }
static void select_col(uint8_t col) static void select_col(uint8_t col)

View File

@ -31,16 +31,6 @@ typedef uint32_t matrix_row_t;
#error "MATRIX_COLS: invalid value" #error "MATRIX_COLS: invalid value"
#endif #endif
#if (MATRIX_ROWS <= 8)
typedef uint8_t matrix_col_t;
#elif (MATRIX_ROWS <= 16)
typedef uint16_t matrix_col_t;
#elif (MATRIX_ROWS <= 32)
typedef uint32_t matrix_col_t;
#else
#error "MATRIX_COLS: invalid value"
#endif
#define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1<<col)) #define MATRIX_IS_ON(row, col) (matrix_get_row(row) && (1<<col))