Skip to content

Commit

Permalink
Change rainbow to palette gradient when possible.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jojo-1000 committed Dec 8, 2021
1 parent 13e6a27 commit 9f4e380
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
66 changes: 59 additions & 7 deletions code/TreeLight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
void TreeLight::init(Menu& menu)
{
this->menu = &menu;
FastLED.addLeds<APA106, pin, RGB>(leds, numLeds);
FastLED.addLeds<WS2812, pin, GRB>(leds, numLeds);
setBrightnessLevel(4);
FastLED.setCorrection(LEDColorCorrection::Typical8mmPixel);
leds.fill_solid(CRGB::Black);
Expand Down Expand Up @@ -242,16 +242,45 @@ void TreeLight::runEffect()
break;
}
case Effect::rainbowHorizontal: {
uint8_t hue = (uint8_t)(effectTime >> 4); // effectTime / 16 => full rainbow in ~4s
leds(0, 7).fill_rainbow(hue, rainbowDeltaHue);
leds(8, 11).fill_rainbow(hue, rainbowDeltaHue * 2);
leds[12] = leds[0];
if (!isColorPalette())
{
uint8_t hue = (uint8_t)(effectTime >> 5); // effectTime / 32 => full rainbow in ~4s
leds(0, 7).fill_rainbow(hue, rainbowDeltaHue);
leds(8, 11).fill_rainbow(hue, rainbowDeltaHue * 2);
leds[12] = leds[0];
}
else
{
uint8_t startIndex = (uint8_t)(effectTime >> 5); // effectTime / 32 => full rainbow in ~4s
uint8_t colorIndex = startIndex;
for (uint8_t i = 0; i < 8; ++i)
{
leds[i] = getPaletteColor(colorIndex);
colorIndex += rainbowDeltaHue;
}
colorIndex = startIndex;
for (uint8_t i = 8; i < 12; ++i)
{
leds[i] = getPaletteColor(colorIndex);
colorIndex += rainbowDeltaHue * 2;
}
leds[12] = leds[0];
}
break;
}
case Effect::rainbowVertical: {
CRGB colors[3];
uint8_t hue = (uint8_t)(effectTime >> 4); // effectTime / 16 => full rainbow in ~4s
fill_rainbow(colors, 3, hue, rainbowDeltaHue);
uint8_t hue = (uint8_t)(effectTime >> 5); // effectTime / 32 => full rainbow in ~4s
if (!isColorPalette())
{
fill_rainbow(colors, 3, hue, rainbowDeltaHue);
}
else
{
colors[0] = getPaletteColor(hue);
colors[1] = getPaletteColor(hue + rainbowDeltaHue);
colors[2] = getPaletteColor(hue + rainbowDeltaHue * 2);
}
// Bottom
leds(0, 7).fill_solid(colors[0]);
// Middle
Expand Down Expand Up @@ -450,3 +479,26 @@ void TreeLight::updateColor()
break;
}
}

bool TreeLight::isColorPalette() const
{
return colorSelection >= 3 && colorSelection < 7;
}

CRGB TreeLight::getPaletteColor(uint8_t mix) const
{
switch (colorSelection)
{
case 3:
return ColorFromPalette(LavaColors_p, mix);
case 4:
return ColorFromPalette(CloudColors_p, mix);
case 5:
return ColorFromPalette(OceanColors_p, mix);
case 6:
return ColorFromPalette(ForestColors_p, mix);
default:
return CRGB::Black;
break;
}
}
2 changes: 2 additions & 0 deletions code/TreeLight.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class TreeLight
void runEffect();
void displayMenu();
void updateColor();
bool isColorPalette() const;
CRGB getPaletteColor(uint8_t mix) const;

private:
Menu* menu;
Expand Down

0 comments on commit 9f4e380

Please sign in to comment.