forked from mirrors/qmk_userspace
		
	Optimize matrix scanning by removing variable shifts (#14947)
This commit is contained in:
		
					parent
					
						
							
								6506e271a3
							
						
					
				
			
			
				commit
				
					
						ee23aae87f
					
				
			
		
					 2 changed files with 14 additions and 11 deletions
				
			
		| 
						 | 
					@ -149,7 +149,7 @@ This is useful for setting up stuff that you may need elsewhere, but isn't hardw
 | 
				
			||||||
* GPIO pin initialisation: `void matrix_init_pins(void)`
 | 
					* GPIO pin initialisation: `void matrix_init_pins(void)`
 | 
				
			||||||
  * 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.
 | 
					  * 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.
 | 
				
			||||||
* `COL2ROW`-based row reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)`
 | 
					* `COL2ROW`-based row reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)`
 | 
				
			||||||
* `ROW2COL`-based column reads: `void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)`
 | 
					* `ROW2COL`-based column reads: `void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter)`
 | 
				
			||||||
* `DIRECT_PINS`-based reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)`
 | 
					* `DIRECT_PINS`-based reads: `void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)`
 | 
				
			||||||
  * 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.
 | 
					  * 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.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -69,7 +69,7 @@ uint8_t thisHand, thatHand;
 | 
				
			||||||
// user-defined overridable functions
 | 
					// user-defined overridable functions
 | 
				
			||||||
__attribute__((weak)) void matrix_init_pins(void);
 | 
					__attribute__((weak)) void matrix_init_pins(void);
 | 
				
			||||||
__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
 | 
					__attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
 | 
				
			||||||
__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
 | 
					__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter);
 | 
				
			||||||
#ifdef SPLIT_KEYBOARD
 | 
					#ifdef SPLIT_KEYBOARD
 | 
				
			||||||
__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
 | 
					__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); }
 | 
				
			||||||
__attribute__((weak)) void matrix_slave_scan_user(void) {}
 | 
					__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[]
 | 
				
			||||||
    // Start with a clear matrix row
 | 
					    // Start with a clear matrix row
 | 
				
			||||||
    matrix_row_t current_row_value = 0;
 | 
					    matrix_row_t current_row_value = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
 | 
					    matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
 | 
				
			||||||
 | 
					    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
 | 
				
			||||||
        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_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
 | 
					            current_row_value |= readPin(pin) ? 0 : row_shifter;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -169,11 +170,12 @@ __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[]
 | 
				
			||||||
    matrix_output_select_delay();
 | 
					    matrix_output_select_delay();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // For each col...
 | 
					    // For each col...
 | 
				
			||||||
    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
 | 
					    matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
 | 
				
			||||||
 | 
					    for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++, row_shifter <<= 1) {
 | 
				
			||||||
        uint8_t pin_state = readMatrixPin(col_pins[col_index]);
 | 
					        uint8_t pin_state = readMatrixPin(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_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
 | 
					        current_row_value |= pin_state ? 0 : row_shifter;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Unselect row
 | 
					    // Unselect row
 | 
				
			||||||
| 
						 | 
					@ -217,7 +219,7 @@ __attribute__((weak)) void matrix_init_pins(void) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
 | 
					__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {
 | 
				
			||||||
    bool key_pressed = false;
 | 
					    bool key_pressed = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Select col
 | 
					    // Select col
 | 
				
			||||||
| 
						 | 
					@ -231,11 +233,11 @@ __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[]
 | 
				
			||||||
        // Check row pin state
 | 
					        // Check row pin state
 | 
				
			||||||
        if (readMatrixPin(row_pins[row_index]) == 0) {
 | 
					        if (readMatrixPin(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_matrix[row_index] |= row_shifter;
 | 
				
			||||||
            key_pressed = true;
 | 
					            key_pressed = true;
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            // Pin HI, clear col bit
 | 
					            // Pin HI, clear col bit
 | 
				
			||||||
            current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
 | 
					            current_matrix[row_index] &= ~row_shifter;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -347,8 +349,9 @@ uint8_t matrix_scan(void) {
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
#elif (DIODE_DIRECTION == ROW2COL)
 | 
					#elif (DIODE_DIRECTION == ROW2COL)
 | 
				
			||||||
    // Set col, read rows
 | 
					    // Set col, read rows
 | 
				
			||||||
    for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
 | 
					    matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
 | 
				
			||||||
        matrix_read_rows_on_col(curr_matrix, current_col);
 | 
					    for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
 | 
				
			||||||
 | 
					        matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue