From 183c17c37c7647a386717e13c233ed4bb9ca8f00 Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Mon, 28 Aug 2023 10:05:45 +0200 Subject: [PATCH] Directly start a song from list --- src/WebContent.h | 11 +++++++++++ src/globals.h | 18 ++++++++++++++++++ src/main.cpp | 16 ++++++++++++++-- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/WebContent.h b/src/WebContent.h index c3f007b..fc80dfd 100644 --- a/src/WebContent.h +++ b/src/WebContent.h @@ -66,6 +66,7 @@ const char index_html[] PROGMEM = R"rawliteral( var liText = this.innerText; var inputField = document.getElementById('titleid'); inputField.value = liText; + playNamedSong(liText); }); }); @@ -92,6 +93,16 @@ const char index_html[] PROGMEM = R"rawliteral( document.getElementById("state").innerHTML = state; } + function playNamedSong(song) { + var xhr = new XMLHttpRequest(); + xhr.open("POST", "/playnamed"); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); + //application/x-www-form-urlencoded + var body = song; + xhr.send("title="+encodeURIComponent(body)); + + } + )rawliteral"; \ No newline at end of file diff --git a/src/globals.h b/src/globals.h index 76ff791..07e445b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -5,6 +5,8 @@ void stop(); void start(); +bool playFile(const char* filename, uint32_t resumeFilePos = 0); + void loop2(void* parameter); void named_song_action(AsyncWebServerRequest *request); @@ -30,6 +32,22 @@ String getRFIDString(byte uidByte[10]) return uidString; } +void writeFile(fs::FS &fs, const char * path, const char * message){ + Serial.printf("Writing file: %s\n", path); + + File file = fs.open(path, FILE_WRITE); + if(!file){ + Serial.println("Failed to open file for writing"); + return; + } + if(file.print(message)){ + Serial.println("File written"); + } else { + Serial.println("Write failed"); + } + file.close(); +} + unsigned long lastStart = 0; const int startDelay = 250; diff --git a/src/main.cpp b/src/main.cpp index 26449ff..484c128 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -88,7 +88,7 @@ void playSongByName(String song) { Serial.println(mp3File.c_str()); deactivateRFID(); activateSD(); - audio.connecttoSD(mp3File.c_str()); + playFile(mp3File.c_str()); activateRFID(); deactivateSD(); } @@ -101,6 +101,18 @@ void playSongByRFID(String id) { } +/** + * @brief Wrapper, so that we can intercept each call for other stuff. + * + * @param filename + * @param resumeFilePos + * @return true + * @return false + */ +bool playFile(const char* filename, uint32_t resumeFilePos) { + return audio.connecttoSD(filename, resumeFilePos); +} + void playNextMp3() @@ -123,7 +135,7 @@ void playNextMp3() Serial.println(mp3File.c_str()); deactivateRFID(); activateSD(); - audio.connecttoSD(mp3File.c_str()); + playFile(mp3File.c_str()); activateRFID(); deactivateSD(); }