From 679b3af632a389d737d8290f505f30e5b048c05d Mon Sep 17 00:00:00 2001 From: Nick Brassel Date: Tue, 21 Mar 2023 20:16:11 +1100 Subject: [PATCH] Sync activity timestamps between sides. (#20192) Co-authored-by: Stefan Kerkmann --- docs/feature_split_keyboard.md | 6 ++++ keyboards/tzarc/djinn/config.h | 3 +- keyboards/tzarc/djinn/djinn.c | 21 +++++-------- keyboards/tzarc/djinn/djinn.h | 3 +- keyboards/tzarc/djinn/djinn_split_sync.c | 5 +--- quantum/keyboard.c | 16 ++++++---- quantum/keyboard.h | 2 ++ quantum/split_common/transaction_id_define.h | 4 +++ quantum/split_common/transactions.c | 31 ++++++++++++++++++++ quantum/split_common/transport.h | 12 ++++++++ 10 files changed, 78 insertions(+), 25 deletions(-) diff --git a/docs/feature_split_keyboard.md b/docs/feature_split_keyboard.md index c095c8712fb6..1705ea922217 100644 --- a/docs/feature_split_keyboard.md +++ b/docs/feature_split_keyboard.md @@ -300,6 +300,12 @@ This enables transmitting the pointing device status to the master side of the s This enables triggering of haptic feedback on the slave side of the split keyboard. For DRV2605L this will send the mode, but for solenoids it is expected that the desired mode is already set up on the slave. +```c +#define SPLIT_ACTIVITY_ENABLE +``` + +This synchronizes the activity timestamps between sides of the split keyboard, allowing for activity timeouts to occur. + ### Custom data sync between sides :id=custom-data-sync QMK's split transport allows for arbitrary data transactions at both the keyboard and user levels. This is modelled on a remote procedure call, with the master invoking a function on the slave side, with the ability to send data from master to slave, process it slave side, and send data back from slave to master. diff --git a/keyboards/tzarc/djinn/config.h b/keyboards/tzarc/djinn/config.h index e8b2abc1f63f..c1c2f6832a37 100644 --- a/keyboards/tzarc/djinn/config.h +++ b/keyboards/tzarc/djinn/config.h @@ -1,4 +1,4 @@ -// Copyright 2018-2022 Nick Brassel (@tzarc) +// Copyright 2018-2023 Nick Brassel (@tzarc) // SPDX-License-Identifier: GPL-2.0-or-later #pragma once @@ -16,6 +16,7 @@ #define SPLIT_LED_STATE_ENABLE #define SPLIT_MODS_ENABLE #define SPLIT_WPM_ENABLE +#define SPLIT_ACTIVITY_ENABLE // SPI Configuration #define SPI_DRIVER SPID3 diff --git a/keyboards/tzarc/djinn/djinn.c b/keyboards/tzarc/djinn/djinn.c index 93b1ee775e1f..17e5833ee9ef 100644 --- a/keyboards/tzarc/djinn/djinn.c +++ b/keyboards/tzarc/djinn/djinn.c @@ -1,4 +1,4 @@ -// Copyright 2018-2022 Nick Brassel (@tzarc) +// Copyright 2018-2023 Nick Brassel (@tzarc) // SPDX-License-Identifier: GPL-2.0-or-later #include #include "quantum.h" @@ -74,7 +74,6 @@ void keyboard_post_init_kb(void) { qp_init(lcd, QP_ROTATION_0); // Turn on the LCD and clear the display - kb_state.lcd_power = 1; qp_power(lcd, true); qp_rect(lcd, 0, 0, 239, 319, HSV_BLACK, true); @@ -187,18 +186,14 @@ void housekeeping_task_kb(void) { } // Turn on/off the LCD - static bool lcd_on = false; - if (lcd_on != (bool)kb_state.lcd_power) { - lcd_on = (bool)kb_state.lcd_power; - qp_power(lcd, lcd_on); - } + bool peripherals_on = last_input_activity_elapsed() < LCD_ACTIVITY_TIMEOUT; // Enable/disable RGB - if (lcd_on) { + if (peripherals_on) { // Turn on RGB writePinHigh(RGB_POWER_ENABLE_PIN); // Modify the RGB state if different to the LCD state - if (rgb_matrix_is_enabled() != lcd_on) { + if (rgb_matrix_is_enabled() != peripherals_on) { // Wait for a small amount of time to allow the RGB capacitors to charge, before enabling RGB output wait_ms(10); // Enable RGB @@ -208,21 +203,21 @@ void housekeeping_task_kb(void) { // Turn off RGB writePinLow(RGB_POWER_ENABLE_PIN); // Disable the PWM output for the RGB - if (rgb_matrix_is_enabled() != lcd_on) { + if (rgb_matrix_is_enabled() != peripherals_on) { rgb_matrix_disable_noeeprom(); } } // Match the backlight to the LCD state - if (is_keyboard_master() && is_backlight_enabled() != lcd_on) { - if (lcd_on) + if (is_keyboard_master() && is_backlight_enabled() != peripherals_on) { + if (peripherals_on) backlight_enable(); else backlight_disable(); } // Draw the UI - if (kb_state.lcd_power) { + if (peripherals_on) { draw_ui_user(false); } diff --git a/keyboards/tzarc/djinn/djinn.h b/keyboards/tzarc/djinn/djinn.h index ff6b555e8e52..70aa34ff08fb 100644 --- a/keyboards/tzarc/djinn/djinn.h +++ b/keyboards/tzarc/djinn/djinn.h @@ -1,4 +1,4 @@ -// Copyright 2018-2022 Nick Brassel (@tzarc) +// Copyright 2018-2023 Nick Brassel (@tzarc) // SPDX-License-Identifier: GPL-2.0-or-later #pragma once #include "quantum.h" @@ -20,7 +20,6 @@ const char* usbpd_str(usbpd_allowance_t allowance); #pragma pack(push) #pragma pack(1) typedef struct kb_runtime_config { - unsigned lcd_power : 1; usbpd_allowance_t current_setting : 2; } kb_runtime_config; #pragma pack(pop) diff --git a/keyboards/tzarc/djinn/djinn_split_sync.c b/keyboards/tzarc/djinn/djinn_split_sync.c index 3c7a58d155d1..8b10e88b4b24 100644 --- a/keyboards/tzarc/djinn/djinn_split_sync.c +++ b/keyboards/tzarc/djinn/djinn_split_sync.c @@ -1,4 +1,4 @@ -// Copyright 2018-2022 Nick Brassel (@tzarc) +// Copyright 2018-2023 Nick Brassel (@tzarc) // SPDX-License-Identifier: GPL-2.0-or-later #include #include "quantum.h" @@ -13,9 +13,6 @@ void kb_state_update(void) { if (is_keyboard_master()) { // Modify allowed current limits usbpd_update(); - - // Turn off the LCD if there's been no matrix activity - kb_state.lcd_power = (last_input_activity_elapsed() < LCD_ACTIVITY_TIMEOUT) ? 1 : 0; } } diff --git a/quantum/keyboard.c b/quantum/keyboard.c index ec2f2e4496ff..6e7d40ddc745 100644 --- a/quantum/keyboard.c +++ b/quantum/keyboard.c @@ -114,7 +114,7 @@ uint32_t last_input_activity_time(void) { return last_input_modification_time; } uint32_t last_input_activity_elapsed(void) { - return timer_elapsed32(last_input_modification_time); + return sync_timer_elapsed32(last_input_modification_time); } static uint32_t last_matrix_modification_time = 0; @@ -122,10 +122,10 @@ 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); + return sync_timer_elapsed32(last_matrix_modification_time); } void last_matrix_activity_trigger(void) { - last_matrix_modification_time = last_input_modification_time = timer_read32(); + last_matrix_modification_time = last_input_modification_time = sync_timer_read32(); } static uint32_t last_encoder_modification_time = 0; @@ -133,10 +133,16 @@ uint32_t last_encoder_activity_time(void) { return last_encoder_modification_time; } uint32_t last_encoder_activity_elapsed(void) { - return timer_elapsed32(last_encoder_modification_time); + return sync_timer_elapsed32(last_encoder_modification_time); } void last_encoder_activity_trigger(void) { - last_encoder_modification_time = last_input_modification_time = timer_read32(); + last_encoder_modification_time = last_input_modification_time = sync_timer_read32(); +} + +void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp) { + last_matrix_modification_time = matrix_timestamp; + last_encoder_modification_time = encoder_timestamp; + last_input_modification_time = MAX(matrix_timestamp, encoder_timestamp); } // Only enable this if console is enabled to print to diff --git a/quantum/keyboard.h b/quantum/keyboard.h index d0b52dd13a0d..caf0fbd4661f 100644 --- a/quantum/keyboard.h +++ b/quantum/keyboard.h @@ -120,6 +120,8 @@ uint32_t last_matrix_activity_elapsed(void); // Number of milliseconds since the uint32_t last_encoder_activity_time(void); // Timestamp of the last encoder activity uint32_t last_encoder_activity_elapsed(void); // Number of milliseconds since the last encoder activity +void set_activity_timestamps(uint32_t matrix_timestamp, uint32_t encoder_timestamp); // Set the timestamps of the last matrix and encoder activity + uint32_t get_matrix_scan_rate(void); #ifdef __cplusplus diff --git a/quantum/split_common/transaction_id_define.h b/quantum/split_common/transaction_id_define.h index 18d3826b838c..4932530aa39b 100644 --- a/quantum/split_common/transaction_id_define.h +++ b/quantum/split_common/transaction_id_define.h @@ -92,6 +92,10 @@ enum serial_transaction_id { PUT_HAPTIC, #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE) +#if defined(SPLIT_ACTIVITY_ENABLE) + PUT_ACTIVITY, +#endif // SPLIT_ACTIVITY_ENABLE + #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) PUT_RPC_INFO, PUT_RPC_REQ_DATA, diff --git a/quantum/split_common/transactions.c b/quantum/split_common/transactions.c index 8e1961b58623..ec34bbba60c3 100644 --- a/quantum/split_common/transactions.c +++ b/quantum/split_common/transactions.c @@ -790,6 +790,34 @@ static void haptic_handlers_slave(matrix_row_t master_matrix[], matrix_row_t sla #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE) +#if defined(SPLIT_ACTIVITY_ENABLE) + +static bool activity_handlers_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { + static uint32_t last_update = 0; + split_slave_activity_sync_t activity_sync; + activity_sync.matrix_timestamp = last_matrix_activity_time(); + activity_sync.encoder_timestamp = last_encoder_activity_time(); + return send_if_data_mismatch(PUT_ACTIVITY, &last_update, &activity_sync, &split_shmem->activity_sync, sizeof(activity_sync)); +} + +static void activity_handlers_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[]) { + set_activity_timestamps(split_shmem->activity_sync.matrix_timestamp, split_shmem->activity_sync.encoder_timestamp); +} + +// clang-format off +# define TRANSACTIONS_ACTIVITY_MASTER() TRANSACTION_HANDLER_MASTER(activity) +# define TRANSACTIONS_ACTIVITY_SLAVE() TRANSACTION_HANDLER_SLAVE_AUTOLOCK(activity) +# define TRANSACTIONS_ACTIVITY_REGISTRATIONS [PUT_ACTIVITY] = trans_initiator2target_initializer(activity_sync), +// clang-format on + +#else // defined(SPLIT_ACTIVITY_ENABLE) + +# define TRANSACTIONS_ACTIVITY_MASTER() +# define TRANSACTIONS_ACTIVITY_SLAVE() +# define TRANSACTIONS_ACTIVITY_REGISTRATIONS + +#endif // defined(SPLIT_ACTIVITY_ENABLE) + //////////////////////////////////////////////////// split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = { @@ -818,6 +846,7 @@ split_transaction_desc_t split_transaction_table[NUM_TOTAL_TRANSACTIONS] = { TRANSACTIONS_POINTING_REGISTRATIONS TRANSACTIONS_WATCHDOG_REGISTRATIONS TRANSACTIONS_HAPTIC_REGISTRATIONS + TRANSACTIONS_ACTIVITY_REGISTRATIONS // clang-format on #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) @@ -846,6 +875,7 @@ bool transactions_master(matrix_row_t master_matrix[], matrix_row_t slave_matrix TRANSACTIONS_POINTING_MASTER(); TRANSACTIONS_WATCHDOG_MASTER(); TRANSACTIONS_HAPTIC_MASTER(); + TRANSACTIONS_ACTIVITY_MASTER(); return true; } @@ -867,6 +897,7 @@ void transactions_slave(matrix_row_t master_matrix[], matrix_row_t slave_matrix[ TRANSACTIONS_POINTING_SLAVE(); TRANSACTIONS_WATCHDOG_SLAVE(); TRANSACTIONS_HAPTIC_SLAVE(); + TRANSACTIONS_ACTIVITY_SLAVE(); } #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) diff --git a/quantum/split_common/transport.h b/quantum/split_common/transport.h index adee4470d22b..8f8c38461e25 100644 --- a/quantum/split_common/transport.h +++ b/quantum/split_common/transport.h @@ -122,6 +122,14 @@ typedef struct _split_slave_haptic_sync_t { } split_slave_haptic_sync_t; #endif // defined(HAPTIC_ENABLE) && defined(SPLIT_HAPTIC_ENABLE) +#if defined(SPLIT_ACTIVITY_ENABLE) +# include "keyboard.h" +typedef struct _split_slave_activity_sync_t { + uint32_t matrix_timestamp; + uint32_t encoder_timestamp; +} split_slave_activity_sync_t; +#endif // defined(SPLIT_ACTIVITY_ENABLE) + #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) typedef struct _rpc_sync_info_t { uint8_t checksum; @@ -204,6 +212,10 @@ typedef struct _split_shared_memory_t { split_slave_haptic_sync_t haptic_sync; #endif // defined(HAPTIC_ENABLE) +#if defined(SPLIT_ACTIVITY_ENABLE) + split_slave_activity_sync_t activity_sync; +#endif // defined(SPLIT_ACTIVITY_ENABLE) + #if defined(SPLIT_TRANSACTION_IDS_KB) || defined(SPLIT_TRANSACTION_IDS_USER) rpc_sync_info_t rpc_info; uint8_t rpc_m2s_buffer[RPC_M2S_BUFFER_SIZE];