From 45606e7fdfdfc25b39292b2b7402716c7e9f4f3c Mon Sep 17 00:00:00 2001 From: elpekenin Date: Fri, 15 Mar 2024 22:47:25 +0100 Subject: [PATCH 1/5] feat: stay awake while BLE connected --- app/Kconfig | 4 ++++ app/src/activity.c | 13 ++++++++++++- docs/docs/config/power.md | 11 ++++++----- 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index bb6997a4..7427bfc8 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -402,6 +402,10 @@ config ZMK_IDLE_SLEEP_TIMEOUT int "Milliseconds of inactivity before entering deep sleep" default 900000 +config ZMK_AWAKE_WHILE_BLE_CONNECTED + bool "Whether to stay awake while BLE is connected" + depends on ZMK_BLE + #ZMK_SLEEP endif diff --git a/app/src/activity.c b/app/src/activity.c index 8f421f85..7c9ab04d 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -26,6 +26,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #endif +#if IS_ENABLED(CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED) +#include +#endif + // Reimplement some of the device work from Zephyr PM to work with the new `sys_poweroff` API. // TODO: Tweak this to smarter runtime PM of subsystems on sleep. @@ -126,7 +130,14 @@ 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_AWAKE_WHILE_BLE_CONNECTED) + // if user inactive and no USB + // keyboard will sleep as soon as BLE is disconnected + && !zmk_ble_active_profile_is_connected() +#endif + ) { // Put devices in suspend power mode before sleeping set_state(ZMK_ACTIVITY_SLEEP); diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index 75e1b26a..06107621 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -18,11 +18,12 @@ In the deep sleep state, the keyboard additionally disconnects from Bluetooth an Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/Kconfig) -| Config | Type | Description | Default | -| ------------------------------- | ---- | ----------------------------------------------------- | ------- | -| `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | -| `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | -| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | +| Config | Type | Description | Default | +| -------------------------------------- | ---- | ----------------------------------------------------- | ------- | +| `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | +| `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | +| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | +| `CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED` | bool | Whether to stay awake while BLE is connected | n | ## External Power Control From 99f9031244101e822162a1690cbbad850510defb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADnez?= <58857054+elpekenin@users.noreply.github.com> Date: Sun, 17 Mar 2024 09:48:41 +0100 Subject: [PATCH 2/5] Update app/src/activity.c Fix logic for peripheral side Co-authored-by: Justin Ridgewell --- app/src/activity.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/src/activity.c b/app/src/activity.c index 7c9ab04d..e4420f48 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -135,7 +135,11 @@ void activity_work_handler(struct k_work *work) { #if IS_ENABLED(CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED) // if user inactive and no USB // keyboard will sleep as soon as BLE is disconnected +#if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && !zmk_ble_active_profile_is_connected() +#else + && !zmk_split_bt_peripheral_is_connected() +#endif #endif ) { // Put devices in suspend power mode before sleeping From 42a33f793715769fa689ff68644384253a878acf Mon Sep 17 00:00:00 2001 From: elpekenin Date: Mon, 18 Mar 2024 09:34:32 +0100 Subject: [PATCH 3/5] Update documentation, rename config --- app/Kconfig | 4 ++-- app/src/activity.c | 12 ++++++------ docs/docs/config/power.md | 12 ++++++------ 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 7427bfc8..63a4b623 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -402,8 +402,8 @@ config ZMK_IDLE_SLEEP_TIMEOUT int "Milliseconds of inactivity before entering deep sleep" default 900000 -config ZMK_AWAKE_WHILE_BLE_CONNECTED - bool "Whether to stay awake while BLE is connected" +config ZMK_NO_SLEEP_WHILE_BLE_CONNECTED + bool "Prevent deep sleep while BLE is connected" depends on ZMK_BLE #ZMK_SLEEP diff --git a/app/src/activity.c b/app/src/activity.c index e4420f48..d0034781 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -26,7 +26,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #endif -#if IS_ENABLED(CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED) +#if IS_ENABLED(CONFIG_ZMK_NO_SLEEP_WHILE_BLE_CONNECTED) #include #endif @@ -130,11 +130,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 IS_ENABLED(CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED) - // if user inactive and no USB - // keyboard will sleep as soon as BLE is disconnected + if (inactive_time > MAX_SLEEP_MS && !is_usb_power_present() +#if IS_ENABLED(CONFIG_ZMK_NO_SLEEP_WHILE_BLE_CONNECTED) + /* if user inactive and USB is not connected, + * keyboard will sleep as soon as BLE is disconnected + */ #if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) && !zmk_ble_active_profile_is_connected() #else diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index 06107621..b7029f87 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -18,12 +18,12 @@ In the deep sleep state, the keyboard additionally disconnects from Bluetooth an Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/Kconfig) -| Config | Type | Description | Default | -| -------------------------------------- | ---- | ----------------------------------------------------- | ------- | -| `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | -| `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | -| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | -| `CONFIG_ZMK_AWAKE_WHILE_BLE_CONNECTED` | bool | Whether to stay awake while BLE is connected | n | +| Config | Type | Description | Default | +| ----------------------------------------- | ---- | ------------------------------------------------------------------------------------------------ | ------- | +| `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | +| `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | +| `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | +| `CONFIG_ZMK_NO_SLEEP_WHILE_BLE_CONNECTED` | bool | Prevent deep sleep while BLE is connected (active profile on central, split comms on peripheral) | n | ## External Power Control From 4ccfeb2abd488e689cef033b32b317a6d9649c57 Mon Sep 17 00:00:00 2001 From: elpekenin Date: Wed, 10 Apr 2024 23:40:38 +0200 Subject: [PATCH 4/5] refactor: easier to read --- app/src/activity.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/activity.c b/app/src/activity.c index 57ccaf9c..6a5151af 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -72,19 +72,21 @@ int activity_event_listener(const zmk_event_t *eh) { 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() + bool prevent_sleep = is_usb_power_present(); + #if IS_ENABLED(CONFIG_ZMK_NO_SLEEP_WHILE_BLE_CONNECTED) /* if user inactive and USB is not connected, * keyboard will sleep as soon as BLE is disconnected */ #if !IS_ENABLED(CONFIG_ZMK_SPLIT) || IS_ENABLED(CONFIG_ZMK_SPLIT_ROLE_CENTRAL) - && !zmk_ble_active_profile_is_connected() + prevent_sleep = prevent_sleep || zmk_ble_active_profile_is_connected(); #else - && !zmk_split_bt_peripheral_is_connected() + prevent_sleep = prevent_sleep || zmk_split_bt_peripheral_is_connected(); #endif #endif - ) { + if (inactive_time > MAX_SLEEP_MX && !prevent_sleep) { // Put devices in suspend power mode before sleeping set_state(ZMK_ACTIVITY_SLEEP); From 0947d355d4864025b44a6706f7bad8369dc9b135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mart=C3=ADnez?= <58857054+elpekenin@users.noreply.github.com> Date: Fri, 12 Apr 2024 15:25:13 +0200 Subject: [PATCH 5/5] Update app/src/activity.c Co-authored-by: Justin Ridgewell --- app/src/activity.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/activity.c b/app/src/activity.c index 6a5151af..60b7d33c 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -86,7 +86,7 @@ void activity_work_handler(struct k_work *work) { prevent_sleep = prevent_sleep || zmk_split_bt_peripheral_is_connected(); #endif #endif - if (inactive_time > MAX_SLEEP_MX && !prevent_sleep) { + if (inactive_time > MAX_SLEEP_MS && !prevent_sleep) { // Put devices in suspend power mode before sleeping set_state(ZMK_ACTIVITY_SLEEP);