pitrix/src/effect_fire.cpp

55 lines
1.6 KiB
C++
Raw Normal View History

#include "effect_fire.h"
#include "my_color_palettes.h"
#include "config.h"
#include "my_fastled.h"
#include "functions.h"
void FireEffect::start() {
this->data = new uint8_t[LED_COUNT];
for (int i=0; i<LED_COUNT; i++) this->data[i]=0;
for (int i=0; i<LED_WIDTH; i++) this->data[i]=this->spark_temp();
}
void FireEffect::stop() {
delete [] this->data;
}
void FireEffect::loop() {
cooldown();
spark();
propagate();
draw();
}
void FireEffect::cooldown() {
for(int i=0; i<LED_COUNT; i++) this->data[i] = scale8(this->data[i], EFFECT_FIRE_COOLDOWN); // 240 or something
}
void FireEffect::spark() {
for(int x=0; x<LED_WIDTH; x++) if (random8(EFFECT_FIRE_SPARK_CHANCE)==0) this->data[x] = this->spark_temp();
}
inline uint8_t FireEffect::spark_temp() {
return random8(180, 255);
}
void FireEffect::propagate() {
for (int y=1; y<LED_HEIGHT; y++) for (int x=0; x<LED_WIDTH; x++) {
int index = y*LED_WIDTH + x;
uint8_t below_index = (y-1)*LED_WIDTH + x;
if (x==0) {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index+1], 64);
} else if (x==LED_WIDTH-1) {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index-1], 64);
} else {
this->data[index] = scale8(this->data[below_index], 128) + scale8(this->data[below_index-1], 32) + scale8(this->data[below_index+1], 32);
}
}
}
void FireEffect::draw() {
for (int y=1; y<LED_HEIGHT; y++) for (int x=0; x<LED_WIDTH; x++) {
setPixel(x, LED_HEIGHT - 1 - y, ColorFromPalette((CRGBPalette16) palette_fire, this->data[y*LED_WIDTH + x]));
}
}