Skip to content

Commit

Permalink
feat(sleep): Option to enter deep sleep after BLE connection severed
Browse files Browse the repository at this point in the history
Introduce a new Kconfig flag to enable enter SLEEP mode on device if BT connection severed from main active profile
  • Loading branch information
mdkul22 committed Nov 25, 2024
1 parent d39941d commit dc45ee4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ config ZMK_IDLE_SLEEP_TIMEOUT
#ZMK_SLEEP
endif

config ZMK_SLEEP_ON_BLE_DISCONNECT
bool "Enable deep sleep on BLE disconnection"
depends on ZMK_SLEEP

if ZMK_SLEEP_ON_BLE_DISCONNECT

config ZMK_SLEEP_DISCONNECT_TIMER
int "Milliseconds of disconnection before entering deep sleep"
default 600000

# ZMK_SLEEP_ON_BLE_DISCONNECT
endif

config ZMK_EXT_POWER
bool "Enable support to control external power output"
default y
Expand Down
5 changes: 4 additions & 1 deletion app/include/zmk/split/bluetooth/peripheral.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#pragma once

#define IS_SPLIT_PERIPHERAL \
(IS_ENABLED(CONFIG_ZMK_SPLIT) && !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL))

bool zmk_split_bt_peripheral_is_connected(void);

bool zmk_split_bt_peripheral_is_bonded(void);
bool zmk_split_bt_peripheral_is_bonded(void);
30 changes: 29 additions & 1 deletion app/src/activity.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
* SPDX-License-Identifier: MIT
*/

#include "zephyr/sys/util_macro.h"
#include <zephyr/device.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/sys/poweroff.h>
#include <zmk/ble.h>
#include <zmk/split/bluetooth/peripheral.h>

#include <zephyr/logging/log.h>

Expand All @@ -26,8 +29,29 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/usb.h>
#endif

#if IS_ENABLED(CONFIG_ZMK_SLEEP_ON_BLE_DISCONNECT)
#define MAX_DISCONNECT_MS CONFIG_ZMK_SLEEP_DISCONNECT_TIMER

static uint32_t ble_last_time_connected;

static uint32_t get_latest_ble_connection_active_timestamp(void) {
#if ZMK_BLE_IS_CENTRAL || !IS_ENABLED(CONFIG_ZMK_SPLIT)
if (zmk_ble_active_profile_is_connected()) {
ble_last_time_connected = k_uptime_get();
}
return ble_last_time_connected;
#elif IS_SPLIT_PERIPHERAL
if (zmk_split_bt_peripheral_is_connected()) {
ble_last_time_connected = k_uptime_get();
}
return ble_last_time_connected;
#endif
}
#endif

bool is_usb_power_present(void) {
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
return false;
return zmk_usb_is_powered();
#else
return false;
Expand Down Expand Up @@ -69,7 +93,11 @@ void activity_work_handler(struct k_work *work) {
int32_t current = k_uptime_get();
int32_t inactive_time = current - activity_last_uptime;
#if IS_ENABLED(CONFIG_ZMK_SLEEP)
if (inactive_time > MAX_SLEEP_MS && !is_usb_power_present()) {
if ((inactive_time > MAX_SLEEP_MS && !is_usb_power_present())
#if IS_ENABLED(CONFIG_ZMK_SLEEP_ON_BLE_DISCONNECT)
|| (current - get_latest_ble_connection_active_timestamp()) > MAX_DISCONNECT_MS
#endif
) {
// Put devices in suspend power mode before sleeping
set_state(ZMK_ACTIVITY_SLEEP);

Expand Down
3 changes: 1 addition & 2 deletions app/src/behaviors/behavior_soft_off.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <zmk/pm.h>
#include <zmk/behavior.h>
#include <zmk/split/bluetooth/peripheral.h>

LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);

Expand All @@ -24,8 +25,6 @@ struct behavior_soft_off_data {
uint32_t press_start;
};

#define IS_SPLIT_PERIPHERAL \
(IS_ENABLED(CONFIG_ZMK_SPLIT) && !IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL))

static int behavior_soft_off_init(const struct device *dev) { return 0; };

Expand Down

0 comments on commit dc45ee4

Please sign in to comment.