This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcheckWaitingDRD.ino
134 lines (99 loc) · 3.62 KB
/
checkWaitingDRD.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/****************************************************************************************************************************
checkWaitingDRD.ino
For ESP8266 / ESP32 boards
ESP_DoubleResetDetector is a library for the ESP8266/Arduino platform
to enable trigger configure mode by resetting ESP32 / ESP8266 twice.
Based on and modified from DataCute https://github.com/datacute/DoubleResetDetector
Built by Khoi Hoang https://github.com/khoih-prog/ESP_DoubleResetDetector
Licensed under MIT license
*****************************************************************************************************************************/
// These defines must be put before #include <ESP_DoubleResetDetector.h>
// to select where to store DoubleResetDetector's variable.
// For ESP32, You must select one to be true (EEPROM or SPIFFS)
// For ESP8266, You must select one to be true (RTC, EEPROM, LITTLEFS or SPIFFS)
// Otherwise, library will use default EEPROM storage
// This example demonstrates how to use new function waitingForDRD() to signal the stage of DRD
// waitingForDRD() returns true if in DRD_TIMEOUT, false when out of DRD_TIMEOUT
// In this example, LED_BUILTIN will blink in DRD_TIMEOUT period, ON when DR has been detected, OFF otherwise
#ifdef ESP8266
#define ESP8266_DRD_USE_RTC false //true
#endif
#define ESP_DRD_USE_LITTLEFS true
#define ESP_DRD_USE_SPIFFS false
#define ESP_DRD_USE_EEPROM false
// Uncomment to have debug
//#define DOUBLERESETDETECTOR_DEBUG true
#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector
// Number of seconds after reset during which a
// subsequent reset will be considered a double reset.
#define DRD_TIMEOUT 10
// RTC Memory Address for the DoubleResetDetector to use
#define DRD_ADDRESS 0
DoubleResetDetector* drd;
#ifdef ESP32
// For ESP32
#ifndef LED_BUILTIN
#define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED
#endif
#define LED_OFF LOW
#define LED_ON HIGH
#else
// For ESP8266
#define LED_ON LOW
#define LED_OFF HIGH
#endif
bool DRD_Detected = false;
void check_status()
{
static ulong checkstatus_timeout = 0;
static bool LEDState = LED_OFF;
static ulong current_millis;
#define DRD_CHECK_INTERVAL 500L
current_millis = millis();
// If DRD_Detected, don't need to blink, just keep LED_BUILTIN ON
if ( !DRD_Detected && ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) )
{
// If in DRD checking loop, blinking the LED_BUILTIN
if ( drd->waitingForDRD() )
{
digitalWrite(LED_BUILTIN, LEDState);
LEDState = !LEDState;
}
else
{
digitalWrite(LED_BUILTIN, LED_OFF);
}
checkstatus_timeout = current_millis + DRD_CHECK_INTERVAL;
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting checkWaitingDRD on");
Serial.println(ARDUINO_BOARD);
Serial.println(ESP_DOUBLE_RESET_DETECTOR_VERSION);
drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS);
if (drd->detectDoubleReset())
{
Serial.println("Double Reset Detected");
digitalWrite(LED_BUILTIN, LED_ON);
DRD_Detected = true;
}
else
{
Serial.println("No Double Reset Detected");
digitalWrite(LED_BUILTIN, LED_OFF);
}
}
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();
check_status();
}