Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync activity timestamps between sides. #20192

Merged
merged 4 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/feature_split_keyboard.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion keyboards/tzarc/djinn/config.h
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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
Expand Down
21 changes: 8 additions & 13 deletions keyboards/tzarc/djinn/djinn.c
Original file line number Diff line number Diff line change
@@ -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 <string.h>
#include "quantum.h"
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand All @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions keyboards/tzarc/djinn/djinn.h
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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)
Expand Down
5 changes: 1 addition & 4 deletions keyboards/tzarc/djinn/djinn_split_sync.c
Original file line number Diff line number Diff line change
@@ -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 <string.h>
#include "quantum.h"
Expand All @@ -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;
}
}

Expand Down
16 changes: 11 additions & 5 deletions quantum/keyboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,35 @@ 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;
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;
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
Expand Down
2 changes: 2 additions & 0 deletions quantum/keyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions quantum/split_common/transaction_id_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
31 changes: 31 additions & 0 deletions quantum/split_common/transactions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions quantum/split_common/transport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
Expand Down