Просмотр исходного кода

Enhanced config structure

master
gituser 1 месяц назад
Родитель
Сommit
d657401629
8 измененных файлов: 124 добавлений и 34 удалений
  1. 2
    2
      Commands.h
  2. 24
    0
      ConfigManager.cpp
  3. 18
    3
      ConfigManager.h
  4. 14
    25
      LEDLamp.ino
  5. 39
    0
      LEDModes.h
  6. 19
    2
      LEDStripManager.cpp
  7. 7
    1
      LEDStripManager.h
  8. 1
    1
      sloeber.ino.cpp

+ 2
- 2
Commands.h Просмотреть файл

CMD_RESET_WIFI_CFG CMD_RESET_WIFI_CFG
}; };


const char* cmdToString(Commands cmd) {
static const char* cmdToString(Commands cmd) {
switch (cmd) { switch (cmd) {
case CMD_CHG_MODE: case CMD_CHG_MODE:
return "CHG_MODE"; return "CHG_MODE";
} }
} }


Commands strToCommand(const char *cmd) {
static Commands strToCommand(const char *cmd) {
if (cmd == nullptr) { if (cmd == nullptr) {
return CMD_UNKNOWN; return CMD_UNKNOWN;
} }

+ 24
- 0
ConfigManager.cpp Просмотреть файл

EEPROM.commit(); 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() { Config& ConfigManager::getConfig() {
return _config; return _config;
} }

+ 18
- 3
ConfigManager.h Просмотреть файл



#include <Arduino.h> #include <Arduino.h>
#include <EEPROM.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 ssid[32];
char pass[64]; char pass[64];
bool wifiValid; bool wifiValid;
};
LEDModes mode;
uint8_t brightness;
RGBWColor color;
} Config;


class ConfigManager { class ConfigManager {


bool loadConfig(); bool loadConfig();
bool saveConfig(const Config &config); bool saveConfig(const Config &config);
void resetConfig(); void resetConfig();
Config& setToDefaultConfig();
Config& getConfig(); Config& getConfig();


private: private:
Config _config; Config _config;
}; };



#endif /* CONFIGMANAGER_H_ */ #endif /* CONFIGMANAGER_H_ */



+ 14
- 25
LEDLamp.ino Просмотреть файл

unsigned long connectStart = 0; unsigned long connectStart = 0;
const byte DNS_PORT = 53; const byte DNS_PORT = 53;


// WLAN Config
//struct Config {
// char ssid[32];
// char pass[64];
// bool valid;
//};

// AP Daten // AP Daten
const char *ap_ssid = "Wemos_Setup"; const char *ap_ssid = "Wemos_Setup";
const char *ap_pass = "admin123"; const char *ap_pass = "admin123";
ESP8266WebServer webServer(80); ESP8266WebServer webServer(80);
WebSocketsServer webSocket(81); WebSocketsServer webSocket(81);
DNSServer dnsServer; DNSServer dnsServer;
LEDStripManager ledMngr(LED_COUNT, LED_STRIP_DATA_PIN);
ConfigManager configManager; ConfigManager configManager;
LEDStripManager ledMngr(LED_COUNT, LED_STRIP_DATA_PIN, configManager);


// ---------- CAPTIVE PORTAL PAGE ---------- // ---------- CAPTIVE PORTAL PAGE ----------
void handleSetupPage() { void handleSetupPage() {


// ---------- SAVE ---------- // ---------- SAVE ----------
void handleSave() { 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; Config cfg;


String ssid = webServer.arg("ssid"); String ssid = webServer.arg("ssid");


ssid.toCharArray(cfg.ssid, sizeof(cfg.ssid)); ssid.toCharArray(cfg.ssid, sizeof(cfg.ssid));
pass.toCharArray(cfg.pass, sizeof(cfg.pass)); pass.toCharArray(cfg.pass, sizeof(cfg.pass));

cfg.wifiValid = true; 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); configManager.saveConfig(cfg);


webServer.send(200, "text/html", getConnectionSuccessPage()); webServer.send(200, "text/html", getConnectionSuccessPage());
} }
} }


} 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) { } else if (strToCommand(cmd) == CMD_RESET_WIFI_CFG) {


resetWifiConfiguration(num); resetWifiConfiguration(num);
} }


void resetWifiConfiguration(uint8_t num) { void resetWifiConfiguration(uint8_t num) {
// EEPROM.begin(sizeof(Config));
// config.valid = false;
// EEPROM.put(0, config);

Config cfg = configManager.getConfig(); Config cfg = configManager.getConfig();
String ssid = ""; String ssid = "";
String pass = ""; String pass = "";
// ---------- SETUP ---------- // ---------- SETUP ----------
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
ledMngr.begin();


configManager.loadConfig(); configManager.loadConfig();
ledMngr.begin();


setState(STATE_BOOT); setState(STATE_BOOT);
connectStart = millis(); connectStart = millis();

+ 39
- 0
LEDModes.h Просмотреть файл

/*
* 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_ */

+ 19
- 2
LEDStripManager.cpp Просмотреть файл

/* /*
* Constructor * 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) {


} }




digitalWrite(LED_STRIP_POWER_PIN, HIGH); // LED zunächst EIN 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.begin(); // Initialisiert den NeoPixel-Streifen
strip.clear(); strip.clear();
strip.show(); // Setzt alle LEDs auf "aus" 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) // Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
strip.setPixelColor(0, strip.Color(255, 0, 0, 0)); strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
strip.show(); 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 uint32_t
LEDStripManager::wheel(uint8_t pos) { LEDStripManager::wheel(uint8_t pos) {
pos = 255 - pos; pos = 255 - pos;

+ 7
- 1
LEDStripManager.h Просмотреть файл



#include "Arduino.h" #include "Arduino.h"
#include <Adafruit_NeoPixel.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_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 const int LED_STRIP_DATA_PIN = D8; // The pin to switch control the LED stripe
class LEDStripManager { class LEDStripManager {


public: public:
LEDStripManager(uint16_t numLeds, uint8_t pin);
LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager);


virtual ~LEDStripManager(); virtual ~LEDStripManager();




void rainbow(uint8_t wait); void rainbow(uint8_t wait);


void onModeChange(LEDModes mode);

private: private:
Adafruit_NeoPixel strip; Adafruit_NeoPixel strip;


ConfigManager &configManager;

uint32_t wheel(uint8_t pos); uint32_t wheel(uint8_t pos);
}; };



+ 1
- 1
sloeber.ino.cpp Просмотреть файл

//This is a automatic generated file //This is a automatic generated file
//Please do not modify this file //Please do not modify this file
//If you touch this file your change will be overwritten during the next build //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"
#include "Arduino.h" #include "Arduino.h"

Загрузка…
Отмена
Сохранить