Skip to content

Commit

Permalink
Merge pull request #556 from davepl/main
Browse files Browse the repository at this point in the history
Fix fire perf, protect against possible GIF load failure
  • Loading branch information
rbergen authored Dec 3, 2023
2 parents f61b561 + 597031d commit eac9ba6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
21 changes: 14 additions & 7 deletions include/effects/matrix/PatternAnimatedGIF.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ struct GIFInfo : public EmbeddedFile
{}
};

static std::map<GIFIdentifier, const GIFInfo, std::less<GIFIdentifier>, psram_allocator<std::pair<GIFIdentifier, const GIFInfo>>> AnimatedGIFs =
static const std::map<GIFIdentifier, const GIFInfo, std::less<GIFIdentifier>, const psram_allocator<std::pair<GIFIdentifier, const GIFInfo>>> AnimatedGIFs =
{
{ GIFIdentifier::Banana, GIFInfo(banana_start, banana_end, 32, 32, 12 ) },
{ GIFIdentifier::Pacman, GIFInfo(pacman_start, pacman_end, 64, 12, 20 ) },
Expand All @@ -117,7 +117,7 @@ g_gifDecoderState;
// We dynamically allocate the GIF decoder because it's pretty big and we don't want to waste the base
// ram on it. This way it, and the GIFs it decodes, can live in PSRAM.

std::unique_ptr<GifDecoder<MATRIX_WIDTH, MATRIX_HEIGHT, 12, true>> g_ptrGIFDecoder = make_unique_psram<GifDecoder<MATRIX_WIDTH, MATRIX_HEIGHT, 12, true>>();
const std::unique_ptr<GifDecoder<MATRIX_WIDTH, MATRIX_HEIGHT, 16, true>> g_ptrGIFDecoder = make_unique_psram<GifDecoder<MATRIX_WIDTH, MATRIX_HEIGHT, 16, true>>();

// PatternAnimatedGIF
//
Expand All @@ -128,8 +128,9 @@ class PatternAnimatedGIF : public LEDStripEffect
private:

GIFIdentifier _gifIndex = GIFIdentifier::INVALID;
CRGB _bkColor = BLACK16;
bool _preClear = false;
CRGB _bkColor = BLACK16;
bool _preClear = false;
bool _gifReadyToDraw = false;

// GIF decoder callbacks. These are static because the decoder doesn't allow you to pass any context, so they
// have to be global. We use the global g_gifDecoderState to track state. The GifDecoder code calls back to
Expand Down Expand Up @@ -163,7 +164,6 @@ class PatternAnimatedGIF : public LEDStripEffect
debugW("drawPixelCallbackInvalid pixel: %d, %d", x + g_gifDecoderState._offsetX, y + g_gifDecoderState._offsetY);
return;
}

g.leds[XY(x + g_gifDecoderState._offsetX, y + g_gifDecoderState._offsetY)] = CRGB(red, green, blue);
}

Expand Down Expand Up @@ -231,6 +231,9 @@ class PatternAnimatedGIF : public LEDStripEffect
// Set up the gifDecoderState with all of the context that it will need to decode and
// draw the GIF, since the static callbacks will have no other context to work with.

assert(gif->second._width <= MATRIX_WIDTH);
assert(gif->second._height <= MATRIX_HEIGHT);

g_gifDecoderState._offsetX = (MATRIX_WIDTH - gif->second._width) / 2;
g_gifDecoderState._offsetY = (MATRIX_HEIGHT - gif->second._height) / 2;
g_gifDecoderState._fps = gif->second._fps;
Expand All @@ -243,7 +246,8 @@ class PatternAnimatedGIF : public LEDStripEffect
g_ptrGIFDecoder->setDrawPixelCallback( drawPixelCallback );
g_ptrGIFDecoder->setDrawLineCallback( drawLineCallback );

if (ERROR_NONE != g_ptrGIFDecoder->startDecoding((uint8_t *) gif->second.contents, gif->second.length))
_gifReadyToDraw = (ERROR_NONE == g_ptrGIFDecoder->startDecoding((uint8_t *) gif->second.contents, gif->second.length));
if (!_gifReadyToDraw)
debugW("Failed to start decoding GIF");
}

Expand All @@ -256,7 +260,10 @@ class PatternAnimatedGIF : public LEDStripEffect
if (_preClear)
g()->Clear(_bkColor);

g_ptrGIFDecoder->decodeFrame(false);
if (_gifReadyToDraw)
g_ptrGIFDecoder->decodeFrame(false);
else
g()->Clear(CRGB::Red);
}
};

Expand Down
7 changes: 1 addition & 6 deletions include/effects/matrix/PatternSMFire2021.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,9 @@ class PatternSMFire2021 : public LEDStripEffect

