From dd6d610df08c7182e733263b1838b73a753b4731 Mon Sep 17 00:00:00 2001 From: Nicolas Munnich Date: Fri, 2 Aug 2024 09:54:17 +0200 Subject: [PATCH 1/4] Added kconfig flag --- app/Kconfig | 7 +++++-- app/src/activity.c | 23 ++++++++++++++++++++++- docs/docs/config/power.md | 1 + 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index a45f2dc23f0..675fca065c4 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -163,8 +163,7 @@ config ZMK_BLE_EXPERIMENTAL_CONN bool "Experimental BLE connection changes" help Enables a combination of settings that are planned to be default in future versions of ZMK - to improve connection stability. This includes changes to timing on BLE pairing initiation, - restores use of the updated/new LLCP implementation, and disables 2M PHY support. + to improve connection stability. config ZMK_BLE_EXPERIMENTAL_SEC bool "Experimental BLE security changes" @@ -430,6 +429,10 @@ config ZMK_EXT_POWER bool "Enable support to control external power output" default y +config ZMK_EXT_POWER_IDLE_OFF + bool "Turn off external power while idle" + depends on ZMK_EXT_POWER + config ZMK_PM bool diff --git a/app/src/activity.c b/app/src/activity.c index 454e91e5da0..25198beb1bc 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -19,6 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +#include #include @@ -60,8 +61,19 @@ int set_state(enum zmk_activity_state state) { enum zmk_activity_state zmk_activity_get_state(void) { return activity_state; } int activity_event_listener(const zmk_event_t *eh) { +#if IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) activity_last_uptime = k_uptime_get(); - + if (activity_state == ZMK_ACTIVITY_IDLE) { + const struct device *ext_power = device_get_binding("EXT_POWER"); + if (ext_power == NULL) { + LOG_ERR("Unable to retrieve ext_power device on idle wake."); + } else { + ext_power_enable(ext_power); + } + } +#else + activity_last_uptime = k_uptime_get(); +#endif /* IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) */ return set_state(ZMK_ACTIVITY_ACTIVE); } @@ -83,6 +95,15 @@ void activity_work_handler(struct k_work *work) { } else #endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */ if (inactive_time > MAX_IDLE_MS) { +#if IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) + const struct device *ext_power = device_get_binding("EXT_POWER"); + if (ext_power == NULL) { + LOG_ERR("Unable to retrieve ext_power device on entering idle."); + return; + } + + ext_power_disable(ext_power); +#endif /* IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) */ set_state(ZMK_ACTIVITY_IDLE); } } diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index 1a142eb2e4b..c3b3a0ecc81 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -21,6 +21,7 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/ | Config | Type | Description | Default | | ------------------------------- | ---- | ----------------------------------------------------- | ------- | | `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | +| `CONFIG_ZMK_EXT_POWER_IDLE_OFF` | bool | Turn off external power while idle | n | | `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | | `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | From fdd82d27d3e1ef904cee7e12a6dd519060dad5d1 Mon Sep 17 00:00:00 2001 From: Nicolas Munnich Date: Fri, 2 Aug 2024 10:55:56 +0200 Subject: [PATCH 2/4] added zmk_idle_usb --- app/Kconfig | 4 ++++ app/src/activity.c | 10 ++++++---- docs/docs/config/power.md | 9 +++++---- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/app/Kconfig b/app/Kconfig index 675fca065c4..8910d33939f 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -406,6 +406,10 @@ config ZMK_IDLE_TIMEOUT int "Milliseconds of inactivity before entering idle state (OLED shutoff, etc)" default 30000 +config ZMK_IDLE_USB + bool "Allow idling while connected to USB" + default y + config ZMK_SLEEP bool "Enable deep sleep support" depends on HAS_POWEROFF diff --git a/app/src/activity.c b/app/src/activity.c index 25198beb1bc..12758f754e4 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -62,7 +62,6 @@ enum zmk_activity_state zmk_activity_get_state(void) { return activity_state; } int activity_event_listener(const zmk_event_t *eh) { #if IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) - activity_last_uptime = k_uptime_get(); if (activity_state == ZMK_ACTIVITY_IDLE) { const struct device *ext_power = device_get_binding("EXT_POWER"); if (ext_power == NULL) { @@ -71,9 +70,8 @@ int activity_event_listener(const zmk_event_t *eh) { ext_power_enable(ext_power); } } -#else - activity_last_uptime = k_uptime_get(); #endif /* IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) */ + activity_last_uptime = k_uptime_get(); return set_state(ZMK_ACTIVITY_ACTIVE); } @@ -94,7 +92,11 @@ void activity_work_handler(struct k_work *work) { sys_poweroff(); } else #endif /* IS_ENABLED(CONFIG_ZMK_SLEEP) */ - if (inactive_time > MAX_IDLE_MS) { + if (inactive_time > MAX_IDLE_MS +#if !IS_ENABLED(CONFIG_ZMK_IDLE_USB) + && !is_usb_power_present() +#endif /* IS_ENABLED(CONFIG_ZMK_IDLE_USB) */ + ) { #if IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) const struct device *ext_power = device_get_binding("EXT_POWER"); if (ext_power == NULL) { diff --git a/docs/docs/config/power.md b/docs/docs/config/power.md index c3b3a0ecc81..8ebf492cd05 100644 --- a/docs/docs/config/power.md +++ b/docs/docs/config/power.md @@ -21,7 +21,7 @@ Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/ | Config | Type | Description | Default | | ------------------------------- | ---- | ----------------------------------------------------- | ------- | | `CONFIG_ZMK_IDLE_TIMEOUT` | int | Milliseconds of inactivity before entering idle state | 30000 | -| `CONFIG_ZMK_EXT_POWER_IDLE_OFF` | bool | Turn off external power while idle | n | +| `CONFIG_ZMK_IDLE_USB` | bool | Enable idling while connected to USB power | y | | `CONFIG_ZMK_SLEEP` | bool | Enable deep sleep support | n | | `CONFIG_ZMK_IDLE_SLEEP_TIMEOUT` | int | Milliseconds of inactivity before entering deep sleep | 900000 | @@ -45,9 +45,10 @@ Driver for enabling or disabling power to peripherals such as displays and light Definition file: [zmk/app/Kconfig](https://github.com/zmkfirmware/zmk/blob/main/app/Kconfig) -| Config | Type | Description | Default | -| ---------------------- | ---- | ----------------------------------------------- | ------- | -| `CONFIG_ZMK_EXT_POWER` | bool | Enable support to control external power output | y | +| Config | Type | Description | Default | +| ------------------------------- | ---- | ----------------------------------------------- | ------- | +| `CONFIG_ZMK_EXT_POWER` | bool | Enable support to control external power output | y | +| `CONFIG_ZMK_EXT_POWER_IDLE_OFF` | bool | Turn off external power while idle | n | ### Devicetree From e4e6a0502c9aa01666138ad6b8c6f7f1dc3e289b Mon Sep 17 00:00:00 2001 From: Nicolas Munnich Date: Fri, 2 Aug 2024 10:59:54 +0200 Subject: [PATCH 3/4] prettier --- app/Kconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Kconfig b/app/Kconfig index 8910d33939f..e3d5e2a98e3 100644 --- a/app/Kconfig +++ b/app/Kconfig @@ -163,7 +163,7 @@ config ZMK_BLE_EXPERIMENTAL_CONN bool "Experimental BLE connection changes" help Enables a combination of settings that are planned to be default in future versions of ZMK - to improve connection stability. + to improve connection stability. config ZMK_BLE_EXPERIMENTAL_SEC bool "Experimental BLE security changes" From 0f5c88862eae8fe8f62279d47f6bd20d919ab830 Mon Sep 17 00:00:00 2001 From: Nicolas Munnich Date: Fri, 2 Aug 2024 11:05:13 +0200 Subject: [PATCH 4/4] Removed external power errors from disabling idle --- app/src/activity.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/app/src/activity.c b/app/src/activity.c index 12758f754e4..bec1dd9ad43 100644 --- a/app/src/activity.c +++ b/app/src/activity.c @@ -101,10 +101,9 @@ void activity_work_handler(struct k_work *work) { const struct device *ext_power = device_get_binding("EXT_POWER"); if (ext_power == NULL) { LOG_ERR("Unable to retrieve ext_power device on entering idle."); - return; + } else { + ext_power_disable(ext_power); } - - ext_power_disable(ext_power); #endif /* IS_ENABLED(CONFIG_ZMK_EXT_POWER_IDLE_OFF) */ set_state(ZMK_ACTIVITY_IDLE); }