Skip to content

Commit

Permalink
Library rework (chore)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewjswan committed Dec 14, 2023
1 parent d291ded commit c8d49ad
Show file tree
Hide file tree
Showing 25 changed files with 180 additions and 89 deletions.
2 changes: 1 addition & 1 deletion common/light/effects/exponent_light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addressable_lambda:
int8_t g = ceil(current_color.g * y);
int8_t b = ceil(current_color.b * y);
it.all() = Color(r,g,b);
it.all() = Color(r, g, b);
x += 1;
if (x == 400)
Expand Down
6 changes: 3 additions & 3 deletions common/light/effects/fastled-colorwaves.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ addressable_lambda:
bri8 += (255 - brightdepth);
uint8_t index = hue8;
index = scale8( index, 240);
index = scale8( index, 240 );
CRGB newcolor = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], index, bri8);
if (id(fastled_variant).state < 128) // направление
CRGB newcolor = color_from_palette(index, current_color, bri8);
if (id(fastled_variant).state < 128) // Направление
{
nblend(leds[(it.size() - 1) - i], newcolor, 128);
}
Expand Down
6 changes: 3 additions & 3 deletions common/light/effects/fastled-confetti.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ addressable_lambda:
variant = (int)id(fastled_variant).state;
}
fadeToBlackBy(leds, it.size(), thisfade); // Задухание светодиодов на значение thisfade
fadeToBlackBy(leds, it.size(), thisfade); // Задухание светодиодов на значение thisfade
uint16_t pos = random16(it.size());
leds[pos] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex + random8(thisdiff) / 4 , 255, LINEARBLEND); // Munge the values and pick a colour from the palette
thisindex = thisindex + thisinc; // base palette counter increments here.
leds[pos] = color_from_palette(thisindex + random8(thisdiff) / 4, current_color); // Munge the values and pick a colour from the palette
thisindex = thisindex + thisinc; // base palette counter increments here.
for (int i = 0; i < it.size(); i++)
{
Expand Down
2 changes: 1 addition & 1 deletion common/light/effects/fastled-dynamic-smooth.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ addressable_lambda:

for (int i = 0; i < it.size(); i++)
{
CRGB color = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], data[i], 255, LINEARBLEND);
CRGB color = color_from_palette(data[i], current_color);
if (id(fastled_speed).state > 0)
{
CRGB ledcolor = leds[i];
Expand Down
4 changes: 2 additions & 2 deletions common/light/effects/fastled-gradient.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ addressable_lambda:
for (int i = 0; i < it.size(); i++)
{
byte idx = map(i, 0, it.size(), 0, 255) + count; // прямой градиент
CRGB color = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], idx, 255, LINEARBLEND);
byte idx = map(i, 0, it.size(), 0, 255) + count; // Прямой градиент
CRGB color = color_from_palette(idx, current_color);
it[i] = Color(color.r, color.g, color.b);
}
Expand Down
4 changes: 2 additions & 2 deletions common/light/effects/fastled-juggle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ addressable_lambda:
variant = (int)id(fastled_variant).state;
}
fadeToBlackBy(leds, it.size(), thisfade); // Затухание к черному
fadeToBlackBy(leds, it.size(), thisfade); // Затухание к черному
for (int i = 0; i < numdots; i++)
{
leds[beatsin16(thisbeat + i + numdots, 0, it.size() - 1)] += ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex , 255, LINEARBLEND); // Munge the values and pick a colour from the palette
leds[beatsin16(thisbeat + i + numdots, 0, it.size() - 1)] += color_from_palette(thisindex, current_color); // Munge the values and pick a colour from the palette
thisindex += thisdiff;
}
Expand Down
8 changes: 4 additions & 4 deletions common/light/effects/fastled-matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ addressable_lambda:
if (thisrot)
{
thisindex++; // Increase palette index to change colours on the fly
thisindex++; // Increase palette index to change colours on the fly
}
if (random8(90) > 80)
{
if (thisdir == -1)
{
leds[0] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex, 255, LINEARBLEND); // Foreground matrix colour
leds[0] = color_from_palette(thisindex, current_color); // Foreground matrix colour
}
else
{
leds[it.size() - 1] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex, 255, LINEARBLEND);
leds[it.size() - 1] = color_from_palette(thisindex, current_color);
}
}
else
Expand All @@ -75,7 +75,7 @@ addressable_lambda:
}
}
if (thisdir == -1) // Move the foreground colour down the line.
if (thisdir == -1) // Move the foreground colour down the line.
{
for (int i = it.size() - 1; i > 0 ; i--)
{
Expand Down
2 changes: 1 addition & 1 deletion common/light/effects/fastled-noice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ addressable_lambda:
for (int i = 0; i < it.size(); i++)
{
byte idx = inoise8(i * id(fastled_variant).state / 10, dist + i * id(fastled_variant).state, count);
CRGB color = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], idx, 255, LINEARBLEND);
CRGB color = color_from_palette(idx, current_color);
it[i] = Color(color.r, color.g, color.b);
}
Expand Down
2 changes: 1 addition & 1 deletion common/light/effects/fastled-one-sin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ addressable_lambda:
{ // For each of the LED's in the strand, set a brightness based on a wave as follows:
int thisbright = qsubd(cubicwave8((i * allfreq) + thisphase), thiscutoff); // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
leds[i] = CHSV(bgclr, 255, bgbri); // First set a background colour, but fully saturated.
leds[i] += ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex + i * thisinc, thisbright, LINEARBLEND);
leds[i] += color_from_palette(thisindex + i * thisinc, current_color, thisbright);
thisindex += thisrot;
}
Expand Down
8 changes: 4 additions & 4 deletions common/light/effects/fastled-plasma.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ addressable_lambda:
}
for (int i = 0; i < it.size(); i++)
{ // For each of the LED's in the strand, set a brightness based on a wave as follows:
int colorIndex = cubicwave8((i * freq1) + thisPhase) / 2 + cos8((i * freq2) + thatPhase) / 2; // Create a wave and add a phase change and add another wave with its own phase change.. Hey, you can even change the frequencies if you wish, but don't change on the fly.
int thisBright = qsuba(colorIndex, beatsin8(7, 0, 96)); // qsub gives it a bit of 'black' dead space by setting sets a minimum value. If colorIndex < current value of beatsin8(), then bright = 0. Otherwise, bright = colorIndex..
leds[i] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], colorIndex, thisBright, LINEARBLEND); // Let's now add the foreground colour.
{ // For each of the LED's in the strand, set a brightness based on a wave as follows:
int colorIndex = cubicwave8((i * freq1) + thisPhase) / 2 + cos8((i * freq2) + thatPhase) / 2; // Create a wave and add a phase change and add another wave with its own phase change.. Hey, you can even change the frequencies if you wish, but don't change on the fly.
int thisBright = qsuba(colorIndex, beatsin8(7, 0, 96)); // qsub gives it a bit of 'black' dead space by setting sets a minimum value. If colorIndex < current value of beatsin8(), then bright = 0. Otherwise, bright = colorIndex..
leds[i] = color_from_palette(colorIndex, current_color, thisBright); // Let's now add the foreground colour.
}
for (int i = 0; i < it.size(); i++)
Expand Down
6 changes: 3 additions & 3 deletions common/light/effects/fastled-serendipitous.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ addressable_lambda:
name: FastLED Serendipitous Circles
update_interval: 5ms
lambda: |-
static uint8_t thisindex = 0; // Указатель на элемент палитры
static uint8_t thisindex = 0; // Указатель на элемент палитры
static uint16_t Xorig = 0x013;
static uint16_t Yorig = 0x021;
Expand Down Expand Up @@ -37,9 +37,9 @@ addressable_lambda:
Y = Yn;
thisindex = (sin8(X) + cos8(Y)) / 2;
leds[X % (it.size())] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thisindex, 255, LINEARBLEND);
leds[X % (it.size())] = color_from_palette(thisindex, current_color);
fadeToBlackBy(leds, it.size(), 16); // 8 bit, 1 = slow, 255 = fast
fadeToBlackBy(leds, it.size(), 16); // 8 bit, 1 = slow, 255 = fast
for (int i = 0; i < it.size(); i++)
{
Expand Down
4 changes: 2 additions & 2 deletions common/light/effects/fastled-three-sin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ addressable_lambda:
for (int i = 0; i < it.size(); i++)
{
uint8_t tmp = sin8(mul1 * i + wave1) + sin8(mul1 * i + wave2) + sin8(mul1 * i + wave3);
leds[i] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], tmp, 255, LINEARBLEND);
uint8_t index = sin8(mul1 * i + wave1) + sin8(mul1 * i + wave2) + sin8(mul1 * i + wave3);
leds[i] = color_from_palette(index, current_color);
}
for (int i = 0; i < it.size(); i++)
Expand Down
4 changes: 2 additions & 2 deletions common/light/effects/fastled-two-sin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ addressable_lambda:
int thisbright = qsuba(cubicwave8((i * allfreq) + thisphase), thiscutoff); // qsub sets a minimum value called thiscutoff. If < thiscutoff, then bright = 0. Otherwise, bright = 128 (as defined in qsub)..
int thatbright = qsuba(cubicwave8((i * allfreq) + 128 + thatphase), thatcutoff); // This wave is 180 degrees out of phase (with the value of 128).
leds[i] = ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thishue, thisbright, LINEARBLEND);
leds[i] += ColorFromPalette(paletteArr[(int)id(fastled_palette).state], thathue, thatbright, LINEARBLEND);
leds[i] = color_from_palette(thishue, current_color, thisbright);
leds[i] += color_from_palette(thathue, current_color, thatbright);
}
for (int i = 0; i < it.size(); i++)
Expand Down
4 changes: 2 additions & 2 deletions common/light/effects/sunrise.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
lambda:
lambda:
name: Sunrise
update_interval: 15s
lambda: |-
Expand All @@ -15,7 +15,7 @@
call.set_brightness(state / 100.0);
call.perform();
if (state < 50)
if (state < ${sunrise})
{
state++;
}
2 changes: 1 addition & 1 deletion common/light/settings/effects_sound.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
effects:
- !include ../effects/rise_and_fade.yaml
- !include ../effects/random.yaml
- !include ../effects/pulse.yaml
- !include ../effects/flicker.yaml
Expand Down Expand Up @@ -38,6 +37,7 @@ effects:
- !include ../effects/sunrise.yaml
- !include ../effects/clock.yaml
- !include ../effects/clock_full.yaml
- !include ../effects/rise_and_fade.yaml
- !include ../effects/strobe_common.yaml
- !include ../effects/strobe_police.yaml
- !include ../effects/fastled-blendwave.yaml
Expand Down
4 changes: 2 additions & 2 deletions common/number/fastled_palette.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
platform: template
name: ${name} Fastled Palette
min_value: 0
max_value: 52
max_value: 56
step: 1
optimistic: true
id: fastled_palette
initial_value: 0
initial_value: 4
mode: slider
entity_category: config
2 changes: 1 addition & 1 deletion common/number/fastled_speed.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ max_value: 255
step: 1
optimistic: true
id: fastled_speed
initial_value: 100
initial_value: 128
mode: slider
entity_category: config
2 changes: 1 addition & 1 deletion common/number/fastled_variant.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ max_value: 255
step: 1
optimistic: true
id: fastled_variant
initial_value: 0
initial_value: 128
mode: slider
entity_category: config
7 changes: 0 additions & 7 deletions common/switch/fastled_solid.yaml

