pitrix/src/effect_matrix.cpp

122 lines
2.6 KiB
C++
Raw Normal View History

#include "effect_matrix.h"
#include "my_color_palettes.h"
#include "functions.h"
MatrixEffectColumn::MatrixEffectColumn(Window* win, int xPos) {
2019-06-07 04:24:16 +00:00
window = win;
x = xPos;
restart();
y = random8(0, window->height);
}
void MatrixEffectColumn::restart() {
2019-06-07 04:24:16 +00:00
y=-1;
length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX);
running = true;
speed = random8(EFFECT_MATRIX_SPEED_MIN, EFFECT_MATRIX_SPEED_MAX);
}
void MatrixEffectColumn::advance() {
2019-06-07 04:24:16 +00:00
y++;
if (y-length > window->height) running = false;
}
void MatrixEffectColumn::draw() {
2019-06-07 04:24:16 +00:00
for(int i=0; i<length; i++) {
CRGB color = _getColor(i);
window->setPixel(x, y-i, &color);
}
}
void MatrixEffectColumn::loop() {
2019-06-07 04:24:16 +00:00
if (!running) {
if (random8() < 20) {
// Start the column again.
restart();
2019-06-07 04:24:16 +00:00
}
} else {
if (millis() - last_move > speed) {
advance();
last_move = millis();
}
2019-06-07 04:24:16 +00:00
draw();
}
}
CRGB MatrixEffectColumn::_getColor(uint8_t i) {
CRGB color;
if (i==0) {
color = CRGB(255, 255, 255);
} else {
color = CHSV(83, 255, 255 * (length - i) / length);
}
return color;
}
CRGB RainbowMatrixEffectColumn::_getColor(uint8_t i) {
CRGB color;
if (i==0) {
color = CRGB(255, 255, 255);
} else {
color = CHSV(255 * x / window->width, 255, 255 * (length - i) / length);
}
return color;
}
CRGB RandomMatrixEffectColumn::_getColor(uint8_t i) {
CRGB color;
//Serial.print("RandomMatrixEffectColumn::_getColor, hue="); Serial.println(_hue);
if (i==0) {
color = CRGB(255, 255, 255);
} else {
color = CHSV(_hue, 255, 255 * (length - i) / length);
}
return color;
}
void RandomMatrixEffectColumn::restart() {
MatrixEffectColumn::restart();
_hue = random8();
}
boolean MatrixEffect::can_be_shown_with_clock() { return true; };
MatrixEffect::MatrixEffect() {
_columns = new MatrixEffectColumn* [window->width];
_init();
}
void MatrixEffect::_init() {
for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, i);
}
void RandomMatrixEffect::_init() {
for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, i);
}
void RainbowMatrixEffect::_init() {
for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, i);
}
MatrixEffect::~MatrixEffect() {
for (int i=0; i<window->width; i++) {
delete _columns[i];
}
delete[] _columns;
}
void MatrixEffect::loop() {
window->clear();
for (int i=0; i<window->width; i++) _columns[i]->loop();
}