aboutsummaryrefslogtreecommitdiff
path: root/quantum/matrix.c
diff options
context:
space:
mode:
authorDrashna Jaelre <drashna@live.com>2021-07-11 14:31:35 -0700
committerGitHub <noreply@github.com>2021-07-11 14:31:35 -0700
commitccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2 (patch)
tree26143876908e9c0b1b279a7da61be0f90cd00de4 /quantum/matrix.c
parent0b06452d00dcd967bfcaffe89e88a4758c878e74 (diff)
downloadqmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.tar.gz
qmk_firmware-ccc0b23a75f30f0ee9e1c6440dd3d56c99d38ea2.zip
Unify matrix for split common and regular matrix (#13330)
Diffstat (limited to 'quantum/matrix.c')
-rw-r--r--quantum/matrix.c133
1 files changed, 125 insertions, 8 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
26static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS; 54static 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
29static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; 57static 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
32static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; 60static 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;
37extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values 65extern matrix_row_t raw_matrix[MATRIX_ROWS]; // raw values
38extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values 66extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values
39 67
68#ifdef SPLIT_KEYBOARD
69// row offsets for each hand
70uint8_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
45static inline void setPinOutput_writeLow(pin_t pin) { 82static 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
219void matrix_init(void) { 256void 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
307bool 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
234uint8_t matrix_scan(void) { 347uint8_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}