85 lines
2.4 KiB
C++
85 lines
2.4 KiB
C++
#include "playlist_manager.h"
|
|
#include "spi_master.h"
|
|
#include <SD.h>
|
|
|
|
PlaylistManager::PlaylistManager() {
|
|
SPIMaster::enable_sd();
|
|
current_rfid_tag_id = String("");
|
|
|
|
if (!SD.exists("/_mapping.txt")) {
|
|
Serial.println("WARNING: /_mapping.txt not found!");
|
|
} else {
|
|
map.clear();
|
|
File f = SD.open("/_mapping.txt");
|
|
Serial.println(" Reading /_mapping.txt...");
|
|
while (f.available()) {
|
|
char buffer[512];
|
|
size_t pos = f.readBytesUntil('\n', buffer, 511);
|
|
buffer[pos] = '\0';
|
|
|
|
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);
|
|
Serial.printf(" Adding mapping: %s=>%s\n", rfid_id.c_str(), folder.c_str());
|
|
map[rfid_id] = PersistedPlaylist(folder);
|
|
}
|
|
}
|
|
f.close();
|
|
}
|
|
}
|
|
|
|
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 {
|
|
PersistedPlaylist* ap = &(map[rfid_id]);
|
|
log_d("PP status is: File %d, bytes %d.", ap->file, ap->position);
|
|
current_playlist = Playlist(rfid_id, ap);
|
|
String path = ap->dir;
|
|
if (path.startsWith("/")) {
|
|
File dir = SD.open(path);
|
|
while(File entry = dir.openNextFile()) {
|
|
String filename = entry.name();
|
|
String ext = filename.substring(filename.length()-4);
|
|
if (!entry.isDirectory() &&
|
|
!filename.startsWith(".") &&
|
|
ext.equals(".mp3")) {
|
|
Serial.printf("Adding %s to the list of files\n", (path + "/" + filename).c_str());
|
|
current_playlist.add_file(path + "/" + filename);
|
|
}
|
|
entry.close();
|
|
}
|
|
dir.close();
|
|
current_playlist.set_current_position(ap->file, ap->position);
|
|
} else if (path.startsWith("http")) {
|
|
Serial.printf("Adding URL %s to the list of files\n", path.c_str());
|
|
current_playlist.add_file(path);
|
|
}
|
|
current_playlist.sort();
|
|
current_rfid_tag_id = rfid_id;
|
|
return current_playlist;
|
|
}
|
|
}
|
|
}
|
|
|
|
void PlaylistManager::set_audio_current_time(uint32_t time) {
|
|
audio_current_time = time;
|
|
}
|
|
|
|
bool PlaylistManager::has_playlist(String rfid_id) {
|
|
return map.count(rfid_id) == 1;
|
|
}
|
|
|
|
String PlaylistManager::pp_to_String() {
|
|
String s = "";
|
|
for(const auto& kv : map) {
|
|
s += kv.first + "=" + kv.second.file + "," + kv.second.position + '\n';
|
|
}
|
|
return s;
|
|
} |