esmp3/src/playlist_manager.cpp

74 lines
2.0 KiB
C++
Raw Normal View History

#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] = 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 {
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);
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);
}
entry.close();
}
dir.close();
} 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;
2022-08-21 09:37:42 +00:00
}
bool PlaylistManager::has_playlist(String rfid_id) {
return map.count(rfid_id) == 1;
}