Skip to content

Commit 734a2c1

Browse files
author
Sascha
committed
Wait when trying to play a webstream while connecting to wifi
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.
1 parent b8c14cc commit 734a2c1

File tree

7 files changed

+42
-74
lines changed

7 files changed

+42
-74
lines changed

src/AudioPlayer.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,12 @@ void AudioPlayer_Task(void *parameter) {
693693
audioReturnCode = false;
694694

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

10821085
case WEBSTREAM: { // This is always just one "track"
10831086
Log_Println(modeWebstream, LOGLEVEL_NOTICE);
1084-
if (Wlan_IsConnected()) {
1085-
xQueueSend(gTrackQueue, &(musicFiles), 0);
1086-
} else {
1087-
Log_Println(webstreamNotAvailable, LOGLEVEL_ERROR);
1088-
System_IndicateError();
1089-
gPlayProperties.playMode = NO_PLAYLIST;
1090-
}
1087+
xQueueSend(gTrackQueue, &(musicFiles), 0);
10911088
break;
10921089
}
10931090

src/LogMessages_DE.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Modus: Alle Tracks eines Ordners sort
7575
const char modeAllTrackRandomLoop[] = "Modus: Alle Tracks eines Ordners zufällig in Endlosschleife";
7676
const char modeWebstream[] = "Modus: Webstream";
7777
const char modeWebstreamM3u[] = "Modus: Webstream (lokale .m3u-Datei)";
78-
const char webstreamNotAvailable[] = "Aktuell kein Webstream möglich, da keine WLAN-Verbindung vorhanden!";
7978
const char modeInvalid[] = "Ungültiger Abspielmodus %d!";
8079
const char modeRepeatNone[] = "Repeatmodus: Kein Repeat";
8180
const char modeRepeatTrack[] = "Repeatmodus: Aktueller Titel";

src/LogMessages_EN.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ const char modeAllTrackAlphSortedLoop[] = "Mode: all tracks (in alph. order) of
7575
const char modeAllTrackRandomLoop[] = "Mode: all tracks (in random order) of directory as infinite loop";
7676
const char modeWebstream[] = "Mode: webstream";
7777
const char modeWebstreamM3u[] = "Mode: Webstream (local .m3u-file)";
78-
const char webstreamNotAvailable[] = "Unable to access webstream as no wifi-connection is available!";
7978
const char modeDoesNotExist[] = "Playmode does not exist!";
8079
const char modeRepeatNone[] = "Repeatmode: no repeat";
8180
const char modeRepeatTrack[] = "Repeatmode: current track";

src/Wlan.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,13 +333,6 @@ void handleWifiStateConnectionSuccess() {
333333
delete dnsServer;
334334
dnsServer = nullptr;
335335

336-
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
337-
if (gPlayLastRfIdWhenWiFiConnected && gTriedToConnectToHost) {
338-
gPlayLastRfIdWhenWiFiConnected = false;
339-
recoverLastRfidPlayedFromNvs(true);
340-
}
341-
#endif
342-
343336
wifiState = WIFI_STATE_CONNECTED;
344337
}
345338

@@ -547,7 +540,7 @@ bool Wlan_DeleteNetwork(String ssid) {
547540
}
548541

549542
bool Wlan_ConnectionTryInProgress(void) {
550-
return wifiState == WIFI_STATE_SCAN_CONN;
543+
return wifiState == WIFI_STATE_INIT || wifiState == WIFI_STATE_CONNECT_LAST || wifiState == WIFI_STATE_SCAN_CONN;
551544
}
552545

553546
String Wlan_GetIpAddress(void) {

src/logmessages.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ extern const char modeAllTrackAlphSortedLoop[];
7171
extern const char modeAllTrackRandomLoop[];
7272
extern const char modeWebstream[];
7373
extern const char modeWebstreamM3u[];
74-
extern const char webstreamNotAvailable[];
7574
extern const char modeInvalid[];
7675
extern const char modeRepeatNone[];
7776
extern const char modeRepeatTrack[];

src/main.cpp

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030

3131
#include <Wire.h>
3232

33-
bool gPlayLastRfIdWhenWiFiConnected = false;
34-
bool gTriedToConnectToHost = false;
35-
3633
static constexpr const char *logo = R"literal(
3734
_____ ____ ____ _
3835
| ____| / ___| | _ \ _ _ (_) _ __ ___
@@ -49,13 +46,6 @@ bool testSPIRAM(void) {
4946
return true;
5047
}
5148

52-
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
53-
bool recoverLastRfid = true;
54-
bool recoverBootCount = true;
55-
bool resetBootCount = false;
56-
uint32_t bootCount = 0;
57-
#endif
58-
5949
////////////
6050

6151
#if (HAL == 2)
@@ -75,49 +65,43 @@ TwoWire i2cBusTwo = TwoWire(1);
7565
// At start of a boot, bootCount is incremented by one and after 30s decremented because
7666
// uptime of 30s is considered as "successful boot".
7767
void recoverBootCountFromNvs(void) {
78-
if (recoverBootCount) {
79-
recoverBootCount = false;
80-
resetBootCount = true;
81-
bootCount = gPrefsSettings.getUInt("bootCount", 999);
82-
83-
if (bootCount == 999) { // first init
84-
bootCount = 1;
85-
gPrefsSettings.putUInt("bootCount", bootCount);
86-
} else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid!
87-
bootCount = 1;
88-
gPrefsSettings.putUInt("bootCount", bootCount);
89-
gPrefsSettings.putString("lastRfid", "-1"); // reset last rfid
90-
Log_Println(bootLoopDetected, LOGLEVEL_ERROR);
91-
recoverLastRfid = false;
92-
} else { // normal operation
93-
gPrefsSettings.putUInt("bootCount", ++bootCount);
94-
}
95-
}
68+
uint32_t bootCount = gPrefsSettings.getUInt("bootCount", 999);
9669

97-
if (resetBootCount && millis() >= 30000) { // reset bootcount
98-
resetBootCount = false;
99-
bootCount = 0;
70+
if (bootCount == 999) { // first init
71+
bootCount = 1;
10072
gPrefsSettings.putUInt("bootCount", bootCount);
73+
} else if (bootCount >= 3) { // considered being a bootloop => don't recover last rfid!
74+
bootCount = 1;
75+
gPrefsSettings.putUInt("bootCount", bootCount);
76+
gPrefsSettings.remove("lastRfid"); // reset last rfid
77+
Log_Println(bootLoopDetected, LOGLEVEL_ERROR);
78+
} else { // normal operation
79+
gPrefsSettings.putUInt("bootCount", ++bootCount);
80+
}
81+
}
82+
83+
void resetBootCount(void) {
84+
static bool wasReset = false;
85+
86+
if (!wasReset && millis() >= 30000) { // reset bootcount
87+
wasReset = true;
88+
gPrefsSettings.putUInt("bootCount", 0);
10189
Log_Println(noBootLoopDetected, LOGLEVEL_INFO);
10290
}
10391
}
10492

10593
// Get last RFID-tag applied from NVS
106-
void recoverLastRfidPlayedFromNvs(bool force) {
107-
if (recoverLastRfid || force) {
108-
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired
109-
recoverLastRfid = false;
110-
return;
111-
}
112-
recoverLastRfid = false;
113-
String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1");
114-
if (!lastRfidPlayed.compareTo("-1")) {
115-
Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO);
116-
} else {
117-
xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0);
118-
gPlayLastRfIdWhenWiFiConnected = !force;
119-
Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str());
120-
}
94+
void recoverLastRfidPlayedFromNvs() {
95+
if (System_GetOperationMode() == OPMODE_BLUETOOTH_SINK) { // Don't recover if BT-mode is desired
96+
return;
97+
}
98+
99+
String lastRfidPlayed = gPrefsSettings.getString("lastRfid", "-1");
100+
if (!lastRfidPlayed.compareTo("-1")) {
101+
Log_Println(unableToRestoreLastRfidFromNVS, LOGLEVEL_INFO);
102+
} else {
103+
xQueueSend(gRfidCardQueue, lastRfidPlayed.c_str(), 0);
104+
Log_Printf(LOGLEVEL_INFO, restoredLastRfidFromNVS, lastRfidPlayed.c_str());
121105
}
122106
}
123107
#endif
@@ -240,6 +224,11 @@ void setup() {
240224
#ifdef CONTROLS_LOCKED_BY_DEFAULT
241225
System_SetLockControls(true);
242226
#endif
227+
228+
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
229+
recoverBootCountFromNvs();
230+
recoverLastRfidPlayedFromNvs();
231+
#endif
243232
}
244233

245234
void loop() {
@@ -269,8 +258,7 @@ void loop() {
269258
Rfid_PreferenceLookupHandler();
270259

271260
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
272-
recoverBootCountFromNvs();
273-
recoverLastRfidPlayedFromNvs();
261+
resetBootCount();
274262
#endif
275263

276264
IrReceiver_Cyclic();

src/main.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
11
#pragma once
2-
3-
extern bool gPlayLastRfIdWhenWiFiConnected;
4-
extern bool gTriedToConnectToHost;
5-
6-
#ifdef PLAY_LAST_RFID_AFTER_REBOOT
7-
extern void recoverLastRfidPlayedFromNvs(bool force = false);
8-
#endif

0 commit comments

Comments
 (0)