working rfid, more player capabilities
This commit is contained in:
parent
0f47f29c74
commit
2a19ebdff8
|
|
@ -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)
|
DirectoryNode* DirectoryNode::advanceToNextMP3(const String* currentGlobal)
|
||||||
{
|
{
|
||||||
bool useFirst = false;
|
bool useFirst = false;
|
||||||
|
|
|
||||||
|
|
@ -26,6 +26,7 @@ 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* currentGlobal);
|
||||||
DirectoryNode* advanceToNextMP3(const String* currentGlobal);
|
DirectoryNode* advanceToNextMP3(const String* currentGlobal);
|
||||||
void advanceToFirstMP3InThisNode();
|
void advanceToFirstMP3InThisNode();
|
||||||
String getDirectoryStructureHTML() const;
|
String getDirectoryStructureHTML() const;
|
||||||
|
|
|
||||||
|
|
@ -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('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('stop');" ontouchend="toggleCheckbox('stop');">Stop</button><br/><br/>
|
||||||
<button class="button" onmouseup="toggleCheckbox('next');" ontouchend="toggleCheckbox('next');">Next</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>
|
<p>
|
||||||
<h2>Content of SD Card:</h2>
|
<h2>Content of SD Card:</h2>
|
||||||
%DIRECTORY%
|
%DIRECTORY%
|
||||||
|
|
@ -46,6 +56,19 @@ const char index_html[] PROGMEM = R"rawliteral(
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
setInterval(getState, 500);
|
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) {
|
function toggleCheckbox(x) {
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open("GET", "/" + x, true);
|
xhr.open("GET", "/" + x, true);
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,8 @@ void start();
|
||||||
|
|
||||||
void loop2(void* parameter);
|
void loop2(void* parameter);
|
||||||
|
|
||||||
|
void named_song_action(AsyncWebServerRequest *request);
|
||||||
|
|
||||||
boolean buttonPressed(const uint8_t pin);
|
boolean buttonPressed(const uint8_t pin);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
94
src/main.cpp
94
src/main.cpp
|
|
@ -12,6 +12,7 @@
|
||||||
#include <SPI.h>
|
#include <SPI.h>
|
||||||
#include <SD.h>
|
#include <SD.h>
|
||||||
#include <MFRC522.h> //RFID Reader
|
#include <MFRC522.h> //RFID Reader
|
||||||
|
#include <map>
|
||||||
|
|
||||||
// define pins for RFID
|
// define pins for RFID
|
||||||
#define CS_RFID 32 // SIC, tried 4 and 32 but only this worked!
|
#define CS_RFID 32 // SIC, tried 4 and 32 but only this worked!
|
||||||
|
|
@ -27,7 +28,7 @@
|
||||||
|
|
||||||
#define CS_SDCARD 22
|
#define CS_SDCARD 22
|
||||||
|
|
||||||
#define RFID_LOOP_INTERVAL 10
|
#define RFID_LOOP_INTERVAL 25
|
||||||
|
|
||||||
#include "globals.h"
|
#include "globals.h"
|
||||||
#include "WebContent.h"
|
#include "WebContent.h"
|
||||||
|
|
@ -48,14 +49,19 @@ DNSServer dns;
|
||||||
DirectoryNode rootNode("/");
|
DirectoryNode rootNode("/");
|
||||||
DirectoryNode *currentNode = NULL;
|
DirectoryNode *currentNode = NULL;
|
||||||
|
|
||||||
boolean blockState = false;
|
|
||||||
|
|
||||||
volatile bool newRfidInt = false;
|
volatile bool newRfidInt = false;
|
||||||
|
|
||||||
MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object.
|
MFRC522 rfid(CS_RFID, RST_RFID); // instatiate a MFRC522 reader object.
|
||||||
|
|
||||||
TaskHandle_t RfidTask;
|
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()
|
void activateSD()
|
||||||
{
|
{
|
||||||
digitalWrite(CS_SDCARD, LOW);
|
digitalWrite(CS_SDCARD, LOW);
|
||||||
|
|
@ -76,9 +82,29 @@ void deactivateRFID()
|
||||||
digitalWrite(CS_RFID, HIGH);
|
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()
|
void playNextMp3()
|
||||||
{
|
{
|
||||||
blockState = true;
|
|
||||||
stop();
|
stop();
|
||||||
if (currentNode == NULL)
|
if (currentNode == NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -100,12 +126,11 @@ void playNextMp3()
|
||||||
audio.connecttoSD(mp3File.c_str());
|
audio.connecttoSD(mp3File.c_str());
|
||||||
activateRFID();
|
activateRFID();
|
||||||
deactivateSD();
|
deactivateSD();
|
||||||
blockState = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void audio_info(const char *info)
|
void audio_info(const char *info)
|
||||||
{
|
{
|
||||||
// Serial.print("info "); Serial.println(info);
|
//Serial.print("info "); Serial.println(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
void mute()
|
void mute()
|
||||||
|
|
@ -127,11 +152,6 @@ String getState()
|
||||||
{
|
{
|
||||||
String state = String();
|
String state = String();
|
||||||
|
|
||||||
if (blockState)
|
|
||||||
{
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
blockState = true;
|
|
||||||
|
|
||||||
if (audio.isRunning())
|
if (audio.isRunning())
|
||||||
{
|
{
|
||||||
|
|
@ -152,7 +172,6 @@ String getState()
|
||||||
if (currentNode->getCurrentPlaying())
|
if (currentNode->getCurrentPlaying())
|
||||||
state += *currentNode->getCurrentPlaying();
|
state += *currentNode->getCurrentPlaying();
|
||||||
}
|
}
|
||||||
blockState = false;
|
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,6 +229,7 @@ void readRFID()
|
||||||
Serial.print("Tag UID: ");
|
Serial.print("Tag UID: ");
|
||||||
String uidString = getRFIDString(rfid.uid.uidByte);
|
String uidString = getRFIDString(rfid.uid.uidByte);
|
||||||
Serial.println(uidString);
|
Serial.println(uidString);
|
||||||
|
playSongByRFID(uidString);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
|
|
@ -294,6 +314,8 @@ void setup()
|
||||||
|
|
||||||
request->send(200, "text/plain", "next");
|
request->send(200, "text/plain", "next");
|
||||||
next(); });
|
next(); });
|
||||||
|
|
||||||
|
server.on("/playnamed", HTTP_POST, named_song_action);
|
||||||
|
|
||||||
server.begin();
|
server.begin();
|
||||||
|
|
||||||
|
|
@ -309,18 +331,40 @@ void setup()
|
||||||
Serial.println("initialization done.");
|
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()
|
void loop()
|
||||||
{
|
{
|
||||||
if (audio.isRunning())
|
if (audio.isRunning())
|
||||||
{
|
{
|
||||||
|
if (asyncStop) {
|
||||||
|
asyncStop = false;
|
||||||
|
stop();
|
||||||
|
}
|
||||||
deactivateRFID();
|
deactivateRFID();
|
||||||
activateSD();
|
activateSD();
|
||||||
audio.loop();
|
audio.loop();
|
||||||
deactivateSD();
|
deactivateSD();
|
||||||
activateRFID();
|
activateRFID();
|
||||||
|
} else if (asyncStart) {
|
||||||
|
asyncStart = false;
|
||||||
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
rfid_loop--;
|
rfid_loop--;
|
||||||
|
|
||||||
if (rfid_loop == 0)
|
if (rfid_loop == 0)
|
||||||
{
|
{
|
||||||
rfid_loop = RFID_LOOP_INTERVAL;
|
rfid_loop = RFID_LOOP_INTERVAL;
|
||||||
|
|
@ -332,25 +376,25 @@ void loop()
|
||||||
readRFID();
|
readRFID();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (buttonPressed(BTN_START_STOP))
|
|
||||||
{
|
|
||||||
if (audio.isRunning())
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
start();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
delay(5);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop2(void *parameter)
|
void loop2(void *parameter)
|
||||||
{
|
{
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
|
if (buttonPressed(BTN_START_STOP))
|
||||||
|
{
|
||||||
|
if (audio.isRunning())
|
||||||
|
{
|
||||||
|
asyncStop = true;
|
||||||
|
asyncStart = false;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
asyncStart = true;
|
||||||
|
asyncStop = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue