-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for, write, then later delete SPIFFS file "/booted" to detect a…
… double reset.
- Loading branch information
Showing
5 changed files
with
192 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
55 changes: 55 additions & 0 deletions
55
examples/DoubleResetDetector_example/DoubleResetDetector_example.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
####################################### |