From 3a9cd32391c2e6a41297d09415969682c1a01819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 13 Mar 2023 22:32:42 +0100 Subject: [PATCH] main: add deep sleep on inactivity --- main/main.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/main/main.c b/main/main.c index 1f186bb..8b15543 100644 --- a/main/main.c +++ b/main/main.c @@ -7,6 +7,7 @@ #include #include "esp_log.h" #include "esp_mac.h" +#include "esp_sleep.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" @@ -284,6 +285,16 @@ void app_main(void) ESP_ERROR_CHECK( tusb_dfu_rf_init() ); // configure DFU runtime (ensures we can use it) ESP_LOGI(TAG, "USB initialized"); + // configure shutdown + io_conf.pin_bit_mask = (1ULL << GPIO_POWER); // GPIO to configure + io_conf.intr_type = GPIO_INTR_DISABLE; // disable interrupt + io_conf.mode = GPIO_MODE_INPUT; // set as input + io_conf.pull_down_en = 0; // don't use pull-down + io_conf.pull_up_en = 0; // there is already an external pull-up + ESP_ERROR_CHECK( gpio_config(&io_conf) ); // configure GPIO + ESP_ERROR_CHECK( esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL) ); // make sure all sources are disabled + ESP_ERROR_CHECK( esp_sleep_enable_ext0_wakeup(GPIO_POWER, 0) ); // use power button to wake up (low when pressed) + // task to decode and show the measurements meas_queue = xQueueCreate(MEAS_QUEUE_SIZE, sizeof(spi_slave_transaction_t)); ESP_ERROR_CHECK( NULL == meas_queue ); @@ -331,8 +342,12 @@ void app_main(void) fsync(fileno(stdout)); } timeout++; - if (timeout > 5) { + if (timeout > 10) { printf("\nshutting down due to inactivity\n"); + fflush(stdout); + fsync(fileno(stdout)); + gpio_set_level(LED_BOARD, 1); // ensure run LED is off + esp_deep_sleep_start(); // go to deep sleep } } }