-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFireChemical.h
More file actions
211 lines (185 loc) · 7.26 KB
/
FireChemical.h
File metadata and controls
211 lines (185 loc) · 7.26 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Slightly modified version of the fire pattern from MessageTorch by Lukas Zeller:
// https://github.com/plan44/messagetorch
// The MIT License (MIT)
// Copyright (c) 2014 Lukas Zeller
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// torch parameters
uint16_t cycle_waitChemicalFire = 1; // 0..255
byte flame_minChemicalFire = 70; // 0..255
byte flame_maxChemicalFire = 250; // 0..255
byte random_spark_probabilityChemicalFire = 1; // 0..100
byte spark_minChemicalFire = 60; // 0..255
byte spark_maxChemicalFire = 255; // 0..255
byte spark_tfrChemicalFire = 40; // 0..256 how much energy is transferred up for a spark per cycle
uint16_t spark_capChemicalFire = 200; // 0..255: spark cells: how much energy is retained from previous cycle
uint16_t up_radChemicalFire = 50; // up radiation
uint16_t side_radChemicalFire = 40; // sidewards radiation
uint16_t heat_capChemicalFire = 0; // 0..255: passive cells: how much energy is retained from previous cycle
byte red_bgChemicalFire = 0;
byte green_bgChemicalFire = 0;
byte blue_bgChemicalFire = 0;
byte red_biasChemicalFire = 35;
byte green_biasChemicalFire = 90;
byte blue_biasChemicalFire = 0;
int red_energyChemicalFire = 100;
int green_energyChemicalFire = 120;
int blue_energyChemicalFire = 0;
byte upside_downChemicalFire = 0; // if set, flame (or rather: drop) animation is upside down. Text remains as-is
// torch mode
// ==========
byte currentEnergyChemicalFire[numLeds]; // current energy level
byte nextEnergyChemicalFire[numLeds]; // next energy level
byte energyModeChemicalFire[numLeds]; // mode how energy is calculated for this point
enum {
torch_passiveChemicalFire = 0, // just environment, glow from nearby radiation
torch_nopChemicalFire = 1, // no processing
torch_sparkChemicalFire= 2, // slowly looses energy, moves up
torch_sparkChemicalFire_temp = 3, // a spark still getting energy from the level below
};
inline void reduceChemicalFire(byte &aByte, byte aAmount, byte aMin = 0)
{
int r = aByte-aAmount;
if (r<aMin)
aByte = aMin;
else
aByte = (byte)r;
}
inline void increaseChemicalFire(byte &aByte, byte aAmount, byte aMax = 255)
{
int r = aByte+aAmount;
if (r>aMax)
aByte = aMax;
else
aByte = (byte)r;
}
uint16_t randomChemicalFire(uint16_t aMinOrMax, uint16_t aMax = 0) // not really sure if this is needed at this stage
{
if (aMax==0) {
aMax = aMinOrMax;
aMinOrMax = 0;
}
uint32_t r = aMinOrMax;
aMax = aMax - aMinOrMax + 1;
r += rand() % aMax;
return r;
}
void resetEnergyChemicalFire()
{
for (int i=0; i<numLeds; i++) {
currentEnergyChemicalFire[i] = 0;
nextEnergyChemicalFire[i] = 0;
energyModeChemicalFire[i] = torch_passiveChemicalFire;
}
}
void calcnextEnergyChemicalFire()
{
int i = 0;
for (int y=0; y<levels; y++) {
for (int x=0; x<ledsPerLevel; x++) {
byte e = currentEnergyChemicalFire[i];
byte m = energyModeChemicalFire[i];
switch (m) {
case torch_sparkChemicalFire: {
// loose transfer up energy as long as the is any
reduceChemicalFire(e, spark_tfrChemicalFire);
// cell above is temp spark, sucking up energy from this cell until empty
if (y<levels-1) {
energyModeChemicalFire[i+ledsPerLevel] = torch_sparkChemicalFire_temp;
}
break;
}
case torch_sparkChemicalFire_temp: {
// just getting some energy from below
byte e2 = currentEnergyChemicalFire[i-ledsPerLevel];
if (e2<spark_tfrChemicalFire) {
// cell below is exhausted, becomes passive
energyModeChemicalFire[i-ledsPerLevel] = torch_passive;
// gobble up rest of energy
increaseChemicalFire(e, e2);
// loose some overall energy
e = ((int)e*spark_capChemicalFire)>>8;
// this cell becomes active spark
energyModeChemicalFire[i] = torch_sparkChemicalFire;
}
else {
increaseChemicalFire(e, spark_tfrChemicalFire);
}
break;
}
case torch_passive: {
e = ((int)e*heat_capChemicalFire)>>8;
increaseChemicalFire(e, ((((int)currentEnergyChemicalFire[i-1]+(int)currentEnergyChemicalFire[i+1])*side_radChemicalFire)>>9) + (((int)currentEnergyChemicalFire[i-ledsPerLevel]*up_radChemicalFire)>>8));
}
default:
break;
}
nextEnergyChemicalFire[i++] = e;
}
}
}
const uint8_t energymapChemicalFire[32] = {0, 64, 96, 112, 128, 144, 152, 160, 168, 176, 184, 184, 192, 200, 200, 208, 208, 216, 216, 224, 224, 224, 232, 232, 232, 240, 240, 240, 240, 248, 248, 248};
void calcNextColorsChemicalFire()
{
for (int i=0; i<numLeds; i++) {
int ei; // index into energy calculation buffer
if (upside_downChemicalFire)
ei = numLeds-i;
else
ei = i;
uint16_t e = nextEnergyChemicalFire[ei];
currentEnergyChemicalFire[ei] = e;
if (e>250)
leds[i] = CRGB(170, 170, e); // blueish extra-bright spark
else {
if (e>0) {
// energy to brightness is non-linear
byte eb = energymap[e>>3];
byte r = red_biasChemicalFire;
byte g = green_biasChemicalFire;
byte b = blue_biasChemicalFire;
increaseChemicalFire(r, (eb*red_energyChemicalFire)>>8);
increaseChemicalFire(g, (eb*green_energyChemicalFire)>>8);
increaseChemicalFire(b, (eb*blue_energyChemicalFire)>>8);
leds[i] = CRGB(r, g, b);
}
else {
// background, no energy
leds[i] = CRGB(red_bgChemicalFire, green_bgChemicalFire, blue_bgChemicalFire);
}
}
}
}
void injectRandomChemicalFire()
{
// random flame energy at bottom row
for (int i=0; i<ledsPerLevel; i++) {
currentEnergyChemicalFire[i] = random8(flame_minChemicalFire, flame_maxChemicalFire);
energyModeChemicalFire[i] = torch_nopChemicalFire;
}
// random sparks at second row
for (int i=ledsPerLevel; i<2*ledsPerLevel; i++) {
if (energyModeChemicalFire[i]!=torch_sparkChemicalFire && random8(100)<random_spark_probabilityChemicalFire) {
currentEnergyChemicalFire[i] = random8(spark_minChemicalFire, spark_maxChemicalFire);
energyModeChemicalFire[i] = torch_sparkChemicalFire;
}
}
}
uint16_t FireChemical() {
injectRandomChemicalFire();
calcnextEnergyChemicalFire();
calcNextColorsChemicalFire();
return 1;
}