From 13e62fea190d61f4ffb175e87876a338cf6f4b6c Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Mon, 22 Aug 2022 13:59:00 +0200 Subject: [PATCH] Shuffle the playlist before playing if "[random]" is in the rfid data. --- include/controller.h | 4 ++-- include/playlist.h | 1 + src/controller.cpp | 8 ++++++-- src/playlist.cpp | 4 ++++ 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/include/controller.h b/include/controller.h index b9892cc..a4d6fdb 100644 --- a/include/controller.h +++ b/include/controller.h @@ -19,13 +19,13 @@ class Controller { unsigned long button_pressed_since = 0; bool button_already_processed = false; String read_rfid_data(); - + public: void handle(); void next_track(); void prev_track(); void play(); - void play(String rfid_id); + void play(String rfid_id, bool shuffle=false); void stop(); void eof_mp3(); }; \ No newline at end of file diff --git a/include/playlist.h b/include/playlist.h index 4bd5731..1edbfce 100644 --- a/include/playlist.h +++ b/include/playlist.h @@ -22,4 +22,5 @@ class Playlist { void restart(); void set_current_time(uint32_t time); uint32_t get_current_time(); + void shuffle(); }; diff --git a/src/controller.cpp b/src/controller.cpp index 90e424a..7c874f9 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -67,7 +67,7 @@ void Controller::handle_rfid() { String data = read_rfid_data(); - play(s_uid); + play(s_uid, data.indexOf("[random]")>=0); } rfid->PICC_HaltA(); } @@ -123,10 +123,14 @@ String Controller::read_rfid_data() { return data; } -void Controller::play(String rfid_id) { +void Controller::play(String rfid_id, bool shuffle) { if (!rfid_id.equals(current_playlist.get_rfid_id())) { if (pm->has_playlist(rfid_id)) { current_playlist = pm->get_playlist(rfid_id); + if (shuffle) { + log_i("Shuffling the playlist."); + current_playlist.shuffle(); + } play(); } else { Serial.printf("There is no playlist for rfid_id %s\n", rfid_id.c_str()); diff --git a/src/playlist.cpp b/src/playlist.cpp index ae7daab..f6aa218 100644 --- a/src/playlist.cpp +++ b/src/playlist.cpp @@ -59,4 +59,8 @@ void Playlist::set_current_time(uint32_t pos) { uint32_t Playlist::get_current_time() { return current_time; +} + +void Playlist::shuffle() { + std::random_shuffle(files.begin(), files.end()); } \ No newline at end of file