Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 7a4b074

Browse files
authored
Add files via upload
1 parent e4950b1 commit 7a4b074

File tree

6 files changed

+298
-0
lines changed

6 files changed

+298
-0
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Double Reset Detector
2+
=====================
3+
4+
This library is based on, modified, bug-fixed and improved from:
5+
6+
[DataCute] (https://github.com/datacute/DoubleResetDetector)
7+
8+
to add support for ESP32.
9+
10+
Using this library to detect a double reset, using RTC Memory for ESP8266 and EEPROM for ESP32.
11+
12+
It is tested and working with
13+
1. The `ESP8266` Arduino platform with a recent stable release [`ESP8266 Core 2.6.2 or newer`] (https://github.com/esp8266/Arduino)
14+
2. The `ESP32` Arduino platform with a recent stable release [`ESP32 Core 1.0.4 or newer`] (https://github.com/espressif/arduino-esp32)
15+
16+
### Releases
17+
#### v1.0.1
18+
19+
### PURPOSE:
20+
21+
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.
22+
23+
### LICENCE: MIT

examples/minimal/minimal.ino

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <DoubleResetDetector.h>
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(115200);
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+
}

keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
DoubleResetDetector
9+
10+
#######################################
11+
# Methods and Functions (KEYWORD2)
12+
#######################################
13+
detectDoubleReset KEYWORD2
14+
loop KEYWORD2
15+
stop KEYWORD2
16+
#######################################
17+
# Instances (KEYWORD2)
18+
#######################################
19+
20+
#######################################
21+
# Constants (LITERAL1)
22+
#######################################

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=ESP_DoubleResetDetector
2+
version=1.0.0
3+
author=Khoi Hoang
4+
maintainer=Khoi Hoang <[email protected]>
5+
license=MIT
6+
sentence=Library to detect a double reset, using RTC Memory for ESP8266 and EEPROM for ESP32
7+
paragraph=An alternative start-up mode can be used. One example use is to allow re-configuration of a device's wifi.
8+
category=Device Control
9+
url=https://github.com/khoih-prog/ESP_DoubleResetDetector
10+
architectures=esp8266,esp32

src/ESP_DoubleResetDetector.cpp

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/****************************************************************************************************************************
2+
* ESP_DoubleResetDetector.cpp
3+
* For ESP8266 / ESP32 boards
4+
*
5+
* ESP_DoubleResetDetector is a library for the ESP8266/Arduino platform
6+
* to enable trigger configure mode by resetting ESP32 / ESP8266 twice.
7+
*
8+
* Forked from DataCute https://github.com/datacute/DoubleResetDetector
9+
*
10+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector
11+
* Licensed under MIT license
12+
* Version: 1.0.0
13+
*
14+
* Version Modified By Date Comments
15+
* ------- ----------- ---------- -----------
16+
* 1.0.0 K Hoang 15/12/2019 Initial coding
17+
*****************************************************************************************************************************/
18+
19+
#include "ESP_DoubleResetDetector.h"
20+
21+
#ifdef ESP32
22+
#include <EEPROM.h>
23+
static uint32_t DOUBLERESETDETECTOR_FLAG;
24+
25+
#define FLAG_DATA_SIZE 4
26+
27+
#ifndef EEPROM_SIZE
28+
#define EEPROM_SIZE 512
29+
#endif
30+
31+
#ifndef EEPROM_START
32+
#define EEPROM_START 256
33+
#endif
34+
35+
#endif
36+
37+
#define DOUBLERESETDETECTOR_DEBUG false
38+
39+
// Flag which will be stored in RTC memory.
40+
// A uint32_t is used so that two different magic numbers can be used,
41+
// without accidentally overwriting memory used for another purpose.
42+
uint32_t doubleResetDetectorFlag;
43+
44+
DoubleResetDetector::DoubleResetDetector(int timeout, int address)
45+
{
46+
#ifdef ESP32
47+
#if (DOUBLERESETDETECTOR_DEBUG)
48+
Serial.println("EEPROM size = " + String(EEPROM_SIZE) + ", start = " + String(EEPROM_START));
49+
#endif
50+
51+
EEPROM.begin(EEPROM_SIZE);
52+
#endif
53+
54+
this->timeout = timeout * 1000;
55+
this->address = address;
56+
doubleResetDetected = false;
57+
waitingForDoubleReset = false;
58+
}
59+
60+
bool DoubleResetDetector::detectDoubleReset()
61+
{
62+
doubleResetDetected = detectRecentlyResetFlag();
63+
64+
if (doubleResetDetected)
65+
{
66+
#if (DOUBLERESETDETECTOR_DEBUG)
67+
Serial.println("doubleResetDetected");
68+
#endif
69+
70+
clearRecentlyResetFlag();
71+
}
72+
else
73+
{
74+
#if (DOUBLERESETDETECTOR_DEBUG)
75+
Serial.println("No doubleResetDetected");
76+
#endif
77+
78+
setRecentlyResetFlag();
79+
waitingForDoubleReset = true;
80+
}
81+
return doubleResetDetected;
82+
}
83+
84+
void DoubleResetDetector::loop()
85+
{
86+
if (waitingForDoubleReset && millis() > timeout)
87+
{
88+
#if (DOUBLERESETDETECTOR_DEBUG)
89+
Serial.println("Stop doubleResetDetecting");
90+
#endif
91+
92+
stop();
93+
}
94+
}
95+
96+
void DoubleResetDetector::stop()
97+
{
98+
clearRecentlyResetFlag();
99+
waitingForDoubleReset = false;
100+
}
101+
102+
bool DoubleResetDetector::detectRecentlyResetFlag()
103+
{
104+
//doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR;
105+
106+
#ifdef ESP8266
107+
ESP.rtcUserMemoryRead(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
108+
#else
109+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
110+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG;
111+
112+
#if (DOUBLERESETDETECTOR_DEBUG)
113+
Serial.println("EEPROM Flag read = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
114+
#endif
115+
#endif
116+
117+
doubleResetDetected = (doubleResetDetectorFlag == DOUBLERESETDETECTOR_FLAG_SET);
118+
return doubleResetDetected;
119+
}
120+
121+
void DoubleResetDetector::setRecentlyResetFlag() {
122+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_SET;
123+
124+
#ifdef ESP8266
125+
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
126+
#else
127+
DOUBLERESETDETECTOR_FLAG = DOUBLERESETDETECTOR_FLAG_SET;
128+
EEPROM.put(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
129+
EEPROM.commit();
130+
131+
#if (DOUBLERESETDETECTOR_DEBUG)
132+
delay(1000);
133+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
134+
Serial.println("SetFlag write = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
135+
#endif
136+
#endif
137+
}
138+
139+
void DoubleResetDetector::clearRecentlyResetFlag() {
140+
doubleResetDetectorFlag = DOUBLERESETDETECTOR_FLAG_CLEAR;
141+
142+
#ifdef ESP8266
143+
ESP.rtcUserMemoryWrite(address, &doubleResetDetectorFlag, sizeof(doubleResetDetectorFlag));
144+
#else
145+
DOUBLERESETDETECTOR_FLAG = DOUBLERESETDETECTOR_FLAG_CLEAR;
146+
EEPROM.put(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
147+
EEPROM.commit();
148+
149+
#if (DOUBLERESETDETECTOR_DEBUG)
150+
delay(1000);
151+
EEPROM.get(EEPROM_START, DOUBLERESETDETECTOR_FLAG);
152+
Serial.println("ClearFlag write = 0x" + String(DOUBLERESETDETECTOR_FLAG, HEX) );
153+
#endif
154+
#endif
155+
}

src/ESP_DoubleResetDetector.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/****************************************************************************************************************************
2+
* ESP_DoubleResetDetector.h
3+
* For ESP8266 / ESP32 boards
4+
*
5+
* ESP_DoubleResetDetector is a library for the ESP8266/Arduino platform
6+
* to enable trigger configure mode by resetting ESP32 / ESP8266 twice.
7+
*
8+
* Forked from DataCute https://github.com/datacute/DoubleResetDetector
9+
*
10+
* Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector
11+
* Licensed under MIT license
12+
* Version: 1.0.0
13+
*
14+
* Version Modified By Date Comments
15+
* ------- ----------- ---------- -----------
16+
* 1.0.0 K Hoang 15/12/2019 Initial coding
17+
*****************************************************************************************************************************/
18+
19+
#ifndef ESP_DoubleResetDetector_H
20+
#define ESP_DoubleResetDetector_H
21+
22+
#if defined(ARDUINO) && (ARDUINO >= 100)
23+
#include <Arduino.h>
24+
#else
25+
#include <WProgram.h>
26+
#endif
27+
28+
#define ESP_DOUBLERESETDETECTOR_VERSION "1.0.0"
29+
30+
#define DOUBLERESETDETECTOR_FLAG_SET 0xD0D01234
31+
#define DOUBLERESETDETECTOR_FLAG_CLEAR 0xD0D04321
32+
33+
class DoubleResetDetector
34+
{
35+
public:
36+
DoubleResetDetector(int timeout, int address);
37+
bool detectDoubleReset();
38+
bool doubleResetDetected;
39+
void loop();
40+
void stop();
41+
42+
private:
43+
int timeout;
44+
int address;
45+
bool waitingForDoubleReset;
46+
bool detectRecentlyResetFlag();
47+
void clearRecentlyResetFlag();
48+
void setRecentlyResetFlag();
49+
uint32_t doubleResetDetectorFlag;
50+
};
51+
#endif // DoubleResetDetector_H

0 commit comments

Comments
 (0)