|
| 1 | +/**************************************************************************************************************************** |
| 2 | + checkWaitingDRD.ino |
| 3 | + DoubleResetDetector_Generic.h |
| 4 | + For AVR Mega, Teensy, STM32, nRF52, SAM DUE, SAMD21, SAMD51 boards |
| 5 | +
|
| 6 | + DoubleResetDetector_Generic is a library for the Arduino AVR, Teensy, SAM-DUE, SAMD, STM32. etc. boards |
| 7 | + to enable trigger configure mode by resetting the boards twice within configurable timeout seconds. |
| 8 | +
|
| 9 | + Based on and modified from DataCute https://github.com/datacute/DoubleResetDetector and |
| 10 | + https://github.com/khoih-prog/ESP_DoubleResetDetector |
| 11 | +
|
| 12 | + Built by Khoi Hoang https://github.com/khoih-prog/DoubleResetDetector_Generic |
| 13 | + Licensed under MIT license |
| 14 | + *****************************************************************************************************************************/ |
| 15 | + |
| 16 | +/**************************************************************************************************************************** |
| 17 | + This example will open a configuration portal when the reset button is pressed twice. |
| 18 | + This method works well on Wemos boards which have a single reset button on board. It avoids using a pin for launching the configuration portal. |
| 19 | +
|
| 20 | + How It Works |
| 21 | + 1) AVR Mega, Teensy |
| 22 | + Save data in EPPROM from address 1020, size 1024 bytes (both configurable) |
| 23 | + Note: Teensy 4.0 has only 1080 bytes of EEPROM-simulated Flash |
| 24 | + 2) SAMD |
| 25 | + Save data in EEPROM-simulated FlashStorage from address 0 (configurable to avoid conflict) |
| 26 | + 3) SAM DUE |
| 27 | + Save data in DueFlashStorage from address 1020 (configurable to avoid conflict) |
| 28 | + 4) Adafruit nRF52-based boards |
| 29 | + Save data in InternalFS, fle "/drd.dat" location 0 |
| 30 | + 5) RTL8720 |
| 31 | + Save data in FlashStorage from address 0 (configurable to avoid conflict) |
| 32 | + 6) STM32 |
| 33 | + Save data in EEPROM-simulated FlashStorage from address 0 (configurable to avoid conflict) |
| 34 | +
|
| 35 | + So when the device starts up it checks the InternalFS file "/drd.dat", EEPROM or (Due)FlashStorage for a flag to see if it has been |
| 36 | + recently reset within the configurable timeout seconds |
| 37 | + It'll then set a flag, and display a message to signal if the DR is detected |
| 38 | +
|
| 39 | + Settings |
| 40 | + There are two values to be set in the sketch. |
| 41 | +
|
| 42 | + DRD_TIMEOUT - Number of seconds to wait for the second reset. Set to 10s in the example. |
| 43 | + DRD_ADDRESS - The address in ESP8266 RTC RAM to store the flag. This memory must not be used for other purposes in the same sketch. Set to 0 in the example. |
| 44 | +
|
| 45 | + This example, originally relied on the Double Reset Detector library from https://github.com/datacute/DoubleResetDetector |
| 46 | + To support ESP32, use ESP_DoubleResetDetector library from https://github.com/khoih-prog/ESP_DoubleResetDetector |
| 47 | + To support AVR, Teensy, SAM DUE, SAMD and STM32, etc., use this DoubleResetDetector_Generic from //https://github.com/khoih-prog/DoubleResetDetector_Generic |
| 48 | + *****************************************************************************************************************************/ |
| 49 | + |
| 50 | + |
| 51 | +// These defines must be put before #include <DoubleResetDetector_Generic.h> |
| 52 | +// to select where to store DoubleResetDetector's variable. |
| 53 | +// For ESP32, You must select one to be true (EEPROM or SPIFFS) |
| 54 | +// For ESP8266, You must select one to be true (RTC, EEPROM, LITTLEFS or SPIFFS) |
| 55 | +// Otherwise, library will use default EEPROM storage |
| 56 | + |
| 57 | +// This example demonstrates how to use new function waitingForDRD() to signal the stage of DRD |
| 58 | +// waitingForDRD() returns true if in DRD_TIMEOUT, false when out of DRD_TIMEOUT |
| 59 | +// In this example, LED_BUILTIN will blink in DRD_TIMEOUT period, ON when DR has been detected, OFF otherwise |
| 60 | + |
| 61 | +#define DEBUG_ETHERNET_GENERIC_PORT SerialDebug |
| 62 | + |
| 63 | +#define DRD_GENERIC_DEBUG true //false |
| 64 | + |
| 65 | +// You have to select true for the first time for any board |
| 66 | +#define FORCE_REFORMAT false |
| 67 | + |
| 68 | +// Uncomment to have debug |
| 69 | +//#define DOUBLERESETDETECTOR_DEBUG true |
| 70 | + |
| 71 | +#include <DoubleResetDetector_Generic.h> //https://github.com/khoih-prog/DoubleResetDetector_Generic |
| 72 | + |
| 73 | +// Number of seconds after reset during which a |
| 74 | +// subsequent reset will be considered a double reset. |
| 75 | +#define DRD_TIMEOUT 10 |
| 76 | + |
| 77 | +// RTC Memory Address for the DoubleResetDetector to use |
| 78 | +#define DRD_ADDRESS 0 |
| 79 | + |
| 80 | +DoubleResetDetector_Generic* drd; |
| 81 | + |
| 82 | +// Change according to your board |
| 83 | +#ifndef LED_BUILTIN |
| 84 | + // Generic boards following LED at pin 13 |
| 85 | + #define LED_BUILTIN 13 |
| 86 | +#endif |
| 87 | + |
| 88 | +// Change according to your board |
| 89 | +#define LED_OFF LOW |
| 90 | +#define LED_ON HIGH |
| 91 | + |
| 92 | +bool DRD_Detected = false; |
| 93 | + |
| 94 | +void check_status() |
| 95 | +{ |
| 96 | + static unsigned long checkstatus_timeout = 0; |
| 97 | + static bool LEDState = LED_OFF; |
| 98 | + |
| 99 | + static unsigned long current_millis; |
| 100 | + |
| 101 | +#define DRD_CHECK_INTERVAL 500L |
| 102 | + |
| 103 | + current_millis = millis(); |
| 104 | + |
| 105 | + // If DRD_Detected, don't need to blink, just keep LED_BUILTIN ON |
| 106 | + if ( !DRD_Detected && ((current_millis > checkstatus_timeout) || (checkstatus_timeout == 0)) ) |
| 107 | + { |
| 108 | + // If in DRD checking loop, blinking the LED_BUILTIN |
| 109 | + if ( drd->waitingForDRD() ) |
| 110 | + { |
| 111 | + digitalWrite(LED_BUILTIN, LEDState); |
| 112 | + |
| 113 | + LEDState = !LEDState; |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 118 | + } |
| 119 | + |
| 120 | + checkstatus_timeout = current_millis + DRD_CHECK_INTERVAL; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +void setup() |
| 125 | +{ |
| 126 | + pinMode(LED_BUILTIN, OUTPUT); |
| 127 | + |
| 128 | + SerialDebug.begin(115200); |
| 129 | + |
| 130 | + while (!SerialDebug); |
| 131 | + |
| 132 | + delay(200); |
| 133 | + |
| 134 | +#if defined(BOARD_NAME) |
| 135 | + SerialDebug.print(F("\nStarting checkWaitingDRD on")); |
| 136 | + SerialDebug.println(BOARD_NAME); |
| 137 | +#else |
| 138 | + SerialDebug.println(F("\nStarting checkWaitingDRD")); |
| 139 | +#endif |
| 140 | + |
| 141 | + SerialDebug.println(DOUBLERESETDETECTOR_GENERIC_VERSION); |
| 142 | + SerialDebug.println("-----------------------------------"); |
| 143 | + |
| 144 | + drd = new DoubleResetDetector_Generic(DRD_TIMEOUT, DRD_ADDRESS); |
| 145 | + |
| 146 | + if (drd->detectDoubleReset()) |
| 147 | + { |
| 148 | + SerialDebug.println("Double Reset Detected"); |
| 149 | + digitalWrite(LED_BUILTIN, LED_ON); |
| 150 | + DRD_Detected = true; |
| 151 | + } |
| 152 | + else |
| 153 | + { |
| 154 | + SerialDebug.println("No Double Reset Detected"); |
| 155 | + digitalWrite(LED_BUILTIN, LED_OFF); |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +void loop() |
| 160 | +{ |
| 161 | + // Call the double reset detector loop method every so often, |
| 162 | + // so that it can recognise when the timeout expires. |
| 163 | + // You can also call drd.stop() when you wish to no longer |
| 164 | + // consider the next reset as a double reset. |
| 165 | + drd->loop(); |
| 166 | + |
| 167 | + check_status(); |
| 168 | +} |
0 commit comments