From 205321c37740caaffbca69e626a8432fd369d20c Mon Sep 17 00:00:00 2001 From: Joel Challis Date: Thu, 21 May 2020 17:59:56 +0100 Subject: Slight speed increases for matrix scanning (#9150) --- quantum/matrix.c | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) (limited to 'quantum/matrix.c') diff --git a/quantum/matrix.c b/quantum/matrix.c index 67d8af6ee8..9e998508a7 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; } } -- cgit v1.2.3