Skip to content

Commit

Permalink
Re-implement LED fading
Browse files Browse the repository at this point in the history
  • Loading branch information
Jojo-1000 committed Nov 20, 2024
1 parent 40503d8 commit 109ed1d
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 21 deletions.
60 changes: 55 additions & 5 deletions src/FastLEDCompat.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,62 @@
#define FAST_LED_COMPAT_H

#include <FastLED.h>
#include <Constants.h>
#include <NeoPixelBusLg.h>

// NeoPixelBusLg that keeps track of the original colors
template <typename T_COLOR_FEATURE, typename T_METHOD, typename T_GAMMA = NeoGammaEquationMethod>
class CustomNeoPixelBusLg : public NeoPixelBusLg<T_COLOR_FEATURE, T_METHOD, T_GAMMA>
{
public:
using ColorObject = typename T_COLOR_FEATURE::ColorObject;
using Base = NeoPixelBusLg<T_COLOR_FEATURE, T_METHOD, T_GAMMA>;
CustomNeoPixelBusLg(uint16_t countPixels, uint8_t pin) : Base(countPixels, pin), buffer(countPixels, 1) { }
CustomNeoPixelBusLg(uint16_t countPixels) : Base(countPixels), buffer(countPixels, 1) { }

void SetPixelColor(uint16_t indexPixel, ColorObject color)
{
buffer.SetPixelColor(indexPixel, 0, color);
Base::SetPixelColor(indexPixel, color);
}
void ClearTo(ColorObject color)
{
buffer.ClearTo(color);
Base::ClearTo(color);
}
void ClearTo(ColorObject color, uint16_t first, uint16_t last)
{
for (uint16_t i = first; i <= last; ++i)
{
buffer.SetPixelColor(i, 0, color);

}
Base::ClearTo(color, first, last);
}
ColorObject GetPixelColor(uint16_t pixelIndex) const { return buffer.GetPixelColor(pixelIndex, 0); }

private:
NeoBuffer<NeoBufferMethod<T_COLOR_FEATURE>> buffer;
};

#if defined(ESP32)
using PixelBus = NeoPixelBusLg<NeoRgbFeature, NeoApa106Method>;
using PixelBus = CustomNeoPixelBusLg<NeoRgbFeature, NeoApa106Method, NeoGammaTableMethod>;
#elif defined(ESP8266)
using PixelBus = NeoPixelBusLg<NeoRgbFeature, NeoEsp8266DmaApa106Method>;
using PixelBus = CustomNeoPixelBusLg<NeoRgbFeature, NeoEsp8266DmaApa106Method, NeoGammaTableMethod>;
#else
using PixelBus = NeoPixelBusLg<NeoGrbFeature, NeoWs2812Method>;
using PixelBus = CustomNeoPixelBusLg<NeoGrbFeature, NeoWs2812Method>;
#endif

inline std::vector<CRGB>& pixelBuffer(const PixelBus& leds){
inline std::vector<CRGB>& pixelBuffer(const PixelBus& leds)
{
static std::vector<CRGB> pixels; // Static to prevent constant reallocation
pixels.resize(leds.PixelCount());
return pixels;
}

inline CRGB toCRGB(const RgbColor& rgb)
{
return CRGB(rgb.R, rgb.G, rgb.B);
}

inline RgbColor toRgb(const CRGB& rgb)
Expand All @@ -29,10 +70,19 @@ inline RgbColor toRgb(const CHSV& hsv)
return toRgb(rgb);
}

inline void blendPixels(PixelBus& leds, CRGB* ledBackup, uint8_t fade)
{
for (int i = 0; i < leds.PixelCount(); ++i)
{
CRGB existing = toCRGB(leds.GetPixelColor(i));
leds.SetPixelColor(i, toRgb(existing));
}
}

inline void fillRainbow(PixelBus& leds, uint8_t start, uint8_t end, uint8_t hue, uint8_t deltaHue)
{
auto& pixels = pixelBuffer(leds);
fill_rainbow(pixels.data() + start, end-start+1, hue, deltaHue);
fill_rainbow(pixels.data() + start, end - start + 1, hue, deltaHue);
for (int i = start; i <= end; ++i)
{
leds.SetPixelColor(i, toRgb(pixels[i]));
Expand Down
30 changes: 17 additions & 13 deletions src/TreeLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void TreeLight::init(Menu& menu)
lastUpdate = millis();
lastFpsCheck = lastUpdate;
fpsCounter = 0;
// ledBackup.fill_solid(CRGB::Black);
ledBackup.fill_solid(CRGB::Black);

// Init random seed
#if defined(ESP32)
Expand Down Expand Up @@ -117,11 +117,7 @@ void TreeLight::update()
unsigned long t = millis();
if (t - lastUpdate < 10)
{
if (leds->CanShow())
{
fpsCounter++;
leds->Show();
}
show();
return;
}
if (menu->isActive())
Expand All @@ -134,18 +130,17 @@ void TreeLight::update()
runEffect();
}
lastUpdate = t;
if (leds->CanShow())
{
fpsCounter++;
leds->Show();
}
show();
}

void TreeLight::resetEffect(bool timerOnly)
{
effectTime = 0;
// Save last effect colors
// ledBackup = leds;
for (int i = 0; i < numLeds; ++i)
{
ledBackup[i] = toCRGB(leds->GetPixelColor(i));
}
if (currentEffect)
{
currentEffect->reset(timerOnly);
Expand Down Expand Up @@ -203,6 +198,15 @@ unsigned int TreeLight::getFPS()
return res;
}

void TreeLight::show(bool dithering)
{
if (leds->CanShow())
{
fpsCounter++;
leds->Show();
}
}

void TreeLight::runEffect()
{
const unsigned int colorDuration = 60000;
Expand All @@ -221,7 +225,7 @@ void TreeLight::runEffect()
// Scale 0 to startFadeIn
uint8_t fade = min((startFadeIn - effectTime) * 256 / startFadeIn, (unsigned long)255);
fade = ease8InOutCubic(fade);
// leds.nblend(ledBackup, fade);
blendPixels(*leds, ledBackup, fade);
}
else if (c.allowAutoColorChange && effectTime > colorDuration)
{
Expand Down
6 changes: 3 additions & 3 deletions src/TreeLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,7 @@ class TreeLight
{
return;
}
leds->SetPixelColor(led, color);
leds->Show();
show();
}
void resetEffect(bool timerOnly = true);
void setBrightnessLevel(uint8_t level);
Expand All @@ -81,6 +80,7 @@ class TreeLight
void initColorMenu();
void setColorSelection(uint8_t index) { colors.setSelection(index); }
unsigned int getFPS();
void show(bool dithering=true);

const TreeColors& getColors() const { return colors; }
TreeColors& getColors() { return colors; }
Expand All @@ -100,7 +100,7 @@ class TreeLight
private:
Menu* menu;
std::unique_ptr<PixelBus> leds;
//CRGBArray<numLeds> ledBackup; // For fade over from different effect
CRGBArray<numLeds> ledBackup; // For fade over from different effect
unsigned long effectTime = 0;
unsigned long lastUpdate = 0;
unsigned long fpsCounter = 0;
Expand Down

0 comments on commit 109ed1d

Please sign in to comment.