FastLED 3.9.15
Loading...
Searching...
No Matches
fire2012.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/vector.h"
4#include "fl/fx/fx1d.h"
5#include "fl/stl/noexcept.h"
6
7namespace fl {
8
10// Fire2012 by Mark Kriegsman, July 2012
11// as part of "Five Elements" shown here: http://youtu.be/knWiGsmgycY
13// This basic one-dimensional 'fire' simulation works roughly as follows:
14// There's a underlying array of 'heat' cells, that model the temperature
15// at each point along the line. Every cycle through the simulation,
16// four steps are performed:
17// 1) All cells cool down a little bit, losing heat to the air
18// 2) The heat from each cell drifts 'up' and diffuses a little
19// 3) Sometimes randomly new 'sparks' of heat are added at the bottom
20// 4) The heat from each cell is rendered as a color into the leds array
21// The heat-to-color mapping uses a black-body radiation approximation.
22//
23// Temperature is in arbitrary units from 0 (cold black) to 255 (white hot).
24//
25// This simulation scales it self a bit depending on NUM_LEDS; it should look
26// "OK" on anywhere from 20 to 100 LEDs without too much tweaking.
27//
28// I recommend running this simulation at anywhere from 30-100 frames per
29// second, meaning an interframe delay of about 10-35 milliseconds.
30//
31// Looks best on a high-density LED setup (60+ pixels/meter).
32//
33//
34// There are two main parameters you can play with to control the look and
35// feel of your fire: COOLING (used in step 1 above), and SPARKING (used
36// in step 3 above).
37//
38// COOLING: How much does the air cool as it rises?
39// Less cooling = taller flames. More cooling = shorter flames.
40// Default 50, suggested range 20-100
41
42// SPARKING: What chance (out of 255) is there that a new spark will be lit?
43// Higher chance = more roaring fire. Lower chance = more flickery fire.
44// Default 120, suggested range 50-200.
45
47
48class Fire2012 : public Fx1d {
49 public:
50 Fire2012(u16 num_leds, u8 cooling = 55, u8 sparking = 120,
51 bool reverse_direction = false,
52 const CRGBPalette16 &palette = (const CRGBPalette16 &)HeatColors_p)
53 : Fx1d(num_leds), cooling(cooling), sparking(sparking),
55 heat.resize(num_leds); // Vector elements are default-initialized
56 }
57
59
60 void draw(DrawContext context) override {
61 fl::span<CRGB> leds = context.leds;
62 if (leds.empty()) {
63 return;
64 }
65
66 // Step 1. Cool down every cell a little
67 for (u16 i = 0; i < mNumLeds; i++) {
68 heat[i] =
69 qsub8(heat[i], random8(0, ((cooling * 10) / mNumLeds) + 2));
70 }
71
72 // Step 2. Heat from each cell drifts 'up' and diffuses a little
73 for (u16 k = mNumLeds - 1; k >= 2; k--) {
74 heat[k] = (heat[k - 1] + heat[k - 2] + heat[k - 2]) / 3;
75 }
76
77 // Step 3. Randomly ignite new 'sparks' of heat near the bottom
78 if (random8() < sparking) {
79 int y = random8(7);
80 heat[y] = qadd8(heat[y], random8(160, 255));
81 }
82
83 // Step 4. Map from heat cells to LED colors
84 for (u16 j = 0; j < mNumLeds; j++) {
85 // Scale the heat value from 0-255 down to 0-240
86 // for best results with color palettes.
87 u8 colorindex = scale8(heat[j], 240);
88 CRGB color = ColorFromPalette(palette, colorindex);
89 int pixelnumber;
91 pixelnumber = (mNumLeds - 1) - j;
92 } else {
93 pixelnumber = j;
94 }
95 leds[pixelnumber] = color;
96 }
97 }
98
99 fl::string fxName() const override { return "Fire2012"; }
100
101 private:
106 CRGBPalette16 palette;
107};
108
109} // namespace fl
fl::CRGB leds[NUM_LEDS]
CRGBPalette16 palette
Definition fire2012.h:106
Fire2012(u16 num_leds, u8 cooling=55, u8 sparking=120, bool reverse_direction=false, const CRGBPalette16 &palette=(const CRGBPalette16 &) HeatColors_p)
Definition fire2012.h:50
void draw(DrawContext context) override
Definition fire2012.h:60
fl::vector_psram< u8 > heat
Definition fire2012.h:102
~Fire2012() FL_NOEXCEPT
Definition fire2012.h:58
fl::string fxName() const override
Definition fire2012.h:99
bool reverse_direction
Definition fire2012.h:105
Fx1d(u16 numLeds)
Definition fx1d.h:12
u16 mNumLeds
Definition fx.h:53
const TProgmemRGBPalette16 HeatColors_p
Approximate "black body radiation" palette, akin to the FastLED HeatColor() function.
LIB8STATIC fl::u8 random8() FL_NOEXCEPT
Generate an 8-bit random number.
Definition random8.h:53
unsigned char u8
Definition stdint.h:131
CRGB ColorFromPalette(const CRGBPalette16 &pal, fl::u8 index, fl::u8 brightness, TBlendType blendType)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR(type)
Definition shared_ptr.h:535
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
fl::span< CRGB > leds