diff options
author | Chad Austin <chad@chadaustin.me> | 2021-10-26 20:01:57 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-26 20:01:57 -0700 |
commit | ee23aae87fac7b5dca24c1f150465bae1c66cb6c (patch) | |
tree | ed12415bd5ca680dc11a8fe3cd112b7429801baf | |
parent | 6506e271a32123963ff490654f1d54be2b58501f (diff) | |
download | qmk_firmware-ee23aae87fac7b5dca24c1f150465bae1c66cb6c.tar.gz qmk_firmware-ee23aae87fac7b5dca24c1f150465bae1c66cb6c.zip |
Optimize matrix scanning by removing variable shifts (#14947)
-rw-r--r-- | docs/custom_quantum_functions.md | 2 | ||||
-rw-r--r-- | quantum/matrix.c | 23 |
2 files changed, 14 insertions, 11 deletions
diff --git a/docs/custom_quantum_functions.md b/docs/custom_quantum_functions.md index 463366ff7..798c346e6 100644 --- a/docs/custom_quantum_functions.md +++ b/docs/custom_quantum_functions.md | |||
@@ -149,7 +149,7 @@ This is useful for setting up stuff that you may need elsewhere, but isn't hardw | |||
149 | * GPIO pin initialisation: `void matrix_init_pins(void)` | 149 | * GPIO pin initialisation: `void matrix_init_pins(void)` |
150 | * This needs to perform the low-level initialisation of all row and column pins. By default this will initialise the input/output state of each of the GPIO pins listed in `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`, based on whether or not the keyboard is set up for `ROW2COL`, `COL2ROW`, or `DIRECT_PINS`. Should the keyboard designer override this function, no initialisation of pin state will occur within QMK itself, instead deferring to the keyboard's override. | 150 | * This needs to perform the low-level initialisation of all row and column pins. By default this will initialise the input/output state of each of the GPIO pins listed in `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`, based on whether or not the keyboard is set up for `ROW2COL`, `COL2ROW`, or `DIRECT_PINS`. Should the keyboard designer override this function, no initialisation of pin state will occur within QMK itself, instead deferring to the keyboard's override. |
151 | * `COL2ROW`-based row reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)` | 151 | * `COL2ROW`-based row reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)` |
152 | * `ROW2COL`-based column reads: `void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)` | 152 | * `ROW2COL`-based column reads: `void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter)` |
153 | * `DIRECT_PINS`-based reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)` | 153 | * `DIRECT_PINS`-based reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)` |
154 | * These three functions need to perform the low-level retrieval of matrix state of relevant input pins, based on the matrix type. Only one of the functions should be implemented, if needed. By default this will iterate through `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`, configuring the inputs and outputs based on whether or not the keyboard is set up for `ROW2COL`, `COL2ROW`, or `DIRECT_PINS`. Should the keyboard designer override this function, no manipulation of matrix GPIO pin state will occur within QMK itself, instead deferring to the keyboard's override. | 154 | * These three functions need to perform the low-level retrieval of matrix state of relevant input pins, based on the matrix type. Only one of the functions should be implemented, if needed. By default this will iterate through `MATRIX_ROW_PINS` and `MATRIX_COL_PINS`, configuring the inputs and outputs based on whether or not the keyboard is set up for `ROW2COL`, `COL2ROW`, or `DIRECT_PINS`. Should the keyboard designer override this function, no manipulation of matrix GPIO pin state will occur within QMK itself, instead deferring to the keyboard's override. |
155 | 155 | ||
diff --git a/quantum/matrix.c b/quantum/matrix.c index 4fbcc2419..483d518ec 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
@@ -69,7 +69,7 @@ uint8_t thisHand, thatHand; | |||
69 | // user-defined overridable functions | 69 | // user-defined overridable functions |
70 | __attribute__((weak)) void matrix_init_pins(void); | 70 | __attribute__((weak)) void matrix_init_pins(void); |
71 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | 71 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); |
72 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | 72 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter); |
73 | #ifdef SPLIT_KEYBOARD | 73 | #ifdef SPLIT_KEYBOARD |
74 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | 74 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } |
75 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | 75 | __attribute__((weak)) void matrix_slave_scan_user(void) {} |
@@ -113,10 +113,11 @@ __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[] | |||
113 | // Start with a clear matrix row | 113 | // Start with a clear matrix row |
114 | matrix_row_t current_row_value = 0; | 114 | matrix_row_t current_row_value = 0; |
115 | 115 | ||
116 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 116 | matrix_row_t row_shifter = MATRIX_ROW_SHIFTER; |
117 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) { | ||
117 | pin_t pin = direct_pins[current_row][col_index]; | 118 | pin_t pin = direct_pins[current_row][col_index]; |
118 | if (pin != NO_PIN) { | 119 | if (pin != NO_PIN) { |
119 | current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); | 120 | current_row_value |= readPin(pin) ? 0 : row_shifter; |
120 | } | 121 | } |
121 | } | 122 | } |
122 | 123 | ||
@@ -169,11 +170,12 @@ __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[] | |||
169 | matrix_output_select_delay(); | 170 | matrix_output_select_delay(); |
170 | 171 | ||
171 | // For each col... | 172 | // For each col... |
172 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | 173 | matrix_row_t row_shifter = MATRIX_ROW_SHIFTER; |
174 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) { | ||
173 | uint8_t pin_state = readMatrixPin(col_pins[col_index]); | 175 | uint8_t pin_state = readMatrixPin(col_pins[col_index]); |
174 | 176 | ||
175 | // Populate the matrix row with the state of the col pin | 177 | // Populate the matrix row with the state of the col pin |
176 | current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | 178 | current_row_value |= pin_state ? 0 : row_shifter; |
177 | } | 179 | } |
178 | 180 | ||
179 | // Unselect row | 181 | // Unselect row |
@@ -217,7 +219,7 @@ __attribute__((weak)) void matrix_init_pins(void) { | |||
217 | } | 219 | } |
218 | } | 220 | } |
219 | 221 | ||
220 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | 222 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) { |
221 | bool key_pressed = false; | 223 | bool key_pressed = false; |
222 | 224 | ||
223 | // Select col | 225 | // Select col |
@@ -231,11 +233,11 @@ __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[] | |||
231 | // Check row pin state | 233 | // Check row pin state |
232 | if (readMatrixPin(row_pins[row_index]) == 0) { | 234 | if (readMatrixPin(row_pins[row_index]) == 0) { |
233 | // Pin LO, set col bit | 235 | // Pin LO, set col bit |
234 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | 236 | current_matrix[row_index] |= row_shifter; |
235 | key_pressed = true; | 237 | key_pressed = true; |
236 | } else { | 238 | } else { |
237 | // Pin HI, clear col bit | 239 | // Pin HI, clear col bit |
238 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | 240 | current_matrix[row_index] &= ~row_shifter; |
239 | } | 241 | } |
240 | } | 242 | } |
241 | 243 | ||
@@ -347,8 +349,9 @@ uint8_t matrix_scan(void) { | |||
347 | } | 349 | } |
348 | #elif (DIODE_DIRECTION == ROW2COL) | 350 | #elif (DIODE_DIRECTION == ROW2COL) |
349 | // Set col, read rows | 351 | // Set col, read rows |
350 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | 352 | matrix_row_t row_shifter = MATRIX_ROW_SHIFTER; |
351 | matrix_read_rows_on_col(curr_matrix, current_col); | 353 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) { |
354 | matrix_read_rows_on_col(curr_matrix, current_col, row_shifter); | ||
352 | } | 355 | } |
353 | #endif | 356 | #endif |
354 | 357 | ||