diff --git a/DoubleResetDetector.cpp b/DoubleResetDetector.cpp new file mode 100644 index 0000000..0544e35 --- /dev/null +++ b/DoubleResetDetector.cpp @@ -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 diff --git a/DoubleResetDetector.h b/DoubleResetDetector.h new file mode 100644 index 0000000..ea04a41 --- /dev/null +++ b/DoubleResetDetector.h @@ -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 + +#if defined(ARDUINO) && (ARDUINO >= 100) +#include +#else +#include +#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__ diff --git a/README.md b/README.md index eba13d4..1923c76 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/examples/DoubleResetDetector_example/DoubleResetDetector_example.ino b/examples/DoubleResetDetector_example/DoubleResetDetector_example.ino new file mode 100644 index 0000000..4ffa919 --- /dev/null +++ b/examples/DoubleResetDetector_example/DoubleResetDetector_example.ino @@ -0,0 +1,55 @@ +#include +#include + +// 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(); +} + diff --git a/keywords.txt b/keywords.txt new file mode 100644 index 0000000..84f7e93 --- /dev/null +++ b/keywords.txt @@ -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) +#######################################