main: add deep sleep on inactivity

This commit is contained in:
King Kévin 2023-03-13 22:32:42 +01:00
parent dd8a3263c6
commit 3a9cd32391
1 changed files with 16 additions and 1 deletions

View File

@ -7,6 +7,7 @@
#include <sys/reent.h>
#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
}
}
}