Skip to content

Commit

Permalink
add 2nd sync timer
Browse files Browse the repository at this point in the history
  • Loading branch information
angweekiat committed Sep 8, 2024
1 parent 4be177a commit 63b989c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/include/zmk/split/bluetooth/central.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ int zmk_split_get_peripheral_battery_level(uint8_t source, uint8_t *level);

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)

int zmk_split_bt_sync_activity(int32_t inactive_duration);
int zmk_split_bt_queue_sync_activity(int32_t inactive_duration);

#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)
25 changes: 18 additions & 7 deletions app/src/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ static enum zmk_activity_state activity_state;
static uint32_t activity_last_uptime;
#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
static uint32_t last_sync_time;
#define SLEEP_TIMERS_SYNC_MS CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_INTERVAL_MS
static uint32_t last_periodic_sync_time;
#if ZMK_SPLIT_SYNC_KEY_PRESS_INTERVAL_MS > 0
static uint32_t last_event_sync_time;
#endif // ZMK_SPLIT_SYNC_KEY_PRESS_INTERVAL_MS > 0
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)

Expand Down Expand Up @@ -73,6 +75,15 @@ enum zmk_activity_state zmk_activity_get_state(void) { return activity_state; }
int activity_event_listener(const zmk_event_t *eh) {
activity_last_uptime = k_uptime_get();

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && ZMK_SPLIT_SYNC_KEY_PRESS_INTERVAL_MS > 0
if (current - last_event_sync_time > ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS) {
last_event_sync_time = current;
zmk_split_bt_queue_sync_activity(0);
}
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && ZMK_SPLIT_SYNC_KEY_PRESS_INTERVAL_MS > 0

return set_state(ZMK_ACTIVITY_ACTIVE);
}

Expand All @@ -95,14 +106,14 @@ void activity_work_handler(struct k_work *work) {
} else
#endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */
if (inactive_time > MAX_IDLE_MS) {
set_state(ZMK_ACTIVITY_IDLE);
}
set_state(ZMK_ACTIVITY_IDLE);
}

#if IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) && \
IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
if (current - last_sync_time > SLEEP_TIMERS_SYNC_MS) {
last_sync_time = current;
zmk_split_bt_sync_activity(inactive_time);
if (current - last_periodic_sync_time > CONFIG_ZMK_SPLIT_SYNC_PERIODIC_INTERVAL_MS) {
last_periodic_sync_time = current;
zmk_split_bt_queue_sync_activity(inactive_time);
}
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING) &&
// IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
Expand Down
8 changes: 6 additions & 2 deletions app/src/split/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ config ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
Does not help to wake up peripheral devices that have gone to deep sleep.

if ZMK_SPLIT_ROLE_CENTRAL && ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
config ZMK_SPLIT_SYNC_LAST_ACTIVITY_INTERVAL_MS
int "Last activity time sync interval in milliseconds"
config ZMK_SPLIT_SYNC_PERIODIC_INTERVAL_MS
int "Last activity time periodic sync interval in milliseconds"
default 30000

config ZMK_SPLIT_SYNC_EVENT_MIN_INTERVAL_MS
int "Last activity time sync interval in milliseconds"
default 0

#ZMK_SPLIT_ROLE_CENTRAL && ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING
endif

Expand Down
2 changes: 1 addition & 1 deletion app/src/split/bluetooth/central.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,7 @@ static void split_central_sync_activity_with_delay() {
k_timer_start(&split_central_sync_activity_delay_timer, K_SECONDS(1), K_SECONDS(1));
}

int zmk_split_bt_sync_activity(int32_t inactive_duration) {
int zmk_split_bt_queue_sync_activity(int32_t inactive_duration) {
activity_inactive_duration = inactive_duration;
return k_work_submit_to_queue(&split_central_split_run_q, &split_central_sync_activity);
}
Expand Down
1 change: 1 addition & 0 deletions app/src/split/bluetooth/service.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ BT_GATT_SERVICE_DEFINE(
BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL,
split_svc_sync_activity, NULL),
#endif // IS_ENABLED(CONFIG_ZMK_SPLIT_SYNC_LAST_ACTIVITY_TIMING)

);

K_THREAD_STACK_DEFINE(service_q_stack, CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE);
Expand Down

0 comments on commit 63b989c

Please sign in to comment.