From 3f04aa1c41d69d69e9c493f7e355d908890e0d5b Mon Sep 17 00:00:00 2001 From: Stefan Ostermann Date: Thu, 5 Oct 2023 21:00:26 +0200 Subject: [PATCH] User Interface, Wifi timeout --- src/WebContent.h | 8 +- src/globals.h | 2 + src/main.cpp | 195 +++++++++++++++++++++++++++-------------------- 3 files changed, 120 insertions(+), 85 deletions(-) diff --git a/src/WebContent.h b/src/WebContent.h index b51d369..9746fc8 100644 --- a/src/WebContent.h +++ b/src/WebContent.h @@ -11,9 +11,9 @@ const char index_html[] PROGMEM = R"rawliteral(

- - -

+ + +

@@ -23,7 +23,7 @@ const char index_html[] PROGMEM = R"rawliteral(
-
diff --git a/src/globals.h b/src/globals.h index 73b74c7..c02b33b 100644 --- a/src/globals.h +++ b/src/globals.h @@ -54,6 +54,8 @@ void writeFile(fs::FS &fs, const char * path, const char * message){ unsigned long lastStart = 0; +uint8_t buttontoignore = 0; + const int startDelay = 250; #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index bb48309..1de5cba 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,6 @@ #include //https://github.com/tzapu/WiFiManager WiFi Configuration Magic #include - #include "Audio.h" #include @@ -26,9 +25,9 @@ #define I2S_BCLK 27 // connect to DAC pin BCK #define I2S_LRC 25 // connect to DAC pin LCK -#define BTN_START_STOP 17 // Button on XX and GND -#define BTN_NEXT 16 -#define BTN_PREV 4 +#define BTN_START_STOP 4 // Button on XX and GND +#define BTN_NEXT 17 +#define BTN_PREV 16 #define CS_SDCARD 22 @@ -44,7 +43,7 @@ File mp3File; Audio audio; -uint volume = 8; +uint volume = 7; uint rfid_loop = RFID_LOOP_INTERVAL; @@ -71,7 +70,7 @@ bool asyncNext = false; bool asyncPrev = false; std::map rfid_map{{"67 152 204 14", "01-The_Box_Tops-The_Letter.mp3"}, - {"67 175 148 160","068-Der_Schatz_im_Bergsee"}}; + {"67 175 148 160", "068-Der_Schatz_im_Bergsee"}}; void activateSD() { @@ -93,7 +92,8 @@ void deactivateRFID() digitalWrite(CS_RFID, HIGH); } -void playSongByName(String song) { +void playSongByName(String song) +{ currentNode = rootNode.advanceToMP3(&song); String mp3File = currentNode->getCurrentPlayingFilePath(); Serial.println(mp3File.c_str()); @@ -104,28 +104,27 @@ void playSongByName(String song) { deactivateSD(); } -void playSongByRFID(String id) { +void playSongByRFID(String id) +{ String song = rfid_map[id]; Serial.println("searching for "); Serial.println(song); playSongByName(song); - } /** * @brief Wrapper, so that we can intercept each call for other stuff. - * - * @param filename - * @param resumeFilePos - * @return true - * @return false + * + * @param filename + * @param resumeFilePos + * @return true + * @return false */ -bool playFile(const char* filename, uint32_t resumeFilePos) { +bool playFile(const char *filename, uint32_t resumeFilePos) +{ return audio.connecttoSD(filename, resumeFilePos); } - - void playNextMp3() { stop(); @@ -142,7 +141,8 @@ void playNextMp3() currentNode = rootNode.advanceToNextMP3(currentNode->getCurrentPlaying()); } - if (currentNode!=NULL) { + if (currentNode != NULL) + { currentNode->setSecondsPlayed(0); } @@ -158,7 +158,7 @@ void playNextMp3() void audio_info(const char *info) { - //Serial.print("info "); Serial.println(info); + // Serial.print("info "); Serial.println(info); } void mute() @@ -180,18 +180,17 @@ String getState() { DynamicJsonDocument jsonState(1024); jsonState["playing"] = audio.isRunning(); - if (currentNode!=NULL) + if (currentNode != NULL) jsonState["title"] = *currentNode->getCurrentPlaying(); - else + else jsonState["title"] = "Angehalten"; - + jsonState["time"] = audio.getAudioCurrentTime(); jsonState["volume"] = audio.getVolume(); jsonState["length"] = audio.getAudioFileDuration(); String output; - serializeJson(jsonState,output); + serializeJson(jsonState, output); return output; - } String processor(const String &var) @@ -207,9 +206,10 @@ void stop() { if (audio.isRunning()) { - Serial.println("stopping audio."); + Serial.println("stopping audio."); audio.stopSong(); - if (currentNode!=NULL) { + if (currentNode != NULL) + { currentNode->setSecondsPlayed(0); } } @@ -230,8 +230,10 @@ void togglePlayPause() { if (currentNode != NULL) { - audio.pauseResume(); - } else { + audio.pauseResume(); + } + else + { playNextMp3(); } } @@ -280,10 +282,7 @@ void setup() deactivateRFID(); activateSD(); - // first parameter is name of access point, second is the password - AsyncWiFiManager wifiManager(&server, &dns); - wifiManager.autoConnect("HannaBox"); Serial.print("Initializing SD card..."); @@ -321,49 +320,59 @@ void setup() Serial.println("Audio initialized."); - server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) - { request->send_P(200, "text/html", index_html, processor); }); + // first parameter is name of access point, second is the password + AsyncWiFiManager wifiManager(&server, &dns); - server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) - { request->send_P(200, "text/css", css); }); + wifiManager.setTimeout(180); - server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request) - { + if (wifiManager.autoConnect("HannaBox")) + { + server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send_P(200, "text/html", index_html, processor); }); + + server.on("/style.css", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send_P(200, "text/css", css); }); + + server.on("/state", HTTP_GET, [](AsyncWebServerRequest *request) + { String state = getState(); request->send(200, "application/json", state.c_str()); }); - server.on("/start", HTTP_GET, [](AsyncWebServerRequest *request) - { + server.on("/start", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", "start"); start(); }); - - server.on("/toggleplaypause", HTTP_GET, [](AsyncWebServerRequest *request) - { + server.on("/toggleplaypause", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", "toggleplaypause"); togglePlayPause(); }); - server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) - { + server.on("/stop", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", "stop"); stop(); }); - server.on("/next", HTTP_GET, [](AsyncWebServerRequest *request) - { + server.on("/next", HTTP_GET, [](AsyncWebServerRequest *request) + { request->send(200, "text/plain", "next"); next(); }); - - server.on("/playnamed", HTTP_POST, named_song_action); - server.on("/progress", HTTP_POST, progress_action); + server.on("/playnamed", HTTP_POST, named_song_action); - server.on("/volume", HTTP_POST, volume_action); + server.on("/progress", HTTP_POST, progress_action); - server.begin(); + server.on("/volume", HTTP_POST, volume_action); + + server.begin(); + Serial.println("Wifi initialized."); + } else { + Serial.println("Wifi timed out. Fallback no Wifi."); + } xTaskCreatePinnedToCore( loop2, /* Function to implement the task */ @@ -377,42 +386,51 @@ void setup() Serial.println("initialization done."); } -void named_song_action(AsyncWebServerRequest *request) { +void named_song_action(AsyncWebServerRequest *request) +{ Serial.println("named song!"); int params = request->params(); - for (int i = 0; i < params; i++) { - AsyncWebParameter* p = request->getParam(i); + for (int i = 0; i < params; i++) + { + AsyncWebParameter *p = request->getParam(i); Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); - if (p->name()=="title") { + if (p->name() == "title") + { playSongByName(p->value()); } } request->send_P(200, "text/plain", "ok"); } -void progress_action(AsyncWebServerRequest *request) { +void progress_action(AsyncWebServerRequest *request) +{ Serial.println("progress!"); int params = request->params(); - for (int i = 0; i < params; i++) { - AsyncWebParameter* p = request->getParam(i); + for (int i = 0; i < params; i++) + { + AsyncWebParameter *p = request->getParam(i); Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); - if (p->name()=="value") { + if (p->name() == "value") + { audio.setAudioPlayPosition(atoi(p->value().c_str())); } } request->send_P(200, "text/plain", "ok"); } -void volume_action(AsyncWebServerRequest *request) { +void volume_action(AsyncWebServerRequest *request) +{ Serial.println("volume!"); int params = request->params(); - for (int i = 0; i < params; i++) { - AsyncWebParameter* p = request->getParam(i); + for (int i = 0; i < params; i++) + { + AsyncWebParameter *p = request->getParam(i); Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); - if (p->name()=="value") { + if (p->name() == "value") + { audio.setVolume(atoi(p->value().c_str())); } } @@ -423,37 +441,46 @@ void loop() { if (audio.isRunning()) { - if (asyncStop) { - asyncStop = false; - stop(); + if (asyncStop) + { + asyncStop = false; + stop(); } deactivateRFID(); activateSD(); audio.loop(); - if (currentNode!=NULL) { + if (currentNode != NULL) + { currentNode->setSecondsPlayed(audio.getAudioCurrentTime()); } - + deactivateSD(); activateRFID(); - } else if (asyncStart) { + } + else if (asyncStart) + { asyncStart = false; start(); - } - - if (asyncTogglePlayPause) { + } + + if (asyncTogglePlayPause) + { asyncTogglePlayPause = false; togglePlayPause(); - } else if (asyncNext) { + } + else if (asyncNext) + { asyncNext = false; next(); - } else if (asyncPrev) { + } + else if (asyncPrev) + { asyncPrev = false; Serial.println("Previous not yet implemented!"); } rfid_loop--; - + if (rfid_loop == 0) { rfid_loop = RFID_LOOP_INTERVAL; @@ -475,11 +502,13 @@ void loop2(void *parameter) if (buttonPressed(BTN_START_STOP)) { asyncTogglePlayPause = true; - } - if (buttonPressed(BTN_NEXT)) { + } + if (buttonPressed(BTN_NEXT)) + { asyncNext = true; } - if (buttonPressed(BTN_PREV)) { + if (buttonPressed(BTN_PREV)) + { asyncPrev = true; } } @@ -487,17 +516,21 @@ void loop2(void *parameter) boolean buttonPressed(const uint8_t pin) { - - if (digitalRead(pin) == LOW) + + if (digitalRead(pin) == LOW && buttontoignore != pin) { - + unsigned long now = millis(); if (now - lastStart > startDelay) { lastStart = now; Serial.println("button pressed."); + buttontoignore = pin; return true; } + } else if (digitalRead(pin) == HIGH && buttontoignore == pin) { + buttontoignore = 0; } + return false; }