This file was deleted.

125 changes: 124 additions & 1 deletion includes/light/light.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,136 @@
#pragma once

#include "esphome.h"
#undef delay

#include "FastLED.h"
#include "palettes.h"

CRGB* leds;
CRGBPalette16 randomPalette;

uint32_t randomPaletteChange = 0;

CRGB color_from_palette(int index, esphome::Color current_color, uint8_t brightness);
CRGB color_from_palette(int index, CRGB current_color, uint8_t brightness);

void InitLeds(int size)
{
if (leds == NULL)
{
leds = new CRGB[size];
}
}
}

CRGB color_from_palette(int index, esphome::Color current_color, uint8_t brightness = 255)
{
CRGB color = CRGB(current_color.r, current_color.g, current_color.b);
return color_from_palette(index, color, brightness);
}

CRGB color_from_palette(int index, CRGB current_color, uint8_t brightness = 255)
{
if ((int)id(fastled_palette).state == 0) // Current led color
{
return ColorFromPalette(CRGBPalette16(current_color), index, brightness, LINEARBLEND);
}

if ((int)id(fastled_palette).state == 1) // Periodically replace palette with a random one
{
if (millis() - randomPaletteChange > 1000 + ((uint32_t)(255 - (int)id(fastled_variant).state)) * 100)
{
randomPalette = CRGBPalette16(CHSV(random8(), 255, random8(128, 255)),
CHSV(random8(), 255, random8(128, 255)),
CHSV(random8(), 192, random8(128, 255)),
CHSV(random8(), 255, random8(128, 255)));
randomPaletteChange = millis();
}
return ColorFromPalette(randomPalette, index, brightness, LINEARBLEND);
}

if ((int)id(fastled_palette).state == 2)
{
#ifdef SOUND_REACTIVE // SOUND_REACTIVE
return ColorFromPalette(getAudioPalette(0), index, brightness, LINEARBLEND);
#else
return ColorFromPalette(paletteArr[random8((int)id(fastled_palette).traits.get_max_value() - 4)], index, brightness, LINEARBLEND);
#endif
}

if ((int)id(fastled_palette).state == 3)
{
#ifdef SOUND_REACTIVE // SOUND_REACTIVE
return ColorFromPalette(getAudioPalette(1), index, brightness, LINEARBLEND);
#else
return ColorFromPalette(paletteArr[random8((int)id(fastled_palette).traits.get_max_value() - 4)], index, brightness, LINEARBLEND);
#endif
}

return ColorFromPalette(paletteArr[(int)id(fastled_palette).state - 4], index, brightness, LINEARBLEND);
}

