63 lines
1.2 KiB
C++
63 lines
1.2 KiB
C++
#ifndef MODE_MATRIX_H
|
|
#define MODE_MATRIX_H
|
|
|
|
#include "mode/Mode.h"
|
|
|
|
class Matrix final : public Mode {
|
|
|
|
struct Glyph {
|
|
double x = 0;
|
|
double y = 0;
|
|
double velocity = 0;
|
|
uint8_t length = 0;
|
|
};
|
|
|
|
Glyph glyphs[10];
|
|
|
|
public:
|
|
|
|
explicit Matrix(Display& display) : Mode(display) {
|
|
for (auto& glyph: glyphs) {
|
|
resetGlyph(glyph);
|
|
}
|
|
}
|
|
|
|
void resetGlyph(Glyph& glyph) const {
|
|
glyph.x = random(width);
|
|
glyph.y = 0;
|
|
glyph.velocity = random(20) + 5;
|
|
glyph.length = random(8) + 2;
|
|
}
|
|
|
|
const char *getName() override {
|
|
return "Matrix";
|
|
}
|
|
|
|
protected:
|
|
|
|
void step(const microseconds_t microseconds) override {
|
|
for (auto& glyph: glyphs) {
|
|
glyph.y += glyph.velocity * static_cast<double>(microseconds) / 1000000.0;
|
|
if (glyph.y - glyph.length >= height) {
|
|
resetGlyph(glyph);
|
|
}
|
|
}
|
|
markDirty();
|
|
}
|
|
|
|
void draw(Display& display) override {
|
|
display.clear();
|
|
for (const auto& glyph: glyphs) {
|
|
for (auto i = 0; i < glyph.length; ++i) {
|
|
display.setPixel(glyph.x, glyph.y - i, {64, 128, 64});
|
|
}
|
|
if ((static_cast<int>(round(glyph.y)) + glyph.length) % 2 == 0) {
|
|
display.setPixel(glyph.x, glyph.y, {0, 255, 0});
|
|
}
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|