From 0d062d44ad86883be8bc262f5ff983586a171713 Mon Sep 17 00:00:00 2001
From: Ricardo Bartels <ricardo@bitchbrothers.com>
Date: Sat, 2 Jan 2021 23:04:35 +0100
Subject: [PATCH] seperates config management in config.h

---
 config.h                          | 79 ++++++++++++++++++++++++++++++
 esp8266-fastled-iot-webserver.ino | 80 +++----------------------------
 2 files changed, 85 insertions(+), 74 deletions(-)
 create mode 100644 config.h

diff --git a/config.h b/config.h
new file mode 100644
index 0000000..dcb79fd
--- /dev/null
+++ b/config.h
@@ -0,0 +1,79 @@
+
+// define EEPROM settings
+//  https://www.kriwanek.de/index.php/de/homeautomation/esp8266/364-eeprom-für-parameter-verwenden
+
+typedef struct {
+  uint8_t brightness;
+  uint8_t currentPatternIndex;
+  uint8_t red;
+  uint8_t green;
+  uint8_t blue;
+  uint8_t power;
+  uint8_t autoplay;
+  uint8_t autoplayDuration;
+  uint8_t currentPaletteIndex;
+  uint8_t speed;
+  char hostname[33];
+  uint8_t MQTTEnabled;
+  char MQTTHost[65];
+  uint16_t MQTTPort;
+  char MQTTUser[33];
+  char MQTTPass[65];
+  char MQTTTopic[65];
+  char MQTTDeviceName[33];
+} configData_t;
+
+
+configData_t cfg;
+configData_t default_cfg;
+
+// set to true if config has changed
+bool save_config = false;
+
+
+void saveConfig(bool save) {
+    // Save configuration from RAM into EEPROM
+    if (save == true) {
+        EEPROM.begin(4095);
+        EEPROM.put(0, cfg );
+        delay(200);
+        EEPROM.commit();
+        EEPROM.end();
+
+        save_config = false;
+    }
+}
+
+void resetConfig() {
+
+        // delete EEPROM config
+        EEPROM.begin(4095);
+        for (int i = 0 ; i < sizeof(cfg) ; i++) {
+            EEPROM.write(i, 0);
+        }
+        delay(200);
+        EEPROM.commit();
+        EEPROM.end();
+
+        // set to default config
+        cfg = default_cfg;
+        saveConfig(true);  
+}
+
+void setHostname(String new_hostname)
+{
+    int j = 0;
+    for (int i = 0; i < new_hostname.length() && i < sizeof(cfg.hostname); i++) {
+        if (new_hostname.charAt(i) == '-' or \
+           (new_hostname.charAt(i) >= '0' && new_hostname.charAt(i) <= '9') or \
+           (new_hostname.charAt(i) >= 'A' && new_hostname.charAt(i) <= 'Z') or \
+           (new_hostname.charAt(i) >= 'a' && new_hostname.charAt(i) <= 'z')) {
+
+            cfg.hostname[j] = new_hostname.charAt(i);
+            j++;
+        }
+    }
+    cfg.hostname[j] = '\0';
+    save_config = true;
+}
+// EOF
diff --git a/esp8266-fastled-iot-webserver.ino b/esp8266-fastled-iot-webserver.ino
index 9325f59..ff24667 100644
--- a/esp8266-fastled-iot-webserver.ino
+++ b/esp8266-fastled-iot-webserver.ino
@@ -370,35 +370,8 @@ if you have connected the ring first it should look like this: const int twpOffs
     uint8_t incomingPacket[PACKET_LENGTH + 1];
 #endif
 
-// define EEPROM settings
-//  https://www.kriwanek.de/index.php/de/homeautomation/esp8266/364-eeprom-für-parameter-verwenden
-
-typedef struct {
-  uint8_t brightness;
-  uint8_t currentPatternIndex;
-  uint8_t red;
-  uint8_t green;
-  uint8_t blue;
-  uint8_t power;
-  uint8_t autoplay;
-  uint8_t autoplayDuration;
-  uint8_t currentPaletteIndex;
-  uint8_t speed;
-  char hostname[33];
-  uint8_t MQTTEnabled;
-  char MQTTHost[65];
-  uint16_t MQTTPort;
-  char MQTTUser[33];
-  char MQTTPass[65];
-  char MQTTTopic[65];
-  char MQTTDeviceName[33];
-} configData_t;
-
-configData_t cfg;
-configData_t default_cfg;
-
-// set to true if config has changed
-bool save_config = false;
+// include config management
+#include "config.h"
 
 ESP8266WebServer webServer(80);
 
@@ -1095,10 +1068,10 @@ void setup() {
         }
 #endif
         if (force_restart) {
-          saveConfig(true);
-          handleReboot();
+            saveConfig(true);
+            handleReboot();
         } else {
-          webServer.send(200, "text/html", "<html><head><meta http-equiv=\"refresh\" content=\"0; url=/settings.htm\"/></head><body></body>");
+            webServer.send(200, "text/html", "<html><head><meta http-equiv=\"refresh\" content=\"0; url=/settings.htm\"/></head><body></body>");
         }
         });
 
@@ -1106,18 +1079,7 @@ void setup() {
 
         // delete EEPROM settings
         if (webServer.arg("type") == String("all")) {
-            // delete EEPROM config
-            EEPROM.begin(4095);
-            for (int i = 0 ; i < sizeof(cfg) ; i++) {
-                EEPROM.write(i, 0);
-            }
-            delay(200);
-            EEPROM.commit();
-            EEPROM.end();
-
-            // set to default config
-            cfg = default_cfg;
-            saveConfig(true);
+            resetConfig();
         }
 
         // delete wireless config
@@ -1472,19 +1434,6 @@ void loop() {
     }
 }
 
-void saveConfig(bool save) {
-    // Save configuration from RAM into EEPROM
-    if (save == true) {
-        EEPROM.begin(4095);
-        EEPROM.put(0, cfg );
-        delay(200);
-        EEPROM.commit();
-        EEPROM.end();
-
-        save_config = false;
-    }
-}
-
 void loadConfig()
 {
     // Loads configuration from EEPROM into RAM
@@ -1736,23 +1685,6 @@ void setSpeed(uint8_t value)
     broadcastInt("speed", speed);
 }
 
-void setHostname(String new_hostname)
-{
-    int j = 0;
-    for (int i = 0; i < new_hostname.length() && i < sizeof(cfg.hostname); i++) {
-        if (new_hostname.charAt(i) == '-' or \
-           (new_hostname.charAt(i) >= '0' && new_hostname.charAt(i) <= '9') or \
-           (new_hostname.charAt(i) >= 'A' && new_hostname.charAt(i) <= 'Z') or \
-           (new_hostname.charAt(i) >= 'a' && new_hostname.charAt(i) <= 'z')) {
-
-            cfg.hostname[j] = new_hostname.charAt(i);
-            j++;
-        }
-    }
-    cfg.hostname[j] = '\0';
-    save_config = true;
-}
-
 void strandTest()
 {
     static uint8_t i = 0;
-- 
GitLab