Browse Source

garland: implement pixel caching for color waves to reduce redundant calculations

pull/2600/head
Dmitry Blinov 5 months ago
parent
commit
147a2266e1
4 changed files with 45 additions and 12 deletions
  1. +3
    -3
      code/espurna/garland/animations/anim_crossing.h
  2. +1
    -1
      code/espurna/garland/animations/anim_run.h
  3. +1
    -1
      code/espurna/garland/animations/anim_waves.h
  4. +40
    -7
      code/espurna/garland/animations/color_wave.h

+ 3
- 3
code/espurna/garland/animations/anim_crossing.h View File

@ -13,7 +13,7 @@ class AnimCrossing : public Anim {
void SetupImpl() override {
wave1 = generateWave(1);
wave1 = generateWave(1, ledstmp);
wave2 = generateWave(-1);
}
@ -30,7 +30,7 @@ class AnimCrossing : public Anim {
private:
ColorWave generateWave(int dir) {
ColorWave generateWave(int dir, Color* pixelCache = nullptr) {
unsigned int waveLen = secureRandom(10, 50);
bool cleanColors = fiftyFifty();
bool startEmpty = fiftyFifty();
@ -38,7 +38,7 @@ class AnimCrossing : public Anim {
byte fade = fiftyFifty() ? 0 : palette->bright() ? secureRandom(180, 220) : 120;
Palette* wavePal = &pals[secureRandom(palsNum)];
return ColorWave(numLeds, wavePal, waveLen, cleanColors, fade, speed, dir, startEmpty);
return ColorWave(numLeds, wavePal, waveLen, cleanColors, fade, pixelCache, speed, dir, startEmpty);
}
ColorWave wave1;


+ 1
- 1
code/espurna/garland/animations/anim_run.h View File

@ -15,7 +15,7 @@ class AnimRun : public Anim {
unsigned int waveLen = secureRandom(10, 30);
bool cleanColors = secureRandom(10) > 7;
byte fade = palette->bright() ? secureRandom(180, 220) : 0;
wave = ColorWave(numLeds, palette, waveLen, cleanColors, fade);
wave = ColorWave(numLeds, palette, waveLen, cleanColors, fade, ledstmp);
}
void Run() override {


+ 1
- 1
code/espurna/garland/animations/anim_waves.h View File

@ -14,7 +14,7 @@ class AnimWaves : public Anim {
unsigned int waveLen = secureRandom(50, 100);
bool cleanColors = secureRandom(10) > 7;
byte fade = palette->bright() ? secureRandom(180, 220) : 0;
wave = ColorWave(numLeds, palette, waveLen, cleanColors, fade);
wave = ColorWave(numLeds, palette, waveLen, cleanColors, fade, ledstmp);
glowSetUp();
}


+ 40
- 7
code/espurna/garland/animations/color_wave.h View File

@ -19,14 +19,27 @@ class ColorWave {
uint16_t _waveLen,
bool _cleanColors,
byte _fade = 0,
Color* _pixelCache = nullptr,
float _speed = secureRandom(5, 15) / 10.0,
int _dir = secureRandom(10) > 5 ? 1 : -1,
int _dir = randDir(),
bool _startEmpty = false)
: numLeds(_numLeds), palette(_palette), waveLen(_waveLen), cleanColors(_cleanColors), dir(_dir), speed(_speed), fade(_fade) {
DEBUG_MSG_P(PSTR("[GARLAND] Wave: waveLen = %d Pal: %-8s fade = %d cleanColors = %d speed = %d\n"),
waveLen, palette->name(), fade, cleanColors, (int)(speed * 10.0));
: numLeds(_numLeds), palette(_palette), waveLen(_waveLen), cleanColors(_cleanColors), fade(_fade), pixelCache(_pixelCache), speed(_speed), dir(_dir) {
if (speed < 0) {
speed = -speed;
dir = -dir;
}
head = _startEmpty ? 0 : numLeds - 1;
fade_step = fade * 2 / waveLen;
DEBUG_MSG_P(PSTR("[GARLAND] Wave: waveLen = %d Pal: %-8s fade = %d cleanColors = %d speed = %d\n"),
waveLen, palette->name(), fade, cleanColors, (int)(speed * 10.0));
if (pixelCache) {
for (auto i = 0; i < numLeds; ++i) {
pixelCache[i] = Color();
}
}
}
Color getLedColor(uint16_t ledNum) {
@ -36,8 +49,11 @@ class ColorWave {
return Color();
}
uint16_t dist_to_head = head - real_led_num;
if (pixelCache && !(pixelCache[real_led_num] == Color())) {
return pixelCache[real_led_num];
}
uint16_t dist_to_head = head - real_led_num;
Color c = cleanColors ? palette->getCleanColor(dist_to_head / waveLen) : palette->getCachedPalColor(dist_to_head * 256 * 20 / waveLen / numLeds);
if (fade_step > 0) {
@ -45,11 +61,27 @@ class ColorWave {
c = c.brightness(fadeFactor);
}
if (pixelCache) {
pixelCache[real_led_num] = c;
}
return c;
}
void move() {
uint16_t prevHead = head;
head += speed;
uint16_t headDelta = head - prevHead;
if (pixelCache && headDelta > 0) {
for (auto i = numLeds - 1; i >= 0; --i) {
if (i >= headDelta) {
pixelCache[i] = pixelCache[i - headDelta];
} else {
pixelCache[i] = Color();
}
}
}
}
private:
@ -57,9 +89,10 @@ class ColorWave {
Palette *palette;
uint16_t waveLen;
bool cleanColors;
int dir;
float speed;
byte fade;
Color* pixelCache;
float speed;
int dir;
float head;
byte fade_step;
};

Loading…
Cancel
Save