2019-11-14 19:42:02 +00:00
|
|
|
#include "playlist_manager.h"
|
|
|
|
#include "spi_master.h"
|
2022-08-19 08:37:03 +00:00
|
|
|
#include <SD.h>
|
2019-11-14 19:42:02 +00:00
|
|
|
|
|
|
|
PlaylistManager::PlaylistManager() {
|
2022-08-19 08:37:03 +00:00
|
|
|
SPIMaster::enable_sd();
|
|
|
|
current_rfid_tag_id = String("");
|
2019-11-17 13:26:23 +00:00
|
|
|
|
2022-08-19 08:37:03 +00:00
|
|
|
if (!SD.exists("/_mapping.txt")) {
|
|
|
|
Serial.println("WARNING: /_mapping.txt not found!");
|
2019-11-17 13:26:23 +00:00
|
|
|
} else {
|
2022-08-19 08:37:03 +00:00
|
|
|
map.clear();
|
2019-11-17 13:26:23 +00:00
|
|
|
File f = SD.open("/_mapping.txt");
|
2022-08-19 08:37:03 +00:00
|
|
|
Serial.println(" Reading /_mapping.txt...");
|
2019-11-17 13:26:23 +00:00
|
|
|
while (f.available()) {
|
|
|
|
char buffer[512];
|
|
|
|
size_t pos = f.readBytesUntil('\n', buffer, 511);
|
|
|
|
buffer[pos] = '\0';
|
2019-11-16 22:03:13 +00:00
|
|
|
|
2019-11-17 13:26:23 +00:00
|
|
|
String data = buffer;
|
|
|
|
uint8_t eq = data.indexOf('=');
|
|
|
|
if (eq>0 && eq<data.length()-1) {
|
|
|
|
String rfid_id = data.substring(0, eq);
|
|
|
|
String folder = data.substring(eq + 1);
|
2022-08-19 08:37:03 +00:00
|
|
|
Serial.printf(" Adding mapping: %s=>%s\n", rfid_id.c_str(), folder.c_str());
|
|
|
|
map[rfid_id] = folder;
|
2019-11-16 22:03:13 +00:00
|
|
|
}
|
2019-11-17 13:26:23 +00:00
|
|
|
}
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 08:37:03 +00:00
|
|
|
Playlist PlaylistManager::get_playlist(String rfid_id) {
|
|
|
|
if (rfid_id.equals(current_rfid_tag_id)) {
|
|
|
|
return current_playlist;
|
|
|
|
} else {
|
|
|
|
if (map.count(rfid_id)==0) {
|
|
|
|
Serial.printf("No known playlist for id %s.\n", rfid_id);
|
|
|
|
return current_playlist;
|
|
|
|
} else {
|
2022-08-21 09:37:42 +00:00
|
|
|
current_playlist = Playlist(rfid_id);
|
|
|
|
String path = map[rfid_id];
|
|
|
|
if (path.startsWith("/")) {
|
|
|
|
File dir = SD.open(path);
|
2022-08-19 08:37:03 +00:00
|
|
|
while(File entry = dir.openNextFile()) {
|
|
|
|
String filename = entry.name();
|
|
|
|
String ext = filename.substring(filename.length()-4);
|
|
|
|
if (!entry.isDirectory() &&
|
|
|
|
!filename.startsWith(".") &&
|
|
|
|
ext.equals(".mp3")) {
|
2022-08-21 09:37:42 +00:00
|
|
|
Serial.printf("Adding %s to the list of files\n", (path + "/" + filename).c_str());
|
|
|
|
current_playlist.add_file(path + "/" + filename);
|
2019-11-29 16:41:16 +00:00
|
|
|
}
|
2022-08-19 08:37:03 +00:00
|
|
|
entry.close();
|
2019-11-29 16:41:16 +00:00
|
|
|
}
|
2022-08-19 08:37:03 +00:00
|
|
|
dir.close();
|
2019-11-29 16:41:16 +00:00
|
|
|
}
|
2022-08-19 08:37:03 +00:00
|
|
|
current_playlist.sort();
|
|
|
|
current_rfid_tag_id = rfid_id;
|
|
|
|
return current_playlist;
|
2019-11-29 16:41:16 +00:00
|
|
|
}
|
2019-11-14 19:42:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-19 08:37:03 +00:00
|
|
|
void PlaylistManager::set_audio_current_time(uint32_t time) {
|
|
|
|
audio_current_time = time;
|
2022-08-21 09:37:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PlaylistManager::has_playlist(String rfid_id) {
|
|
|
|
return map.count(rfid_id) == 1;
|
2022-08-19 08:37:03 +00:00
|
|
|
}
|