esp32-sk6812/src/Animation/MatrixCodeAnimation.cpp

58 lines
1.4 KiB
C++

#include "Animation/MatrixCodeAnimation.h"
#include <iostream>
MatrixCodeAnimation::MatrixCodeAnimation(Fader *fader, int vspeed, int spawn_rate)
: Animation(fader),
m_vspeed(vspeed),
m_spawnRate(spawn_rate)
{
reset();
}
void MatrixCodeAnimation::loop(uint64_t frame)
{
int nled = m_fader->modules_per_strip();
int nstrip = m_fader->strips();
if(frame == 0) {
m_fader->set_fadestep(8);
}
// create new pixels
std::uniform_int_distribution<int> spawnRNG(0, (60*nstrip / m_spawnRate) - 1);
std::uniform_int_distribution<int> vspeedRNG(m_vspeed - 32, m_vspeed + 32);
if(!m_stopping) {
for(int i = 0; i < nstrip; i++) {
if(spawnRNG(m_gen) == 0) {
m_pixels.emplace_back(Pixel{i, 256*nled-1, vspeedRNG(m_gen)});
}
}
}
m_fader->fade_color(Fader::Color{0, 0, 0, 0});
// move all pixels down and render them
for(auto &px: m_pixels) {
px.y -= px.vspeed;
if(px.y >= 0) {
m_fader->set_color(px.x, px.y/256, Fader::Color{0, 192, 0, 16});
}
}
m_pixels.remove_if(
[](const Pixel &px) { return px.y < 0; });
m_fader->update();
m_running = !m_stopping || m_fader->something_changed();
}
void MatrixCodeAnimation::reset(void)
{
m_pixels.clear();
m_stopping = false;
m_running = true;
}