| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- /*
- * LEDStripManager.cpp
- *
- * Created on: 19.06.2026
- * Author: FSmilari
- */
-
- #include "LEDStripManager.h"
-
- /*
- * Constructor
- */
- LEDStripManager::LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager, WebSocketsServer &webSocket)
- : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800), configManager(configManager), webSocket(webSocket) {
-
- }
-
- /*
- * Destructor
- */
- LEDStripManager::~LEDStripManager() {
- }
-
- void LEDStripManager::begin() {
-
- pinMode(LED_STRIP_POWER_PIN, OUTPUT);
- pinMode(LED_STRIP_DATA_PIN, OUTPUT);
-
- digitalWrite(LED_STRIP_POWER_PIN, HIGH); // LED zunächst EIN
- digitalWrite(LED_STRIP_DATA_PIN, HIGH);
-
- Config cfg = configManager.getConfig();
-
- if (cfg.version != CONFIG_VERSION) {
- // Initalize cfg with defaults
- cfg = configManager.setToDefaultConfig();
- configManager.loadConfig();
- cfg = configManager.getConfig();
- }
-
- strip.begin(); // Initialisiert den NeoPixel-Streifen
- strip.clear();
- strip.show(); // Setzt alle LEDs auf "aus"
-
- switch (cfg.mode) {
- case LM_SINGLE_COLOR:
- this->setBrightness(cfg.brightness);
- setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
- return;
- case LM_SEGMENTS:
- this->setBrightness(cfg.brightness);
- setSegments();
- return;
- default:
- // Setzt die erste LED (Index 0) auf rotes Licht (Rot, Grün, Blau, Weiss)
- strip.setPixelColor(0, strip.Color(255, 0, 0, 0));
- // Setzt die zweite LED auf grün
- strip.setPixelColor(1, strip.Color(0, 255, 0, 0));
- strip.setPixelColor(2, strip.Color(0, 0, 255, 0));
- strip.setPixelColor(3, strip.Color(0, 0, 0, 255));
- // Schickt die Farbinformationen zum LED-Streifen
- show();
- return;
- }
- }
-
- void LEDStripManager::clear() {
- strip.clear();
- }
-
- void LEDStripManager::show() {
- strip.show();
- }
-
- void LEDStripManager::on() {
- Serial.println("LEDStripManager LED ON");
- begin();
- webSocket.sendTXT(0, "LED ON");
- }
-
- void LEDStripManager::off() {
- Serial.println("LEDStripManager LED OFF");
- clear();
- show();
- delay(50);
- digitalWrite(LED_STRIP_POWER_PIN, LOW); // LED zunächst AUS
- digitalWrite(LED_STRIP_DATA_PIN, LOW);
- webSocket.sendTXT(0, "LED OFF");
- }
-
- bool LEDStripManager::isOn() {
- return digitalRead(LED_STRIP_POWER_PIN) == HIGH;
- }
-
- void LEDStripManager::setBrightness(uint8_t brightness) {
- strip.setBrightness(brightness);
- strip.show();
- }
-
- void LEDStripManager::setPixel(uint16_t index, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
- if (index >= strip.numPixels()) {
- return;
- }
- strip.setPixelColor(index, strip.Color(red, green, blue, white));
- strip.show();
- }
-
- void LEDStripManager::setAll(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, strip.Color(red, green, blue, white));
- }
- strip.show();
- }
-
- void LEDStripManager::onModeChange(LEDModes mode) {
- Config cfg = configManager.getConfig();
- cfg.mode = mode;
- configManager.saveConfig(cfg);
-
- // Handle only single action modes here. For Animations use loop functions with nonblocking timers
- switch (mode) {
- case LM_SINGLE_COLOR:
- this->setBrightness(cfg.brightness);
- setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
- return;
- case LM_SEGMENTS:
- this->setBrightness(cfg.brightness);
- setSegments();
- return;
- default:
- return;
- }
- }
-
- void LEDStripManager::onColorChange(String cfgVal, uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
- Config cfg = configManager.getConfig();
-
- RGBWColor *color = nullptr;
-
- if (cfgVal == "singleColor")
- color = &cfg.singleColor;
- else if (cfgVal == "segmentColor1")
- color = &cfg.segmentColor1;
- else if (cfgVal == "segmentColor2")
- color = &cfg.segmentColor2;
- else if (cfgVal == "segmentColor3")
- color = &cfg.segmentColor3;
-
- if (color != nullptr) {
- color->r = red;
- color->g = green;
- color->b = blue;
- color->w = white;
-
- configManager.saveConfig(cfg);
- }
-
- if (cfgVal == "singleColor") {
- this->setAll(red, green, blue, white);
- } else {
- setSegments();
- }
- }
-
- void LEDStripManager::onBrightnessChange(uint8_t brightness) {
- Config cfg = configManager.getConfig();
- cfg.brightness = brightness;
- configManager.saveConfig(cfg);
-
- this->setBrightness(brightness);
- }
-
- void LEDStripManager::onCfgParamChange(String cfgVal, uint32_t amount) {
- Config cfg = configManager.getConfig();
-
- if (cfgVal == "rainbowDelay")
- cfg.rainbowDelay = amount;
- else if (cfgVal == "spectralStepTime")
- cfg.spectralStepTime = amount;
- else if (cfgVal == "fadingHoldTime")
- cfg.fadingHoldTime = amount;
- else if (cfgVal == "fadingFadeTime")
- cfg.fadingFadeTime = amount;
- else if (cfgVal == "flashOnTime")
- cfg.flashOnTime = amount;
- else if (cfgVal == "flashOffTime")
- cfg.flashOffTime = amount;
-
- configManager.saveConfig(cfg);
- }
-
- void LEDStripManager::setSegments() {
-
- Config cfg = configManager.getConfig();
- RGBWColor c1 = cfg.segmentColor1;
- RGBWColor c2 = cfg.segmentColor2;
- RGBWColor c3 = cfg.segmentColor3;
-
- int nbrPixel = strip.numPixels();
- int third = nbrPixel / 3;
-
- for (int i = 0; i < nbrPixel; i++) {
- if (i < third)
- setPixel(i, c1.r, c1.g, c1.b, c1.w);
- else if (i < 2 * third)
- setPixel(i, c2.r, c2.g, c2.b, c2.w);
- else
- setPixel(i, c3.r, c3.g, c3.b, c3.w);
- }
- show();
- }
-
- uint32_t LEDStripManager::wheel(uint8_t pos) {
- pos = 255 - pos;
-
- if (pos < 85) {
- return strip.Color(255 - pos * 3, 0, pos * 3);
- }
-
- if (pos < 170) {
- pos -= 85;
- return strip.Color(0, pos * 3, 255 - pos * 3);
- }
-
- pos -= 170;
-
- return strip.Color(pos * 3, 255 - pos * 3, 0);
- }
-
- void LEDStripManager::rainbow() {
-
- Config cfg = configManager.getConfig();
-
- const uint32_t delay = cfg.rainbowDelay;
- static uint32_t lastUpdate = 0;
-
- if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
-
- if (millis() - lastUpdate < delay)
- return;
-
- lastUpdate = millis();
-
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- uint32_t pixelHue = firstPixelHue + (i * 65536L / strip.numPixels());
- strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
- }
-
- strip.show();
-
- firstPixelHue = (firstPixelHue + 256) % 65536;
- }
- }
-
- void LEDStripManager::doSpectral() {
-
- Config cfg = configManager.getConfig();
-
- static uint16_t hue = 0;
- static uint32_t lastUpdate = 0;
- const uint16_t hueStep = 65536 / 6; // Abstand der Hauptfarben
- const uint32_t stepTime = cfg.spectralStepTime; // Zeit bis zur nächsten Farbe
-
- if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
-
- uint32_t now = millis();
-
- if (now - lastUpdate >= 20) {
- lastUpdate = now;
-
- hue += (uint32_t) hueStep * 20 / stepTime;
-
- uint32_t color = strip.gamma32(strip.ColorHSV(hue));
-
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, color);
- }
-
- strip.show();
- }
- }
- }
-
- void LEDStripManager::fade() {
-
- static const uint8_t colors[][3] = {
- { 255, 0, 0 }, // Rot
- { 255, 32, 0 }, // Orange
- { 255, 110, 0 }, // Gelb
- { 0, 255, 0 }, // Grün
- { 0, 255, 255 }, // Cyan
- { 0, 0, 255 }, // Blau
- { 255, 0, 255 } // Violett
- };
-
- enum State {
- HOLD,
- FADE_OUT,
- FADE_IN
- };
-
- Config cfg = configManager.getConfig();
-
- static State state = HOLD;
- static uint8_t currentColor = 0;
- static uint32_t stateStart = millis();
-
- if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
-
- uint32_t holdTime = cfg.fadingHoldTime;
- uint32_t fadeTime = cfg.fadingFadeTime;
- uint32_t now = millis();
- uint32_t elapsed = now - stateStart;
-
- float brightness = 1.0f;
- float maxBrightness = float(cfg.brightness) / 255;
-
- switch (state) {
-
- case HOLD:
- brightness = maxBrightness;
- if (elapsed >= holdTime) {
- state = FADE_OUT;
- stateStart = now;
- }
- break;
-
- case FADE_OUT:
- brightness = maxBrightness - (float) elapsed / fadeTime;
- if (brightness < 0.0f)
- brightness = 0.0f;
-
- if (elapsed >= fadeTime) {
- currentColor = (currentColor + 1) % (sizeof(colors) / sizeof(colors[0]));
- state = FADE_IN;
- stateStart = now;
- }
- break;
-
- case FADE_IN:
- brightness = (float) elapsed / fadeTime;
- if (brightness > maxBrightness)
- brightness = maxBrightness;
-
- if (elapsed >= fadeTime) {
- state = HOLD;
- stateStart = now;
- }
- break;
- }
-
- uint8_t r = colors[currentColor][0] * brightness;
- uint8_t g = colors[currentColor][1] * brightness;
- uint8_t b = colors[currentColor][2] * brightness;
-
- for (uint16_t i = 0; i < strip.numPixels(); i++) {
- strip.setPixelColor(i, strip.Color(r, g, b));
- }
-
- strip.show();
- }
- }
-
- void LEDStripManager::flash() {
-
- static bool ledOn = false;
- static uint32_t lastChange = 0;
-
- if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
-
- Config cfg = configManager.getConfig();
- uint32_t onMillis = cfg.flashOnTime;
- uint32_t offMillis = cfg.flashOffTime;
- uint32_t now = millis();
-
- if (ledOn) {
- if (now - lastChange >= onMillis) {
- // LEDs ausschalten
- clear();
- show();
- ledOn = false;
- lastChange = now;
- }
- } else {
- if (now - lastChange >= offMillis) {
- // LEDs einschalten
- setAll(cfg.singleColor.r, cfg.singleColor.g, cfg.singleColor.b, cfg.singleColor.w);
- ledOn = true;
- lastChange = now;
- }
- }
- }
- }
|