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