aboutsummaryrefslogtreecommitdiffstats
path: root/quantum/matrix.c
diff options
context:
space:
mode:
authorGravatar Joel Challis <git@zvecr.com>2020-05-21 17:59:56 +0100
committerGravatar GitHub <noreply@github.com>2020-05-21 17:59:56 +0100
commit205321c37740caaffbca69e626a8432fd369d20c (patch)
treee0efcd82de1bd537368be7501684529e71a73dec /quantum/matrix.c
parent1816ad01d0efcc987b5aa7a833331b0f9d903d4f (diff)
downloadqmk_firmware-205321c37740caaffbca69e626a8432fd369d20c.tar.gz
Slight speed increases for matrix scanning (#9150)
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c
index 67d8af6ee..9e998508a 100644
--- a/quantum/matrix.c
+++ b/quantum/matrix.c
@@ -48,17 +48,22 @@ static void init_pins(void) {
}
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];
- current_matrix[current_row] = 0;
+ // Start with a clear matrix row
+ matrix_row_t current_row_value = 0;
for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
pin_t pin = direct_pins[current_row][col_index];
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)
@@ -84,12 +89,9 @@ static void init_pins(void) {
}
}
-static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
- // Store last value of row prior to reading
- matrix_row_t last_row_value = current_matrix[current_row];
-
- // Clear data in matrix row
- current_matrix[current_row] = 0;
+static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
+ // Start with a clear matrix row
+ matrix_row_t current_row_value = 0;
// Select row and wait for row selecton to stabilize
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]);
// 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(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)
@@ -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++) {
// Store last value of row prior to reading
matrix_row_t last_row_value = current_matrix[row_index];
+ matrix_row_t current_row_value = last_row_value;
// Check row pin state
if (readPin(row_pins[row_index]) == 0) {
// Pin LO, set col bit
- current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
+ current_row_value |= (MATRIX_ROW_SHIFTER << current_col);
} else {
// 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
- if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
- matrix_changed = true;
+ if ((last_row_value != current_row_value)) {
+ matrix_changed |= true;
+ current_matrix[row_index] = current_row_value;
}
}