add zmk_is_externally_powered

This commit is contained in:
zhiayang 2024-02-23 01:44:59 -05:00
parent 4bdbd29b01
commit 54fae8a18a
No known key found for this signature in database
GPG key ID: 5E2F30AD6F08571F
3 changed files with 9 additions and 9 deletions

View file

@ -8,3 +8,4 @@
uint8_t zmk_battery_state_of_charge(void);
bool zmk_battery_is_charging(void);
bool zmk_is_externally_powered(void);

View file

@ -84,14 +84,6 @@ static void zmk_pm_resume_devices(void) {
#endif /* !CONFIG_PM_DEVICE_RUNTIME_EXCLUSIVE */
#endif /* CONFIG_PM_DEVICE */
bool is_usb_power_present(void) {
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
return zmk_usb_is_powered();
#else
return false;
#endif /* IS_ENABLED(CONFIG_USB_DEVICE_STACK) */
}
static enum zmk_activity_state activity_state;
static uint32_t activity_last_uptime;
@ -127,7 +119,7 @@ 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() || zmk_battery_is_charging())) {
if (inactive_time > MAX_SLEEP_MS && !zmk_is_externally_powered()) {
// Put devices in suspend power mode before sleeping
set_state(ZMK_ACTIVITY_SLEEP);

View file

@ -29,6 +29,13 @@ static bool last_battery_is_charging = false;
uint8_t zmk_battery_state_of_charge(void) { return last_state_of_charge; }
bool zmk_battery_is_charging(void) { return last_battery_is_charging; }
bool zmk_is_externally_powered(void) {
bool ret = zmk_battery_is_charging();
#if IS_ENABLED(CONFIG_USB_DEVICE_STACK)
ret |= zmk_usb_is_powered();
#endif
return ret;
}
#if DT_HAS_CHOSEN(zmk_battery)
static const struct device *const battery = DEVICE_DT_GET(DT_CHOSEN(zmk_battery));