Skip to content

Commit

Permalink
Replaced SPIFFS implementation with much simpler RTC User Memory
Browse files Browse the repository at this point in the history
As a bonus, a flaky power supply doesn't trigger double reset detection.
  • Loading branch information
datacute committed Oct 15, 2016
1 parent 205d314 commit 233d870
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 85 deletions.
81 changes: 36 additions & 45 deletions DoubleResetDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,53 @@
*/

#include "DoubleResetDetector.h"
#define DEBUG_DOUBLERESETCONFIG

DoubleResetDetector::DoubleResetDetector(int timeout) {
// Flag which will be stored in RTC memory.
// A uint32_t is used so that two different magic numbers can be used,
// without accidentally overwriting memory used for another purpose.
uint32_t doubleResetDetectorFlag;

DoubleResetDetector::DoubleResetDetector(int timeout, int address) {
this->timeout = timeout*1000;
fsMounted = false;
doubleReset = false;
waitingForDoubleReboot = false;
this->address = address;
doubleResetDetected = false;
waitingForDoubleReset = false;
}

void DoubleResetDetector::detectDoubleReset() {
fsMounted = SPIFFS.begin();
detectDoubleReset(fsMounted);
bool DoubleResetDetector::detectDoubleReset() {
doubleResetDetected = detectRecentlyResetFlag();
if (doubleResetDetected) {
clearRecentlyResetFlag();
} else {
setRecentlyResetFlag();
waitingForDoubleReset = true;
}
return doubleResetDetected;
}

void DoubleResetDetector::detectDoubleReset(bool mounted) {
fsMounted = mounted;
if (!fsMounted) return;
// This file's existance means we were recently reset
if (SPIFFS.exists("/booted")) {
Serial.println("/booted already exists. Double reset detected.");
if (!SPIFFS.remove("/booted")) {
Serial.println("failed to remove /booted");
} else {
Serial.println("/booted removed");
}
doubleReset = true;
} else {
File bootedFile = SPIFFS.open("/booted", "w");
if (!bootedFile) {
Serial.println("failed to open config file for writing");
} else {
bootedFile.println("booted");
bootedFile.close();
Serial.println("/booted created to help detect a double reset.");
waitingForDoubleReboot = true;
}
doubleReset = false;
}
void DoubleResetDetector::loop() {
if (waitingForDoubleReset && millis() > timeout) stop();
}

bool DoubleResetDetector::doubleResetDetected() {
return doubleReset;
void DoubleResetDetector::stop() {
clearRecentlyResetFlag();
waitingForDoubleReset = false;
}

void DoubleResetDetector::loop() {
if (waitingForDoubleReboot && millis() > timeout) stop();
bool DoubleResetDetector::detectRecentlyResetFlag() {
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR;
ESP.rtcUserMemoryRead(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
doubleResetDetected = doubleResetDetectorFlag == DOUBLERESETDETECTOR_FLAG_SET;
return doubleResetDetected;
}

void DoubleResetDetector::stop() {
if (fsMounted && SPIFFS.exists("/booted")) {
if (!SPIFFS.remove("/booted")) {
Serial.println("failed to remove /booted");
} else {
Serial.println("/booted removed");
}
}
waitingForDoubleReboot = false;
void DoubleResetDetector::setRecentlyResetFlag() {
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_SET;
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
}

void DoubleResetDetector::clearRecentlyResetFlag() {
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR;
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
}
// EOF
22 changes: 12 additions & 10 deletions DoubleResetDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@
#ifndef DoubleResetDetector_H__
#define DoubleResetDetector_H__

#include <FS.h>

#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif

#define DOUBLERESETDETECTOR_VERSION "0.0.1"
#define DOUBLERESETDETECTOR_VERSION "0.0.2"
#define DOUBLERESETDETECTOR_FLAG_SET 0xD0D01234
#define DOUBLERESETDETECTOR_FLAG_CLEAR 0xD0D04321

class DoubleResetDetector
{
public:
DoubleResetDetector(int timeout);
void detectDoubleReset();
void detectDoubleReset(bool mounted);
bool doubleResetDetected();
DoubleResetDetector(int timeout, int address);
bool detectDoubleReset();
bool doubleResetDetected;
void loop();
void stop();

private:
int timeout;
bool waitingForDoubleReboot;
bool doubleReset;
bool fsMounted;
int address;
bool waitingForDoubleReset;
bool detectRecentlyResetFlag();
void clearRecentlyResetFlag();
void setRecentlyResetFlag();
uint32_t doubleResetDetectorFlag;
};
#endif // DoubleResetDetector_H__
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Double Reset Detector
=====================

Library to detect a double reset, using ESP8266 SPIFSS
Library to detect a double reset, using ESP8266 RTC Memory

VERSION: 0.0.1
VERSION: 0.0.2

PURPOSE: Detects a double reset so that an alternative start-up mode can
be used. One example use is to allow re-configuration of a device's wifi.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,29 @@
#include <FS.h>
#include <DoubleResetDetector.h>

// Number of seconds after reset during which a
// subseqent reset will be considered a double reset.
#define DRD_TIMEOUT 10

DoubleResetDetector drd(DRD_TIMEOUT);
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0

bool fsMounted = false;

// The Double Reset Detector uses the SPIFFS.
// If you also wish to use SPIFFS in your code,
// then you can inform the Double Reset Detector
// that it has been mounted.
void initFS()
{
Serial.println("Mounting FS...");

fsMounted = SPIFFS.begin();
if (fsMounted) {
Serial.println("Mounted file system");
drd.detectDoubleReset(fsMounted);

// use SPIFFS in your own code here

} else {
Serial.println("Failed to mount FS");
}
}
DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS);

void setup()
{
pinMode(LED_BUILTIN, OUTPUT);

Serial.begin(9600);
Serial.println();
Serial.println("DoubleResetDetector Example Program");
Serial.println("-----------------------------------");

initFS();

if (drd.doubleResetDetected()) {
if (drd.detectDoubleReset()) {
Serial.println("Double Reset Detected");
digitalWrite(LED_BUILTIN, LOW);
} else {
Serial.println("No Double Reset Detected");
digitalWrite(LED_BUILTIN, HIGH);
}
}

Expand All @@ -52,4 +35,3 @@ void loop()
// consider the next reset as a double reset.
drd.loop();
}

2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#######################################
# Syntax Coloring Map For Messenger
# Syntax Coloring Map
#######################################

#######################################
Expand Down

0 comments on commit 233d870

Please sign in to comment.