diff options
| author | Drashna Jaelre <drashna@live.com> | 2021-07-11 14:31:35 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-07-11 14:31:35 -0700 |
| commit | ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2 (patch) | |
| tree | 26143876908e9c0b1b279a7da61be0f90cd00de4 /quantum | |
| parent | 0b06452d00dcd967bfcaffe89e88a4758c878e74 (diff) | |
| download | qmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.tar.gz qmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.zip | |
Unify matrix for split common and regular matrix (#13330)
Diffstat (limited to 'quantum')
| -rw-r--r-- | quantum/matrix.c | 133 | ||||
| -rw-r--r-- | quantum/split_common/matrix.c | 340 |
2 files changed, 125 insertions, 348 deletions
diff --git a/quantum/matrix.c b/quantum/matrix.c index 566d9ff34..ed4643f81 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c | |||
| @@ -21,15 +21,43 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
| 21 | #include "matrix.h" | 21 | #include "matrix.h" |
| 22 | #include "debounce.h" | 22 | #include "debounce.h" |
| 23 | #include "quantum.h" | 23 | #include "quantum.h" |
| 24 | #ifdef SPLIT_KEYBOARD | ||
| 25 | # include "split_common/split_util.h" | ||
| 26 | # include "split_common/transactions.h" | ||
| 27 | |||
| 28 | # ifndef ERROR_DISCONNECT_COUNT | ||
| 29 | # define ERROR_DISCONNECT_COUNT 5 | ||
| 30 | # endif // ERROR_DISCONNECT_COUNT | ||
| 31 | |||
| 32 | # define ROWS_PER_HAND (MATRIX_ROWS / 2) | ||
| 33 | #else | ||
| 34 | # define ROWS_PER_HAND (MATRIX_ROWS) | ||
| 35 | #endif | ||
| 36 | |||
| 37 | #ifdef DIRECT_PINS_RIGHT | ||
| 38 | # define SPLIT_MUTABLE | ||
| 39 | #else | ||
| 40 | # define SPLIT_MUTABLE const | ||
| 41 | #endif | ||
| 42 | #ifdef MATRIX_ROW_PINS_RIGHT | ||
| 43 | # define SPLIT_MUTABLE_ROW | ||
| 44 | #else | ||
| 45 | # define SPLIT_MUTABLE_ROW const | ||
| 46 | #endif | ||
| 47 | #ifdef MATRIX_COL_PINS_RIGHT | ||
| 48 | # define SPLIT_MUTABLE_COL | ||
| 49 | #else | ||
| 50 | # define SPLIT_MUTABLE_COL const | ||
| 51 | #endif | ||
| 24 | 52 | ||
| 25 | #ifdef DIRECT_PINS | 53 | #ifdef DIRECT_PINS |
| 26 | static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; | 54 | static SPLIT_MUTABLE pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; |
| 27 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | 55 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) |
| 28 | # ifdef MATRIX_ROW_PINS | 56 | # ifdef MATRIX_ROW_PINS |
| 29 | static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | 57 | static SPLIT_MUTABLE_ROW pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; |
| 30 | # endif // MATRIX_ROW_PINS | 58 | # endif // MATRIX_ROW_PINS |
| 31 | # ifdef MATRIX_COL_PINS | 59 | # ifdef MATRIX_COL_PINS |
| 32 | static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | 60 | static SPLIT_MUTABLE_COL pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; |
| 33 | # endif // MATRIX_COL_PINS | 61 | # endif // MATRIX_COL_PINS |
| 34 | #endif | 62 | #endif |
| 35 | 63 | ||
| @@ -37,10 +65,19 @@ static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | |||
| 37 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values | 65 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values |
| 38 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values | 66 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values |
| 39 | 67 | ||
| 68 | #ifdef SPLIT_KEYBOARD | ||
| 69 | // row offsets for each hand | ||
| 70 | uint8_t thisHand, thatHand; | ||
| 71 | #endif | ||
| 72 | |||
| 40 | // user-defined overridable functions | 73 | // user-defined overridable functions |
| 41 | __attribute__((weak)) void matrix_init_pins(void); | 74 | __attribute__((weak)) void matrix_init_pins(void); |
| 42 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | 75 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); |
| 43 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | 76 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); |
| 77 | #ifdef SPLIT_KEYBOARD | ||
| 78 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | ||
| 79 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | ||
| 80 | #endif | ||
| 44 | 81 | ||
| 45 | static inline void setPinOutput_writeLow(pin_t pin) { | 82 | static inline void setPinOutput_writeLow(pin_t pin) { |
| 46 | ATOMIC_BLOCK_FORCEON { | 83 | ATOMIC_BLOCK_FORCEON { |
| @@ -192,7 +229,7 @@ __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[] | |||
| 192 | matrix_output_select_delay(); | 229 | matrix_output_select_delay(); |
| 193 | 230 | ||
| 194 | // For each row... | 231 | // For each row... |
| 195 | for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) { | 232 | for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { |
| 196 | // Check row pin state | 233 | // Check row pin state |
| 197 | if (readMatrixPin(row_pins[row_index]) == 0) { | 234 | if (readMatrixPin(row_pins[row_index]) == 0) { |
| 198 | // Pin LO, set col bit | 235 | // Pin LO, set col bit |
| @@ -217,6 +254,37 @@ __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[] | |||
| 217 | #endif | 254 | #endif |
| 218 | 255 | ||
| 219 | void matrix_init(void) { | 256 | void matrix_init(void) { |
| 257 | #ifdef SPLIT_KEYBOARD | ||
| 258 | split_pre_init(); | ||
| 259 | |||
| 260 | // Set pinout for right half if pinout for that half is defined | ||
| 261 | if (!isLeftHand) { | ||
| 262 | # ifdef DIRECT_PINS_RIGHT | ||
| 263 | const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT; | ||
| 264 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 265 | for (uint8_t j = 0; j < MATRIX_COLS; j++) { | ||
| 266 | direct_pins[i][j] = direct_pins_right[i][j]; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | # endif | ||
| 270 | # ifdef MATRIX_ROW_PINS_RIGHT | ||
| 271 | const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT; | ||
| 272 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 273 | row_pins[i] = row_pins_right[i]; | ||
| 274 | } | ||
| 275 | # endif | ||
| 276 | # ifdef MATRIX_COL_PINS_RIGHT | ||
| 277 | const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; | ||
| 278 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
| 279 | col_pins[i] = col_pins_right[i]; | ||
| 280 | } | ||
| 281 | # endif | ||
| 282 | } | ||
| 283 | |||
| 284 | thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
| 285 | thatHand = ROWS_PER_HAND - thisHand; | ||
| 286 | #endif | ||
| 287 | |||
| 220 | // initialize key pins | 288 | // initialize key pins |
| 221 | matrix_init_pins(); | 289 | matrix_init_pins(); |
| 222 | 290 | ||
| @@ -226,17 +294,62 @@ void matrix_init(void) { | |||
| 226 | matrix[i] = 0; | 294 | matrix[i] = 0; |
| 227 | } | 295 | } |
| 228 | 296 | ||
| 229 | debounce_init(MATRIX_ROWS); | 297 | debounce_init(ROWS_PER_HAND); |
| 230 | 298 | ||
| 231 | matrix_init_quantum(); | 299 | matrix_init_quantum(); |
| 300 | |||
| 301 | #ifdef SPLIT_KEYBOARD | ||
| 302 | split_post_init(); | ||
| 303 | #endif | ||
| 232 | } | 304 | } |
| 233 | 305 | ||
| 306 | #ifdef SPLIT_KEYBOARD | ||
| 307 | bool matrix_post_scan(void) { | ||
| 308 | bool changed = false; | ||
| 309 | if (is_keyboard_master()) { | ||
| 310 | static uint8_t error_count; | ||
| 311 | |||
| 312 | matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; | ||
| 313 | if (!transport_master(matrix + thisHand, slave_matrix)) { | ||
| 314 | error_count++; | ||
| 315 | |||
| 316 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
| 317 | // reset other half if disconnected | ||
| 318 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 319 | matrix[thatHand + i] = 0; | ||
| 320 | slave_matrix[i] = 0; | ||
| 321 | } | ||
| 322 | |||
| 323 | changed = true; | ||
| 324 | } | ||
| 325 | } else { | ||
| 326 | error_count = 0; | ||
| 327 | |||
| 328 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 329 | if (matrix[thatHand + i] != slave_matrix[i]) { | ||
| 330 | matrix[thatHand + i] = slave_matrix[i]; | ||
| 331 | changed = true; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | matrix_scan_quantum(); | ||
| 337 | } else { | ||
| 338 | transport_slave(matrix + thatHand, matrix + thisHand); | ||
| 339 | |||
| 340 | matrix_slave_scan_kb(); | ||
| 341 | } | ||
| 342 | |||
| 343 | return changed; | ||
| 344 | } | ||
| 345 | #endif | ||
| 346 | |||
| 234 | uint8_t matrix_scan(void) { | 347 | uint8_t matrix_scan(void) { |
| 235 | matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; | 348 | matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; |
| 236 | 349 | ||
| 237 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | 350 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) |
| 238 | // Set row, read cols | 351 | // Set row, read cols |
| 239 | for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) { | 352 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { |
| 240 | matrix_read_cols_on_row(curr_matrix, current_row); | 353 | matrix_read_cols_on_row(curr_matrix, current_row); |
| 241 | } | 354 | } |
| 242 | #elif (DIODE_DIRECTION == ROW2COL) | 355 | #elif (DIODE_DIRECTION == ROW2COL) |
| @@ -249,8 +362,12 @@ uint8_t matrix_scan(void) { | |||
| 249 | bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0; | 362 | bool changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0; |
| 250 | if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix)); | 363 | if (changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix)); |
| 251 | 364 | ||
| 252 | debounce(raw_matrix, matrix, MATRIX_ROWS, changed); | 365 | #ifdef SPLIT_KEYBOARD |
| 253 | 366 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); | |
| 367 | changed = (changed || matrix_post_scan()); | ||
| 368 | #else | ||
| 369 | debounce(raw_matrix, matrix, ROWS_PER_HAND, changed); | ||
| 254 | matrix_scan_quantum(); | 370 | matrix_scan_quantum(); |
| 371 | #endif | ||
| 255 | return (uint8_t)changed; | 372 | return (uint8_t)changed; |
| 256 | } | 373 | } |
diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c deleted file mode 100644 index d8e078e9b..000000000 --- a/quantum/split_common/matrix.c +++ /dev/null | |||
| @@ -1,340 +0,0 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | #include <stdint.h> | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include <string.h> | ||
| 20 | #include "util.h" | ||
| 21 | #include "matrix.h" | ||
| 22 | #include "debounce.h" | ||
| 23 | #include "quantum.h" | ||
| 24 | #include "split_util.h" | ||
| 25 | #include "config.h" | ||
| 26 | #include "transactions.h" | ||
| 27 | |||
| 28 | #ifndef ERROR_DISCONNECT_COUNT | ||
| 29 | # define ERROR_DISCONNECT_COUNT 5 | ||
| 30 | #endif // ERROR_DISCONNECT_COUNT | ||
| 31 | |||
| 32 | #define ROWS_PER_HAND (MATRIX_ROWS / 2) | ||
| 33 | |||
| 34 | #ifdef DIRECT_PINS | ||
| 35 | static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; | ||
| 36 | #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW) | ||
| 37 | # ifdef MATRIX_ROW_PINS | ||
| 38 | static pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
| 39 | # endif // MATRIX_ROW_PINS | ||
| 40 | # ifdef MATRIX_COL_PINS | ||
| 41 | static pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
| 42 | # endif // MATRIX_COL_PINS | ||
| 43 | #endif | ||
| 44 | |||
| 45 | /* matrix state(1:on, 0:off) */ | ||
| 46 | extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values | ||
| 47 | extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values | ||
| 48 | |||
| 49 | // row offsets for each hand | ||
| 50 | uint8_t thisHand, thatHand; | ||
| 51 | |||
| 52 | // user-defined overridable functions | ||
| 53 | __attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } | ||
| 54 | __attribute__((weak)) void matrix_slave_scan_user(void) {} | ||
| 55 | __attribute__((weak)) void matrix_init_pins(void); | ||
| 56 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row); | ||
| 57 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); | ||
| 58 | |||
| 59 | static inline void setPinOutput_writeLow(pin_t pin) { | ||
| 60 | ATOMIC_BLOCK_FORCEON { | ||
| 61 | setPinOutput(pin); | ||
| 62 | writePinLow(pin); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | static inline void setPinInputHigh_atomic(pin_t pin) { | ||
| 67 | ATOMIC_BLOCK_FORCEON { setPinInputHigh(pin); } | ||
| 68 | } | ||
| 69 | |||
| 70 | static inline uint8_t readMatrixPin(pin_t pin) { | ||
| 71 | if (pin != NO_PIN) { | ||
| 72 | return readPin(pin); | ||
| 73 | } else { | ||
| 74 | return 1; | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | // matrix code | ||
| 79 | |||
| 80 | #ifdef DIRECT_PINS | ||
| 81 | |||
| 82 | __attribute__((weak)) void matrix_init_pins(void) { | ||
| 83 | for (int row = 0; row < MATRIX_ROWS; row++) { | ||
| 84 | for (int col = 0; col < MATRIX_COLS; col++) { | ||
| 85 | pin_t pin = direct_pins[row][col]; | ||
| 86 | if (pin != NO_PIN) { | ||
| 87 | setPinInputHigh(pin); | ||
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 94 | // Start with a clear matrix row | ||
| 95 | matrix_row_t current_row_value = 0; | ||
| 96 | |||
| 97 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 98 | pin_t pin = direct_pins[current_row][col_index]; | ||
| 99 | if (pin != NO_PIN) { | ||
| 100 | current_row_value |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | // Update the matrix | ||
| 105 | current_matrix[current_row] = current_row_value; | ||
| 106 | } | ||
| 107 | |||
| 108 | #elif defined(DIODE_DIRECTION) | ||
| 109 | # if defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS) | ||
| 110 | # if (DIODE_DIRECTION == COL2ROW) | ||
| 111 | |||
| 112 | static bool select_row(uint8_t row) { | ||
| 113 | pin_t pin = row_pins[row]; | ||
| 114 | if (pin != NO_PIN) { | ||
| 115 | setPinOutput_writeLow(pin); | ||
| 116 | return true; | ||
| 117 | } | ||
| 118 | return false; | ||
| 119 | } | ||
| 120 | |||
| 121 | static void unselect_row(uint8_t row) { | ||
| 122 | pin_t pin = row_pins[row]; | ||
| 123 | if (pin != NO_PIN) { | ||
| 124 | setPinInputHigh_atomic(pin); | ||
| 125 | } | ||
| 126 | } | ||
| 127 | |||
| 128 | static void unselect_rows(void) { | ||
| 129 | for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { | ||
| 130 | unselect_row(x); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | __attribute__((weak)) void matrix_init_pins(void) { | ||
| 135 | unselect_rows(); | ||
| 136 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 137 | if (col_pins[x] != NO_PIN) { | ||
| 138 | setPinInputHigh_atomic(col_pins[x]); | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | __attribute__((weak)) void matrix_read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) { | ||
| 144 | // Start with a clear matrix row | ||
| 145 | matrix_row_t current_row_value = 0; | ||
| 146 | |||
| 147 | if (!select_row(current_row)) { // Select row | ||
| 148 | return; // skip NO_PIN row | ||
| 149 | } | ||
| 150 | matrix_output_select_delay(); | ||
| 151 | |||
| 152 | // For each col... | ||
| 153 | for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) { | ||
| 154 | uint8_t pin_state = readMatrixPin(col_pins[col_index]); | ||
| 155 | |||
| 156 | // Populate the matrix row with the state of the col pin | ||
| 157 | current_row_value |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index); | ||
| 158 | } | ||
| 159 | |||
| 160 | // Unselect row | ||
| 161 | unselect_row(current_row); | ||
| 162 | matrix_output_unselect_delay(); // wait for all Col signals to go HIGH | ||
| 163 | |||
| 164 | // Update the matrix | ||
| 165 | current_matrix[current_row] = current_row_value; | ||
| 166 | } | ||
| 167 | |||
| 168 | # elif (DIODE_DIRECTION == ROW2COL) | ||
| 169 | |||
| 170 | static bool select_col(uint8_t col) { | ||
| 171 | pin_t pin = col_pins[col]; | ||
| 172 | if (pin != NO_PIN) { | ||
| 173 | setPinOutput_writeLow(pin); | ||
| 174 | return true; | ||
| 175 | } | ||
| 176 | return false; | ||
| 177 | } | ||
| 178 | |||
| 179 | static void unselect_col(uint8_t col) { | ||
| 180 | pin_t pin = col_pins[col]; | ||
| 181 | if (pin != NO_PIN) { | ||
| 182 | setPinInputHigh_atomic(pin); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | static void unselect_cols(void) { | ||
| 187 | for (uint8_t x = 0; x < MATRIX_COLS; x++) { | ||
| 188 | unselect_col(x); | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | __attribute__((weak)) void matrix_init_pins(void) { | ||
| 193 | unselect_cols(); | ||
| 194 | for (uint8_t x = 0; x < ROWS_PER_HAND; x++) { | ||
| 195 | if (row_pins[x] != NO_PIN) { | ||
| 196 | setPinInputHigh_atomic(row_pins[x]); | ||
| 197 | } | ||
| 198 | } | ||
| 199 | } | ||
| 200 | |||
| 201 | __attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) { | ||
| 202 | // Select col | ||
| 203 | if (!select_col(current_col)) { // select col | ||
| 204 | return; // skip NO_PIN col | ||
| 205 | } | ||
| 206 | matrix_output_select_delay(); | ||
| 207 | |||
| 208 | // For each row... | ||
| 209 | for (uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++) { | ||
| 210 | // Check row pin state | ||
| 211 | if (readMatrixPin(row_pins[row_index]) == 0) { | ||
| 212 | // Pin LO, set col bit | ||
| 213 | current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col); | ||
| 214 | } else { | ||
| 215 | // Pin HI, clear col bit | ||
| 216 | current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col); | ||
| 217 | } | ||
| 218 | } | ||
| 219 | |||
| 220 | // Unselect col | ||
| 221 | unselect_col(current_col); | ||
| 222 | matrix_output_unselect_delay(); // wait for all Row signals to go HIGH | ||
| 223 | } | ||
| 224 | |||
| 225 | # else | ||
| 226 | # error DIODE_DIRECTION must be one of COL2ROW or ROW2COL! | ||
| 227 | # endif | ||
| 228 | # endif // defined(MATRIX_ROW_PINS) && defined(MATRIX_COL_PINS) | ||
| 229 | #else | ||
| 230 | # error DIODE_DIRECTION is not defined! | ||
| 231 | #endif | ||
| 232 | |||
| 233 | void matrix_init(void) { | ||
| 234 | split_pre_init(); | ||
| 235 | |||
| 236 | // Set pinout for right half if pinout for that half is defined | ||
| 237 | if (!isLeftHand) { | ||
| 238 | #ifdef DIRECT_PINS_RIGHT | ||
| 239 | const pin_t direct_pins_right[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS_RIGHT; | ||
| 240 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 241 | for (uint8_t j = 0; j < MATRIX_COLS; j++) { | ||
| 242 | direct_pins[i][j] = direct_pins_right[i][j]; | ||
| 243 | } | ||
| 244 | } | ||
| 245 | #endif | ||
| 246 | #ifdef MATRIX_ROW_PINS_RIGHT | ||
| 247 | const pin_t row_pins_right[MATRIX_ROWS] = MATRIX_ROW_PINS_RIGHT; | ||
| 248 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 249 | row_pins[i] = row_pins_right[i]; | ||
| 250 | } | ||
| 251 | #endif | ||
| 252 | #ifdef MATRIX_COL_PINS_RIGHT | ||
| 253 | const pin_t col_pins_right[MATRIX_COLS] = MATRIX_COL_PINS_RIGHT; | ||
| 254 | for (uint8_t i = 0; i < MATRIX_COLS; i++) { | ||
| 255 | col_pins[i] = col_pins_right[i]; | ||
| 256 | } | ||
| 257 | #endif | ||
| 258 | } | ||
| 259 | |||
| 260 | thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
| 261 | thatHand = ROWS_PER_HAND - thisHand; | ||
| 262 | |||
| 263 | // initialize key pins | ||
| 264 | matrix_init_pins(); | ||
| 265 | |||
| 266 | // initialize matrix state: all keys off | ||
| 267 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 268 | raw_matrix[i] = 0; | ||
| 269 | matrix[i] = 0; | ||
| 270 | } | ||
| 271 | |||
| 272 | debounce_init(ROWS_PER_HAND); | ||
| 273 | |||
| 274 | matrix_init_quantum(); | ||
| 275 | |||
| 276 | split_post_init(); | ||
| 277 | } | ||
| 278 | |||
| 279 | bool matrix_post_scan(void) { | ||
| 280 | bool changed = false; | ||
| 281 | if (is_keyboard_master()) { | ||
| 282 | static uint8_t error_count; | ||
| 283 | |||
| 284 | matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; | ||
| 285 | if (!transport_master(matrix + thisHand, slave_matrix)) { | ||
| 286 | error_count++; | ||
| 287 | |||
| 288 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
| 289 | // reset other half if disconnected | ||
| 290 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 291 | matrix[thatHand + i] = 0; | ||
| 292 | slave_matrix[i] = 0; | ||
| 293 | } | ||
| 294 | |||
| 295 | changed = true; | ||
| 296 | } | ||
| 297 | } else { | ||
| 298 | error_count = 0; | ||
| 299 | |||
| 300 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
| 301 | if (matrix[thatHand + i] != slave_matrix[i]) { | ||
| 302 | matrix[thatHand + i] = slave_matrix[i]; | ||
| 303 | changed = true; | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } | ||
| 307 | |||
| 308 | matrix_scan_quantum(); | ||
| 309 | } else { | ||
| 310 | transport_slave(matrix + thatHand, matrix + thisHand); | ||
| 311 | |||
| 312 | matrix_slave_scan_kb(); | ||
| 313 | } | ||
| 314 | |||
| 315 | return changed; | ||
| 316 | } | ||
| 317 | |||
| 318 | uint8_t matrix_scan(void) { | ||
| 319 | matrix_row_t curr_matrix[MATRIX_ROWS] = {0}; | ||
| 320 | |||
| 321 | #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) | ||
| 322 | // Set row, read cols | ||
| 323 | for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) { | ||
| 324 | matrix_read_cols_on_row(curr_matrix, current_row); | ||
| 325 | } | ||
| 326 | #elif (DIODE_DIRECTION == ROW2COL) | ||
| 327 | // Set col, read rows | ||
| 328 | for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) { | ||
| 329 | matrix_read_rows_on_col(curr_matrix, current_col); | ||
| 330 | } | ||
| 331 | #endif | ||
| 332 | |||
| 333 | bool local_changed = memcmp(raw_matrix, curr_matrix, sizeof(curr_matrix)) != 0; | ||
| 334 | if (local_changed) memcpy(raw_matrix, curr_matrix, sizeof(curr_matrix)); | ||
| 335 | |||
| 336 | debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, local_changed); | ||
| 337 | |||
| 338 | bool remote_changed = matrix_post_scan(); | ||
| 339 | return (uint8_t)(local_changed || remote_changed); | ||
| 340 | } | ||
