From d00a6866068f43af1e1cba009295b6f278e0d58c Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Thu, 22 Oct 2020 22:12:12 +1100 Subject: [PATCH] Allow recording of the last matrix activity time, to simplify implementation of display timeouts and the like. --- quantum/split_common/matrix.c | 34 ++++++++++++++++++++++++---------- tmk_core/common/keyboard.c | 13 +++++++------ tmk_core/common/keyboard.h | 3 +++ 3 files changed, 34 insertions(+), 16 deletions(-) diff --git a/quantum/split_common/matrix.c b/quantum/split_common/matrix.c index 51bf8b109566..a390d1556f96 100644 --- a/quantum/split_common/matrix.c +++ b/quantum/split_common/matrix.c @@ -245,23 +245,37 @@ void matrix_init(void) { split_post_init(); } -void matrix_post_scan(void) { +bool matrix_pre_scan(void) { + bool changed = false; if (is_keyboard_master()) { static uint8_t error_count; - if (!transport_master(matrix + thatHand)) { + matrix_row_t slave_matrix[ROWS_PER_HAND] = {0}; + if (!transport_master(slave_matrix)) { error_count++; - - if (error_count > ERROR_DISCONNECT_COUNT) { - // reset other half if disconnected - for (int i = 0; i < ROWS_PER_HAND; ++i) { - matrix[thatHand + i] = 0; - } - } } else { error_count = 0; } + if (error_count > ERROR_DISCONNECT_COUNT) { + // reset other half if disconnected + for (int i = 0; i < ROWS_PER_HAND; ++i) { + slave_matrix[i] = 0; + } + } + + for (int i = 0; i < ROWS_PER_HAND; ++i) { + if (matrix[thatHand + i] != slave_matrix[i]) { + matrix[thatHand + i] = slave_matrix[i]; + changed = true; + } + } + } + return changed; +} + +void matrix_post_scan(void) { + if (is_keyboard_master()) { matrix_scan_quantum(); } else { transport_slave(matrix + thisHand); @@ -271,7 +285,7 @@ void matrix_post_scan(void) { } uint8_t matrix_scan(void) { - bool changed = false; + bool changed = matrix_pre_scan(); #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW) // Set row, read cols diff --git a/tmk_core/common/keyboard.c b/tmk_core/common/keyboard.c index a1fbc01da674..aea09169fb2f 100644 --- a/tmk_core/common/keyboard.c +++ b/tmk_core/common/keyboard.c @@ -97,6 +97,10 @@ along with this program. If not, see . # include "dip_switch.h" #endif +static uint32_t last_matrix_modification_time = 0; +uint32_t last_matrix_activity_time(void) { return last_matrix_modification_time; } +uint32_t last_matrix_activity_elapsed(void) { return timer_elapsed32(last_matrix_modification_time); } + // Only enable this if console is enabled to print to #if defined(DEBUG_MATRIX_SCAN_RATE) && defined(CONSOLE_ENABLE) static uint32_t matrix_timer = 0; @@ -338,11 +342,8 @@ void keyboard_task(void) { housekeeping_task_kb(); housekeeping_task_user(); -#if defined(OLED_DRIVER_ENABLE) && !defined(OLED_DISABLE_TIMEOUT) - uint8_t ret = matrix_scan(); -#else - matrix_scan(); -#endif + uint8_t matrix_changed = matrix_scan(); + if (matrix_changed) last_matrix_modification_time = timer_read32(); if (should_process_keypress()) { for (uint8_t r = 0; r < MATRIX_ROWS; r++) { @@ -409,7 +410,7 @@ void keyboard_task(void) { oled_task(); # ifndef OLED_DISABLE_TIMEOUT // Wake up oled if user is using those fabulous keys! - if (ret) oled_on(); + if (matrix_changed) oled_on(); # endif #endif diff --git a/tmk_core/common/keyboard.h b/tmk_core/common/keyboard.h index d04e685cdb2e..cc5b2e5e4229 100644 --- a/tmk_core/common/keyboard.h +++ b/tmk_core/common/keyboard.h @@ -73,6 +73,9 @@ void keyboard_post_init_user(void); void housekeeping_task_kb(void); void housekeeping_task_user(void); +uint32_t last_matrix_activity_time(void); // Timestamp of the last matrix activity +uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the last matrix activity + #ifdef __cplusplus } #endif