├── LICENSE ├── README.md ├── examples └── minimal │ └── minimal.ino ├── keywords.txt ├── library.properties └── src ├── DoubleResetDetector.cpp └── DoubleResetDetector.h /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Stephen Denne 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Double Reset Detector 2 | ===================== 3 | 4 | Library to detect a double reset, using ESP8266 RTC Memory 5 | 6 | PURPOSE: Detects a double reset so that an alternative start-up mode can 7 | be used. One example use is to allow re-configuration of a device's wifi. 8 | 9 | LICENCE: MIT 10 | -------------------------------------------------------------------------------- /examples/minimal/minimal.ino: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | // Number of seconds after reset during which a 4 | // subseqent reset will be considered a double reset. 5 | #define DRD_TIMEOUT 10 6 | 7 | // RTC Memory Address for the DoubleResetDetector to use 8 | #define DRD_ADDRESS 0 9 | 10 | DoubleResetDetector drd(DRD_TIMEOUT, DRD_ADDRESS); 11 | 12 | void setup() 13 | { 14 | pinMode(LED_BUILTIN, OUTPUT); 15 | 16 | Serial.begin(9600); 17 | Serial.println(); 18 | Serial.println("DoubleResetDetector Example Program"); 19 | Serial.println("-----------------------------------"); 20 | 21 | if (drd.detectDoubleReset()) { 22 | Serial.println("Double Reset Detected"); 23 | digitalWrite(LED_BUILTIN, LOW); 24 | } else { 25 | Serial.println("No Double Reset Detected"); 26 | digitalWrite(LED_BUILTIN, HIGH); 27 | } 28 | } 29 | 30 | void loop() 31 | { 32 | // Call the double reset detector loop method every so often, 33 | // so that it can recognise when the timeout expires. 34 | // You can also call drd.stop() when you wish to no longer 35 | // consider the next reset as a double reset. 36 | drd.loop(); 37 | } 38 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | 10 | ####################################### 11 | # Methods and Functions (KEYWORD2) 12 | ####################################### 13 | detectDoubleReset KEYWORD2 14 | doubleResetDetected KEYWORD2 15 | loop KEYWORD2 16 | stop KEYWORD2 17 | ####################################### 18 | # Instances (KEYWORD2) 19 | ####################################### 20 | DoubleResetDetector KEYWORD2 21 | ####################################### 22 | # Constants (LITERAL1) 23 | ####################################### 24 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=DoubleResetDetector 2 | version=1.0.3 3 | author=Stephen Denne 4 | maintainer=Stephen Denne 5 | sentence=Library to detect a double reset, using ESP8266 RTC Memory. 6 | paragraph=An alternative start-up mode can be used. One example use is to allow re-configuration of a device's wifi. 7 | category=Device Control 8 | url=https://github.com/datacute/DoubleResetDetector 9 | architectures=esp8266 -------------------------------------------------------------------------------- /src/DoubleResetDetector.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | FILE: DoubleResetDetector.cpp 3 | VERSION: 1.0.0 4 | PURPOSE: Trigger configure mode by resetting ESP8266 twice. 5 | LICENCE: MIT 6 | */ 7 | 8 | #include "DoubleResetDetector.h" 9 | 10 | // Flag which will be stored in RTC memory. 11 | // A uint32_t is used so that two different magic numbers can be used, 12 | // without accidentally overwriting memory used for another purpose. 13 | uint32_t doubleResetDetectorFlag; 14 | 15 | DoubleResetDetector::DoubleResetDetector(int timeout, int address) { 16 | this->timeout = timeout*1000; 17 | this->address = address; 18 | doubleResetDetected = false; 19 | waitingForDoubleReset = false; 20 | } 21 | 22 | bool DoubleResetDetector::detectDoubleReset() { 23 | doubleResetDetected = detectRecentlyResetFlag(); 24 | if (doubleResetDetected) { 25 | clearRecentlyResetFlag(); 26 | } else { 27 | setRecentlyResetFlag(); 28 | waitingForDoubleReset = true; 29 | } 30 | return doubleResetDetected; 31 | } 32 | 33 | void DoubleResetDetector::loop() { 34 | if (waitingForDoubleReset && millis() > timeout) stop(); 35 | } 36 | 37 | void DoubleResetDetector::stop() { 38 | clearRecentlyResetFlag(); 39 | waitingForDoubleReset = false; 40 | } 41 | 42 | bool DoubleResetDetector::detectRecentlyResetFlag() { 43 | doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR; 44 | ESP.rtcUserMemoryRead(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag)); 45 | doubleResetDetected = doubleResetDetectorFlag == DOUBLERESETDETECTOR_FLAG_SET; 46 | return doubleResetDetected; 47 | } 48 | 49 | void DoubleResetDetector::setRecentlyResetFlag() { 50 | doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_SET; 51 | ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag)); 52 | } 53 | 54 | void DoubleResetDetector::clearRecentlyResetFlag() { 55 | doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR; 56 | ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag)); 57 | } 58 | // EOF 59 | -------------------------------------------------------------------------------- /src/DoubleResetDetector.h: -------------------------------------------------------------------------------- 1 | /* 2 | FILE: DoubleResetDetector.h 3 | VERSION: 1.0.0 4 | PURPOSE: Trigger configure mode by resetting ESP8266 twice. 5 | LICENCE: MIT 6 | */ 7 | 8 | #ifndef DoubleResetDetector_H__ 9 | #define DoubleResetDetector_H__ 10 | 11 | #if defined(ARDUINO) && (ARDUINO >= 100) 12 | #include 13 | #else 14 | #include 15 | #endif 16 | 17 | #define DOUBLERESETDETECTOR_VERSION "1.0.0" 18 | #define DOUBLERESETDETECTOR_FLAG_SET 0xD0D01234 19 | #define DOUBLERESETDETECTOR_FLAG_CLEAR 0xD0D04321 20 | 21 | class DoubleResetDetector 22 | { 23 | public: 24 | DoubleResetDetector(int timeout, int address); 25 | bool detectDoubleReset(); 26 | bool doubleResetDetected; 27 | void loop(); 28 | void stop(); 29 | 30 | private: 31 | unsigned long timeout; 32 | int address; 33 | bool waitingForDoubleReset; 34 | bool detectRecentlyResetFlag(); 35 | void clearRecentlyResetFlag(); 36 | void setRecentlyResetFlag(); 37 | uint32_t doubleResetDetectorFlag; 38 | }; 39 | #endif // DoubleResetDetector_H__ 40 | --------------------------------------------------------------------------------