// 
// SignalLed.cpp
// 

#include "SignalLed.h"
#include "Globals.h"

SignalLedClass SignalLed(NEO_DI);

void SignalLedClass::begin(uint8_t br, uint8_t flashBr, uint32_t duration, uint32_t interval) {
  _Brightness = br;
  _FlashBrightness = flashBr;
  _FlashDuration = duration;
  _FlashInterval = interval;
  _Neo.begin(); 
  _Neo.setBrightness(br);
}

void SignalLedClass::setColor(SignalLedColors c) {
  _Color = c;
  if (!_IsFlashing) { // Während des Blitzen keine Farbänderung
    _Neo.setPixelColor(0, (uint32_t)c); 
    _Neo.show();
  }
}
void SignalLedClass::config(uint8_t br, uint8_t flashBr, uint32_t duration, uint32_t interval) {
  _Brightness = br;
  _FlashBrightness = flashBr;
  _FlashDuration = duration;
  _FlashInterval = interval;
  if (_IsFlashing) { // Während des Blitzen keine Helligkeitsänderung
    _Neo.setBrightness(flashBr);
    _Neo.show();
  }
  else {
    _Neo.setBrightness(br); 
    _Neo.show();
  }
}

void SignalLedClass::handle() {
  if (_IsFlashing) { // Blitzphase
    if (millis() > (_LastAction + _FlashDuration)) { // Blitz abschalten
      _LastAction = millis();
      _IsFlashing = false;
      _Neo.setPixelColor(0, (uint32_t)_Color);
      _Neo.setBrightness(_Brightness);
      _Neo.show();
    }
  }
  else { // Zeit zwischen den Blitzen
    if (millis() > (_LastAction + _FlashInterval)) { // Blitz anschalten
      _LastAction = millis();
      _IsFlashing = true;
      _Neo.setPixelColor(0, (uint32_t)_Color);
      _Neo.setBrightness(_FlashBrightness);
      _Neo.show();
    }
  }
}

void SignalLedClass::signalError() {
  setColor(SignalLedColors::error);
  _Neo.setBrightness(255);
}

void SignalLedClass::setBrightness(uint8_t br) {
  _Brightness = br;
  _Neo.setBrightness(br);
  _Neo.show();
}
