Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Support inverted scan logic for optical switches #19053

Merged
merged 8 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions data/mappings/info_config.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"LED_MATRIX_SPD_STEP": {"info_key": "led_matrix.speed_steps", "value_type": "int"},
"MANUFACTURER": {"info_key": "manufacturer", "value_type": "str"},
"MATRIX_HAS_GHOST": {"info_key": "matrix_pins.ghost", "value_type": "bool"},
"MATRIX_INPUT_PRESSED_STATE": {"info_key": "matrix_pins.input_pressed_state", "value_type": "int"},
"MATRIX_IO_DELAY": {"info_key": "matrix_pins.io_delay", "value_type": "int"},
"MOUSEKEY_DELAY": {"info_key": "mousekey.delay", "value_type": "int"},
"MOUSEKEY_INTERVAL": {"info_key": "mousekey.interval", "value_type": "int"},
Expand Down
1 change: 1 addition & 0 deletions data/schemas/keyboard.jsonschema
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"custom": {"type": "boolean"},
"custom_lite": {"type": "boolean"},
"ghost": {"type": "boolean"},
"input_pressed_state": {"$ref": "qmk.definitions.v1#/unsigned_int"},
"io_delay": {"$ref": "qmk.definitions.v1#/unsigned_int"},
"direct": {
"type": "array",
Expand Down
12 changes: 12 additions & 0 deletions docs/porting_your_keyboard_to_qmk.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ The next section of the `info` file deals with your keyboard's matrix. The first

The size of the `matrix_pins.cols` and `matrix_pins.rows` arrays infer the size of the matrix (previously `MATRIX_ROWS` and `MATRIX_COLS`).

## Configuration Options

To invert the keypress logic, configure `input_pressed_state`:

```json
"matrix_pins": {
"input_pressed_state": 1,
},
```

This configures state of the GPIO pins when the key is pressed - `1` for high, `0` for low. Default value is `0`.

Finally, you can specify the direction your diodes point. This can be `COL2ROW` or `ROW2COL`.

```json
Expand Down
10 changes: 6 additions & 4 deletions quantum/matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
# define SPLIT_MUTABLE_COL const
#endif

#ifndef MATRIX_INPUT_PRESSED_STATE
# define MATRIX_INPUT_PRESSED_STATE 0
#endif

#ifdef DIRECT_PINS
static SPLIT_MUTABLE pin_t direct_pins[ROWS_PER_HAND][MATRIX_COLS] = DIRECT_PINS;
#elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
Expand Down Expand Up @@ -93,7 +97,7 @@ static inline void setPinInputHigh_atomic(pin_t pin) {

static inline uint8_t readMatrixPin(pin_t pin) {
if (pin != NO_PIN) {
return readPin(pin);
return (readPin(pin) == MATRIX_INPUT_PRESSED_STATE) ? 0 : 1;
} else {
return 1;
}
Expand Down Expand Up @@ -121,9 +125,7 @@ __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[]
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];
if (pin != NO_PIN) {
current_row_value |= readPin(pin) ? 0 : row_shifter;
}
current_row_value |= readMatrixPin(pin) ? 0 : row_shifter;
}

// Update the matrix
Expand Down