aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKing Kévin <kingkevin@cuvoodoo.info>2021-06-24 21:49:57 +0200
committerKing Kévin <kingkevin@cuvoodoo.info>2021-06-24 21:49:57 +0200
commit9b8632c8937be429b18ecbe6a5d26a94853d6358 (patch)
treeb392317b67a8b4d97a3ef988f40338c616a68612
parentae875daba7dc33f63d81237aeeac074590353746 (diff)
app: ensure track is not repeatedklo-assistant
-rw-r--r--application.c60
1 files changed, 55 insertions, 5 deletions
diff --git a/application.c b/application.c
index d3090c4..46c2d8d 100644
--- a/application.c
+++ b/application.c
@@ -232,6 +232,56 @@ static void command_reset(void* argument);
*/
static void command_bootloader(void* argument);
+/** get random track from folder
+ * @param[in] folder MP3 SD card folder number
+ * @return a track within the folder (0 on invalid folder)
+ */
+static uint16_t track_random(uint8_t folder)
+{
+ uint8_t tracks; // number of available tracks in folder
+ switch (folder) {
+ case WELCOME_FOLDER:
+ tracks = WELCOME_TRACKS;
+ break;
+ case MUSIC_FOLDER:
+ tracks = MUSIC_TRACKS;
+ break;
+ case EXIT_FOLDER:
+ tracks = EXIT_TRACKS;
+ break;
+ case TALK_FOLDER:
+ tracks = TALK_TRACKS;
+ break;
+ case TECHNO_FOLDER:
+ tracks = TECHNO_TRACKS;
+ break;
+ case KANGURU_FOLDER:
+ tracks = KANGURU_TRACKS;
+ break;
+ default: // invalid folder
+ return 0;
+ }
+
+ static uint16_t played[10] = {0}; // the last tracks played
+ bool track_ok = false; // when we found a valid track number
+ uint16_t track_nr; // the track we will play
+ while (!track_ok) {
+ track_nr = (folder << 8) + (rand() % tracks) + 1; // generate random track number
+ track_ok = true;
+ for (uint8_t i = 0; i < LENGTH(played); i++) { // go though played list
+ if (track_nr == played[i]) { // we already played the track
+ track_ok = false; // the track we have is not OK
+ break;
+ }
+ }
+ }
+ for (uint8_t i = 0; i < LENGTH(played) - 1; i++) { // shift playlist
+ played[i + 1] = played[i];
+ }
+ played[0] = track_nr; // save the track we chose
+ return track_nr; // return the random track within folder
+}
+
/** send command to MP3 playes
* @param[in] cmd command to send
* @param[in] data argument for command (such as track number)
@@ -341,7 +391,7 @@ static bool mp3_response(void)
const uint16_t time_passed = (rtc_get_counter_val() - timer_door_closed) / RTC_TICKS_SECOND; // how many seconds have passed since door has been closed
switch (playing_state) {
case PLAYING_STATE_INTRO: // the welcome message finished
- mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (MUSIC_FOLDER << 8) + (rtc_get_counter_val() % MUSIC_TRACKS) + 1); // play random music track
+ mp3_command(MP3_CMD_PLAY_FOLDER_FILE, track_random(MUSIC_FOLDER)); // play random music track
playing_state = PLAYING_STATE_SONG; // remember we are playing a song (for the first time)
break;
case PLAYING_STATE_SONG: // the song finished
@@ -373,18 +423,18 @@ static bool mp3_response(void)
playing_state = PLAYING_STATE_TIMER_OUTRO; // remember we are playing the timer outro announcement
break;
case PLAYING_STATE_TIMER_OUTRO: // the timer outro announcement finished
- mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (TALK_FOLDER << 8) + (rtc_get_counter_val() % TALK_TRACKS) + 1); // play random talk track
+ mp3_command(MP3_CMD_PLAY_FOLDER_FILE, track_random(TALK_FOLDER)); // play random talk track
playing_state = PLAYING_STATE_TALK; // remember we are playing the talk track
break;
case PLAYING_STATE_TALK: // the talk track finished
- mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (MUSIC_FOLDER << 8) + (rtc_get_counter_val() % MUSIC_TRACKS) + 1); // play random music track
+ mp3_command(MP3_CMD_PLAY_FOLDER_FILE, track_random(MUSIC_FOLDER)); // play random music track
playing_state = PLAYING_STATE_SONG; // remember we are playing a song (again)
break;
case PLAYING_STATE_TECHNO: // techno song completed, play the next one
- mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (TECHNO_FOLDER << 8) + (rtc_get_counter_val() % TECHNO_TRACKS) + 1); // play random techno music track
+ mp3_command(MP3_CMD_PLAY_FOLDER_FILE, track_random(TECHNO_FOLDER)); // play random techno music track
break;
case PLAYING_STATE_KANGURU: // kanguru sketch completed, play the next one
- mp3_command(MP3_CMD_PLAY_FOLDER_FILE, (KANGURU_FOLDER << 8) + (rtc_get_counter_val() % KANGURU_TRACKS) + 1); // play random techno music track
+ mp3_command(MP3_CMD_PLAY_FOLDER_FILE, track_random(KANGURU_FOLDER)); // play random techno music track
break;
default:
playing_state = PLAYING_STATE_OFF; // we won't play anything else