diff --git a/src/config.cpp b/src/config.cpp index 3919c91..0ad9e6f 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -9,3 +9,5 @@ Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); AsyncWebServer server(80); Audio audio; Preferences preferences; + +long lastActionTime = 0; diff --git a/src/config.h b/src/config.h index 44803fa..589ecd9 100644 --- a/src/config.h +++ b/src/config.h @@ -19,8 +19,10 @@ // GPIO #define LED 2 #define BUTTON 33 +#define DEEP_SLEEP_WAKEUP GPIO_NUM_33 -#define DEBOUNCE_DELAY 1000 +#define DEBOUNCE_DELAY_MS 1000 +#define DEEP_SLEEP_DELAY_MS 3600000 // 1h // Screen #define SSD1306_NO_SPLASH @@ -54,5 +56,6 @@ extern Adafruit_SSD1306 display; extern AsyncWebServer server; extern Audio audio; extern Preferences preferences; +extern long lastActionTime; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 856db1c..abb28e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ byte buttonLastState = HIGH; long lastDebounceTime = 0; +bool wasRunning = false; void setup() { @@ -38,32 +39,43 @@ void setup() displayWifiCreds(); displayStatus(); - // Setup is done, light up the LED - Serial.println("All setup & ready to go!"); setCpuFrequencyMhz(80); + + // Setup is done, light up the LED + delay(500); + Serial.println("All setup & ready to go!"); digitalWrite(LED, HIGH); + updateLastAction(); + wasRunning = audio.isRunning(); } void loop() { - if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY) + if ((millis() - lastDebounceTime) > DEBOUNCE_DELAY_MS) { byte buttonCurrentState = digitalRead(BUTTON); if (buttonCurrentState == LOW && buttonLastState == HIGH) play(); buttonLastState = buttonCurrentState; } - audio.loop(); -} -void audio_info(const char *info){ - String s_info = info; - s_info.toLowerCase(); - s_info.trim(); - if (s_info == "closing audio file") { - setCpuFrequencyMhz(80); - } - else if (s_info == "stream ready") { + audio.loop(); + + bool running = audio.isRunning(); + if (running && !wasRunning) + { + wasRunning = true; setCpuFrequencyMhz(240); } + else if (!running && wasRunning) + { + wasRunning = false; + updateLastAction(); + setCpuFrequencyMhz(80); + } + + if (!running && ((millis() - lastActionTime) > DEEP_SLEEP_DELAY_MS)) + { + deepSleep(); + } } diff --git a/src/utils.cpp b/src/utils.cpp index 9006480..3d13da8 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -5,6 +5,7 @@ #include #include "config.h" +#include "utils.h" bool fileIsValid(String fileName) { @@ -31,6 +32,7 @@ void displayText(String text) void play() { + updateLastAction(); String selectedFile = preferences.getString(SELECTED_FILE); String path = "/" + selectedFile; Serial.println("Playing file: " + path); @@ -38,7 +40,6 @@ void play() audio.connecttoFS(SD, path.c_str()); } - void diagnosticPrint(String text) { Serial.print(text); @@ -92,8 +93,26 @@ void displayStatus() void selectFile(String fileName) { - preferences.putString(SELECTED_FILE, fileName); - displayStatus(); - Serial.print("Select new file: "); - Serial.println(fileName); + preferences.putString(SELECTED_FILE, fileName); + displayStatus(); + Serial.print("Select new file: "); + Serial.println(fileName); +} + +void deepSleep() +{ + display.clearDisplay(); + display.setCursor(0, 0); + display.println("Veille. Retourne-moi pour me reveiller."); + display.display(); + + Serial.println("Deep sleep!"); + esp_sleep_enable_ext0_wakeup(DEEP_SLEEP_WAKEUP, HIGH); + digitalWrite(LED, LOW); + esp_deep_sleep_start(); +} + +void updateLastAction() +{ + lastActionTime = millis(); } diff --git a/src/utils.h b/src/utils.h index a622399..012f18f 100644 --- a/src/utils.h +++ b/src/utils.h @@ -19,4 +19,9 @@ void displayStatus(); void diagnosticPrint(String text); void diagnosticPrintln(String text); +void deepSleep(); + +void audio_info(const char *info); +void updateLastAction(); + #endif diff --git a/src/webHandlers.cpp b/src/webHandlers.cpp index 253d7d6..8a825ec 100644 --- a/src/webHandlers.cpp +++ b/src/webHandlers.cpp @@ -11,6 +11,7 @@ void onStop(AsyncWebServerRequest *request) { + updateLastAction(); Serial.println("Stop playing"); audio.stopSong(); request->send(200); @@ -43,6 +44,7 @@ void onStatus(AsyncWebServerRequest *request) void onListFiles(AsyncWebServerRequest *request) { + updateLastAction(); Serial.print("List files cursor="); int cursor = 0; if (request->hasParam("cursor")) { @@ -87,6 +89,7 @@ void onListFiles(AsyncWebServerRequest *request) void onSelectFile(AsyncWebServerRequest *request) { + updateLastAction(); Serial.println("Select file"); if (request->hasParam("fileName", true)) { @@ -98,6 +101,7 @@ void onSelectFile(AsyncWebServerRequest *request) void onChangeVolume(AsyncWebServerRequest *request) { + updateLastAction(); Serial.print("Volume: "); if (request->hasParam("modifier", true)) { @@ -120,12 +124,14 @@ void onChangeVolume(AsyncWebServerRequest *request) void onUpload(AsyncWebServerRequest *request) { + updateLastAction(); Serial.println("onUpload"); request->send(200); } void onUploadFile(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { + updateLastAction(); if (!index) { Serial.printf("Upload start: %s\n", filename.c_str());