-
Notifications
You must be signed in to change notification settings - Fork 0
/
fire.h
142 lines (121 loc) · 5.78 KB
/
fire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * \
* Implementation of a fire-like effect. *
* *
* Author: Kip (https://github.com/kip93/). *
* Source: https://github.com/kip93/lamp/ *
* License: BSD 3-Clause *
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef FIRE_H_
#define FIRE_H_
#include "effect.h" // Abstract effect structure.
/**
* Fire effect implementation intended for a circular cylindrical LED matrix.
*/
class Fire : public Effect {
public: ///////////////////////////////////////////////////////////////////////
/**
* Destructor. Free up resources.
*/
~Fire() {
delete callback;
}
/**
* Update the contents of the LED matrix.
*/
void update() {
fill(callback);
show(30); // Show changes and keep the code to ~30 FPS.
callback -> update();
}
private: //////////////////////////////////////////////////////////////////////
/**
* The fire-ish colour palette.
*/
static const ColourPalette palette PROGMEM;
/**
* Get a colour from the palette.
*/
static CRGB get_colour(uint8_t index) {
return interpolate_colour(palette, index);
}
/**
* Effect callback that will compute and show the fire.
*/
class Callback : public FillCallback {
public:
/**
* Callback function. Shows the fire animation.
*
* @param i The row index.
* @param j The column index.
*
* @returns An RGB colour to be set at the given coordinates.
*/
CRGB call(uint8_t i, uint8_t j) {
return get_colour(fire[index(i, j)]);
}
/**
* Create the next frame of the fire-like animation.
*/
void update() {
// Shift all rows up and "decay" the colours.
for(uint8_t i = rows - 1; i > 0;--i) {
for(uint8_t j = 0; j < cols; ++j) {
fire[index(i, j)] = qadd8(fire[index(i - 1, j)], 0x1F) - 0x0F;
}
}
// Create a new random bottom row.
for(uint8_t j = 0; j < cols; ++j) {
fire[index(0, j)] = random8(0x20, 0x90);
}
}
private:
/**
* An array with the info to be shown. Contents are indices to be used with the
* colour palette.
*/
uint8_t fire[num_leds] = { // Initialise to a dark background.
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
};
/**
* Compute the linear coordinate of a pixel in the array based on its real
* position.
*
* @param i The row coordinate of the pixel.
* @param j The column coordinate of the pixel.
*
* @warning This will not validate the inputs, and using invalid coordinates
* results in undefined behaviour.
*/
static uint16_t index(uint8_t i, uint8_t j) {
return ((uint16_t) i) + ((uint16_t) j) * ((uint16_t) rows);
}
};
/**
* The callback instance to be sent to the parent class.
*/
Callback * const callback = new Callback();
};
// Populate the contents of the colour palette.
const ColourPalette Fire::palette PROGMEM = {
0xFFD033, 0xFFBE33, 0xFEAE01, 0xFE9A01,
0xFE8F01, 0xFA7D01, 0xEF7101, 0xD86001,
0xAF4A01, 0x923701, 0x651802, 0x470202,
0x2E0001, 0x140101, 0x070101, 0x050000,
};
#endif // FIRE_H_