#ifdef SOUND_REACTIVE // SOUND_REACTIVE
CRGBPalette16 getAudioPalette(int pal)
{
// https://forum.makerforums.info/t/hi-is-it-possible-to-define-a-gradient-palette-at-runtime-the-define-gradient-palette-uses-the/63339

uint8_t xyz[16]; // Needs to be 4 times however many colors are being used.
// 3 colors = 12, 4 colors = 16, etc.

xyz[0] = 0; // Anchor of first color - must be zero
xyz[1] = 0;
xyz[2] = 0;
xyz[3] = 0;

CRGB rgb = getCRGBForBand(1, pal);
xyz[4] = 1; // Anchor of first color
xyz[5] = rgb.r;
xyz[6] = rgb.g;
xyz[7] = rgb.b;

rgb = getCRGBForBand(128, pal);
xyz[8] = 128;
xyz[9] = rgb.r;
xyz[10] = rgb.g;
xyz[11] = rgb.b;

rgb = getCRGBForBand(255, pal);
xyz[12] = 255; // Anchor of last color - must be 255
xyz[13] = rgb.r;
xyz[14] = rgb.g;
xyz[15] = rgb.b;

return CRGBPalette16(xyz);
}

CRGB getCRGBForBand(int x, int pal)
{
extern int fftResult[]; // Summary of bins Array. 16 summary bins.

CRGB value;
CHSV hsv;

if(pal == 0)
{
if(x == 1)
{
value = CRGB(uint8_t(fftResult[10] / 2), uint8_t(fftResult[4]/2), uint8_t(fftResult[0] / 2));
}
else if(x == 255)
{
value = CRGB(uint8_t(fftResult[10] / 2), uint8_t(fftResult[0]/2), uint8_t(fftResult[4] / 2));
}
else
{
value = CRGB(uint8_t(fftResult[0] / 2), uint8_t(fftResult[4]/2), uint8_t(fftResult[10] / 2));
}
}
else if(pal == 1)
{
int b = map(x, 1, 255, 0, 8); // Convert palette position to lower half of freq band
hsv = CHSV(uint8_t(fftResult[b]), 255, uint8_t(map(fftResult[b], 0, 255, 30, 255))); // Pick hue
hsv2rgb_rainbow(hsv, value); // Convert to R,G,B
}
return value;
}
#endif // SOUND_REACTIVE
2 changes: 2 additions & 0 deletions includes/light/palettes.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#pragma once

#include <FastLED.h>

DEFINE_GRADIENT_PALETTE( Fire_gp ) {
Expand Down
Loading

0 comments on commit c8d49ad

Please sign in to comment.