From 87410dc41175f36740414aa3e7407cf9997163ca Mon Sep 17 00:00:00 2001 From: Jay Greco Date: Sat, 2 Oct 2021 20:09:30 +0000 Subject: [PATCH 1/3] Custom matrix lite support for split keyboards --- quantum/matrix_common.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index 66c89970b109..999c1c691c99 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -104,7 +104,9 @@ __attribute__((weak)) void matrix_init(void) { matrix[i] = 0; } +#ifndef SPLIT_KEYBOARD debounce_init(MATRIX_ROWS); +#endif matrix_init_quantum(); } @@ -112,8 +114,9 @@ __attribute__((weak)) void matrix_init(void) { __attribute__((weak)) uint8_t matrix_scan(void) { bool changed = matrix_scan_custom(raw_matrix); +#ifndef SPLIT_KEYBOARD debounce(raw_matrix, matrix, MATRIX_ROWS, changed); - +#endif matrix_scan_quantum(); return changed; } From 1d0dc183001b91847fc517812a09e7252d092fad Mon Sep 17 00:00:00 2001 From: Jay Greco Date: Sat, 2 Oct 2021 21:46:54 +0000 Subject: [PATCH 2/3] WIP: matrix -> matrix_common refactor --- quantum/matrix.c | 2 +- quantum/matrix_common.c | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 33586c431b38..3b7886cfff1c 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -63,7 +63,7 @@ extern matrix_row_t matrix[MATRIX_ROWS]; // debounced values #ifdef SPLIT_KEYBOARD // row offsets for each hand -uint8_t thisHand, thatHand; +extern uint8_t thisHand, thatHand; #endif // user-defined overridable functions diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index 999c1c691c99..3eff0af4b9dd 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -4,15 +4,29 @@ #include "wait.h" #include "print.h" #include "debug.h" +#ifdef SPLIT_KEYBOARD +# include "split_common/split_util.h" +#endif #ifndef MATRIX_IO_DELAY # define MATRIX_IO_DELAY 30 #endif +#ifdef SPLIT_KEYBOARD +# define ROWS_PER_HAND (MATRIX_ROWS / 2) +#else +# define ROWS_PER_HAND (MATRIX_ROWS) +#endif + /* matrix state(1:on, 0:off) */ matrix_row_t raw_matrix[MATRIX_ROWS]; matrix_row_t matrix[MATRIX_ROWS]; +#ifdef SPLIT_KEYBOARD +// row offsets for each hand +uint8_t thisHand, thatHand; +#endif + #ifdef MATRIX_MASKED extern const matrix_row_t matrix_mask[]; #endif @@ -95,6 +109,8 @@ __attribute__((weak)) void matrix_init_custom(void) {} __attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; } +__attribute__((weak)) uint8_t matrix_post_scan_custom(void) { return false; }; + __attribute__((weak)) void matrix_init(void) { matrix_init_custom(); @@ -104,19 +120,26 @@ __attribute__((weak)) void matrix_init(void) { matrix[i] = 0; } -#ifndef SPLIT_KEYBOARD - debounce_init(MATRIX_ROWS); +#ifdef SPLIT_KEYBOARD + thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); + thatHand = ROWS_PER_HAND - thisHand; #endif + debounce_init(ROWS_PER_HAND); + matrix_init_quantum(); } __attribute__((weak)) uint8_t matrix_scan(void) { bool changed = matrix_scan_custom(raw_matrix); -#ifndef SPLIT_KEYBOARD - debounce(raw_matrix, matrix, MATRIX_ROWS, changed); +#ifdef SPLIT_KEYBOARD + debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); + changed = (changed || matrix_post_scan_custom()); +#else + debounce(raw_matrix, matrix, ROWS_PER_HAND, changed); #endif + matrix_scan_quantum(); return changed; } From cee54ab626a5abb934deabb72affb3b5a57c9045 Mon Sep 17 00:00:00 2001 From: Jay Greco Date: Mon, 4 Oct 2021 05:28:56 +0000 Subject: [PATCH 3/3] Move matrix_post_scan() to matrix_common.c --- quantum/matrix.c | 35 -------------------- quantum/matrix.h | 1 + quantum/matrix_common.c | 71 +++++++++++++++++++++++++++++++---------- 3 files changed, 56 insertions(+), 51 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 3b7886cfff1c..fbb141208977 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -70,10 +70,6 @@ extern uint8_t thisHand, thatHand; __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_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col); -#ifdef SPLIT_KEYBOARD -__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } -__attribute__((weak)) void matrix_slave_scan_user(void) {} -#endif static inline void setPinOutput_writeLow(pin_t pin) { ATOMIC_BLOCK_FORCEON { @@ -308,37 +304,6 @@ __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_mat transport_master(master_matrix, slave_matrix); return true; // Treat the transport as always connected } - -bool matrix_post_scan(void) { - bool changed = false; - if (is_keyboard_master()) { - matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; - if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { - for (int i = 0; i < ROWS_PER_HAND; ++i) { - if (matrix[thatHand + i] != slave_matrix[i]) { - matrix[thatHand + i] = slave_matrix[i]; - changed = true; - } - } - } else { - // reset other half if disconnected - for (int i = 0; i < ROWS_PER_HAND; ++i) { - matrix[thatHand + i] = 0; - slave_matrix[i] = 0; - } - - changed = true; - } - - matrix_scan_quantum(); - } else { - transport_slave(matrix + thatHand, matrix + thisHand); - - matrix_slave_scan_kb(); - } - - return changed; -} #endif uint8_t matrix_scan(void) { diff --git a/quantum/matrix.h b/quantum/matrix.h index 5c696622fcf2..90d22d7446e0 100644 --- a/quantum/matrix.h +++ b/quantum/matrix.h @@ -75,6 +75,7 @@ void matrix_init_user(void); void matrix_scan_user(void); #ifdef SPLIT_KEYBOARD +bool matrix_post_scan(void); void matrix_slave_scan_kb(void); void matrix_slave_scan_user(void); #endif diff --git a/quantum/matrix_common.c b/quantum/matrix_common.c index 3eff0af4b9dd..11addc445173 100644 --- a/quantum/matrix_common.c +++ b/quantum/matrix_common.c @@ -6,18 +6,17 @@ #include "debug.h" #ifdef SPLIT_KEYBOARD # include "split_common/split_util.h" -#endif - -#ifndef MATRIX_IO_DELAY -# define MATRIX_IO_DELAY 30 -#endif +# include "split_common/transactions.h" -#ifdef SPLIT_KEYBOARD # define ROWS_PER_HAND (MATRIX_ROWS / 2) #else # define ROWS_PER_HAND (MATRIX_ROWS) #endif +#ifndef MATRIX_IO_DELAY +# define MATRIX_IO_DELAY 30 +#endif + /* matrix state(1:on, 0:off) */ matrix_row_t raw_matrix[MATRIX_ROWS]; matrix_row_t matrix[MATRIX_ROWS]; @@ -98,20 +97,61 @@ uint8_t matrix_key_count(void) { return count; } +#ifdef SPLIT_KEYBOARD +bool matrix_post_scan(void) { + bool changed = false; + if (is_keyboard_master()) { + matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; + if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { + for (int i = 0; i < ROWS_PER_HAND; ++i) { + if (matrix[thatHand + i] != slave_matrix[i]) { + matrix[thatHand + i] = slave_matrix[i]; + changed = true; + } + } + } else { + // reset other half if disconnected + for (int i = 0; i < ROWS_PER_HAND; ++i) { + matrix[thatHand + i] = 0; + slave_matrix[i] = 0; + } + + changed = true; + } + + matrix_scan_quantum(); + } else { + transport_slave(matrix + thatHand, matrix + thisHand); + + matrix_slave_scan_kb(); + } + + return changed; +} +#endif + /* `matrix_io_delay ()` exists for backwards compatibility. From now on, use matrix_output_unselect_delay(). */ __attribute__((weak)) void matrix_io_delay(void) { wait_us(MATRIX_IO_DELAY); } - __attribute__((weak)) void matrix_output_select_delay(void) { waitInputPinDelay(); } __attribute__((weak)) void matrix_output_unselect_delay(uint8_t line, bool key_pressed) { matrix_io_delay(); } // CUSTOM MATRIX 'LITE' __attribute__((weak)) void matrix_init_custom(void) {} - __attribute__((weak)) bool matrix_scan_custom(matrix_row_t current_matrix[]) { return true; } -__attribute__((weak)) uint8_t matrix_post_scan_custom(void) { return false; }; +#ifdef SPLIT_KEYBOARD +__attribute__((weak)) void matrix_slave_scan_kb(void) { matrix_slave_scan_user(); } +__attribute__((weak)) void matrix_slave_scan_user(void) {} +#endif __attribute__((weak)) void matrix_init(void) { +#ifdef SPLIT_KEYBOARD + split_pre_init(); + + thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); + thatHand = ROWS_PER_HAND - thisHand; +#endif + matrix_init_custom(); // initialize matrix state: all keys off @@ -120,14 +160,13 @@ __attribute__((weak)) void matrix_init(void) { matrix[i] = 0; } -#ifdef SPLIT_KEYBOARD - thisHand = isLeftHand ? 0 : (ROWS_PER_HAND); - thatHand = ROWS_PER_HAND - thisHand; -#endif - debounce_init(ROWS_PER_HAND); matrix_init_quantum(); + +#ifdef SPLIT_KEYBOARD + split_post_init(); +#endif } __attribute__((weak)) uint8_t matrix_scan(void) { @@ -135,12 +174,12 @@ __attribute__((weak)) uint8_t matrix_scan(void) { #ifdef SPLIT_KEYBOARD debounce(raw_matrix, matrix + thisHand, ROWS_PER_HAND, changed); - changed = (changed || matrix_post_scan_custom()); + changed = (changed || matrix_post_scan()); #else debounce(raw_matrix, matrix, ROWS_PER_HAND, changed); + matrix_scan_quantum(); #endif - matrix_scan_quantum(); return changed; }