Skip to content

Commit

Permalink
Address crashing on resets (lbussy#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
lbussy committed Apr 18, 2020
1 parent fa0b7e1 commit b2c27f6
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void setup() {
_delay(200); // Let pins settle, else detect is inconsistent

if (digitalRead(RESETWIFI) == LOW) {
Log.notice(F("%s low, presenting portal." CR), RESETWIFI);
Log.notice(F("%s low, presenting portal." CR), stringify(RESETWIFI));
doWiFi(true);
} else if (rst == true) {
Log.notice(F("DRD: Triggered, presenting portal." CR));
Expand Down
11 changes: 11 additions & 0 deletions src/pushhelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ void setDoBrewfTarget() {
doBrewfTarget = true; // Semaphore required for Ticker + radio event
}

void setDoReset() {
doReset = true; // Semaphore required for reset in callback
}

void tickerLoop() {
Target *target = Target::getInstance();
BFTarget *bfTarget = BFTarget::getInstance();
Expand Down Expand Up @@ -194,4 +198,11 @@ void tickerLoop() {
doBrewfTarget = false;
brewfTarget->push();
}

// Check for Reset Pending
// Necessary because we cannot delay in a callback
if (doReset) {
doReset = false;
resetController();
}
}
4 changes: 3 additions & 1 deletion src/pushhelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ void updateLoop();
void setDoURLTarget();
void setDoBFTarget();
void setDoBrewfTarget();
void setDoReset();
extern volatile bool doBubble;
extern struct Bubbles bubbles;

static bool __attribute__((unused)) doURLTarget = false; // Semaphore for Target timer
static bool __attribute__((unused)) doBFTarget = false; // Semaphore for BF timer
static bool __attribute__((unused)) doBrewfTarget = false; // Semaphore for BRF timer
static bool __attribute__((unused)) doBrewfTarget = false; // Semaphore for BRF timer
static bool __attribute__((unused)) doReset = false; // Semahore for reset

#endif // _PUSHHELPER_H
12 changes: 8 additions & 4 deletions src/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ SOFTWARE. */
#include "tools.h"

void _delay(unsigned long ulDelay) {
// Safe blocking delay() replacement with yield()
// Safe semi-blocking delay
#ifdef ESP32
vTaskDelay(ulDelay); // Builtin to ESP32
#elif defined ESP8266
unsigned long ulNow = millis();
unsigned long ulThen = ulNow + ulDelay;
while (ulThen > millis()) {
yield();
yield(); // ESP8266 needs to yield()
}
#endif
}

void reboot() {
void resetController() {
Log.notice(F("Reboot request - rebooting system." CR));
_delay(5000);
saveBpm();
_delay(1000);
ESP.restart();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ SOFTWARE. */
#define EEPROM_ADDRESS 0x00

void _delay(unsigned long);
void reboot();
void resetController();
void loadBpm();
void saveBpm();

Expand Down
3 changes: 1 addition & 2 deletions src/webserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ void setActionPageHandlers()
Log.verbose(F("Processing /reset/." CR));
// Redirect to Reset page
request->send(SPIFFS, "/reset.htm");
_delay(5000);
reboot();
setDoReset();
});

server.on("/otastart/", HTTP_GET, [](AsyncWebServerRequest *request) {
Expand Down
1 change: 1 addition & 0 deletions src/webserver.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ SOFTWARE. */
#include "version.h"
#include "config.h"
#include "thatVersion.h"
#include "pushhelper.h"
#include <ArduinoLog.h>
#include <ArduinoJson.h>
#include <AsyncJson.h>
Expand Down
2 changes: 1 addition & 1 deletion src/wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void doWiFi(bool ignore = false) { // Handle WiFi and optionally ignore current
_delay(3000);
digitalWrite(LED, HIGH);
Log.notice(F("Hit timeout for on-demand portal, exiting." CR));
reboot();
setDoReset();
}
} else { // Normal WiFi connection attempt
blinker.attach_ms(STABLINK, wifiBlinker);
Expand Down
1 change: 1 addition & 0 deletions src/wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ SOFTWARE. */
#include "config.h"
#include "jsonconfig.h"
#include "tools.h"
#include "pushhelper.h"
#include <ESP8266WiFi.h>
#include <Ticker.h>
#include <ArduinoLog.h>
Expand Down

0 comments on commit b2c27f6

Please sign in to comment.