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