|
| 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 | +} |
0 commit comments