From 2b55f6b0691133ea76110d64a017ffe859878354 Mon Sep 17 00:00:00 2001 From: Joakim Tufvegren Date: Mon, 16 Aug 2021 16:34:04 +0200 Subject: [PATCH 1/3] Use memcmp and memcpy to compare and copy slave matrix. ...and memset to initialize `matrix` and `raw_matrix`. Increased my scan rate (while connected) by ~100 (on Ergodox Infinity). Effect on AVR is unknown. Co-authored-by: Stefan Kerkmann --- quantum/matrix.c | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 33586c431b38..4b9f548d9b28 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -288,10 +288,8 @@ void matrix_init(void) { matrix_init_pins(); // initialize matrix state: all keys off - for (uint8_t i = 0; i < MATRIX_ROWS; i++) { - raw_matrix[i] = 0; - matrix[i] = 0; - } + memset(matrix, 0, sizeof(matrix)); + memset(raw_matrix, 0, sizeof(raw_matrix)); debounce_init(ROWS_PER_HAND); @@ -314,19 +312,11 @@ bool matrix_post_scan(void) { 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; - } - } + changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0; + if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix)); } else { // reset other half if disconnected - for (int i = 0; i < ROWS_PER_HAND; ++i) { - matrix[thatHand + i] = 0; - slave_matrix[i] = 0; - } - + memset(matrix + thatHand, 0, sizeof(slave_matrix)); changed = true; } From 7f8d2f0e49f8cafb6e4bb083c762a8f9be0ea3c1 Mon Sep 17 00:00:00 2001 From: Joakim Tufvegren Date: Mon, 16 Aug 2021 20:09:59 +0200 Subject: [PATCH 2/3] Fix `matrix_post_scan` signalling change on every scan while disconnected. --- quantum/matrix.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index 4b9f548d9b28..dea28be8aa3b 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -310,16 +310,22 @@ __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_mat bool matrix_post_scan(void) { bool changed = false; if (is_keyboard_master()) { - matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; + static bool last_connected = false; + matrix_row_t slave_matrix[ROWS_PER_HAND]; if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0; - if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix)); - } else { - // reset other half if disconnected - memset(matrix + thatHand, 0, sizeof(slave_matrix)); + + last_connected = true; + } else if (last_connected) { + // reset other half when disconnected + memset(slave_matrix, 0, sizeof(slave_matrix)); changed = true; + + last_connected = false; } + if (changed) memcpy(matrix + thatHand, slave_matrix, sizeof(slave_matrix)); + matrix_scan_quantum(); } else { transport_slave(matrix + thatHand, matrix + thisHand); From cf14b95bfd6525a986ac0155f7e1662b5f3bc331 Mon Sep 17 00:00:00 2001 From: Joakim Tufvegren Date: Tue, 17 Aug 2021 22:59:30 +0200 Subject: [PATCH 3/3] Undo removal of initialization of `slave_matrix`. This has the effect of increasing my Ergodox Infinity firmware size by 8 bytes instead of decreasing by 8 bytes, and lowers the scan rate while connected back to the initial value before these changes, but _might_ solve some issues on AVR. --- quantum/matrix.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/quantum/matrix.c b/quantum/matrix.c index dea28be8aa3b..4fbcc2419cc6 100644 --- a/quantum/matrix.c +++ b/quantum/matrix.c @@ -310,8 +310,8 @@ __attribute__((weak)) bool transport_master_if_connected(matrix_row_t master_mat bool matrix_post_scan(void) { bool changed = false; if (is_keyboard_master()) { - static bool last_connected = false; - matrix_row_t slave_matrix[ROWS_PER_HAND]; + static bool last_connected = false; + matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; if (transport_master_if_connected(matrix + thisHand, slave_matrix)) { changed = memcmp(matrix + thatHand, slave_matrix, sizeof(slave_matrix)) != 0;