Slight speed increases for matrix scanning (#9150)

This commit is contained in:
Joel Challis 2020-05-21 17:59:56 +01:00 committed by GitHub
parent 1816ad01d0
commit 205321c377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 32 deletions

View file

@ -48,17 +48,22 @@ static void init_pins(void) {
} }
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
matrix_row_t last_row_value = current_matrix[current_row]; // Start with a clear matrix row
current_matrix[current_row] = 0; matrix_row_t current_row_value = 0;
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
pin_t pin = direct_pins[current_row][col_index]; pin_t pin = direct_pins[current_row][col_index];
if (pin != NO_PIN) { if (pin != NO_PIN) {
current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
} }
} }
return (last_row_value != current_matrix[current_row]); // If the row has changed, store the row and return the changed flag.
if (current_matrix[current_row] != current_row_value) {
current_matrix[current_row] = current_row_value;
return true;
}
return false;
} }
#elif defined(DIODE_DIRECTION) #elif defined(DIODE_DIRECTION)
@ -84,12 +89,9 @@ static void init_pins(void) {
} }
} }
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
// Store last value of row prior to reading // Start with a clear matrix row
matrix_row_t last_row_value = current_matrix[current_row]; matrix_row_t current_row_value = 0;
// Clear data in matrix row
current_matrix[current_row] = 0;
// Select row and wait for row selecton to stabilize // Select row and wait for row selecton to stabilize
select_row(current_row); select_row(current_row);
@ -101,13 +103,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
uint8_t pin_state = readPin(col_pins[col_index]); uint8_t pin_state = readPin(col_pins[col_index]);
// Populate the matrix row with the state of the col pin // Populate the matrix row with the state of the col pin
current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
} }
// Unselect row // Unselect row
unselect_row(current_row); unselect_row(current_row);
return (last_row_value != current_matrix[current_row]); // If the row has changed, store the row and return the changed flag.
if (current_matrix[current_row] != current_row_value) {
current_matrix[current_row] = current_row_value;
return true;
}
return false;
} }
# elif (DIODE_DIRECTION == ROW2COL) # elif (DIODE_DIRECTION == ROW2COL)
@ -143,19 +150,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
// Store last value of row prior to reading // Store last value of row prior to reading
matrix_row_t last_row_value = current_matrix[row_index]; matrix_row_t last_row_value = current_matrix[row_index];
matrix_row_t current_row_value = last_row_value;
// Check row pin state // Check row pin state
if (readPin(row_pins[row_index]) == 0) { if (readPin(row_pins[row_index]) == 0) {
// Pin LO, set col bit // Pin LO, set col bit
current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
} else { } else {
// Pin HI, clear col bit // Pin HI, clear col bit
current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
} }
// Determine if the matrix changed state // Determine if the matrix changed state
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { if ((last_row_value != current_row_value)) {
matrix_changed = true; matrix_changed |= true;
current_matrix[row_index] = current_row_value;
} }
} }

View file

@ -65,17 +65,22 @@ static void init_pins(void) {
} }
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
matrix_row_t last_row_value = current_matrix[current_row]; // Start with a clear matrix row
current_matrix[current_row] = 0; matrix_row_t current_row_value = 0;
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
pin_t pin = direct_pins[current_row][col_index]; pin_t pin = direct_pins[current_row][col_index];
if (pin != NO_PIN) { if (pin != NO_PIN) {
current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
} }
} }
return (last_row_value != current_matrix[current_row]); // If the row has changed, store the row and return the changed flag.
if (current_matrix[current_row] != current_row_value) {
current_matrix[current_row] = current_row_value;
return true;
}
return false;
} }
#elif defined(DIODE_DIRECTION) #elif defined(DIODE_DIRECTION)
@ -101,12 +106,9 @@ static void init_pins(void) {
} }
} }
static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
// Store last value of row prior to reading // Start with a clear matrix row
matrix_row_t last_row_value = current_matrix[current_row]; matrix_row_t current_row_value = 0;
// Clear data in matrix row
current_matrix[current_row] = 0;
// Select row and wait for row selecton to stabilize // Select row and wait for row selecton to stabilize
select_row(current_row); select_row(current_row);
@ -118,13 +120,18 @@ static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
uint8_t pin_state = readPin(col_pins[col_index]); uint8_t pin_state = readPin(col_pins[col_index]);
// Populate the matrix row with the state of the col pin // Populate the matrix row with the state of the col pin
current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
} }
// Unselect row // Unselect row
unselect_row(current_row); unselect_row(current_row);
return (last_row_value != current_matrix[current_row]); // If the row has changed, store the row and return the changed flag.
if (current_matrix[current_row] != current_row_value) {
current_matrix[current_row] = current_row_value;
return true;
}
return false;
} }
# elif (DIODE_DIRECTION == ROW2COL) # elif (DIODE_DIRECTION == ROW2COL)
@ -160,19 +167,21 @@ static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) {
// Store last value of row prior to reading // Store last value of row prior to reading
matrix_row_t last_row_value = current_matrix[row_index]; matrix_row_t last_row_value = current_matrix[row_index];
matrix_row_t current_row_value = last_row_value;
// Check row pin state // Check row pin state
if (readPin(row_pins[row_index]) == 0) { if (readPin(row_pins[row_index]) == 0) {
// Pin LO, set col bit // Pin LO, set col bit
current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
} else { } else {
// Pin HI, clear col bit // Pin HI, clear col bit
current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); current_row_value &= ~(MATRIX_ROW_SHIFTER << current_col);
} }
// Determine if the matrix changed state // Determine if the matrix changed state
if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) { if ((last_row_value != current_row_value)) {
matrix_changed = true; matrix_changed |= true;
current_matrix[row_index] = current_row_value;
} }
} }