diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 96cec8a..1b87b42 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -15,7 +15,7 @@ If you don't find anything, please [open a new issue](https://github.com/khoih-p Please ensure to specify the following: * Arduino IDE version (e.g. 1.8.19) or Platform.io version -* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v3.0.2 or ESP32 v2.0.4) +* `ESP8266` or `ESP32` Core Version (e.g. ESP8266 core v3.0.2 or ESP32 v2.0.5) * Contextual information (e.g. what you were trying to achieve) * Simplest possible steps to reproduce * Anything that might be relevant in your opinion, such as: @@ -29,10 +29,10 @@ Please ensure to specify the following: Arduino IDE version: 1.8.19 ESP8266 Core Version 3.0.2 OS: Ubuntu 20.04 LTS -Linux xy-Inspiron-3593 5.15.0-46-generic #49~20.04.1-Ubuntu SMP Thu Aug 4 19:15:44 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux +Linux xy-Inspiron-3593 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Context: -I encountered a non-response while trying to press the reset button twice. +I encountered a crash while using this library Steps to reproduce: 1. ... @@ -40,12 +40,33 @@ Steps to reproduce: 3. ... 4. ... ``` + +--- + ### Sending Feature Requests Feel free to post feature requests. It's helpful if you can explain exactly why the feature would be useful. There are usually some outstanding feature requests in the [existing issues list](https://github.com/khoih-prog/ESP_MultiResetDetector/issues?q=is%3Aopen+is%3Aissue+label%3Aenhancement), feel free to add comments to them. +--- + ### Sending Pull Requests Pull Requests with changes and fixes are also welcome! + +Please use the `astyle` to reformat the updated library code as follows (demo for Ubuntu Linux) + +1. Change directory to the library GitHub + +``` +xy@xy-Inspiron-3593:~$ cd Arduino/xy/ESP_MultiResetDetector_GitHub/ +xy@xy-Inspiron-3593:~/Arduino/xy/ESP_MultiResetDetector_GitHub$ +``` + +2. Issue astyle command + +``` +xy@xy-Inspiron-3593:~/Arduino/xy/ESP_MultiResetDetector_GitHub$ bash utils/restyle.sh +``` + diff --git a/README.md b/README.md index 3fb255b..c470baf 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -## ESP_MultiResetDetector +## ESP_MultiResetDetector Library [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_MultiResetDetector.svg?)](https://www.ardu-badge.com/ESP_MultiResetDetector) [![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP_MultiResetDetector.svg)](https://github.com/khoih-prog/ESP_MultiResetDetector/releases) @@ -9,6 +9,8 @@ Donate to my libraries using BuyMeACoffee + + --- --- @@ -124,10 +126,11 @@ This [**ESP_MultiResetDetector** library](https://github.com/khoih-prog/ESP_Mult ## Prerequisites 1. [`Arduino IDE 1.8.19+` for Arduino](https://github.com/arduino/Arduino). [![GitHub release](https://img.shields.io/github/release/arduino/Arduino.svg)](https://github.com/arduino/Arduino/releases/latest) -2. [`ESP32 Core 2.0.4+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/) +2. [`ESP32 Core 2.0.5+`](https://github.com/espressif/arduino-esp32) for ESP32-based boards. [![Latest release](https://img.shields.io/github/release/espressif/arduino-esp32.svg)](https://github.com/espressif/arduino-esp32/releases/latest/) 3. [`ESP8266 Core 3.0.2+`](https://github.com/esp8266/Arduino) for ESP8266-based boards. [![Latest release](https://img.shields.io/github/release/esp8266/Arduino.svg)](https://github.com/esp8266/Arduino/releases/latest/). SPIFFS is deprecated from ESP8266 core 2.7.1+, to use LittleFS. 4. [`LittleFS_esp32 v1.0.6+`](https://github.com/lorol/LITTLEFS) for ESP32-based boards using LittleFS with ESP32 core **v1.0.5-**. To install, check [![arduino-library-badge](https://www.ardu-badge.com/badge/LittleFS_esp32.svg?)](https://www.ardu-badge.com/LittleFS_esp32). **Notice**: This [`LittleFS_esp32 library`](https://github.com/lorol/LITTLEFS) has been integrated to Arduino [ESP32 core v1.0.6+](https://github.com/espressif/arduino-esp32/tree/master/libraries/LITTLEFS) and **you don't need to install it if using ESP32 core v1.0.6+** +--- --- ## Installation @@ -324,7 +327,7 @@ This is terminal debug output when running [ESP32_FSWebServer_DRD](https://githu ```cpp Starting ESP32_FSWebServer_DRD with DoubleResetDetect using SPIFFS on ESP32_DEV -ESP_WiFiManager v1.11.0 +ESP_WiFiManager v1.12.1 ESP_MultiResetDetector v1.3.2 FS File: /ConfigSW.json, size: 150B FS File: /CanadaFlag_1.png, size: 40.25KB @@ -390,7 +393,7 @@ This is terminal debug output when running [minimal](examples/minimal) on ***ESP #### 2.1 Data Corrupted => reset to 0 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 /home/kh/Arduino/libraries/LITTLEFS-master/src/lfs.c:1003:error: Corrupted dir pair at {0x0, 0x1} @@ -411,7 +414,7 @@ Saving config file OK #### 2.2 Reset Detected => Reporting 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFE0001 @@ -426,7 +429,7 @@ No Multi Reset Detected #### 2.3 Reset Detected => Reporting 2 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFD0002 @@ -441,7 +444,7 @@ No Multi Reset Detected #### 2.4 Reset Detected => Reporting 3 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFC0003 @@ -456,7 +459,7 @@ No Multi Reset Detected #### 2.5 Reset Detected => Reporting 4 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFB0004 @@ -471,7 +474,7 @@ No Multi Reset Detected #### 2.6 Reset Detected => Reporting 5. Multi Reset Detected -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFA0005 @@ -485,7 +488,7 @@ Multi Reset Detected #### 2.7 Timed out => reset to 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFB0004 @@ -503,7 +506,7 @@ Saving config file OK #### 2.8 Reset Detected => Reporting 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP32_DEV using LittleFS ESP_MultiResetDetector v1.3.2 @@ -527,7 +530,7 @@ This is terminal debug output when running [minimal](examples/minimal) on ***ESP #### 3.1 Data Corrupted => reset to 0 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 multiResetDetectorFlag = 0x00000000 @@ -542,7 +545,7 @@ No Multi Reset Detected #### 3.2 Reset Detected => Reporting 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFE0001 @@ -557,7 +560,7 @@ No Multi Reset Detected #### 3.3 Reset Detected => Reporting 2 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFD0002 @@ -572,7 +575,7 @@ No Multi Reset Detected #### 3.4 Reset Detected => Reporting 3 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFC0003 @@ -587,7 +590,7 @@ No Multi Reset Detected #### 3.5 Reset Detected => Reporting 4 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFB0004 @@ -601,7 +604,7 @@ Saving config file OK #### 3.6 Reset Detected => Reporting 5. Multi Reset Detected -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFA0005 @@ -615,7 +618,7 @@ Multi Reset Detected #### 3.7 Timed out => reset to 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFB0004 @@ -633,7 +636,7 @@ Saving config file OK #### 3.8 Reset Detected => Reporting 1 -``` +```cpp Starting ESP_MultiResetDetector minimal on ESP8266_NODEMCU using LittleFS ESP_MultiResetDetector v1.3.2 LittleFS Flag read = 0xFFFE0001 @@ -653,7 +656,7 @@ No Multi Reset Detected This is terminal debug output when running [ESPAsync_WiFi](https://github.com/khoih-prog/ESPAsync_WiFiManager_Lite/tree/main/examples/ESPAsync_WiFi) on ***ESP32 ESP32S3_DEV.***. Config Portal was requested by MRD to input and save Credentials. -``` +```cpp Starting ESPAsync_WiFi using LittleFS on ESP32S3_DEV ESPAsync_WiFiManager_Lite v1.9.0 ESP_MultiResetDetector v1.3.2 @@ -776,6 +779,8 @@ Submit issues to: [ESP_MultiResetDetector issues](https://github.com/khoih-prog/ 4. Add waitingForMRD() function to signal in MRD waiting period. 6. Fix ESP32 chipID for example`ConfigOnMultiReset` 7. Remove dependency on `LittleFS_esp32` library to prevent PIO error when using new ESP32 core v1.0.6+ +8. Add astyle using `allman` style. Restyle the library + --- --- @@ -812,5 +817,5 @@ If you want to contribute to this project: ### Copyright -Copyright 2020- Khoi Hoang +Copyright (c) 2020- Khoi Hoang diff --git a/changelog.md b/changelog.md index 79c207c..70db854 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,4 @@ -## ESP_MultiResetDetector +## ESP_MultiResetDetector Library [![arduino-library-badge](https://www.ardu-badge.com/badge/ESP_MultiResetDetector.svg?)](https://www.ardu-badge.com/ESP_MultiResetDetector) [![GitHub release](https://img.shields.io/github/release/khoih-prog/ESP_MultiResetDetector.svg)](https://github.com/khoih-prog/ESP_MultiResetDetector/releases) @@ -6,6 +6,12 @@ [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](#Contributing) [![GitHub issues](https://img.shields.io/github/issues/khoih-prog/ESP_MultiResetDetector.svg)](http://github.com/khoih-prog/ESP_MultiResetDetector/issues) + +Donate to my libraries using BuyMeACoffee + + + + --- --- diff --git a/examples/ConfigOnMRD_ESP32_minimal/ConfigOnMRD_ESP32_minimal.ino b/examples/ConfigOnMRD_ESP32_minimal/ConfigOnMRD_ESP32_minimal.ino index 4c33284..570d360 100644 --- a/examples/ConfigOnMRD_ESP32_minimal/ConfigOnMRD_ESP32_minimal.ino +++ b/examples/ConfigOnMRD_ESP32_minimal/ConfigOnMRD_ESP32_minimal.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** ConfigOnMRD_ESP32_minimal.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 *****************************************************************************************************************************/ @@ -18,47 +18,95 @@ #define ESP_MRD_USE_LITTLEFS false #define ESP_MRD_USE_SPIFFS true #define ESP_MRD_USE_EEPROM false -#else - #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. +#else + #error This code is intended to run on the ESP32 platform! Please check your Tools->Board setting. #endif #include //https://github.com/khoih-prog/ESP_WiFiManager #define MRD_TIMES 3 #define MRD_TIMEOUT 10 #define MRD_ADDRESS 0 -#define MULTIRESETDETECTOR_DEBUG true +#define MULTIRESETDETECTOR_DEBUG true #include //https://github.com/khoih-prog/ESP_MultiResetDetector MultiResetDetector* mrd; const int PIN_LED = 2; bool initialConfig = false; -void setup() { +void setup() +{ pinMode(PIN_LED, OUTPUT); - Serial.begin(115200); while (!Serial); delay(200); - Serial.print(F("\nStarting ConfigOnMRD_ESP32_minimal on ")); Serial.print(ARDUINO_BOARD); + Serial.begin(115200); + + while (!Serial) + ; + + delay(200); + Serial.print(F("\nStarting ConfigOnMRD_ESP32_minimal on ")); + Serial.print(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_WIFIMANAGER_VERSION); Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); +#endif + Serial.println(ESP_WIFIMANAGER_VERSION); + Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS); - if (mrd->detectMultiReset()) { Serial.println(F("MRD")); initialConfig = true; } + + if (mrd->detectMultiReset()) + { + Serial.println(F("MRD")); + initialConfig = true; + } + ESP_WiFiManager ESP_wifiManager("ConfigOnMRD_ESP32_minimal"); - if (ESP_wifiManager.WiFi_SSID() == "") { Serial.println(F("No AP credentials")); initialConfig = true; } - if (initialConfig) { - Serial.println(F("Starting Config Portal")); digitalWrite(PIN_LED, HIGH); - if (!ESP_wifiManager.startConfigPortal()) { Serial.println(F("Not connected to WiFi")); } - else { Serial.println(F("connected")); } + + if (ESP_wifiManager.WiFi_SSID() == "") + { + Serial.println(F("No AP credentials")); + initialConfig = true; + } + + if (initialConfig) + { + Serial.println(F("Starting Config Portal")); + digitalWrite(PIN_LED, HIGH); + + if (!ESP_wifiManager.startConfigPortal()) + { + Serial.println(F("Not connected to WiFi")); + } + else + { + Serial.println(F("connected")); + } + } + else + { + WiFi.mode(WIFI_STA); + WiFi.begin(); } - else { WiFi.mode(WIFI_STA); WiFi.begin(); } + unsigned long startedAt = millis(); - digitalWrite(PIN_LED, LOW); Serial.print(F("After waiting ")); + digitalWrite(PIN_LED, LOW); + Serial.print(F("After waiting ")); int connRes = WiFi.waitForConnectResult(); float waited = (millis() - startedAt); - Serial.print(waited / 1000); Serial.print(F(" secs , Connection result is ")); Serial.println(connRes); - if (WiFi.status() != WL_CONNECTED) { Serial.println(F("Failed to connect")); } - else { Serial.print(F("Local IP: ")); Serial.println(WiFi.localIP()); } + Serial.print(waited / 1000); + Serial.print(F(" secs , Connection result is ")); + Serial.println(connRes); + + if (WiFi.status() != WL_CONNECTED) + { + Serial.println(F("Failed to connect")); + } + else + { + Serial.print(F("Local IP: ")); + Serial.println(WiFi.localIP()); + } +} +void loop() +{ + mrd->loop(); } -void loop() { mrd->loop(); } diff --git a/examples/ConfigOnMRD_ESP8266_minimal/ConfigOnMRD_ESP8266_minimal.ino b/examples/ConfigOnMRD_ESP8266_minimal/ConfigOnMRD_ESP8266_minimal.ino index 5bc84af..37f867f 100644 --- a/examples/ConfigOnMRD_ESP8266_minimal/ConfigOnMRD_ESP8266_minimal.ino +++ b/examples/ConfigOnMRD_ESP8266_minimal/ConfigOnMRD_ESP8266_minimal.ino @@ -1,64 +1,108 @@ /**************************************************************************************************************************** ConfigOnMRD_ES8266_minimal.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 *****************************************************************************************************************************/ - + #if defined(ESP8266) #define USE_LITTLEFS true #define ESP_MRD_USE_LITTLEFS true #define ESP_MRD_USE_SPIFFS false #define ESP_MRD_USE_EEPROM false -#else - #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. +#else + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif #include //https://github.com/khoih-prog/ESP_WiFiManager #define MRD_TIMES 3 #define MRD_TIMEOUT 10 #define MRD_ADDRESS 0 -#define MULTIRESETDETECTOR_DEBUG true +#define MULTIRESETDETECTOR_DEBUG true #include //https://github.com/khoih-prog/ESP_MultiResetDetector MultiResetDetector* mrd; const int PIN_LED = 2; bool initialConfig = false; -void setup() { +void setup() +{ pinMode(PIN_LED, OUTPUT); - Serial.begin(115200); while (!Serial); delay(200); - Serial.print(F("\nStarting ConfigOnMRD_ES8266_minimal on ")); Serial.print(ARDUINO_BOARD); + Serial.begin(115200); + + while (!Serial) + ; + + delay(200); + Serial.print(F("\nStarting ConfigOnMRD_ES8266_minimal on ")); + Serial.print(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_WIFIMANAGER_VERSION); Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); - if (WiFi.SSID() == "") { Serial.println(F("No AP credentials")); initialConfig = true; } +#endif + Serial.println(ESP_WIFIMANAGER_VERSION); + Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); + + if (WiFi.SSID() == "") + { + Serial.println(F("No AP credentials")); + initialConfig = true; + } + mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS); - if (mrd->detectMultiReset()) { Serial.println(F("MRD")); initialConfig = true; } - if (initialConfig) { - Serial.println(F("Starting Config Portal")); digitalWrite(PIN_LED, LOW); + + if (mrd->detectMultiReset()) + { + Serial.println(F("MRD")); + initialConfig = true; + } + + if (initialConfig) + { + Serial.println(F("Starting Config Portal")); + digitalWrite(PIN_LED, LOW); ESP_WiFiManager ESP_wifiManager("ConfigOnMRD_ES8266_minimal"); ESP_wifiManager.setConfigPortalTimeout(0); - if (!ESP_wifiManager.startConfigPortal()) { Serial.println(F("Not connected to WiFi")); } - else { Serial.println(F("connected")); } + + if (!ESP_wifiManager.startConfigPortal()) + { + Serial.println(F("Not connected to WiFi")); + } + else + { + Serial.println(F("connected")); + } } - digitalWrite(PIN_LED, HIGH); Serial.print(F("After waiting ")); //WiFi.mode(WIFI_STA); + + digitalWrite(PIN_LED, HIGH); + Serial.print(F("After waiting ")); //WiFi.mode(WIFI_STA); unsigned long startedAt = millis(); int connRes = WiFi.waitForConnectResult(); float waited = (millis() - startedAt); - Serial.print(waited / 1000); Serial.print(F(" secs , Connection result is ")); Serial.println(connRes); - if (WiFi.status() != WL_CONNECTED) { Serial.println(F("Failed to connect")); } - else { Serial.print(F("Local IP: ")); Serial.println(WiFi.localIP()); } + Serial.print(waited / 1000); + Serial.print(F(" secs , Connection result is ")); + Serial.println(connRes); + + if (WiFi.status() != WL_CONNECTED) + { + Serial.println(F("Failed to connect")); + } + else + { + Serial.print(F("Local IP: ")); + Serial.println(WiFi.localIP()); + } +} +void loop() +{ + mrd->loop(); } -void loop() { mrd->loop(); } diff --git a/examples/ConfigOnMultiReset/ConfigOnMultiReset.ino b/examples/ConfigOnMultiReset/ConfigOnMultiReset.ino index f26d833..1277ec5 100644 --- a/examples/ConfigOnMultiReset/ConfigOnMultiReset.ino +++ b/examples/ConfigOnMultiReset/ConfigOnMultiReset.ino @@ -1,21 +1,21 @@ /**************************************************************************************************************************** ConfigOnMultiReset.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 *****************************************************************************************************************************/ /**************************************************************************************************************************** This example will open a configuration portal when the reset button is pressed twice. 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. - + How It Works 1) ESP8266 Save data in RTC memory, EPPROM, LittleFS or SPIFFS @@ -23,25 +23,25 @@ Save data in a) EEPROM from address 256, size 512 bytes (both configurable) b) SPIFFS - + For LittleFS or SPIFFS, file name is "/mrd.dat" - + So when the device starts up it checks this region of ram for a flag to see if it has been recently reset. If so it launches a configuration portal, if not it sets the reset flag. After running for a while this flag is cleared so that it will only launch the configuration portal in response to closely spaced resets. - + Settings There are values to be set in the sketch. MRD_TIMES - Number of subsequent resets during MRD_TIMEOUT to activate MRD_TIMEOUT - Number of seconds to wait for the second reset. Set to 10 in the example. MRD_ADDRESS - The address in ESP8266 RTC RAM/EEPROM address to store the flag. Must not be used in the same sketch - + This example, originally relied on the Double Reset Detector library from https://github.com/datacute/DoubleResetDetector To support ESP32, use ESP_MultiResetDetector library from //https://github.com/khoih-prog/ESP_MultiResetDetector *****************************************************************************************************************************/ #if !( defined(ESP8266) || defined(ESP32) ) -#error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. + #error This code is intended to run on the ESP8266 or ESP32 platform! Please check your Tools->Board setting. #endif // Use from 0 to 4. Higher number, more debugging messages and memory usage. @@ -54,7 +54,7 @@ #include #include #include - + // From v1.1.0 #include WiFiMulti wifiMulti; @@ -73,7 +73,7 @@ #warning Using ESP32 Core 1.0.6 or 2.0.0+ // The library has been merged into esp32 core from release 1.0.6 #include - + FS* filesystem = &LittleFS; #define FileFS LittleFS #define FS_Name "LittleFS" @@ -81,12 +81,12 @@ #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library // The library has been merged into esp32 core from release 1.0.6 #include // https://github.com/lorol/LITTLEFS - + FS* filesystem = &LITTLEFS; #define FileFS LITTLEFS #define FS_Name "LittleFS" #endif - + #elif USE_SPIFFS #include FS* filesystem = &SPIFFS; @@ -99,24 +99,24 @@ #define FileFS FFat #define FS_Name "FFat" #endif - ////// - -#define LED_BUILTIN 2 -#define LED_ON HIGH -#define LED_OFF LOW + ////// + + #define LED_BUILTIN 2 + #define LED_ON HIGH + #define LED_OFF LOW #else #include //https://github.com/esp8266/Arduino //needed for library #include #include - + // From v1.1.0 #include ESP8266WiFiMulti wifiMulti; - + #define USE_LITTLEFS true - + #if USE_LITTLEFS #include FS* filesystem = &LittleFS; @@ -128,9 +128,9 @@ #define FS_Name "SPIFFS" #endif ////// - + #define ESP_getChipId() (ESP.getChipId()) - + #define LED_ON LOW #define LED_OFF HIGH #endif @@ -174,7 +174,7 @@ #define ESP_MRD_USE_LITTLEFS false #define ESP_MRD_USE_SPIFFS true #endif - + #define ESP_MRD_USE_EEPROM false #define ESP8266_MRD_USE_RTC false #endif @@ -262,34 +262,34 @@ bool initialConfig = false; // Use USE_DHCP_IP == true for dynamic DHCP IP, false to use static IP which you have to change accordingly to your network #if (defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP) -// Force DHCP to be true -#if defined(USE_DHCP_IP) -#undef USE_DHCP_IP -#endif -#define USE_DHCP_IP true + // Force DHCP to be true + #if defined(USE_DHCP_IP) + #undef USE_DHCP_IP + #endif + #define USE_DHCP_IP true #else -// You can select DHCP or Static IP here -#define USE_DHCP_IP true -//#define USE_DHCP_IP false + // You can select DHCP or Static IP here + #define USE_DHCP_IP true + //#define USE_DHCP_IP false #endif #if ( USE_DHCP_IP || ( defined(USE_STATIC_IP_CONFIG_IN_CP) && !USE_STATIC_IP_CONFIG_IN_CP ) ) -// Use DHCP -#warning Using DHCP IP -IPAddress stationIP = IPAddress(0, 0, 0, 0); -IPAddress gatewayIP = IPAddress(192, 168, 2, 1); -IPAddress netMask = IPAddress(255, 255, 255, 0); -#else -// Use static IP -#warning Using static IP -#ifdef ESP32 -IPAddress stationIP = IPAddress(192, 168, 2, 232); + // Use DHCP + #warning Using DHCP IP + IPAddress stationIP = IPAddress(0, 0, 0, 0); + IPAddress gatewayIP = IPAddress(192, 168, 2, 1); + IPAddress netMask = IPAddress(255, 255, 255, 0); #else -IPAddress stationIP = IPAddress(192, 168, 2, 186); -#endif + // Use static IP + #warning Using static IP + #ifdef ESP32 + IPAddress stationIP = IPAddress(192, 168, 2, 232); + #else + IPAddress stationIP = IPAddress(192, 168, 2, 186); + #endif -IPAddress gatewayIP = IPAddress(192, 168, 2, 1); -IPAddress netMask = IPAddress(255, 255, 255, 0); + IPAddress gatewayIP = IPAddress(192, 168, 2, 1); + IPAddress netMask = IPAddress(255, 255, 255, 0); #endif #define USE_CONFIGURABLE_DNS true @@ -319,25 +319,25 @@ uint8_t connectMultiWiFi(void); /////////////////////////////////////////// // New in v1.4.0 /****************************************** - * // Defined in ESP_WiFiManager.h -typedef struct -{ + // Defined in ESP_WiFiManager.h + typedef struct + { IPAddress _ap_static_ip; IPAddress _ap_static_gw; IPAddress _ap_static_sn; -} WiFi_AP_IPConfig; + } WiFi_AP_IPConfig; -typedef struct -{ + typedef struct + { IPAddress _sta_static_ip; IPAddress _sta_static_gw; IPAddress _sta_static_sn; -#if USE_CONFIGURABLE_DNS + #if USE_CONFIGURABLE_DNS IPAddress _sta_static_dns1; IPAddress _sta_static_dns2; -#endif -} WiFi_STA_IPConfig; + #endif + } WiFi_STA_IPConfig; ******************************************/ WiFi_AP_IPConfig WM_AP_IPconfig; @@ -355,7 +355,7 @@ void initSTAIPConfigStruct(WiFi_STA_IPConfig &in_WM_STA_IPconfig) in_WM_STA_IPconfig._sta_static_ip = stationIP; in_WM_STA_IPconfig._sta_static_gw = gatewayIP; in_WM_STA_IPconfig._sta_static_sn = netMask; -#if USE_CONFIGURABLE_DNS +#if USE_CONFIGURABLE_DNS in_WM_STA_IPconfig._sta_static_dns1 = dns1IP; in_WM_STA_IPconfig._sta_static_dns2 = dns2IP; #endif @@ -372,13 +372,14 @@ void displayIPConfigStruct(WiFi_STA_IPConfig in_WM_STA_IPconfig) void configWiFi(WiFi_STA_IPConfig in_WM_STA_IPconfig) { - #if USE_CONFIGURABLE_DNS - // Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5 - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2); - #else - // Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2. - WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn); - #endif +#if USE_CONFIGURABLE_DNS + // Set static IP, Gateway, Subnetmask, DNS1 and DNS2. New in v1.0.5 + WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn, + in_WM_STA_IPconfig._sta_static_dns1, in_WM_STA_IPconfig._sta_static_dns2); +#else + // Set static IP, Gateway, Subnetmask, Use auto DNS1 and DNS2. + WiFi.config(in_WM_STA_IPconfig._sta_static_ip, in_WM_STA_IPconfig._sta_static_gw, in_WM_STA_IPconfig._sta_static_sn); +#endif } /////////////////////////////////////////// @@ -387,18 +388,18 @@ uint8_t connectMultiWiFi() { #if ESP32 // For ESP32, this better be 0 to shorten the connect time - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 0 +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 0 #else // For ESP8266, this better be 2200 to enable connect the 1st time - #define WIFI_MULTI_1ST_CONNECT_WAITING_MS 2200L +#define WIFI_MULTI_1ST_CONNECT_WAITING_MS 2200L #endif #define WIFI_MULTI_CONNECT_WAITING_MS 100L - + uint8_t status; LOGERROR(F("ConnectMultiWiFi with :")); - + if ( (Router_SSID != "") && (Router_Pass != "") ) { LOGERROR3(F("* Flash-stored Router_SSID = "), Router_SSID, F(", Router_Pass = "), Router_Pass ); @@ -407,12 +408,13 @@ uint8_t connectMultiWiFi() for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) { // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") + && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) { LOGERROR3(F("* Additional SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); } } - + LOGERROR(F("Connecting MultiWifi...")); WiFi.mode(WIFI_STA); @@ -515,7 +517,7 @@ void loadConfigData() // New in v1.4.0 memset(&WM_STA_IPconfig, 0, sizeof(WM_STA_IPconfig)); ////// - + if (file) { file.readBytes((char *) &WM_config, sizeof(WM_config)); @@ -523,7 +525,7 @@ void loadConfigData() // New in v1.4.0 file.readBytes((char *) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); ////// - + file.close(); LOGERROR(F("OK")); @@ -536,7 +538,7 @@ void loadConfigData() LOGERROR(F("failed")); } } - + void saveConfigData() { File file = FileFS.open(CONFIG_FILENAME, "w"); @@ -549,7 +551,7 @@ void saveConfigData() // New in v1.4.0 file.write((uint8_t*) &WM_STA_IPconfig, sizeof(WM_STA_IPconfig)); ////// - + file.close(); LOGERROR(F("OK")); } @@ -566,6 +568,7 @@ void setup() pinMode(PIN_LED, OUTPUT); Serial.begin(115200); + while (!Serial); delay(200); @@ -582,6 +585,7 @@ void setup() // Format FileFS if not yet #ifdef ESP32 + if (!FileFS.begin(true)) #else if (!FileFS.begin()) @@ -622,16 +626,16 @@ void setup() ESP_wifiManager.setConfigPortalChannel(0); ////// -#if !USE_DHCP_IP - // Set (static IP, Gateway, Subnetmask, DNS1 and DNS2) or (IP, Gateway, Subnetmask). New in v1.0.5 - // New in v1.4.0 - ESP_wifiManager.setSTAStaticIPConfig(WM_STA_IPconfig); - ////// +#if !USE_DHCP_IP + // Set (static IP, Gateway, Subnetmask, DNS1 and DNS2) or (IP, Gateway, Subnetmask). New in v1.0.5 + // New in v1.4.0 + ESP_wifiManager.setSTAStaticIPConfig(WM_STA_IPconfig); + ////// #endif // New from v1.1.1 #if USING_CORS_FEATURE - ESP_wifiManager.setCORSHeader("Your Access-Control-Allow-Origin"); + ESP_wifiManager.setCORSHeader("Your Access-Control-Allow-Origin"); #endif // We can't use WiFi.SSID() in ESP32 as it's only valid after connected. @@ -707,7 +711,8 @@ void setup() strncpy(WM_config.WiFi_Creds[i].wifi_pw, tempPW.c_str(), sizeof(WM_config.WiFi_Creds[i].wifi_pw) - 1); // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") + && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) { LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); @@ -718,7 +723,7 @@ void setup() ESP_wifiManager.getSTAStaticIPConfig(WM_STA_IPconfig); displayIPConfigStruct(WM_STA_IPconfig); ////// - + saveConfigData(); } @@ -734,7 +739,8 @@ void setup() for (uint8_t i = 0; i < NUM_WIFI_CREDENTIALS; i++) { // Don't permit NULL SSID and password len < MIN_AP_PASSWORD_SIZE (8) - if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) + if ( (String(WM_config.WiFi_Creds[i].wifi_ssid) != "") + && (strlen(WM_config.WiFi_Creds[i].wifi_pw) >= MIN_AP_PASSWORD_SIZE) ) { LOGERROR3(F("* Add SSID = "), WM_config.WiFi_Creds[i].wifi_ssid, F(", PW = "), WM_config.WiFi_Creds[i].wifi_pw ); wifiMulti.addAP(WM_config.WiFi_Creds[i].wifi_ssid, WM_config.WiFi_Creds[i].wifi_pw); diff --git a/examples/checkWaitingMRD/checkWaitingMRD.ino b/examples/checkWaitingMRD/checkWaitingMRD.ino index a8e63ec..7aec248 100644 --- a/examples/checkWaitingMRD/checkWaitingMRD.ino +++ b/examples/checkWaitingMRD/checkWaitingMRD.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** 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 *****************************************************************************************************************************/ @@ -39,7 +39,7 @@ // Number of subsequent resets during MRD_TIMEOUT to activate #define MRD_TIMES 3 -// Number of seconds after reset during which a +// Number of seconds after reset during which a // subsequent reset will be considered a multi reset. #define MRD_TIMEOUT 10 @@ -89,13 +89,13 @@ void check_status() { digitalWrite(LED_BUILTIN, LEDState); - LEDState = !LEDState; + LEDState = !LEDState; } else { digitalWrite(LED_BUILTIN, LED_OFF); } - + checkstatus_timeout = current_millis + MRD_CHECK_INTERVAL; } } @@ -103,13 +103,15 @@ void check_status() void setup() { pinMode(LED_BUILTIN, OUTPUT); - + Serial.begin(115200); + while (!Serial); delay(200); - Serial.print("\nStarting checkWaitingMRD on"); Serial.println(ARDUINO_BOARD); + Serial.print("\nStarting checkWaitingMRD on"); + Serial.println(ARDUINO_BOARD); #if ESP_MRD_USE_LITTLEFS Serial.println(F(" using LittleFS")); @@ -118,18 +120,18 @@ void setup() #else Serial.println(F(" using EEPROM")); #endif - + Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); - + mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS); - if (mrd->detectMultiReset()) + if (mrd->detectMultiReset()) { Serial.println("Multi Reset Detected"); digitalWrite(LED_BUILTIN, LED_ON); MRD_Detected = true; - } - else + } + else { Serial.println("No Multi Reset Detected"); digitalWrite(LED_BUILTIN, LED_OFF); diff --git a/examples/minimal/minimal.ino b/examples/minimal/minimal.ino index 0071d20..7c7efea 100644 --- a/examples/minimal/minimal.ino +++ b/examples/minimal/minimal.ino @@ -1,14 +1,14 @@ /**************************************************************************************************************************** minimal.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 *****************************************************************************************************************************/ @@ -34,7 +34,7 @@ // Number of subsequent resets during MRD_TIMEOUT to activate #define MRD_TIMES 5 -// Number of seconds after reset during which a +// Number of seconds after reset during which a // subsequent reset will be considered a multi reset. #define MRD_TIMEOUT 10 @@ -66,12 +66,15 @@ MultiResetDetector* mrd; void setup() { pinMode(LED_BUILTIN, OUTPUT); - + Serial.begin(115200); - while (!Serial); + + while (!Serial); + delay(200); - - Serial.print(F("\nStarting ESP_MultiResetDetector minimal on ")); Serial.print(ARDUINO_BOARD); + + Serial.print(F("\nStarting ESP_MultiResetDetector minimal on ")); + Serial.print(ARDUINO_BOARD); #if ESP_MRD_USE_LITTLEFS Serial.println(F(" using LittleFS")); @@ -80,17 +83,17 @@ void setup() #else Serial.println(F(" using EEPROM")); #endif - + Serial.println(ESP_MULTI_RESET_DETECTOR_VERSION); - + mrd = new MultiResetDetector(MRD_TIMEOUT, MRD_ADDRESS); - if (mrd->detectMultiReset()) + if (mrd->detectMultiReset()) { Serial.println("Multi Reset Detected"); digitalWrite(LED_BUILTIN, LED_ON); - } - else + } + else { Serial.println("No Multi Reset Detected"); digitalWrite(LED_BUILTIN, LED_OFF); diff --git a/library.json b/library.json index 4a2e264..38cc64a 100644 --- a/library.json +++ b/library.json @@ -25,6 +25,7 @@ "dependencies": [ ], + "license": "MIT", "frameworks": "arduino", "platforms": [ "espressif32", "espressif8266" ], "examples": "examples/*/*/*.ino", diff --git a/src/ESP_MultiResetDetector.h b/src/ESP_MultiResetDetector.h index 56b659a..d5358f4 100644 --- a/src/ESP_MultiResetDetector.h +++ b/src/ESP_MultiResetDetector.h @@ -41,7 +41,7 @@ #ifndef ESP_MULTI_RESET_DETECTOR_VERSION #define ESP_MULTI_RESET_DETECTOR_VERSION "ESP_MultiResetDetector v1.3.2" - + #define ESP_MULTI_RESET_DETECTOR_VERSION_MAJOR 1 #define ESP_MULTI_RESET_DETECTOR_VERSION_MINOR 3 #define ESP_MULTI_RESET_DETECTOR_VERSION_PATCH 2 @@ -74,7 +74,7 @@ #if (MULTIRESETDETECTOR_DEBUG) #warning Neither RTC, EEPROM, LITTLEFS nor SPIFFS selected. Default to EEPROM #endif - + #ifdef ESP_MRD_USE_EEPROM #undef ESP_MRD_USE_EEPROM #define ESP_MRD_USE_EEPROM true @@ -98,53 +98,53 @@ #elif ( ESP_MRD_USE_LITTLEFS || ESP_MRD_USE_SPIFFS ) -#include + #include -#ifdef ESP32 + #ifdef ESP32 - #if ESP_MRD_USE_LITTLEFS - // Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h - //#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2) - #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) - #if (MULTIRESETDETECTOR_DEBUG) - #warning Using ESP32 Core 1.0.6 or 2.0.0+ + #if ESP_MRD_USE_LITTLEFS + // Check cores/esp32/esp_arduino_version.h and cores/esp32/core_version.h + //#if ( ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(2, 0, 0) ) //(ESP_ARDUINO_VERSION_MAJOR >= 2) + #if ( defined(ESP_ARDUINO_VERSION_MAJOR) && (ESP_ARDUINO_VERSION_MAJOR >= 2) ) + #if (MULTIRESETDETECTOR_DEBUG) + #warning Using ESP32 Core 1.0.6 or 2.0.0+ + #endif + + // The library has been merged into esp32 core from release 1.0.6 + #include + + #define FileFS LittleFS + #define FS_Name "LittleFS" + #else + #if (MULTIRESETDETECTOR_DEBUG) + #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library + #endif + + // The library has been merged into esp32 core from release 1.0.6 + #include // https://github.com/lorol/LITTLEFS + + #define FileFS LITTLEFS + #define FS_Name "LittleFS" #endif - - // The library has been merged into esp32 core from release 1.0.6 - #include - - #define FileFS LittleFS - #define FS_Name "LittleFS" #else - #if (MULTIRESETDETECTOR_DEBUG) - #warning Using ESP32 Core 1.0.5-. You must install LITTLEFS library - #endif - - // The library has been merged into esp32 core from release 1.0.6 - #include // https://github.com/lorol/LITTLEFS - - #define FileFS LITTLEFS - #define FS_Name "LittleFS" + #include "SPIFFS.h" + // ESP32 core 1.0.4 still uses SPIFFS + #define FileFS SPIFFS #endif - #else - #include "SPIFFS.h" - // ESP32 core 1.0.4 still uses SPIFFS - #define FileFS SPIFFS - #endif -#else - // From ESP8266 core 2.7.1 - #include - - #if ESP_MRD_USE_LITTLEFS - #define FileFS LittleFS #else - #define FileFS SPIFFS - #endif + // From ESP8266 core 2.7.1 + #include + + #if ESP_MRD_USE_LITTLEFS + #define FileFS LittleFS + #else + #define FileFS SPIFFS + #endif -#endif // #if ESP_MRD_USE_EEPROM + #endif // #if ESP_MRD_USE_EEPROM -#define MRD_FILENAME "/mrd.dat" + #define MRD_FILENAME "/mrd.dat" #endif //#if ESP_MRD_USE_EEPROM @@ -167,7 +167,7 @@ // Flag clear to 0xFFFE0001 if no MRD within MRD_TIMEOUT. Flag will increase 1 for each reset within MRD_TIMEOUT // So MULTIRESETDETECTOR_FLAG_SET is not necessary. -// Will use upper 2 bytes to verify if corrupted data. +// Will use upper 2 bytes to verify if corrupted data. #define USING_INVERTED true @@ -185,7 +185,7 @@ class MultiResetDetector MultiResetDetector(int timeout, int address) { mrd_times = MRD_TIMES; - + #if ESP_MRD_USE_EEPROM #if (MULTIRESETDETECTOR_DEBUG) Serial.printf("EEPROM size = %d, start = %d\n", EEPROM_SIZE, EEPROM_START); @@ -194,11 +194,12 @@ class MultiResetDetector EEPROM.begin(EEPROM_SIZE); #elif ( ESP_MRD_USE_LITTLEFS || ESP_MRD_USE_SPIFFS ) // LittleFS / SPIFFS code. Format FileFS if not yet - #ifdef ESP32 +#ifdef ESP32 + if (!FileFS.begin(true)) - #else +#else if (!FileFS.begin()) - #endif +#endif { #if (MULTIRESETDETECTOR_DEBUG) @@ -211,6 +212,7 @@ class MultiResetDetector #endif multiResetDetectorFlag = MULTIRESETDETECTOR_FLAG = 0; } + #else #ifdef ESP8266 //RTC only for ESP8266 @@ -248,7 +250,7 @@ class MultiResetDetector return multiResetDetected; }; - + bool waitingForMRD() { return waitingForMultiReset; @@ -276,28 +278,29 @@ class MultiResetDetector private: - + uint32_t MULTIRESETDETECTOR_FLAG; - + unsigned long mrd_times; - + unsigned long timeout; - + int address; bool waitingForMultiReset; - + uint32_t multiResetDetectorFlag; - + bool readRecentlyResetFlag() { #if (ESP_MRD_USE_EEPROM) EEPROM.get(EEPROM_START, MULTIRESETDETECTOR_FLAG); multiResetDetectorFlag = MULTIRESETDETECTOR_FLAG; - #if (MULTIRESETDETECTOR_DEBUG) +#if (MULTIRESETDETECTOR_DEBUG) Serial.printf("EEPROM Flag read = 0x%08X\n", MULTIRESETDETECTOR_FLAG); - #endif +#endif #elif ( ESP_MRD_USE_LITTLEFS || ESP_MRD_USE_SPIFFS ) + // LittleFS / SPIFFS code if (FileFS.exists(MRD_FILENAME)) { @@ -306,32 +309,33 @@ class MultiResetDetector if (!file) { - #if (MULTIRESETDETECTOR_DEBUG) +#if (MULTIRESETDETECTOR_DEBUG) Serial.println(F("Loading config file failed")); - #endif +#endif return false; } file.readBytes((char *) &MULTIRESETDETECTOR_FLAG, sizeof(MULTIRESETDETECTOR_FLAG)); multiResetDetectorFlag = MULTIRESETDETECTOR_FLAG; - #if (MULTIRESETDETECTOR_DEBUG) +#if (MULTIRESETDETECTOR_DEBUG) - #if ESP_MRD_USE_LITTLEFS +#if ESP_MRD_USE_LITTLEFS Serial.printf("LittleFS Flag read = 0x%08X\n", MULTIRESETDETECTOR_FLAG); - #else +#else Serial.printf("SPIFFS Flag read = 0x%08X\n", MULTIRESETDETECTOR_FLAG); - #endif +#endif - #endif +#endif file.close(); } + #else - #ifdef ESP8266 +#ifdef ESP8266 //RTC only for ESP8266 ESP.rtcUserMemoryRead(address, &multiResetDetectorFlag, sizeof(multiResetDetectorFlag)); - #endif +#endif #endif return true; @@ -344,29 +348,29 @@ class MultiResetDetector //multiResetDetected = (multiResetDetectorFlag == MULTIRESETDETECTOR_FLAG_SET); // Check lower 2 bytes is > 0 and upper 2 bytes agrees - -#if USING_INVERTED + +#if USING_INVERTED uint16_t upperBytes = ~(multiResetDetectorFlag >> 16); #else uint16_t upperBytes = multiResetDetectorFlag >> 16; #endif - + uint16_t lowerBytes = multiResetDetectorFlag & 0x0000FFFF; - + #if (MULTIRESETDETECTOR_DEBUG) Serial.printf("multiResetDetectorFlag = 0x%08X\n", multiResetDetectorFlag); Serial.printf("lowerBytes = 0x%04X, upperBytes = 0x%04X\n", lowerBytes, upperBytes); -#endif - - if ( ( lowerBytes >= MRD_TIMES ) && ( lowerBytes == upperBytes ) ) +#endif + + if ( ( lowerBytes >= MRD_TIMES ) && ( lowerBytes == upperBytes ) ) { multiResetDetected = true; } else { multiResetDetected = false; - - if (lowerBytes != upperBytes) + + if (lowerBytes != upperBytes) { // To reset if data corrupted multiResetDetectorFlag = MULTIRESETDETECTOR_FLAG_CLEAR; @@ -376,7 +380,7 @@ class MultiResetDetector #endif } } - + return multiResetDetected; }; @@ -385,24 +389,24 @@ class MultiResetDetector // Add 1 every time detecting a reset // To read first, increase and update 2 checking bytes readRecentlyResetFlag(); - + #if USING_INVERTED // 2 lower bytes MULTIRESETDETECTOR_FLAG = (MULTIRESETDETECTOR_FLAG & 0x0000FFFF) + 1; - + // 2 upper bytes uint16_t upperBytes = ~MULTIRESETDETECTOR_FLAG; MULTIRESETDETECTOR_FLAG = (upperBytes << 16) | MULTIRESETDETECTOR_FLAG; -#else +#else // 2 lower bytes MULTIRESETDETECTOR_FLAG = (MULTIRESETDETECTOR_FLAG & 0x0000FFFF) + 1; - + // 2 upper bytes MULTIRESETDETECTOR_FLAG = (MULTIRESETDETECTOR_FLAG << 16) | MULTIRESETDETECTOR_FLAG; #endif multiResetDetectorFlag = MULTIRESETDETECTOR_FLAG; - + #if (ESP_MRD_USE_EEPROM) EEPROM.put(EEPROM_START, MULTIRESETDETECTOR_FLAG); EEPROM.commit(); @@ -434,6 +438,7 @@ class MultiResetDetector Serial.println(F("Saving config file failed")); #endif } + #else #ifdef ESP8266 //RTC only for ESP8266 diff --git a/utils/astyle_library.conf b/utils/astyle_library.conf new file mode 100644 index 0000000..8a73bc2 --- /dev/null +++ b/utils/astyle_library.conf @@ -0,0 +1,70 @@ +# Code formatting rules for Arduino libraries, modified from for KH libraries: +# +# https://github.com/arduino/Arduino/blob/master/build/shared/examples_formatter.conf +# + +# astyle --style=allman -s2 -t2 -C -S -xW -Y -M120 -f -p -xg -H -xb -c --xC120 -xL *.h *.cpp *.ino + +--mode=c +--lineend=linux +--style=allman + +# -r or -R +#--recursive + +# -c => Converts tabs into spaces +convert-tabs + +# -s2 => 2 spaces indentation +--indent=spaces=2 + +# -t2 => tab =2 spaces +#--indent=tab=2 + +# -C +--indent-classes + +# -S +--indent-switches + +# -xW +--indent-preproc-block + +# -Y => indent classes, switches (and cases), comments starting at column 1 +--indent-col1-comments + +# -M120 => maximum of 120 spaces to indent a continuation line +--max-continuation-indent=120 + +# -xC120 => max‑code‑length will break a line if the code exceeds # characters +--max-code-length=120 + +# -f => +--break-blocks + +# -p => put a space around operators +--pad-oper + +# -xg => Insert space padding after commas +--pad-comma + +# -H => put a space after if/for/while +pad-header + +# -xb => Break one line headers (e.g. if/for/while) +--break-one-line-headers + +# -c => Converts tabs into spaces +#--convert-tabs + +# if you like one-liners, keep them +#keep-one-line-statements + +# -xV +--attach-closing-while + +#unpad-paren + +# -xp +remove-comment-prefix + diff --git a/utils/restyle.sh b/utils/restyle.sh new file mode 100644 index 0000000..bcd846f --- /dev/null +++ b/utils/restyle.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for dir in . ; do + find $dir -type f \( -name "*.c" -o -name "*.h" -o -name "*.cpp" -o -name "*.ino" \) -exec astyle --suffix=none --options=./utils/astyle_library.conf \{\} \; +done +