app: ensure track is not repeated

This commit is contained in:
King Kévin 2021-06-24 21:49:57 +02:00
parent ae875daba7
commit 9b8632c893
1 changed files with 55 additions and 5 deletions

View File

@ -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