From 4d7992fc094a150dbd03173217fa2b1dd62a1a8c Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Wed, 26 Oct 2022 13:00:50 +0300 Subject: [PATCH 1/9] core: allow locking the matrix state introducing flexibility on matrix tasks timing. matrix state can now be locked when we don't need a matrix scan --- quantum/keyboard.c | 79 ++++++++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 34 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index eb5e4b583a99..b194fb497f8a 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -252,6 +252,14 @@ __attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); } +/** \brief is_matrix_locked + * + * FIXME: needs doc + */ +__attribute__((weak)) bool is_matrix_locked(void) { + return false; +} + /** \brief keyboard_setup * * FIXME: needs doc @@ -449,54 +457,57 @@ static inline void generate_tick_event(void) { * @return false Matrix didn't change */ static bool matrix_task(void) { - static matrix_row_t matrix_previous[MATRIX_ROWS]; + if (!is_matrix_locked()) { + static matrix_row_t matrix_previous[MATRIX_ROWS]; - matrix_scan(); + matrix_scan(); - bool matrix_changed = false; - for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { - matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); - } + bool matrix_changed = false; + for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { + matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); + } - matrix_scan_perf_task(); + matrix_scan_perf_task(); - // Short-circuit the complete matrix processing if it is not necessary - if (!matrix_changed) { - generate_tick_event(); - return matrix_changed; - } + // Short-circuit the complete matrix processing if it is not necessary + if (!matrix_changed) { + generate_tick_event(); + return matrix_changed; + } - if (debug_config.matrix) { - matrix_print(); - } + if (debug_config.matrix) { + matrix_print(); + } - const bool process_keypress = should_process_keypress(); + const bool process_keypress = should_process_keypress(); - for (uint8_t row = 0; row < MATRIX_ROWS; row++) { - const matrix_row_t current_row = matrix_get_row(row); - const matrix_row_t row_changes = current_row ^ matrix_previous[row]; + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + const matrix_row_t current_row = matrix_get_row(row); + const matrix_row_t row_changes = current_row ^ matrix_previous[row]; - if (!row_changes || has_ghost_in_row(row, current_row)) { - continue; - } + if (!row_changes || has_ghost_in_row(row, current_row)) { + continue; + } - matrix_row_t col_mask = 1; - for (uint8_t col = 0; col < MATRIX_COLS; col++, col_mask <<= 1) { - if (row_changes & col_mask) { - const bool key_pressed = current_row & col_mask; + matrix_row_t col_mask = 1; + for (uint8_t col = 0; col < MATRIX_COLS; col++, col_mask <<= 1) { + if (row_changes & col_mask) { + const bool key_pressed = current_row & col_mask; - if (process_keypress) { - action_exec(MAKE_KEYEVENT(row, col, key_pressed)); - } + if (process_keypress) { + action_exec(MAKE_KEYEVENT(row, col, key_pressed)); + } - switch_events(row, col, key_pressed); + switch_events(row, col, key_pressed); + } } - } - matrix_previous[row] = current_row; - } + matrix_previous[row] = current_row; + } - return matrix_changed; + return matrix_changed; + } else + return false; } /** \brief Tasks previously located in matrix_scan_quantum From f5ed0cefc8286a97f8520fbfeac9aa1a0db25824 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Wed, 26 Oct 2022 14:54:37 +0300 Subject: [PATCH 2/9] some formatting --- quantum/keyboard.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index b194fb497f8a..01dc28192c1c 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -111,7 +111,7 @@ along with this program. If not, see . static uint32_t last_input_modification_time = 0; uint32_t last_input_activity_time(void) { - return last_input_modification_time; + return last_input_modification_time; } uint32_t last_input_activity_elapsed(void) { return timer_elapsed32(last_input_modification_time); @@ -119,7 +119,7 @@ uint32_t last_input_activity_elapsed(void) { static uint32_t last_matrix_modification_time = 0; uint32_t last_matrix_activity_time(void) { - return last_matrix_modification_time; + return last_matrix_modification_time; } uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); @@ -130,7 +130,7 @@ void last_matrix_activity_trigger(void) { static uint32_t last_encoder_modification_time = 0; uint32_t last_encoder_activity_time(void) { - return last_encoder_modification_time; + return last_encoder_modification_time; } uint32_t last_encoder_activity_elapsed(void) { return timer_elapsed32(last_encoder_modification_time); @@ -457,12 +457,16 @@ static inline void generate_tick_event(void) { * @return false Matrix didn't change */ static bool matrix_task(void) { - if (!is_matrix_locked()) { + bool matrix_changed = false; + + if (is_matrix_locked()) { + generate_tick_event(); + return matrix_changed; + } else { static matrix_row_t matrix_previous[MATRIX_ROWS]; matrix_scan(); - bool matrix_changed = false; for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); } @@ -506,8 +510,7 @@ static bool matrix_task(void) { } return matrix_changed; - } else - return false; + } } /** \brief Tasks previously located in matrix_scan_quantum From 81ae361d07c8a35a5c7712f798f6aa712826ed88 Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Wed, 26 Oct 2022 20:47:09 +0300 Subject: [PATCH 3/9] rename to matrix_available swap the logic, introduce prototypes & readme --- quantum/keyboard.c | 10 +++++----- quantum/matrix.h | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 01dc28192c1c..fd2f74906b13 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -252,12 +252,12 @@ __attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); } -/** \brief is_matrix_locked +/** \brief matrix_available * - * FIXME: needs doc + * Override this function if you have a condition where matrix tasks should not be available */ -__attribute__((weak)) bool is_matrix_locked(void) { - return false; +__attribute__((weak)) bool matrix_available(void) { + return true; } /** \brief keyboard_setup @@ -459,7 +459,7 @@ static inline void generate_tick_event(void) { static bool matrix_task(void) { bool matrix_changed = false; - if (is_matrix_locked()) { + if (!matrix_available()) { generate_tick_event(); return matrix_changed; } else { diff --git a/quantum/matrix.h b/quantum/matrix.h index d968efeb0f51..89f1885b07c1 100644 --- a/quantum/matrix.h +++ b/quantum/matrix.h @@ -46,6 +46,8 @@ void matrix_setup(void); void matrix_init(void); /* scan all key states on matrix */ uint8_t matrix_scan(void); +/* whether matrix tasks are available */ +bool matrix_available(void); /* whether a switch is on */ bool matrix_is_on(uint8_t row, uint8_t col); /* matrix state on row */ From adbe4e242d524efae419f3c892fc8aa76325f7cc Mon Sep 17 00:00:00 2001 From: dexter93 Date: Thu, 27 Oct 2022 02:01:18 +0300 Subject: [PATCH 4/9] Update quantum/keyboard.c Co-authored-by: Sergey Vlasov --- quantum/keyboard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index fd2f74906b13..30913ba00c2c 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -111,7 +111,7 @@ along with this program. If not, see . static uint32_t last_input_modification_time = 0; uint32_t last_input_activity_time(void) { - return last_input_modification_time; + return last_input_modification_time; } uint32_t last_input_activity_elapsed(void) { return timer_elapsed32(last_input_modification_time); From 63096d774ea2efb3d88a1d161d20d27b87390f9b Mon Sep 17 00:00:00 2001 From: dexter93 Date: Thu, 27 Oct 2022 02:01:25 +0300 Subject: [PATCH 5/9] Update quantum/keyboard.c Co-authored-by: Sergey Vlasov --- quantum/keyboard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 30913ba00c2c..a6055cefb046 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -119,7 +119,7 @@ uint32_t last_input_activity_elapsed(void) { static uint32_t last_matrix_modification_time = 0; uint32_t last_matrix_activity_time(void) { - return last_matrix_modification_time; + return last_matrix_modification_time; } uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); From 89456221585bc17f988ece61306eca79e454c7df Mon Sep 17 00:00:00 2001 From: dexter93 Date: Thu, 27 Oct 2022 02:01:34 +0300 Subject: [PATCH 6/9] Update quantum/keyboard.c Co-authored-by: Sergey Vlasov --- quantum/keyboard.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index a6055cefb046..7542a531e32a 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -130,7 +130,7 @@ void last_matrix_activity_trigger(void) { static uint32_t last_encoder_modification_time = 0; uint32_t last_encoder_activity_time(void) { - return last_encoder_modification_time; + return last_encoder_modification_time; } uint32_t last_encoder_activity_elapsed(void) { return timer_elapsed32(last_encoder_modification_time); From 25f91c7ce6b18f2e53dff20475532a23c228937c Mon Sep 17 00:00:00 2001 From: Dimitris Mantzouranis Date: Thu, 27 Oct 2022 13:19:06 +0300 Subject: [PATCH 7/9] remove redundant else --- quantum/keyboard.c | 66 +++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 7542a531e32a..618f4f5f2e1b 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -462,55 +462,55 @@ static bool matrix_task(void) { if (!matrix_available()) { generate_tick_event(); return matrix_changed; - } else { - static matrix_row_t matrix_previous[MATRIX_ROWS]; + } - matrix_scan(); + static matrix_row_t matrix_previous[MATRIX_ROWS]; - for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { - matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); - } + matrix_scan(); - matrix_scan_perf_task(); + for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { + matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); + } - // Short-circuit the complete matrix processing if it is not necessary - if (!matrix_changed) { - generate_tick_event(); - return matrix_changed; - } + matrix_scan_perf_task(); - if (debug_config.matrix) { - matrix_print(); - } + // Short-circuit the complete matrix processing if it is not necessary + if (!matrix_changed) { + generate_tick_event(); + return matrix_changed; + } - const bool process_keypress = should_process_keypress(); + if (debug_config.matrix) { + matrix_print(); + } - for (uint8_t row = 0; row < MATRIX_ROWS; row++) { - const matrix_row_t current_row = matrix_get_row(row); - const matrix_row_t row_changes = current_row ^ matrix_previous[row]; + const bool process_keypress = should_process_keypress(); - if (!row_changes || has_ghost_in_row(row, current_row)) { - continue; - } + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + const matrix_row_t current_row = matrix_get_row(row); + const matrix_row_t row_changes = current_row ^ matrix_previous[row]; - matrix_row_t col_mask = 1; - for (uint8_t col = 0; col < MATRIX_COLS; col++, col_mask <<= 1) { - if (row_changes & col_mask) { - const bool key_pressed = current_row & col_mask; + if (!row_changes || has_ghost_in_row(row, current_row)) { + continue; + } - if (process_keypress) { - action_exec(MAKE_KEYEVENT(row, col, key_pressed)); - } + matrix_row_t col_mask = 1; + for (uint8_t col = 0; col < MATRIX_COLS; col++, col_mask <<= 1) { + if (row_changes & col_mask) { + const bool key_pressed = current_row & col_mask; - switch_events(row, col, key_pressed); + if (process_keypress) { + action_exec(MAKE_KEYEVENT(row, col, key_pressed)); } - } - matrix_previous[row] = current_row; + switch_events(row, col, key_pressed); + } } - return matrix_changed; + matrix_previous[row] = current_row; } + + return matrix_changed; } /** \brief Tasks previously located in matrix_scan_quantum From b5a97b8f72b01cff4dbd6dd46fd76583117e8581 Mon Sep 17 00:00:00 2001 From: dexter93 Date: Wed, 18 Jan 2023 14:41:17 +0200 Subject: [PATCH 8/9] Partially revert the bool handling looks a bit uglier, but should be slightly more efficient Co-authored-by: Stefan Kerkmann --- quantum/keyboard.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index 618f4f5f2e1b..fc162a6ba826 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -457,17 +457,15 @@ static inline void generate_tick_event(void) { * @return false Matrix didn't change */ static bool matrix_task(void) { - bool matrix_changed = false; - if (!matrix_available()) { generate_tick_event(); - return matrix_changed; + return false; } static matrix_row_t matrix_previous[MATRIX_ROWS]; matrix_scan(); - + bool matrix_changed = false; for (uint8_t row = 0; row < MATRIX_ROWS && !matrix_changed; row++) { matrix_changed |= matrix_previous[row] ^ matrix_get_row(row); } From 0d3e79fb2e1cbf2fb33f8da89f7a508d51ab30d0 Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Mon, 13 Feb 2023 03:36:10 +1100 Subject: [PATCH 9/9] Apply suggestions from code review --- quantum/keyboard.c | 8 ++++---- quantum/matrix.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/quantum/keyboard.c b/quantum/keyboard.c index fc162a6ba826..002d84f35087 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -252,11 +252,11 @@ __attribute__((weak)) void keyboard_post_init_kb(void) { keyboard_post_init_user(); } -/** \brief matrix_available +/** \brief matrix_can_read * - * Override this function if you have a condition where matrix tasks should not be available + * Allows overriding when matrix scanning operations should be executed. */ -__attribute__((weak)) bool matrix_available(void) { +__attribute__((weak)) bool matrix_can_read(void) { return true; } @@ -457,7 +457,7 @@ static inline void generate_tick_event(void) { * @return false Matrix didn't change */ static bool matrix_task(void) { - if (!matrix_available()) { + if (!matrix_can_read()) { generate_tick_event(); return false; } diff --git a/quantum/matrix.h b/quantum/matrix.h index 89f1885b07c1..8767b71ab429 100644 --- a/quantum/matrix.h +++ b/quantum/matrix.h @@ -46,8 +46,8 @@ void matrix_setup(void); void matrix_init(void); /* scan all key states on matrix */ uint8_t matrix_scan(void); -/* whether matrix tasks are available */ -bool matrix_available(void); +/* whether matrix scanning operations should be executed */ +bool matrix_can_read(void); /* whether a switch is on */ bool matrix_is_on(uint8_t row, uint8_t col); /* matrix state on row */