working rfid, more player capabilities

This commit is contained in:
Stefan Ostermann 2023-07-09 23:00:24 +02:00
parent 0f47f29c74
commit 2a19ebdff8
5 changed files with 125 additions and 25 deletions

View File

@ -122,6 +122,36 @@ void DirectoryNode::advanceToFirstMP3InThisNode() {
}
}
DirectoryNode* DirectoryNode::advanceToMP3(const String* currentGlobal) {
for (auto subdir : subdirectories)
{
if (subdir->getName()==*currentGlobal) {
subdir->advanceToFirstMP3InThisNode();
return subdir;
}
// Have each subdirectory advance its song
for (size_t i = 0; i < subdir->mp3Files.size(); i++)
{
if (*currentGlobal == subdir->mp3Files[i])
{
// Found the current MP3 file
if (i < subdir->mp3Files.size() - 1)
{
subdir->currentPlaying = &subdir->mp3Files[i];
return subdir;
}
}
}
}
// If we get here, there were no MP3 files or subdirectories left to check
currentPlaying = nullptr;
Serial.println("no more nodes found");
return this;
}
DirectoryNode* DirectoryNode::advanceToNextMP3(const String* currentGlobal)
{
bool useFirst = false;

View File

@ -26,6 +26,7 @@ public:
void addMP3File(const String& mp3File);
void buildDirectoryTree(const char* currentPath);
void printDirectoryTree(int level = 0) const;
DirectoryNode* advanceToMP3(const String* currentGlobal);
DirectoryNode* advanceToNextMP3(const String* currentGlobal);
void advanceToFirstMP3InThisNode();
String getDirectoryStructureHTML() const;

View File

@ -39,6 +39,16 @@ const char index_html[] PROGMEM = R"rawliteral(
<button class="button" onmouseup="toggleCheckbox('start');" ontouchend="toggleCheckbox('start');">Start</button><br/><br/>
<button class="button" onmouseup="toggleCheckbox('stop');" ontouchend="toggleCheckbox('stop');">Stop</button><br/><br/>
<button class="button" onmouseup="toggleCheckbox('next');" ontouchend="toggleCheckbox('next');">Next</button><br/><br/>
<form action='playnamed' method='post'>
<div>
<label for="title">Song title to play</label>
<input name="title" id="titleid" value="music" />
</div>
<div>
<button>Submit</button>
</div>
</form>
<p>
<h2>Content of SD Card:</h2>
%DIRECTORY%
@ -46,6 +56,19 @@ const char index_html[] PROGMEM = R"rawliteral(
<script>
setInterval(getState, 500);
// Get the <li> elements
var liElements = document.querySelectorAll('ul li');
// Add click event listener to each <li> element
liElements.forEach(function(li) {
li.addEventListener('click', function() {
var liText = this.innerText;
var inputField = document.getElementById('titleid');
inputField.value = liText;
});
});
function toggleCheckbox(x) {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/" + x, true);

View File

@ -7,6 +7,8 @@ void start();
void loop2(void* parameter);
void named_song_action(AsyncWebServerRequest *request);
boolean buttonPressed(const uint8_t pin);
/**

View File

@ -12,6 +12,7 @@
#include <SPI.h>
#include <SD.h>
#include <MFRC522.h> //RFID Reader
#include <map>
// define pins for RFID
#define CS_RFID 32 // SIC, tried 4 and 32 but only this worked!
@ -27,7 +28,7 @@
#define CS_SDCARD 22
#define RFID_LOOP_INTERVAL 10
#define RFID_LOOP_INTERVAL 25
#include "globals.h"
#include "WebContent.h"
@ -48,14 +49,19 @@ DNSServer dns;
DirectoryNode rootNode("/");
DirectoryNode *currentNode = NULL;
boolean blockState = false;
volatile bool newRfidInt = false;
MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object.
TaskHandle_t RfidTask;
bool asyncStop = false;
bool asyncStart = false;
std::map<String, String> rfid_map{{"67 152 204 14", "bob_dylan-Forrest_Gump_Soundtrack,_Disc_1.mp3"},
{"67 175 148 160","068-Der_Schatz_im_Bergsee"}};
void activateSD()
{
digitalWrite(CS_SDCARD, LOW);
@ -76,9 +82,29 @@ void deactivateRFID()
digitalWrite(CS_RFID, HIGH);
}
void playSongByName(String song) {
currentNode = rootNode.advanceToMP3(&song);
String mp3File = currentNode->getCurrentPlayingFilePath();
Serial.println(mp3File.c_str());
deactivateRFID();
activateSD();
audio.connecttoSD(mp3File.c_str());
activateRFID();
deactivateSD();
}
void playSongByRFID(String id) {
String song = rfid_map[id];
Serial.println("searching for ");
Serial.println(song);
playSongByName(song);
}
void playNextMp3()
{
blockState = true;
stop();
if (currentNode == NULL)
{
@ -100,12 +126,11 @@ void playNextMp3()
audio.connecttoSD(mp3File.c_str());
activateRFID();
deactivateSD();
blockState = false;
}
void audio_info(const char *info)
{
// Serial.print("info "); Serial.println(info);
//Serial.print("info "); Serial.println(info);
}
void mute()
@ -127,11 +152,6 @@ String getState()
{
String state = String();
if (blockState)
{
return state;
}
blockState = true;
if (audio.isRunning())
{
@ -152,7 +172,6 @@ String getState()
if (currentNode->getCurrentPlaying())
state += *currentNode->getCurrentPlaying();
}
blockState = false;
return state;
}
@ -210,6 +229,7 @@ void readRFID()
Serial.print("Tag UID: ");
String uidString = getRFIDString(rfid.uid.uidByte);
Serial.println(uidString);
playSongByRFID(uidString);
}
void setup()
@ -294,6 +314,8 @@ void setup()
request->send(200, "text/plain", "next");
next(); });
server.on("/playnamed", HTTP_POST, named_song_action);
server.begin();
@ -309,18 +331,40 @@ void setup()
Serial.println("initialization done.");
}
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);
Serial.printf("POST[%s]: %s\n", p->name().c_str(), p->value().c_str());
if (p->name()=="title") {
playSongByName(p->value());
}
}
request->send_P(200, "text/html", index_html, processor);
}
void loop()
{
if (audio.isRunning())
{
if (asyncStop) {
asyncStop = false;
stop();
}
deactivateRFID();
activateSD();
audio.loop();
deactivateSD();
activateRFID();
} else if (asyncStart) {
asyncStart = false;
start();
}
rfid_loop--;
if (rfid_loop == 0)
{
rfid_loop = RFID_LOOP_INTERVAL;
@ -332,25 +376,25 @@ void loop()
readRFID();
}
}
if (buttonPressed(BTN_START_STOP))
{
if (audio.isRunning())
{
stop();
}
else
{
start();
}
}
delay(5);
}
void loop2(void *parameter)
{
for (;;)
{
if (buttonPressed(BTN_START_STOP))
{
if (audio.isRunning())
{
asyncStop = true;
asyncStart = false;
}
else
{
asyncStart = true;
asyncStop = false;
}
}
}
}