3030
3131#include < Wire.h>
3232
33- bool gPlayLastRfIdWhenWiFiConnected = false ;
34- bool gTriedToConnectToHost = false ;
35-
3633static 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".
7767void 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
245234void 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 ();
0 commit comments