diff options
| author | Jay Greco <jayv.greco@gmail.com> | 2021-12-27 02:03:40 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-12-27 21:03:40 +1100 |
| commit | ac31863021791c8bbaa98a83afe90ec6505f4897 (patch) | |
| tree | ea01199de3ac472defb20e1b5f5a8b5b8aaf483b | |
| parent | 4519af69a971d4fdc005c70a9d38be67a0680815 (diff) | |
Custom matrix lite support for split keyboards (#14674)
* Custom matrix lite support for split keyboards
* WIP: matrix -> matrix_common refactor
* Move matrix_post_scan() to matrix_common.c
| -rw-r--r-- | quantum/matrix.c | 35 | ||||
| -rw-r--r-- | quantum/matrix.h | 1 | ||||
| -rw-r--r-- | quantum/matrix_common.c | 75 |
3 files changed, 72 insertions, 39 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index 483d518ecc..a58cc752fb 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
| @@ -63,17 +63,13 @@ extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values | |||
| 63 | 63 | ||
| 64 | #ifdef SPLIT_KEYBOARD | 64 | #ifdef SPLIT_KEYBOARD |
| 65 | // row offsets for each hand | 65 | // row offsets for each hand |
| 66 | uint8_t thisHand, thatHand; | 66 | extern uint8_t thisHand, thatHand; |
| 67 | #endif | 67 | #endif |
| 68 | 68 | ||
| 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, matrix_row_t row_shifter); | 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 | ||
| 74 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | ||
| 75 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | ||
| 76 | #endif | ||
| 77 | 73 | ||
| 78 | static inline void setPinOutput_writeLow(pin_t pin) { | 74 | static inline void setPinOutput_writeLow(pin_t pin) { |
| 79 | ATOMIC_BLOCK_FORCEON { | 75 | ATOMIC_BLOCK_FORCEON { |
| @@ -308,35 +304,6 @@ __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_mat | |||
| 308 | transport_master(master_matrix, slave_matrix); | 304 | transport_master(master_matrix, slave_matrix); |
| 309 | return true; // Treat the transport as always connected | 305 | return true; // Treat the transport as always connected |
| 310 | } | 306 | } |
| 311 | |||
| 312 | bool matrix_post_scan(void) { | ||
| 313 | bool changed = false; | ||
| 314 | if (is_keyboard_master()) { | ||
| 315 | static bool last_connected = false; | ||
| 316 | matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; | ||
| 317 | if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { | ||
| 318 | changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0; | ||
| 319 | |||
| 320 | last_connected = true; | ||
| 321 | } else if (last_connected) { | ||
| 322 | // reset other half when disconnected | ||
| 323 | memset(slave_matrix, 0, sizeof(slave_matrix)); | ||
| 324 | changed = true; | ||
| 325 | |||
| 326 | last_connected = false; | ||
| 327 | } | ||
| 328 | |||
| 329 | if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix)); | ||
| 330 | |||
| 331 | matrix_scan_quantum(); | ||
| 332 | } else { | ||
| 333 | transport_slave(matrix + thatHand, matrix + thisHand); | ||
| 334 | |||
| 335 | matrix_slave_scan_kb(); | ||
| 336 | } | ||
| 337 | |||
| 338 | return changed; | ||
| 339 | } | ||
| 340 | #endif | 307 | #endif |
| 341 | 308 | ||
| 342 | uint8_t matrix_scan(void) { | 309 | uint8_t matrix_scan(void) { |
diff --git a/quantum/matrix.h b/quantum/matrix.h index 1a3f362fba..d968efeb0f 100644 --- a/quantum/matrix.h +++ b/quantum/matrix.h | |||
| @@ -73,6 +73,7 @@ void matrix_init_user(void); | |||
| 73 | void matrix_scan_user(void); | 73 | void matrix_scan_user(void); |
| 74 | 74 | ||
| 75 | #ifdef SPLIT_KEYBOARD | 75 | #ifdef SPLIT_KEYBOARD |
| 76 | bool matrix_post_scan(void); | ||
| 76 | void matrix_slave_scan_kb(void); | 77 | void matrix_slave_scan_kb(void); |
| 77 | void matrix_slave_scan_user(void); | 78 | void matrix_slave_scan_user(void); |
| 78 | #endif | 79 | #endif |
diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index fe1d5b1edd..79f77421e1 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c | |||
| @@ -4,6 +4,14 @@ | |||
| 4 | #include "wait.h" | 4 | #include "wait.h" |
| 5 | #include "print.h" | 5 | #include "print.h" |
| 6 | #include "debug.h" | 6 | #include "debug.h" |
| 7 | #ifdef SPLIT_KEYBOARD | ||
| 8 | # include "split_common/split_util.h" | ||
| 9 | # include "split_common/transactions.h" | ||
| 10 | |||
| 11 | # define ROWS_PER_HAND (MATRIX_ROWS / 2) | ||
| 12 | #else | ||
| 13 | # define ROWS_PER_HAND (MATRIX_ROWS) | ||
| 14 | #endif | ||
| 7 | 15 | ||
| 8 | #ifndef MATRIX_IO_DELAY | 16 | #ifndef MATRIX_IO_DELAY |
| 9 | # define MATRIX_IO_DELAY 30 | 17 | # define MATRIX_IO_DELAY 30 |
| @@ -13,6 +21,11 @@ | |||
| 13 | matrix_row_t raw_matrix[MATRIX_ROWS]; | 21 | matrix_row_t raw_matrix[MATRIX_ROWS]; |
| 14 | matrix_row_t matrix[MATRIX_ROWS]; | 22 | matrix_row_t matrix[MATRIX_ROWS]; |
| 15 | 23 | ||
| 24 | #ifdef SPLIT_KEYBOARD | ||
| 25 | // row offsets for each hand | ||
| 26 | uint8_t thisHand, thatHand; | ||
| 27 | #endif | ||
| 28 | |||
| 16 | #ifdef MATRIX_MASKED | 29 | #ifdef MATRIX_MASKED |
| 17 | extern const matrix_row_t matrix_mask[]; | 30 | extern const matrix_row_t matrix_mask[]; |
| 18 | #endif | 31 | #endif |
| @@ -78,18 +91,61 @@ uint8_t matrix_key_count(void) { | |||
| 78 | return count; | 91 | return count; |
| 79 | } | 92 | } |
| 80 | 93 | ||
| 94 | #ifdef SPLIT_KEYBOARD | ||
| 95 | bool matrix_post_scan(void) { | ||
| 96 | bool changed = false; | ||
| 97 | if (is_keyboard_master()) { | ||
| 98 | matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; | ||
| 99 | if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { | ||
| 100 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 101 | if (matrix[thatHand + i] != slave_matrix[i]) { | ||
| 102 | matrix[thatHand + i] = slave_matrix[i]; | ||
| 103 | changed = true; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } else { | ||
| 107 | // reset other half if disconnected | ||
| 108 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 109 | matrix[thatHand + i] = 0; | ||
| 110 | slave_matrix[i] = 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | changed = true; | ||
| 114 | } | ||
| 115 | |||
| 116 | matrix_scan_quantum(); | ||
| 117 | } else { | ||
| 118 | transport_slave(matrix + thatHand, matrix + thisHand); | ||
| 119 | |||
| 120 | matrix_slave_scan_kb(); | ||
| 121 | } | ||
| 122 | |||
| 123 | return changed; | ||
| 124 | } | ||
| 125 | #endif | ||
| 126 | |||
| 81 | /* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */ | 127 | /* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */ |
| 82 | __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } | 128 | __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } |
| 83 | |||
| 84 | __attribute__((weak)) void matrix_output_select_delay(void) { waitInputPinDelay(); } | 129 | __attribute__((weak)) void matrix_output_select_delay(void) { waitInputPinDelay(); } |
| 85 | __attribute__((weak)) void matrix_output_unselect_delay(uint8_t line, bool key_pressed) { matrix_io_delay(); } | 130 | __attribute__((weak)) void matrix_output_unselect_delay(uint8_t line, bool key_pressed) { matrix_io_delay(); } |
| 86 | 131 | ||
| 87 | // CUSTOM MATRIX 'LITE' | 132 | // CUSTOM MATRIX 'LITE' |
| 88 | __attribute__((weak)) void matrix_init_custom(void) {} | 133 | __attribute__((weak)) void matrix_init_custom(void) {} |
| 89 | |||
| 90 | __attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; } | 134 | __attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; } |
| 91 | 135 | ||
| 136 | #ifdef SPLIT_KEYBOARD | ||
| 137 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | ||
| 138 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | ||
| 139 | #endif | ||
| 140 | |||
| 92 | __attribute__((weak)) void matrix_init(void) { | 141 | __attribute__((weak)) void matrix_init(void) { |
| 142 | #ifdef SPLIT_KEYBOARD | ||
| 143 | split_pre_init(); | ||
| 144 | |||
| 145 | thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
| 146 | thatHand = ROWS_PER_HAND - thisHand; | ||
| 147 | #endif | ||
| 148 | |||
| 93 | matrix_init_custom(); | 149 | matrix_init_custom(); |
| 94 | 150 | ||
| 95 | // initialize matrix state: all keys off | 151 | // initialize matrix state: all keys off |
| @@ -98,17 +154,26 @@ __attribute__((weak)) void matrix_init(void) { | |||
| 98 | matrix[i] = 0; | 154 | matrix[i] = 0; |
| 99 | } | 155 | } |
| 100 | 156 | ||
| 101 | debounce_init(MATRIX_ROWS); | 157 | debounce_init(ROWS_PER_HAND); |
| 102 | 158 | ||
| 103 | matrix_init_quantum(); | 159 | matrix_init_quantum(); |
| 160 | |||
| 161 | #ifdef SPLIT_KEYBOARD | ||
| 162 | split_post_init(); | ||
| 163 | #endif | ||
| 104 | } | 164 | } |
| 105 | 165 | ||
| 106 | __attribute__((weak)) uint8_t matrix_scan(void) { | 166 | __attribute__((weak)) uint8_t matrix_scan(void) { |
| 107 | bool changed = matrix_scan_custom(raw_matrix); | 167 | bool changed = matrix_scan_custom(raw_matrix); |
| 108 | 168 | ||
| 109 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | 169 | #ifdef SPLIT_KEYBOARD |
| 110 | 170 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); | |
| 171 | changed = (changed || matrix_post_scan()); | ||
| 172 | #else | ||
| 173 | debounce(raw_matrix, matrix, ROWS_PER_HAND, changed); | ||
| 111 | matrix_scan_quantum(); | 174 | matrix_scan_quantum(); |
| 175 | #endif | ||
| 176 | |||
| 112 | return changed; | 177 | return changed; |
| 113 | } | 178 | } |
| 114 | 179 | ||
