Skip to content

Commit

Permalink
Wait when trying to play a webstream while connecting to wifi
Browse files Browse the repository at this point in the history
The previous code had some issues. First of all, with
DONT_ACCEPT_SAME_RFID_TWICE it would not work at all. But without a
change, not even that works correctly (due to gTriedToConnectToHost
never being set).

IMHO the code is simplified a lot. The whole control flow can be
simplified if we just wait until a connection try hast completed.
  • Loading branch information
Sascha committed Dec 28, 2023
1 parent b8c14cc commit e434fe8
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 73 deletions.
13 changes: 5 additions & 8 deletions src/AudioPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -693,9 +693,12 @@ void AudioPlayer_Task(void *parameter) {
audioReturnCode = false;

if (gPlayProperties.playMode == WEBSTREAM || (gPlayProperties.playMode == LOCAL_M3U && gPlayProperties.isWebstream)) { // Webstream
// wait for wlan to connect or fail to connect before continuing
while (Wlan_ConnectionTryInProgress()) {
vTaskDelay(portTICK_PERIOD_MS * 100u);
}
audioReturnCode = audio->connecttohost(*(gPlayProperties.playlist + gPlayProperties.currentTrackNumber));
gPlayProperties.playlistFinished = false;
gTriedToConnectToHost = true;
} else if (gPlayProperties.playMode != WEBSTREAM && !gPlayProperties.isWebstream) {
// Files from SD
if (!gFSystem.exists(*(gPlayProperties.playlist + gPlayProperties.currentTrackNumber))) { // Check first if file/folder exists
Expand Down Expand Up @@ -1081,13 +1084,7 @@ void AudioPlayer_TrackQueueDispatcher(const char *_itemToPlay, const uint32_t _l

case WEBSTREAM: { // This is always just one "track"
Log_Println(modeWebstream, LOGLEVEL_NOTICE);
if (Wlan_IsConnected()) {
xQueueSend(gTrackQueue, &(musicFiles), 0);
} else {
Log_Println(webstreamNotAvailable, LOGLEVEL_ERROR);
System_IndicateError();
gPlayProperties.playMode = NO_PLAYLIST;
}
xQueueSend(gTrackQueue, &(musicFiles), 0);
break;
}

Expand Down
1 change: 0 additions & 1 deletion src/LogMessages_DE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Modus: Alle Tracks eines Ordners sort
const char modeAllTrackRandomLoop[] = "Modus: Alle Tracks eines Ordners zufällig in Endlosschleife";
const char modeWebstream[] = "Modus: Webstream";
const char modeWebstreamM3u[] = "Modus: Webstream (lokale .m3u-Datei)";
const char webstreamNotAvailable[] = "Aktuell kein Webstream möglich, da keine WLAN-Verbindung vorhanden!";
const char modeInvalid[] = "Ungültiger Abspielmodus %d!";
const char modeRepeatNone[] = "Repeatmodus: Kein Repeat";
const char modeRepeatTrack[] = "Repeatmodus: Aktueller Titel";
Expand Down
1 change: 0 additions & 1 deletion src/LogMessages_EN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Mode: all tracks (in alph. order) of
const char modeAllTrackRandomLoop[] = "Mode: all tracks (in random order) of directory as infinite loop";
const char modeWebstream[] = "Mode: webstream";
const char modeWebstreamM3u[] = "Mode: Webstream (local .m3u-file)";
const char webstreamNotAvailable[] = "Unable to access webstream as no wifi-connection is available!";
const char modeDoesNotExist[] = "Playmode does not exist!";
const char modeRepeatNone[] = "Repeatmode: no repeat";
const char modeRepeatTrack[] = "Repeatmode: current track";
Expand Down
9 changes: 1 addition & 8 deletions src/Wlan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,13 +333,6 @@ void handleWifiStateConnectionSuccess() {
delete dnsServer;
dnsServer = nullptr;

#ifdef PLAY_LAST_RFID_AFTER_REBOOT
if (gPlayLastRfIdWhenWiFiConnected && gTriedToConnectToHost) {
gPlayLastRfIdWhenWiFiConnected = false;
recoverLastRfidPlayedFromNvs(true);
}
#endif

wifiState = WIFI_STATE_CONNECTED;
}

Expand Down Expand Up @@ -547,7 +540,7 @@ bool Wlan_DeleteNetwork(String ssid) {
}

bool Wlan_ConnectionTryInProgress(void) {
return wifiState == WIFI_STATE_SCAN_CONN;
return wifiState == WIFI_STATE_INIT || wifiState == WIFI_STATE_CONNECT_LAST || wifiState == WIFI_STATE_SCAN_CONN;
}

String Wlan_GetIpAddress(void) {
Expand Down
1 change: 0 additions & 1 deletion src/logmessages.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ extern const char modeAllTrackAlphSortedLoop[];
extern const char modeAllTrackRandomLoop[];
extern const char modeWebstream[];
extern const char modeWebstreamM3u[];
extern const char webstreamNotAvailable[];
extern const char modeInvalid[];
extern const char modeRepeatNone[];
extern const char modeRepeatTrack[];
Expand Down
84 changes: 36 additions & 48 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@

#include <Wire.h>

bool gPlayLastRfIdWhenWiFiConnected = false;
bool gTriedToConnectToHost = false;

static constexpr const char *logo = R"literal(
_____ ____ ____ _
| ____| / ___| | _ \ _ _ (_) _ __ ___
Expand All @@ -49,13 +46,6 @@ bool testSPIRAM(void) {
return true;
}

#ifdef PLAY_LAST_RFID_AFTER_REBOOT
bool recoverLastRfid = true;
bool recoverBootCount = true;
bool resetBootCount = false;
uint32_t bootCount = 0;
#endif

////////////

#if (HAL == 2)
Expand All @@ -75,49 +65,43 @@ TwoWire i2cBusTwo = TwoWire(1);
// At start of a boot, bootCount is incremented by one and after 30s decremented because
// uptime of 30s is considered as "successful boot".
void recoverBootCountFromNvs(void) {
if (recoverBootCount) {
recoverBootCount = false;
resetBootCount = true;
bootCount = gPrefsSettings.getUInt("bootCount", 999);

if (bootCount == 999) { // first init
bootCount = 1;
gPrefsSettings.putUInt("bootCount", bootCount);
} else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid!
bootCount = 1;
gPrefsSettings.putUInt("bootCount", bootCount);
gPrefsSettings.putString("lastRfid", "-1"); // reset last rfid
Log_Println(bootLoopDetected, LOGLEVEL_ERROR);
recoverLastRfid = false;
} else { // normal operation
gPrefsSettings.putUInt("bootCount", ++bootCount);
}
}
uint32_t bootCount = gPrefsSettings.getUInt("bootCount", 999);

if (resetBootCount && millis() >= 30000) { // reset bootcount
resetBootCount = false;
bootCount = 0;
if (bootCount == 999) { // first init
bootCount = 1;
gPrefsSettings.putUInt("bootCount", bootCount);
} else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid!
bootCount = 1;
gPrefsSettings.putUInt("bootCount", bootCount);
gPrefsSettings.remove("lastRfid"); // reset last rfid
Log_Println(bootLoopDetected, LOGLEVEL_ERROR);
} else { // normal operation
gPrefsSettings.putUInt("bootCount", ++bootCount);
}
}

void resetBootCount(void) {
static bool wasReset = false;

if (!wasReset && millis() >= 30000) { // reset bootcount
wasReset = true;
gPrefsSettings.putUInt("bootCount", 0);
Log_Println(noBootLoopDetected, LOGLEVEL_INFO);
}
}

// Get last RFID-tag applied from NVS
void recoverLastRfidPlayedFromNvs(bool force) {
if (recoverLastRfid || force) {
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired
recoverLastRfid = false;
return;
}
recoverLastRfid = false;
String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1");
if (!lastRfidPlayed.compareTo("-1")) {
Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO);
} else {
xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0);
gPlayLastRfIdWhenWiFiConnected = !force;
Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str());
}
void recoverLastRfidPlayedFromNvs() {
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired
return;
}

String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1");
if (!lastRfidPlayed.compareTo("-1")) {
Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO);
} else {
xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0);
Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str());
}
}
#endif
Expand Down Expand Up @@ -240,6 +224,11 @@ void setup() {
#ifdef CONTROLS_LOCKED_BY_DEFAULT
System_SetLockControls(true);
#endif

#ifdef PLAY_LAST_RFID_AFTER_REBOOT
recoverBootCountFromNvs();
recoverLastRfidPlayedFromNvs();
#endif
}

void loop() {
Expand Down Expand Up @@ -269,8 +258,7 @@ void loop() {
Rfid_PreferenceLookupHandler();

#ifdef PLAY_LAST_RFID_AFTER_REBOOT
recoverBootCountFromNvs();
recoverLastRfidPlayedFromNvs();
resetBootCount();
#endif

IrReceiver_Cyclic();
Expand Down
6 changes: 0 additions & 6 deletions src/main.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,2 @@
#pragma once

extern bool gPlayLastRfIdWhenWiFiConnected;
extern bool gTriedToConnectToHost;

#ifdef PLAY_LAST_RFID_AFTER_REBOOT
extern void recoverLastRfidPlayedFromNvs(bool force = false);
#endif

0 comments on commit e434fe8

Please sign in to comment.