diff --git a/src/controller.cpp b/src/controller.cpp index e5b5829..49cc747 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -142,7 +142,8 @@ void Controller::_check_rfid() { if (time.tm_mon == 11) { // tm_mon is "months since january", so 11 means december. pl->advent_shuffle(time.tm_mday); } 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()) { pl->shuffle(); diff --git a/src/playlist.cpp b/src/playlist.cpp index a07a1b6..4613228 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -317,12 +317,19 @@ void Playlist::shuffle(uint8_t random_offset) { } 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.erase(_files.begin() + day - 1, _files.end()); + _files.erase(_files.begin() + day, _files.end()); } void Playlist::reset() { diff --git a/src/playlist_manager.cpp b/src/playlist_manager.cpp index 5f3576c..0b1f7d4 100644 --- a/src/playlist_manager.cpp +++ b/src/playlist_manager.cpp @@ -109,7 +109,6 @@ Playlist* PlaylistManager::get_playlist_for_folder(String folder) { p = new Playlist(folder); _playlists[folder] = p; if (p->persistence == PERSIST_PERMANENTLY) { - // TODO Load persistence from file String search = folder; search += "="; SPIMaster::select_sd(); @@ -197,38 +196,44 @@ String PlaylistManager::create_mapping_txt() { } 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(); - search += '='; - - bool old_file_existed = false; - - SPIMaster::select_sd(); - if (SD.exists("_positions.txt")) { - SD.rename("/_positions.txt", "/_positions.temp.txt"); - old_file_existed = true; - } - File dst = SD.open("/_positions.txt", "w"); - - if (old_file_existed) { - File src = SD.open("/_positions.temp.txt", "r"); - - while (true) { - String line = src.readStringUntil('\n'); - line.trim(); - if (line.startsWith(search)) continue; - dst.println(line); + String search = p->path(); + search += '='; + + bool old_file_existed = false; + + SPIMaster::select_sd(); + if (SD.exists("_positions.txt")) { + SD.rename("/_positions.txt", "/_positions.temp.txt"); + old_file_existed = true; + } + File dst = SD.open("/_positions.txt", "w"); + + if (old_file_existed) { + File src = SD.open("/_positions.temp.txt", "r"); + + while (true) { + String line = src.readStringUntil('\n'); + line.trim(); + if (line.startsWith(search)) continue; + dst.println(line); + } + + src.close(); + SD.remove("/_positions.temp.txt"); } - src.close(); - SD.remove("/_positions.temp.txt"); + dst.print(search); + dst.print(p->get_current_track_id()); + dst.print(','); + dst.println(p->get_position()); + dst.close(); + SPIMaster::select_sd(false); + + _playlists.erase(p->path()); } - - dst.print(search); - dst.print(p->get_current_track_id()); - dst.print(','); - dst.println(p->get_position()); - dst.close(); - SPIMaster::select_sd(false); }