Skip to content

Commit

Permalink
Check for, write, then later delete SPIFFS file "/booted" to detect a…
Browse files Browse the repository at this point in the history
… double reset.
  • Loading branch information
datacute committed Aug 21, 2016
1 parent eaebff3 commit 205d314
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
67 changes: 67 additions & 0 deletions DoubleResetDetector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
FILE: DoubleResetDetector.cpp
VERSION: 0.0.1
PURPOSE: Trigger configure mode by resetting Arduino twice.
LICENCE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
*/

#include "DoubleResetDetector.h"
#define DEBUG_DOUBLERESETCONFIG

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

void DoubleResetDetector::detectDoubleReset() {
fsMounted = SPIFFS.begin();
detectDoubleReset(fsMounted);
}

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;
}
}

bool DoubleResetDetector::doubleResetDetected() {
return doubleReset;
}

void DoubleResetDetector::loop() {
if (waitingForDoubleReboot && millis() > timeout) stop();
}

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;
}
// EOF
37 changes: 37 additions & 0 deletions DoubleResetDetector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
FILE: DoubleResetDetector.h
VERSION: 0.0.1
PURPOSE: Trigger configure mode by resetting Arduino twice.
LICENCE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
*/

#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"

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

private:
int timeout;
bool waitingForDoubleReboot;
bool doubleReset;
bool fsMounted;
};
#endif // DoubleResetDetector_H__
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
# DoubleResetDetector
Double Reset Detector
=====================

Library to detect a double reset, using ESP8266 SPIFSS

VERSION: 0.0.1

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.

LICENCE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#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);

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");
}
}

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

initFS();

if (drd.doubleResetDetected()) {
Serial.println("Double Reset Detected");
} else {
Serial.println("No Double Reset Detected");
}
}

void loop()
{
// Call the double reset detector loop method every so often,
// so that it can recognise when the timeout expires.
// You can also call drd.stop() when you wish to no longer
// consider the next reset as a double reset.
drd.loop();
}

23 changes: 23 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#######################################
# Syntax Coloring Map For Messenger
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################


#######################################
# Methods and Functions (KEYWORD2)
#######################################
detectDoubleReset KEYWORD2
doubleResetDetected KEYWORD2
loop KEYWORD2
stop KEYWORD2
#######################################
# Instances (KEYWORD2)
#######################################
DoubleResetDetector KEYWORD2
#######################################
# Constants (LITERAL1)
#######################################

0 comments on commit 205d314

Please sign in to comment.