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 2
/
checkWaitingMRD.ino
150 lines (112 loc) · 4.18 KB
/
checkWaitingMRD.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/****************************************************************************************************************************
checkWaitingMRD.ino
For ESP8266 / ESP32 boards
ESP_MultiResetDetector is a library for the ESP8266/Arduino platform
to enable trigger configure mode by resetting ESP32 / ESP8266 multiple times.
Based on and modified from
1) DataCute https://github.com/datacute/MultiResetDetector
2) Khoi Hoang https://github.com/khoih-prog/ESP_MultiResetDetector
Built by Khoi Hoang https://github.com/khoih-prog/ESP_MultiResetDetector
Licensed under MIT license
*****************************************************************************************************************************/
// These defines must be put before #include <ESP_MultiResetDetector.h>
// to select where to store MultiResetDetector'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 waitingForMRD() to signal the stage of MRD
// waitingForMRD() returns true if in MRD_TIMEOUT, false when out of MRD_TIMEOUT
// In this example, LED_BUILTIN will blink in MRD_TIMEOUT period, ON when DR has been detected, OFF otherwise
#ifdef ESP8266
#define ESP8266_MRD_USE_RTC false //true
#endif
#define ESP_MRD_USE_LITTLEFS true
#define ESP_MRD_USE_SPIFFS false
#define ESP_MRD_USE_EEPROM false
// Uncomment to have debug
//#define MULTIRESETDETECTOR_DEBUG true
// These definitions must be placed before #include <ESP_MultiResetDetector.h> to be used
// Otherwise, default values (MRD_TIMES = 3, MRD_TIMEOUT = 10 seconds and MRD_ADDRESS = 0) will be used
// Number of subsequent resets during MRD_TIMEOUT to activate
#define MRD_TIMES 3
// Number of seconds after reset during which a
// subsequent reset will be considered a multi reset.
#define MRD_TIMEOUT 10
// RTC/EEPROM Memory Address for the MultiResetDetector to use
#define MRD_ADDRESS 0
#include <ESP_MultiResetDetector.h> //https://github.com/khoih-prog/ESP_MultiResetDetector
MultiResetDetector* mrd;
#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 MRD_Detected = false;
void check_status()
{
static ulong checkstatus_timeout = 0;
static bool LEDState = LED_OFF;
static ulong current_millis;
#define MRD_CHECK_INTERVAL 500L
current_millis = millis();
// If MRD_Detected, don't need to blink, just keep LED_BUILTIN ON
if ( !MRD_Detected && ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) )
{
// If in MRD checking loop, blinking the LED_BUILTIN
if ( mrd->waitingForMRD() )
{
digitalWrite(LED_BUILTIN, LEDState);
LEDState = !LEDState;
}
else
{
digitalWrite(LED_BUILTIN, LED_OFF);
}
checkstatus_timeout = current_millis + MRD_CHECK_INTERVAL;
}
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(200);
Serial.print("\nStarting checkWaitingMRD on");
Serial.println(ARDUINO_BOARD);
#if ESP_MRD_USE_LITTLEFS
Serial.println(F(" using LittleFS"));
#elif ESP_MRD_USE_SPIFFS
Serial.println(F(" using SPIFFS"));
#else
Serial.println(F(" using EEPROM"));
#endif
Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION);
mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS);
if (mrd->detectMultiReset())
{
Serial.println("Multi Reset Detected");
digitalWrite(LED_BUILTIN, LED_ON);
MRD_Detected = true;
}
else
{
Serial.println("No Multi 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 mrd.stop() when you wish to no longer
// consider the next reset as a double reset.
mrd->loop();
check_status();
}