diff --git a/src/DirectoryNode.cpp b/src/DirectoryNode.cpp index e5b8111..d6de8f5 100644 --- a/src/DirectoryNode.cpp +++ b/src/DirectoryNode.cpp @@ -3,7 +3,7 @@ #include DirectoryNode::DirectoryNode(const String &nodeName) - : name(nodeName), currentPlaying(nullptr) + : name(nodeName), currentPlaying("") { id = DirectoryNode::idCounter; DirectoryNode::idCounter++; @@ -37,19 +37,19 @@ const std::vector &DirectoryNode::getMP3Files() const return mp3Files; } -void DirectoryNode::setCurrentPlaying(const String *mp3File) +void DirectoryNode::setCurrentPlaying(const String mp3File) { currentPlaying = mp3File; for (int i = 0; i < mp3Files.size(); i++) { - if (mp3Files[i] == *mp3File && ids.size() > i) + if (mp3Files[i] == mp3File && ids.size() > i) { currentPlayingId = ids[i]; } } } -const String *DirectoryNode::getCurrentPlaying() const +const String DirectoryNode::getCurrentPlaying() const { return currentPlaying; } @@ -219,7 +219,7 @@ void DirectoryNode::advanceToFirstMP3InThisNode() { if (mp3Files.size() > 0) { - setCurrentPlaying(&mp3Files[0]); + setCurrentPlaying(mp3Files[0]); } } @@ -231,7 +231,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id) if (id == ids[i]) { // Found the current MP3 file - currentPlaying = &mp3Files[i]; + currentPlaying = mp3Files[i]; currentPlayingId = id; return this; } @@ -249,7 +249,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id) // Recursively search in subdirectory DirectoryNode *result = subdir->advanceToMP3(id); - if (result != nullptr && result->getCurrentPlaying() != nullptr) + if (result != nullptr && !result->getCurrentPlaying().isEmpty()) { return result; } @@ -260,24 +260,24 @@ DirectoryNode *DirectoryNode::advanceToMP3(const uint16_t id) return nullptr; } -DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) +DirectoryNode *DirectoryNode::advanceToMP3(const String songName) { - if (songName == nullptr) + if (songName.isEmpty()) { - Serial.println("advanceToMP3: songName is null"); + Serial.println("advanceToMP3: songName is empty"); return nullptr; } // Check if the input is an absolute path (starts with '/') or just a filename - bool isAbsolutePath = songName->startsWith("/"); + bool isAbsolutePath = songName.startsWith("/"); // Normalize trailing slash for absolute folder path targets - String normalizedPath = *songName; + String normalizedPath = songName; if (isAbsolutePath && normalizedPath.length() > 1 && normalizedPath.endsWith("/")) { normalizedPath.remove(normalizedPath.length() - 1); } // Lowercased copies for case-insensitive comparisons (FAT can uppercase names) - String lowTarget = *songName; + String lowTarget = songName; lowTarget.toLowerCase(); String lowNormPath = normalizedPath; lowNormPath.toLowerCase(); @@ -287,9 +287,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) { if (isAbsolutePath) { - if (mp3Files[i].equalsIgnoreCase(*songName)) + if (mp3Files[i].equalsIgnoreCase(songName)) { - setCurrentPlaying(&mp3Files[i]); + setCurrentPlaying(mp3Files[i]); return this; } } @@ -299,7 +299,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) f.toLowerCase(); if (f.endsWith(lowTarget)) { - setCurrentPlaying(&mp3Files[i]); + setCurrentPlaying(mp3Files[i]); return this; } } @@ -321,7 +321,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) } } - if (!isAbsolutePath && subdir->getName().equalsIgnoreCase(*songName)) + if (!isAbsolutePath && subdir->getName().equalsIgnoreCase(songName)) { subdir->advanceToFirstMP3InThisNode(); return subdir; @@ -333,9 +333,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) if (isAbsolutePath) { - if (subdir->mp3Files[i].equalsIgnoreCase(*songName)) + if (subdir->mp3Files[i].equalsIgnoreCase(songName)) { - subdir->setCurrentPlaying(&subdir->mp3Files[i]); + subdir->setCurrentPlaying(subdir->mp3Files[i]); return subdir; } } @@ -345,7 +345,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) f.toLowerCase(); if (f.endsWith(lowTarget)) { - subdir->setCurrentPlaying(&subdir->mp3Files[i]); + subdir->setCurrentPlaying(subdir->mp3Files[i]); return subdir; } } @@ -359,7 +359,7 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) } // If we get here, no matching song was found - Serial.println("advanceToMP3: No song found for: " + *songName); + Serial.println("advanceToMP3: No song found for: " + songName); return nullptr; } @@ -374,9 +374,9 @@ DirectoryNode *DirectoryNode::advanceToMP3(const String *songName) DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds) { // Safety check for null pointer - if (currentPlaying == nullptr) + if (currentPlaying.isEmpty()) { - Serial.println("goToPreviousMP3: currentPlaying is null"); + Serial.println("goToPreviousMP3: currentPlaying is empty"); return nullptr; } @@ -391,7 +391,7 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds) int currentIndex = -1; for (size_t i = 0; i < mp3Files.size(); i++) { - if (*currentPlaying == mp3Files[i]) + if (currentPlaying == mp3Files[i]) { currentIndex = i; break; @@ -403,7 +403,7 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds) { Serial.print("goToPreviousMP3: Moving to previous song in same directory: "); Serial.println(mp3Files[currentIndex - 1]); - setCurrentPlaying(&mp3Files[currentIndex - 1]); + setCurrentPlaying(mp3Files[currentIndex - 1]); return this; } @@ -413,9 +413,9 @@ DirectoryNode *DirectoryNode::goToPreviousMP3(uint32_t thresholdSeconds) return nullptr; // Let the caller handle global previous logic } -DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGlobal) +DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String currentGlobal) { - if (currentGlobal == nullptr) + if (currentGlobal.isEmpty()) { Serial.println("findPreviousMP3Globally: currentGlobal is null"); return nullptr; @@ -431,7 +431,7 @@ DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGloba { DirectoryNode *node = allMP3s[i].first; int fileIndex = allMP3s[i].second; - if (node->mp3Files[fileIndex] == *currentGlobal) + if (node->mp3Files[fileIndex] == currentGlobal) { currentGlobalIndex = i; break; @@ -447,7 +447,7 @@ DirectoryNode *DirectoryNode::findPreviousMP3Globally(const String *currentGloba Serial.print("findPreviousMP3Globally: Moving to previous song globally: "); Serial.println(prevNode->mp3Files[prevFileIndex]); - prevNode->setCurrentPlaying(&prevNode->mp3Files[prevFileIndex]); + prevNode->setCurrentPlaying(prevNode->mp3Files[prevFileIndex]); return prevNode; } @@ -475,21 +475,21 @@ const size_t DirectoryNode::getNumOfFiles() return subdirectories.size(); } -DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal) +DirectoryNode *DirectoryNode::advanceToNextMP3(const String currentGlobal) { bool useFirst = false; - Serial.println(currentGlobal->c_str()); - if (currentGlobal != nullptr) + Serial.println(currentGlobal.c_str()); + if (!currentGlobal.isEmpty()) { for (size_t i = 0; i < mp3Files.size(); i++) { - if (*currentGlobal == mp3Files[i]) + if (currentGlobal == mp3Files[i]) { // Found the current playing MP3 file if (i < mp3Files.size() - 1) { // Advance to the next MP3 file in the same directory - setCurrentPlaying(&mp3Files[i + 1]); + setCurrentPlaying(mp3Files[i + 1]); return this; } useFirst = true; @@ -506,20 +506,20 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal) if (useFirst && subdir->mp3Files.size() > 0) { - subdir->setCurrentPlaying(&subdir->mp3Files[0]); + subdir->setCurrentPlaying(subdir->mp3Files[0]); return subdir; } // Have each subdirectory advance its song for (size_t i = 0; i < subdir->mp3Files.size(); i++) { - if (*currentGlobal == subdir->mp3Files[i]) + if (currentGlobal == subdir->mp3Files[i]) { // Found the current playing MP3 file if (i < subdir->mp3Files.size() - 1) { // Advance to the next MP3 file in the same directory - subdir->setCurrentPlaying(&subdir->mp3Files[i + 1]); + subdir->setCurrentPlaying(subdir->mp3Files[i + 1]); return subdir; } else @@ -533,7 +533,7 @@ DirectoryNode *DirectoryNode::advanceToNextMP3(const String *currentGlobal) } // If we get here, there were no MP3 files or subdirectories left to check - currentPlaying = nullptr; + currentPlaying = ""; Serial.println("no more nodes found"); return this; } @@ -583,19 +583,4 @@ void DirectoryNode::streamDirectoryHTML(Print &out) const { } -void DirectoryNode::appendIndentation(String &html, int level) const -{ - for (int i = 0; i < level; i++) - { - html.concat(" "); - } -} -String DirectoryNode::getCurrentPlayingFilePath() const -{ - if (currentPlaying != nullptr) - { - return *currentPlaying; - } - return ""; -} diff --git a/src/DirectoryNode.h b/src/DirectoryNode.h index 990b5b9..8f8e0b2 100644 --- a/src/DirectoryNode.h +++ b/src/DirectoryNode.h @@ -17,12 +17,11 @@ private: std::vector subdirectories; std::vector mp3Files; std::vector ids; - const String* currentPlaying; + String currentPlaying; uint16_t currentPlayingId = 0; uint16_t secondsPlayed = 0; - - + public: DirectoryNode(const String& nodeName); @@ -37,8 +36,8 @@ public: const size_t getNumOfFiles(); - void setCurrentPlaying(const String* mp3File); - const String* getCurrentPlaying() const; + void setCurrentPlaying(const String mp3File); + const String getCurrentPlaying() const; const uint16_t getCurrentPlayingId() const; void setSecondsPlayed(const uint32_t seconds); @@ -50,17 +49,15 @@ public: void addMP3File(const String& mp3File); void buildDirectoryTree(const char* currentPath); void printDirectoryTree(int level = 0) const; - DirectoryNode* advanceToMP3(const String* songName); - DirectoryNode* advanceToNextMP3(const String* currentGlobal); + DirectoryNode* advanceToMP3(const String songName); + DirectoryNode* advanceToNextMP3(const String currentGlobal); DirectoryNode* goToPreviousMP3(uint32_t thresholdSeconds = 3); - DirectoryNode* findPreviousMP3Globally(const String* currentGlobal); + DirectoryNode* findPreviousMP3Globally(const String currentGlobal); void buildFlatMP3List(std::vector>& allMP3s); DirectoryNode* advanceToMP3(const uint16_t id); void advanceToFirstMP3InThisNode(); void streamDirectoryHTML(Print &out) const; - void appendIndentation(String& html, int level) const; DirectoryNode* findFirstDirectoryWithMP3s(); - String getCurrentPlayingFilePath() const; }; diff --git a/src/main.cpp b/src/main.cpp index cd3dcd1..65cf01a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -277,7 +277,7 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0) } // Check if the current playing song is valid - if (currentNode->getCurrentPlaying() == nullptr) + if (currentNode->getCurrentPlaying().isEmpty()) { currentNode = nullptr; Serial.print(F("No song found for ID: ")); @@ -285,7 +285,7 @@ void playSongById(uint16_t id, uint32_t continueSeconds = 0) return; } - String mp3File = currentNode->getCurrentPlayingFilePath(); + String mp3File = currentNode->getCurrentPlaying(); if (mp3File.length() == 0) { currentNode = nullptr; @@ -323,7 +323,7 @@ void playSongByName(String song) return; } - currentNode = rootNode.advanceToMP3(&song); + currentNode = rootNode.advanceToMP3(song); if (currentNode == nullptr) { Serial.print(F("No node found for song: ")); @@ -332,7 +332,7 @@ void playSongByName(String song) } // Check if the current playing song is valid - if (currentNode->getCurrentPlaying() == nullptr) + if (currentNode->getCurrentPlaying().isEmpty()) { currentNode = nullptr; Serial.print(F("No song found for name: ")); @@ -340,7 +340,7 @@ void playSongByName(String song) return; } - String mp3File = currentNode->getCurrentPlayingFilePath(); + String mp3File = currentNode->getCurrentPlaying(); if (mp3File.length() == 0) { currentNode = nullptr; @@ -405,16 +405,16 @@ void playSongByRFID(String id) continuousMode = (entry.mode == 'c'); // Try to locate the target in the directory tree - currentNode = rootNode.advanceToMP3(&entry.target); - if (currentNode == nullptr || currentNode->getCurrentPlaying() == nullptr) + currentNode = rootNode.advanceToMP3(entry.target); + if (currentNode == nullptr) { Serial.print(F("No node/file found for mapping target: ")); Serial.println(entry.target); return; } - String mp3File = currentNode->getCurrentPlayingFilePath(); - if (mp3File.length() == 0) + String mp3File = currentNode->getCurrentPlaying(); + if (mp3File.isEmpty()) { Serial.print(F("Empty file path for mapping target: ")); Serial.println(entry.target); @@ -466,9 +466,9 @@ void playSongByRFID(String id) int fileIdx = folderFlatList[0].second; Serial.print("Shuffle start: "); Serial.println(startNode->getMP3Files()[fileIdx]); - startNode->setCurrentPlaying(&startNode->getMP3Files()[fileIdx]); + startNode->setCurrentPlaying(startNode->getMP3Files()[fileIdx]); currentNode = startNode; - mp3File = currentNode->getCurrentPlayingFilePath(); + mp3File = currentNode->getCurrentPlaying(); } else { @@ -477,7 +477,7 @@ void playSongByRFID(String id) { DirectoryNode *node = folderFlatList[i].first; int fileIdx = folderFlatList[i].second; - if (node->getCurrentPlayingFilePath() == mp3File) + if (node->getCurrentPlaying() == mp3File) { folderFlatIndex = (int)i; break; @@ -555,7 +555,7 @@ void playNextMp3() } Serial.print("Advancing to "); - String mp3File = currentNode->getCurrentPlayingFilePath(); + String mp3File = currentNode->getCurrentPlaying(); // FIXME crash here if last song. if (mp3File.isEmpty()) { @@ -693,15 +693,12 @@ String getState() jsonState["playing"] = audio.isRunning(); - if (currentNode != nullptr && currentNode->getCurrentPlaying() != nullptr) - jsonState["title"] = *currentNode->getCurrentPlaying(); + if (currentNode != nullptr && !currentNode->getCurrentPlaying().isEmpty()) + jsonState["title"] = currentNode->getCurrentPlaying(); else jsonState["title"] = "Stopped"; + jsonState["filepath"] = currentNode->getCurrentPlaying(); - if (currentNode != nullptr && currentNode->getCurrentPlaying() != nullptr) - jsonState["filepath"] = currentNode->getCurrentPlayingFilePath(); - else - jsonState["filepath"] = ""; jsonState["time"] = audio.getAudioCurrentTime(); jsonState["volume"] = audio.getVolume(); @@ -883,7 +880,7 @@ void start() { if (currentNode != NULL) { - currentNode->setCurrentPlaying(NULL); + currentNode->setCurrentPlaying(""); currentNode = NULL; } @@ -921,7 +918,7 @@ void previous() } // Validate current state - const String *currentSong = currentNode->getCurrentPlaying(); + const String currentSong = currentNode->getCurrentPlaying(); if (currentSong == NULL) { #ifdef DEBUG @@ -931,7 +928,7 @@ void previous() } Serial.print("previous(): Current song: "); - Serial.println(*currentSong); + Serial.println(currentSong); // Use audio library's current time instead of tracked seconds for more accuracy uint32_t currentAudioTime = audio.getAudioCurrentTime(); @@ -945,25 +942,25 @@ void previous() if (newNode != NULL) { // Check if we're restarting the same song or moving to a different song - const String *newSong = newNode->getCurrentPlaying(); + const String newSong = newNode->getCurrentPlaying(); - if (newSong != NULL && currentSong == newSong && currentAudioTime > 2) + if (currentSong == newSong && currentAudioTime > 2) { // Restart current song if it's been playing for more than 2 seconds Serial.println("previous(): Restarting current song"); audio.setAudioPlayPosition(0); currentNode->setSecondsPlayed(0); } - else if (newSong != NULL && currentSong != newSong) + else if (currentSong != newSong) { // Move to previous song in same directory Serial.print("previous(): Moving to previous song in directory: "); - Serial.println(*newSong); + Serial.println(newSong); currentNode = newNode; stop(); deactivateRFID(); activateSD(); - playFile(currentNode->getCurrentPlayingFilePath().c_str()); + playFile(currentNode->getCurrentPlaying().c_str()); } } else @@ -976,14 +973,14 @@ void previous() if (globalPrevNode != NULL) { - const String *globalPrevSong = globalPrevNode->getCurrentPlaying(); - if (globalPrevSong != NULL) + const String globalPrevSong = globalPrevNode->getCurrentPlaying(); + if (!globalPrevSong.isEmpty()) { Serial.print(F("previous(): Found previous song globally: ")); - Serial.println(*globalPrevSong); + Serial.println(globalPrevSong); currentNode = globalPrevNode; stop(); - playFile(currentNode->getCurrentPlayingFilePath().c_str()); + playFile(globalPrevSong.c_str()); } #ifdef DEBUG else @@ -1020,10 +1017,10 @@ void audio_eof_mp3(const char *info) // Try to find current index if not set if (folderFlatIndex < 0) { - String cur = currentNode ? currentNode->getCurrentPlayingFilePath() : String(); + String cur = currentNode ? currentNode->getCurrentPlaying() : String(); for (size_t i = 0; i < folderFlatList.size(); i++) { - if (folderFlatList[i].first->getCurrentPlayingFilePath() == cur) + if (folderFlatList[i].first->getCurrentPlaying() == cur) { folderFlatIndex = (int)i; break; @@ -1037,12 +1034,12 @@ void audio_eof_mp3(const char *info) folderFlatIndex++; DirectoryNode *nextNode = folderFlatList[folderFlatIndex].first; int fileIdx = folderFlatList[folderFlatIndex].second; - nextNode->setCurrentPlaying(&nextNode->getMP3Files()[fileIdx]); + nextNode->setCurrentPlaying(nextNode->getMP3Files()[fileIdx]); currentNode = nextNode; currentNode->setSecondsPlayed(0); deactivateRFID(); activateSD(); - playFile(currentNode->getCurrentPlayingFilePath().c_str()); + playFile(currentNode->getCurrentPlaying().c_str()); } else { @@ -1053,12 +1050,12 @@ void audio_eof_mp3(const char *info) folderFlatIndex = 0; DirectoryNode *nextNode = folderFlatList[folderFlatIndex].first; int fileIdx = folderFlatList[folderFlatIndex].second; - nextNode->setCurrentPlaying(&nextNode->getMP3Files()[fileIdx]); + nextNode->setCurrentPlaying(nextNode->getMP3Files()[fileIdx]); currentNode = nextNode; currentNode->setSecondsPlayed(0); deactivateRFID(); activateSD(); - playFile(currentNode->getCurrentPlayingFilePath().c_str()); + playFile(currentNode->getCurrentPlaying().c_str()); } else {