FastLED 3.9.15
Loading...
Searching...
No Matches
fire2012.h
Go to the documentation of this file.
1#pragma once
2
3#include "FastLED.h"
4#include "fl/namespace.h"
5#include "fx/fx1d.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(uint16_t num_leds, uint8_t cooling = 55, uint8_t 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.reset(new uint8_t[num_leds]()); // Initialize to zero
56 }
57
59
60 void draw(DrawContext context) override {
61 CRGB *leds = context.leds;
62 if (leds == nullptr) {
63 return;
64 }
65
66 // Step 1. Cool down every cell a little
67 for (uint16_t 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 (uint16_t 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 (uint16_t 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 uint8_t 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::Str fxName() const override { return "Fire2012"; }
100
101 private:
103 uint8_t cooling;
104 uint8_t sparking;
106 CRGBPalette16 palette;
107};
108
109} // namespace fl
CRGB leds[NUM_LEDS]
Definition Apa102.ino:11
int y
Definition Audio.ino:72
central include file for FastLED, defines the CFastLED class/object
CRGBPalette16 palette
Definition fire2012.h:106
void draw(DrawContext context) override
Definition fire2012.h:60
uint8_t cooling
Definition fire2012.h:103
fl::scoped_array< uint8_t > heat
Definition fire2012.h:102
fl::Str fxName() const override
Definition fire2012.h:99
bool reverse_direction
Definition fire2012.h:105
Fire2012(uint16_t num_leds, uint8_t cooling=55, uint8_t sparking=120, bool reverse_direction=false, const CRGBPalette16 &palette=(const CRGBPalette16 &) HeatColors_p)
Definition fire2012.h:50
uint8_t sparking
Definition fire2012.h:104
Fx1d(uint16_t numLeds)
Definition fx1d.h:14
uint16_t mNumLeds
Definition fx.h:53
_DrawContext DrawContext
Definition fx.h:21
Definition str.h:388
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
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:46
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:40
Implements the FastLED namespace macros.
CRGB ColorFromPalette(const CRGBPalette16 &pal, uint8_t index, uint8_t brightness, TBlendType blendType)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_SMART_PTR(type)
Definition ptr.h:31
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55