| @@ -17,7 +17,7 @@ enum Commands { | |||
| CMD_RESET_WIFI_CFG | |||
| }; | |||
| const char* cmdToString(Commands cmd) { | |||
| static const char* cmdToString(Commands cmd) { | |||
| switch (cmd) { | |||
| case CMD_CHG_MODE: | |||
| return "CHG_MODE"; | |||
| @@ -34,7 +34,7 @@ const char* cmdToString(Commands cmd) { | |||
| } | |||
| } | |||
| Commands strToCommand(const char *cmd) { | |||
| static Commands strToCommand(const char *cmd) { | |||
| if (cmd == nullptr) { | |||
| return CMD_UNKNOWN; | |||
| } | |||
| @@ -47,6 +47,30 @@ void ConfigManager::resetConfig() { | |||
| EEPROM.commit(); | |||
| } | |||
| Config& ConfigManager::setToDefaultConfig() { | |||
| memset(&_config, 0, sizeof(Config)); | |||
| String ssid = ""; | |||
| String pass = ""; | |||
| ssid.toCharArray(_config.ssid, sizeof(_config.ssid)); | |||
| pass.toCharArray(_config.pass, sizeof(_config.pass)); | |||
| _config.wifiValid = false; | |||
| _config.version = CONFIG_VERSION; | |||
| _config.mode = LM_SINGLE_COLOR; | |||
| _config.color.r = 0; | |||
| _config.color.g = 0; | |||
| _config.color.b = 0; | |||
| _config.color.w = 255; | |||
| _config.brightness = 128; | |||
| EEPROM.begin(sizeof(Config)); | |||
| EEPROM.put(0, _config); | |||
| EEPROM.commit(); | |||
| return _config; | |||
| } | |||
| Config& ConfigManager::getConfig() { | |||
| return _config; | |||
| } | |||
| @@ -10,12 +10,27 @@ | |||
| #include <Arduino.h> | |||
| #include <EEPROM.h> | |||
| #include "LEDModes.h" | |||
| #define CONFIG_VERSION 1 | |||
| typedef struct __attribute__((packed)) { | |||
| uint8_t r; | |||
| uint8_t g; | |||
| uint8_t b; | |||
| uint8_t w; | |||
| } RGBWColor; | |||
| typedef struct __attribute__((packed)) { | |||
| uint8_t version; | |||
| struct Config { | |||
| char ssid[32]; | |||
| char pass[64]; | |||
| bool wifiValid; | |||
| }; | |||
| LEDModes mode; | |||
| uint8_t brightness; | |||
| RGBWColor color; | |||
| } Config; | |||
| class ConfigManager { | |||
| @@ -26,12 +41,12 @@ class ConfigManager { | |||
| bool loadConfig(); | |||
| bool saveConfig(const Config &config); | |||
| void resetConfig(); | |||
| Config& setToDefaultConfig(); | |||
| Config& getConfig(); | |||
| private: | |||
| Config _config; | |||
| }; | |||
| #endif /* CONFIGMANAGER_H_ */ | |||
| @@ -17,13 +17,6 @@ AppState state = STATE_BOOT; | |||
| unsigned long connectStart = 0; | |||
| const byte DNS_PORT = 53; | |||
| // WLAN Config | |||
| //struct Config { | |||
| // char ssid[32]; | |||
| // char pass[64]; | |||
| // bool valid; | |||
| //}; | |||
| // AP Daten | |||
| const char *ap_ssid = "Wemos_Setup"; | |||
| const char *ap_pass = "admin123"; | |||
| @@ -31,9 +24,8 @@ const char *ap_pass = "admin123"; | |||
| ESP8266WebServer webServer(80); | |||
| WebSocketsServer webSocket(81); | |||
| DNSServer dnsServer; | |||
| LEDStripManager ledMngr(LED_COUNT, LED_STRIP_DATA_PIN); | |||
| ConfigManager configManager; | |||
| LEDStripManager ledMngr(LED_COUNT, LED_STRIP_DATA_PIN, configManager); | |||
| // ---------- CAPTIVE PORTAL PAGE ---------- | |||
| void handleSetupPage() { | |||
| @@ -51,15 +43,6 @@ void handleSTAControlPage() { | |||
| // ---------- SAVE ---------- | |||
| void handleSave() { | |||
| // String ssid = webServer.arg("ssid"); | |||
| // String pass = webServer.arg("pass"); | |||
| // | |||
| // ssid.toCharArray(config.ssid, 32); | |||
| // pass.toCharArray(config.pass, 64); | |||
| // config.valid = true; | |||
| // | |||
| // saveConfig(); | |||
| Config cfg; | |||
| String ssid = webServer.arg("ssid"); | |||
| @@ -67,9 +50,14 @@ void handleSave() { | |||
| ssid.toCharArray(cfg.ssid, sizeof(cfg.ssid)); | |||
| pass.toCharArray(cfg.pass, sizeof(cfg.pass)); | |||
| cfg.wifiValid = true; | |||
| cfg.version = CONFIG_VERSION; | |||
| cfg.mode = LM_SINGLE_COLOR; | |||
| cfg.color.r = 0; | |||
| cfg.color.g = 0; | |||
| cfg.color.b = 0; | |||
| cfg.color.w = 255; | |||
| cfg.brightness = 128; | |||
| configManager.saveConfig(cfg); | |||
| webServer.send(200, "text/html", getConnectionSuccessPage()); | |||
| @@ -156,6 +144,11 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) | |||
| } | |||
| } | |||
| } else if (strToCommand(cmd) == CMD_CHG_MODE) { | |||
| const char *value = doc["val"]; | |||
| LEDModes mode = parseMode(value); | |||
| ledMngr.onModeChange(mode); | |||
| } else if (strToCommand(cmd) == CMD_RESET_WIFI_CFG) { | |||
| resetWifiConfiguration(num); | |||
| @@ -174,10 +167,6 @@ void webSocketEvent(uint8_t num, WStype_t type, uint8_t *payload, size_t length) | |||
| } | |||
| void resetWifiConfiguration(uint8_t num) { | |||
| // EEPROM.begin(sizeof(Config)); | |||
| // config.valid = false; | |||
| // EEPROM.put(0, config); | |||
| Config cfg = configManager.getConfig(); | |||
| String ssid = ""; | |||
| String pass = ""; | |||
| @@ -271,9 +260,9 @@ void startSTAWebServer() { | |||
| // ---------- SETUP ---------- | |||
| void setup() { | |||
| Serial.begin(115200); | |||
| ledMngr.begin(); | |||
| configManager.loadConfig(); | |||
| ledMngr.begin(); | |||
| setState(STATE_BOOT); | |||
| connectStart = millis(); | |||
| @@ -0,0 +1,39 @@ | |||
| /* | |||
| * LEDModes.h | |||
| * | |||
| * Created on: 23.06.2026 | |||
| * Author: FSmilari | |||
| */ | |||
| #ifndef LEDMODES_H_ | |||
| #define LEDMODES_H_ | |||
| #include <string.h> | |||
| enum LEDModes { | |||
| LM_SINGLE_COLOR = 0, | |||
| LM_RAINBOW = 1, | |||
| LM_SPECTRAL = 2, | |||
| LM_FADING = 3, | |||
| LM_FLASH = 4, | |||
| LM_SEGMENTS = 5 | |||
| }; | |||
| static LEDModes parseMode(const char *mode) { | |||
| if (strcmp(mode, "LM_SINGLE_COLOR") == 0) | |||
| return LM_SINGLE_COLOR; | |||
| if (strcmp(mode, "LM_RAINBOW") == 0) | |||
| return LM_RAINBOW; | |||
| if (strcmp(mode, "LM_SPECTRAL") == 0) | |||
| return LM_SPECTRAL; | |||
| if (strcmp(mode, "LM_FADING") == 0) | |||
| return LM_FADING; | |||
| if (strcmp(mode, "LM_FLASH") == 0) | |||
| return LM_FLASH; | |||
| if (strcmp(mode, "LM_SEGMENTS") == 0) | |||
| return LM_SEGMENTS; | |||
| return LM_SINGLE_COLOR; // Default fallback | |||
| } | |||
| #endif /* LEDMODES_H_ */ | |||
| @@ -10,7 +10,8 @@ | |||
| /* | |||
| * Constructor | |||
| */ | |||
| LEDStripManager::LEDStripManager(uint16_t numLeds, uint8_t pin) : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800) { | |||
| LEDStripManager::LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager) | |||
| : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800), configManager(configManager) { | |||
| } | |||
| @@ -27,10 +28,17 @@ void LEDStripManager::begin() { | |||
| digitalWrite(LED_STRIP_POWER_PIN, HIGH); // LED zunächst EIN | |||
| Config cfg = configManager.getConfig(); | |||
| if (cfg.version != CONFIG_VERSION) { | |||
| // Initalize cfg with defaults | |||
| cfg = configManager.setToDefaultConfig(); | |||
| } | |||
| strip.begin(); // Initialisiert den NeoPixel-Streifen | |||
| strip.clear(); | |||
| strip.show(); // Setzt alle LEDs auf "aus" | |||
| strip.setBrightness(255); // Helligkeit (0 bis 255) | |||
| strip.setBrightness(cfg.brightness); // Helligkeit (0 bis 255) | |||
| // Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss) | |||
| strip.setPixelColor(0, strip.Color(255, 0, 0, 0)); | |||
| @@ -96,6 +104,15 @@ void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue) { | |||
| strip.show(); | |||
| } | |||
| void LEDStripManager::onModeChange(LEDModes mode) { | |||
| Config cfg = configManager.getConfig(); | |||
| cfg.mode = mode; | |||
| configManager.saveConfig(cfg); | |||
| //TODO: react on mode change on strip | |||
| } | |||
| uint32_t | |||
| LEDStripManager::wheel(uint8_t pos) { | |||
| pos = 255 - pos; | |||
| @@ -3,6 +3,8 @@ | |||
| #include "Arduino.h" | |||
| #include <Adafruit_NeoPixel.h> | |||
| #include "ConfigManager.h" | |||
| #include "LEDModes.h" | |||
| const int LED_STRIP_POWER_PIN = D2; // The pin to switch on / off the LED stripe | |||
| const int LED_STRIP_DATA_PIN = D8; // The pin to switch control the LED stripe | |||
| @@ -10,7 +12,7 @@ const int LED_STRIP_DATA_PIN = D8; // The pin to switch control the LED stripe | |||
| class LEDStripManager { | |||
| public: | |||
| LEDStripManager(uint16_t numLeds, uint8_t pin); | |||
| LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager); | |||
| virtual ~LEDStripManager(); | |||
| @@ -32,9 +34,13 @@ class LEDStripManager { | |||
| void rainbow(uint8_t wait); | |||
| void onModeChange(LEDModes mode); | |||
| private: | |||
| Adafruit_NeoPixel strip; | |||
| ConfigManager &configManager; | |||
| uint32_t wheel(uint8_t pos); | |||
| }; | |||
| @@ -2,7 +2,7 @@ | |||
| //This is a automatic generated file | |||
| //Please do not modify this file | |||
| //If you touch this file your change will be overwritten during the next build | |||
| //This file has been generated on 2026-06-21 13:25:01 | |||
| //This file has been generated on 2026-06-23 22:04:18 | |||
| #include "Arduino.h" | |||
| #include "Arduino.h" | |||