Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
61 lines
1.7 KiB
C++
61 lines
1.7 KiB
C++
#include "deep_sleep_component.h"
|
|
#ifdef USE_ZEPHYR
|
|
#include "esphome/core/log.h"
|
|
#include <zephyr/sys/poweroff.h>
|
|
#include <zephyr/kernel.h>
|
|
#include <zephyr/stats/stats.h>
|
|
#include <zephyr/pm/pm.h>
|
|
|
|
namespace esphome::deep_sleep {
|
|
|
|
static const char *const TAG = "deep_sleep";
|
|
|
|
void DeepSleepComponent::wakeup() { k_sem_give(&this->wakeup_sem_); }
|
|
|
|
optional<uint32_t> DeepSleepComponent::get_run_duration_() const { return this->run_duration_; }
|
|
|
|
void DeepSleepComponent::dump_config_platform_() {}
|
|
|
|
bool DeepSleepComponent::prepare_to_sleep_() { return true; }
|
|
|
|
void DeepSleepComponent::deep_sleep_() {
|
|
k_timeout_t sleep_duration = K_FOREVER;
|
|
if (this->sleep_duration_.has_value()) {
|
|
sleep_duration = K_USEC(*this->sleep_duration_);
|
|
} else {
|
|
#ifndef USE_ZIGBEE
|
|
// the device can be woken up through one of the following signals:
|
|
// - The DETECT signal, optionally generated by the GPIO peripheral.
|
|
// - The ANADETECT signal, optionally generated by the LPCOMP module.
|
|
// - The SENSE signal, optionally generated by the NFC module to wake-on-field.
|
|
// - Detecting a valid USB voltage on the VBUS pin (VBUS,DETECT).
|
|
// - A reset.
|
|
//
|
|
// The system is reset when it wakes up from System OFF mode.
|
|
sys_poweroff();
|
|
#endif
|
|
}
|
|
// It might wake up immediately if k_sem_give was called again after wake up
|
|
int ret = k_sem_take(&this->wakeup_sem_, sleep_duration);
|
|
if (ret == 0) {
|
|
ESP_LOGD(TAG, "Woken up by another thread");
|
|
} else {
|
|
ESP_LOGD(TAG, "Timeout expired (normal sleep)");
|
|
}
|
|
}
|
|
|
|
bool DeepSleepComponent::should_teardown_() {
|
|
if (this->sleep_duration_.has_value()) {
|
|
return false;
|
|
}
|
|
#ifdef USE_ZIGBEE
|
|
return false;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
} // namespace esphome::deep_sleep
|
|
|
|
#endif
|