From 799f21ab560b0da34877673e482792f6744fd6bb Mon Sep 17 00:00:00 2001 From: Fisch Date: Mon, 25 Jul 2022 12:53:27 +0200 Subject: [PATCH] move ota stuff into better functions --- src/main.cpp | 69 +++++++++++++++++++++++------------------------ src/simpleota.cpp | 1 + src/simpleota.h | 4 +++ 3 files changed, 39 insertions(+), 35 deletions(-) create mode 100644 src/simpleota.cpp create mode 100644 src/simpleota.h diff --git a/src/main.cpp b/src/main.cpp index 76bbe95..b3f1576 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,12 @@ #include #include #include +#include "simpleota.h" #define OTA_WIFI_SSID "Chaos-West temp alternative" #define OTA_WIFI_PASSWORD "" #define OTA_WAIT_TIMEOUT 1000 // in 0.1s increments -> 10s -void normal_loop(); void normal_setup(); void checkOTA(); bool initOTA(); @@ -34,6 +34,7 @@ void checkOTA() { while (WiFi.waitForConnectResult() != WL_CONNECTED) { Serial.println("no OTA WiFi found, proceed normal boot"); otaMode = NONE; + WiFi.disconnect(); return; } @@ -41,28 +42,22 @@ void checkOTA() { } void normal_setup() { - Serial.println("Booting normal"); - - WiFi.disconnect(); - WiFi.mode(WIFI_STA); - WiFi.disconnect(); - Serial.printf("wifi channel: %d\n", WiFi.channel()); + } void setup() { Serial.begin(115200); - delay(1000); - Serial.println(); - Serial.println("Initializing..."); - + Serial.println("Initializing"); checkOTA(); - if(!initOTA()) { //initialize ota when ota enabled - normal_setup(); + if(initOTA()) { //initialize ota if ota enabled + return; //if ota do nothing else for setup } + Serial.println("Booting normal"); + } bool initOTA() { @@ -108,38 +103,42 @@ bool initOTA() { } -void loop() { +bool ota_loop() { + if (otaMode != NONE) { + ArduinoOTA.handle(); - if (otaMode != NONE) { - ArduinoOTA.handle(); + if(otaMode == WAITING) { + static long mil = millis(); + static boolean huehott = false; - if(otaMode == WAITING) { - static long mil = millis(); - static boolean huehott = false; + if(millis() - mil > 100) { + huehott = !huehott; + mil = millis(); - if(millis() - mil > 100) { - huehott = !huehott; - mil = millis(); - - otaWaitCounter++; - if(otaWaitCounter >= OTA_WAIT_TIMEOUT) { - Serial.println("OTA wait timeout, proceeding normal boot"); - otaMode = NONE; - normal_setup(); - } - } - } + otaWaitCounter++; + if(otaWaitCounter >= OTA_WAIT_TIMEOUT) { + Serial.println("OTA wait timeout, proceeding normal boot"); + otaMode = NONE; + normal_setup(); + } + } + } + return true; } else { - normal_loop(); - + return false; } } -void normal_loop() { +void loop() { + if (ota_loop()) { + return; //just wait for ota + } + static unsigned long last_print=0; if (millis()-last_print>1000) { last_print=millis(); Serial.println("Loop"); } -} \ No newline at end of file + +} diff --git a/src/simpleota.cpp b/src/simpleota.cpp new file mode 100644 index 0000000..56a9e24 --- /dev/null +++ b/src/simpleota.cpp @@ -0,0 +1 @@ +#include "simpleota.h" \ No newline at end of file diff --git a/src/simpleota.h b/src/simpleota.h new file mode 100644 index 0000000..a67b110 --- /dev/null +++ b/src/simpleota.h @@ -0,0 +1,4 @@ +#ifndef simpleota_h +#define simpleota_h + +#endif \ No newline at end of file