|
| 1 | +/**************************************************************************************************************************** |
| 2 | + checkWaitingDRD.ino |
| 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 | + Based on and modified 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 | + *****************************************************************************************************************************/ |
| 13 | + |
| 14 | +// These defines must be put before #include <ESP_DoubleResetDetector.h> |
| 15 | +// to select where to store DoubleResetDetector's variable. |
| 16 | +// For ESP32, You must select one to be true (EEPROM or SPIFFS) |
| 17 | +// For ESP8266, You must select one to be true (RTC, EEPROM, LITTLEFS or SPIFFS) |
| 18 | +// Otherwise, library will use default EEPROM storage |
| 19 | + |
| 20 | +// This example demonstrate how to use new function waitingForDRD() to signal the stage of DRD |
| 21 | +// waitingForDRD() returns true if in DRD_TIMEOUT, false when out of DRD_TIMEOUT |
| 22 | +// In this example, LED_BUILTIN will blink in DRD_TIMEOUT period, ON when DR has been detected, OFF otherwise |
| 23 | + |
| 24 | +#ifdef ESP8266 |
| 25 | + #define ESP8266_DRD_USE_RTC false //true |
| 26 | +#endif |
| 27 | + |
| 28 | +#define ESP_DRD_USE_LITTLEFS true |
| 29 | +#define ESP_DRD_USE_SPIFFS false |
| 30 | +#define ESP_DRD_USE_EEPROM false |
| 31 | + |
| 32 | +// Uncomment to have debug |
| 33 | +//#define DOUBLERESETDETECTOR_DEBUG true |
| 34 | + |
| 35 | +#include <ESP_DoubleResetDetector.h> //https://github.com/khoih-prog/ESP_DoubleResetDetector |
| 36 | + |
| 37 | +// Number of seconds after reset during which a |
| 38 | +// subsequent reset will be considered a double reset. |
| 39 | +#define DRD_TIMEOUT 10 |
| 40 | + |
| 41 | +// RTC Memory Address for the DoubleResetDetector to use |
| 42 | +#define DRD_ADDRESS 0 |
| 43 | + |
| 44 | +DoubleResetDetector* drd; |
| 45 | + |
| 46 | +#ifdef ESP32 |
| 47 | + |
| 48 | + // For ESP32 |
| 49 | + #ifndef LED_BUILTIN |
| 50 | + #define LED_BUILTIN 2 // Pin D2 mapped to pin GPIO2/ADC12 of ESP32, control on-board LED |
| 51 | + #endif |
| 52 | + |
| 53 | + #define LED_OFF LOW |
| 54 | + #define LED_ON HIGH |
| 55 | + |
| 56 | +#else |
| 57 | + |
| 58 | + // For ESP8266 |
| 59 | + #define LED_ON LOW |
| 60 | + #define LED_OFF HIGH |
| 61 | + |
| 62 | +#endif |
| 63 | + |
| 64 | +bool DRD_Detected = false; |
| 65 | + |
| 66 | +void check_status() |
| 67 | +{ |
| 68 | + static ulong checkstatus_timeout = 0; |
| 69 | + static bool LEDState = LED_OFF; |
| 70 | + |
| 71 | + static ulong current_millis; |
| 72 | + |
| 73 | +#define DRD_CHECK_INTERVAL 500L |
| 74 | + |
| 75 | + current_millis = millis(); |
| 76 | + |
| 77 | + // If DRD_Detected, don't need to blink, just keep LED_BUILTIN ON |
| 78 | + if ( !DRD_Detected && ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) ) |
| 79 | + { |
| 80 | + // If in DRD checking loop, blinking the LED_BUILTIN |
| 81 | + if ( drd->waitingForDRD() ) |
| 82 | + { |
| 83 | + digitalWrite(LED_BUILTIN, LEDState); |
| 84 | + |
| 85 | + LEDState = !LEDState; |
| 86 | + } |
| 87 | + else |
| 88 | + { |
| 89 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 90 | + } |
| 91 | + |
| 92 | + checkstatus_timeout = current_millis + DRD_CHECK_INTERVAL; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +void setup() |
| 97 | +{ |
| 98 | + pinMode(LED_BUILTIN, OUTPUT); |
| 99 | + |
| 100 | + Serial.begin(115200); |
| 101 | + while (!Serial); |
| 102 | + |
| 103 | + delay(200); |
| 104 | + |
| 105 | + Serial.print("\nStarting checkWaitingDRD on"); Serial.println(ARDUINO_BOARD); |
| 106 | + Serial.println(ESP_DOUBLE_RESET_DETECTOR_VERSION); |
| 107 | + |
| 108 | + drd = new DoubleResetDetector(DRD_TIMEOUT, DRD_ADDRESS); |
| 109 | + |
| 110 | + if (drd->detectDoubleReset()) |
| 111 | + { |
| 112 | + Serial.println("Double Reset Detected"); |
| 113 | + digitalWrite(LED_BUILTIN, LED_ON); |
| 114 | + DRD_Detected = true; |
| 115 | + } |
| 116 | + else |
| 117 | + { |
| 118 | + Serial.println("No Double Reset Detected"); |
| 119 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +void loop() |
| 124 | +{ |
| 125 | + // Call the double reset detector loop method every so often, |
| 126 | + // so that it can recognise when the timeout expires. |
| 127 | + // You can also call drd.stop() when you wish to no longer |
| 128 | + // consider the next reset as a double reset. |
| 129 | + drd->loop(); |
| 130 | + |
| 131 | + check_status(); |
| 132 | +} |
0 commit comments