// Get the flame color using the black body radiation approximation, but when the palette is paused
// we make flame in that base color instead of the normal red

CRGB color = GetBlackBodyHeatColor(Col/255.0f, g()->IsPalettePaused() ?
g()->ColorFromCurrentPalette(0, Bri)
: CRGB::Red).fadeToBlackBy(255-Bri);

// NightDriver mod - invert Y argument.

nblend(g()->leds[XY(x, MATRIX_HEIGHT - 1 - y)], color, pcnt);
nblend(g()->leds[XY(x, MATRIX_HEIGHT - 1 - y)], GetBlackBodyHeatColor(Col/255.0f, g()->ColorFromCurrentPalette(0, Bri)).fadeToBlackBy(255-Bri), pcnt);
}
}

Expand Down
17 changes: 8 additions & 9 deletions src/effects.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,31 +203,30 @@ void LoadEffectFactories()
#endif

ADD_EFFECT(EFFECT_MATRIX_SPECTRUMBAR, SpectrumBarEffect, "Audiograph");
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum", NUM_BANDS, spectrumBasicColors, false, 100, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "AudioWave", MATRIX_WIDTH, CRGB(0,0,40), 0, 1.25, 1.25);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Pacman", GIFIdentifier::Pacman);
ADD_EFFECT(EFFECT_MATRIX_PONG_CLOCK, PatternPongClock);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Colorball", GIFIdentifier::ColorSphere);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "AudioWave", MATRIX_WIDTH, CRGB(0,0,40), 0, 1.25, 1.25);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum", NUM_BANDS, spectrumBasicColors, false, 100, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "USA", NUM_BANDS, USAColors_p, true, 0, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum 2", 32, spectrumBasicColors, false, 100, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum++", NUM_BANDS, spectrumBasicColors, false, 0, 40, -1.0, 2.0);
ADD_EFFECT(EFFECT_MATRIX_SMFIRE2021, PatternSMFire2021);
ADD_EFFECT(EFFECT_MATRIX_GHOST_WAVE, GhostWave, "GhostWave", 0, 30, false, 10);
ADD_EFFECT(EFFECT_MATRIX_SMGAMMA, PatternSMGamma);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Three Rings", GIFIdentifier::ThreeRings);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Rings", GIFIdentifier::ThreeRings);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Atomic", GIFIdentifier::Atomic);
ADD_EFFECT(EFFECT_MATRIX_ANIMATEDGIF, PatternAnimatedGIF, "Bananaman", GIFIdentifier::Banana, true, CRGB::DarkBlue);
ADD_EFFECT(EFFECT_MATRIX_SMMETA_BALLS, PatternSMMetaBalls);
ADD_EFFECT(EFFECT_MATRIX_SMSUPERNOVA, PatternSMSupernova);
ADD_EFFECT(EFFECT_MATRIX_CUBE, PatternCube);
ADD_EFFECT(EFFECT_MATRIX_LIFE, PatternLife);
ADD_EFFECT(EFFECT_MATRIX_CIRCUIT, PatternCircuit);

ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "USA", NUM_BANDS, USAColors_p, true, 0, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum 2", 32, spectrumBasicColors, false, 100, 0, 0.75, 0.75);
ADD_EFFECT(EFFECT_MATRIX_SPECTRUM_ANALYZER, SpectrumAnalyzerEffect, "Spectrum++", NUM_BANDS, spectrumBasicColors, false, 0, 40, -1.0, 2.0);
ADD_EFFECT(EFFECT_MATRIX_WAVEFORM, WaveformEffect, "WaveIn", 8);
ADD_EFFECT(EFFECT_MATRIX_GHOST_WAVE, GhostWave, "WaveOut", 0, 0, true, 0);

ADD_STARRY_NIGHT_EFFECT(MusicStar, "Stars", RainbowColors_p, 1.0, 1, LINEARBLEND, 2.0, 0.5, 10.0); // Rainbow Music Star

ADD_EFFECT(EFFECT_MATRIX_PONG_CLOCK, PatternPongClock);

#if ENABLE_WIFI
ADD_EFFECT(EFFECT_MATRIX_SUBSCRIBERS, PatternSubscribers);
Expand Down Expand Up @@ -359,7 +358,7 @@ void LoadEffectFactories()

#elif BELT

// Yes, I made a sparkly LED belt and wore it to a party. Batteries toO!
// Yes, I made a sparkly LED belt and wore it to a party. Batteries too!
ADD_EFFECT(EFFECT_TWINKLE, TwinkleEffect, NUM_LEDS / 4, 10);

#elif MAGICMIRROR
Expand Down

0 comments on commit eac9ba6

Please sign in to comment.