| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /*
- * LEDStripManager.cpp
- *
- * Created on: 19.06.2026
- * Author: FSmilari
- */
-
- #include "LEDStripManager.h"
-
- /*
- * Constructor
- */
- LEDStripManager::LEDStripManager(uint16_t numLeds, uint8_t pin, ConfigManager &configManager)
- : strip(numLeds, pin, NEO_GRBW + NEO_KHZ800), configManager(configManager) {
-
- }
-
- /*
- * 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.color.r, cfg.color.g, cfg.color.b, cfg.color.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();
- }
-
- 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);
- }
-
- 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.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
- return;
- case LM_SEGMENTS:
- this->setBrightness(cfg.brightness);
- setSegments();
- return;
- default:
- return;
- }
- }
-
- void LEDStripManager::onColorChange(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) {
- Config cfg = configManager.getConfig();
- cfg.color.r = red;
- cfg.color.g = green;
- cfg.color.b = blue;
- cfg.color.w = white;
- configManager.saveConfig(cfg);
-
- this->setAll(red, green, blue, white);
- }
-
- void LEDStripManager::onBrightnessChange(uint8_t brightness) {
- Config cfg = configManager.getConfig();
- cfg.brightness = brightness;
- configManager.saveConfig(cfg);
-
- this->setBrightness(brightness);
- }
-
- void LEDStripManager::setSegments() {
- int nbrPixel = strip.numPixels();
- int third = nbrPixel / 3;
-
- for (int i = 0; i < nbrPixel; i++) {
- if (i < third)
- setPixel(i, 255, 0, 0, 0);
- else if (i < 2 * third)
- setPixel(i, 0, 255, 0, 0);
- else
- setPixel(i, 0, 0, 255, 0);
- }
- 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() {
-
- static uint8_t delay = 20;
- 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::flash() {
-
- static const uint32_t onMillis = 1000;
- static const uint32_t offMillis = 500;
-
- static bool ledOn = false;
- static uint32_t lastChange = 0;
-
- if (digitalRead(LED_STRIP_POWER_PIN) == HIGH) {
-
- Config cfg = configManager.getConfig();
- 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.color.r, cfg.color.g, cfg.color.b, cfg.color.w);
- ledOn = true;
- lastChange = now;
- }
- }
- }
- }
|