Fixed advent mode and persistence stuff.

This commit is contained in:
Fabian Schlenz 2019-11-29 21:20:19 +01:00
parent 4eef69516e
commit 51bef05465
3 changed files with 48 additions and 35 deletions

View File

@ -142,7 +142,8 @@ void Controller::_check_rfid() {
if (time.tm_mon == 11) { // tm_mon is "months since january", so 11 means december. if (time.tm_mon == 11) { // tm_mon is "months since january", so 11 means december.
pl->advent_shuffle(time.tm_mday); pl->advent_shuffle(time.tm_mday);
} else { } else {
// TODO DEBUG("Album is in advent mode, but it isn't december (yet). Not playing.\n");
return;
} }
} else if (data.indexOf("[random]") != -1 && pl->is_fresh()) { } else if (data.indexOf("[random]") != -1 && pl->is_fresh()) {
pl->shuffle(); pl->shuffle();

View File

@ -317,12 +317,19 @@ void Playlist::shuffle(uint8_t random_offset) {
} }
void Playlist::advent_shuffle(uint8_t day) { void Playlist::advent_shuffle(uint8_t day) {
if (day > 24) day = 24; TRACE("advent_shuffle running...\n");
// Not enough songs till the current day? Play all songs in the default order.
if (day > _files.size()) {
return;
}
// We are in the "different playlist every day" mode. So we don't persist it in order to not miss changes.
persistence = PERSIST_NONE;
if (day > _files.size()) return;
_files.insert(_files.begin(), _files[day - 1]); _files.insert(_files.begin(), _files[day - 1]);
_files.erase(_files.begin() + day - 1, _files.end()); _files.erase(_files.begin() + day, _files.end());
} }
void Playlist::reset() { void Playlist::reset() {

View File

@ -109,7 +109,6 @@ Playlist* PlaylistManager::get_playlist_for_folder(String folder) {
p = new Playlist(folder); p = new Playlist(folder);
_playlists[folder] = p; _playlists[folder] = p;
if (p->persistence == PERSIST_PERMANENTLY) { if (p->persistence == PERSIST_PERMANENTLY) {
// TODO Load persistence from file
String search = folder; String search = folder;
search += "="; search += "=";
SPIMaster::select_sd(); SPIMaster::select_sd();
@ -197,38 +196,44 @@ String PlaylistManager::create_mapping_txt() {
} }
void PlaylistManager::persist(Playlist* p) { void PlaylistManager::persist(Playlist* p) {
if (p->persistence != PERSIST_PERMANENTLY) return; if (p->persistence == PERSIST_NONE) {
_playlists.erase(p->path());
return;
} else if (p->persistence == PERSIST_PERMANENTLY) {
String search = p->path(); String search = p->path();
search += '='; search += '=';
bool old_file_existed = false; bool old_file_existed = false;
SPIMaster::select_sd(); SPIMaster::select_sd();
if (SD.exists("_positions.txt")) { if (SD.exists("_positions.txt")) {
SD.rename("/_positions.txt", "/_positions.temp.txt"); SD.rename("/_positions.txt", "/_positions.temp.txt");
old_file_existed = true; old_file_existed = true;
} }
File dst = SD.open("/_positions.txt", "w"); File dst = SD.open("/_positions.txt", "w");
if (old_file_existed) { if (old_file_existed) {
File src = SD.open("/_positions.temp.txt", "r"); File src = SD.open("/_positions.temp.txt", "r");
while (true) { while (true) {
String line = src.readStringUntil('\n'); String line = src.readStringUntil('\n');
line.trim(); line.trim();
if (line.startsWith(search)) continue; if (line.startsWith(search)) continue;
dst.println(line); dst.println(line);
}
src.close();
SD.remove("/_positions.temp.txt");
} }
src.close(); dst.print(search);
SD.remove("/_positions.temp.txt"); dst.print(p->get_current_track_id());
} dst.print(',');
dst.println(p->get_position());
dst.close();
SPIMaster::select_sd(false);
dst.print(search); _playlists.erase(p->path());
dst.print(p->get_current_track_id()); }
dst.print(',');
dst.println(p->get_position());
dst.close();
SPIMaster::select_sd(false);
} }