1
0
mirror of https://github.com/esphome/esphome.git synced 2025-04-08 20:00:27 +01:00

add deep sleep to ble

This commit is contained in:
Tomasz Duda 2024-05-18 21:13:04 +02:00
parent 1739b4bcfa
commit c6fb21d7d3
7 changed files with 65 additions and 6 deletions

View File

@ -9,10 +9,8 @@ static const char *const TAG = "deep_sleep";
bool global_has_deep_sleep = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
void DeepSleepComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
global_has_deep_sleep = true;
void DeepSleepComponent::setup_deep_sleep_() {
this->next_enter_deep_sleep_ = false;
const optional<uint32_t> run_duration = get_run_duration_();
if (run_duration.has_value()) {
ESP_LOGI(TAG, "Scheduling Deep Sleep to start in %" PRIu32 " ms", *run_duration);
@ -22,6 +20,13 @@ void DeepSleepComponent::setup() {
}
}
void DeepSleepComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
global_has_deep_sleep = true;
setup_deep_sleep_();
}
void DeepSleepComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Setting up Deep Sleep...");
if (this->sleep_duration_.has_value()) {
@ -64,6 +69,7 @@ void DeepSleepComponent::begin_sleep(bool manual) {
App.run_safe_shutdown_hooks();
deep_sleep_();
setup_deep_sleep_();
}
float DeepSleepComponent::get_setup_priority() const { return setup_priority::LATE; }

View File

@ -109,6 +109,7 @@ class DeepSleepComponent : public Component {
void dump_config_platform_();
bool prepare_to_sleep_();
void deep_sleep_();
void setup_deep_sleep_();
optional<uint64_t> sleep_duration_;
#ifdef USE_ESP32

View File

@ -23,7 +23,7 @@ void arch_init() {
wdt_config.window.max = 2000;
wdt_channel_id = wdt_install_timeout(wdt, &wdt_config);
if (wdt_channel_id >= 0) {
wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG);
wdt_setup(wdt, WDT_OPT_PAUSE_HALTED_BY_DBG | WDT_OPT_PAUSE_IN_SLEEP);
}
}
}

View File

@ -68,7 +68,17 @@ BT_CONN_CB_DEFINE(conn_callbacks) = {
void BLEServer::setup() {
k_work_init(&advertise_work, advertise);
resume_();
}
void BLEServer::loop() {
if (suspended_) {
resume_();
suspended_ = false;
}
}
void BLEServer::resume_() {
int rc = bt_enable(bt_ready);
if (rc != 0) {
ESP_LOGE(TAG, "Bluetooth enable failed: %d", rc);
@ -76,6 +86,11 @@ void BLEServer::setup() {
}
}
void BLEServer::on_shutdown() {
bt_disable();
suspended_ = true;
}
} // namespace zephyr_ble_server
} // namespace esphome

View File

@ -8,6 +8,12 @@ namespace zephyr_ble_server {
class BLEServer : public Component {
public:
void setup() override;
void loop() override;
void on_shutdown() override;
protected:
void resume_();
bool suspended_ = false;
};
} // namespace zephyr_ble_server

View File

@ -39,6 +39,12 @@ output:
mode:
output: true
id: rest_gpio
- platform: gpio
pin:
number: 13
mode:
output: true
id: ldo
dfu_mode:
reset_output: rest_gpio
@ -97,3 +103,12 @@ sensor:
pin: AIN0
name: "AIN0 Voltage"
update_interval: 5sec
deep_sleep:
text_sensor:
- platform: debug
reset_reason:
name: "Reset Reason"
zephyr_shell:

View File

@ -14,7 +14,6 @@ switch:
- platform: gpio
pin:
number: 15
inverted: true
mode:
output: true
id: gpio_15
@ -32,6 +31,12 @@ output:
mode:
output: true
id: rest_gpio
- platform: gpio
pin:
number: 13
mode:
output: true
id: ldo
dfu_mode:
reset_output: rest_gpio
@ -63,3 +68,14 @@ sensor:
pin: AIN0
name: "AIN0 Voltage"
update_interval: 5sec
deep_sleep:
run_duration: 10s
sleep_duration: 10s
text_sensor:
- platform: debug
reset_reason:
name: "Reset Reason"
zephyr_shell: