Extended MatrixEffect to also get RainbowMatrixEffect and RandomMatrixEffect.

Side note: You know you understand C++ when you understand the meaning 
and reason for "MatrixEffect** _columns". ;-)
This commit is contained in:
Fabian Schlenz 2019-06-16 12:43:05 +02:00
parent 3edbf6b252
commit e8f3ea3281
3 changed files with 103 additions and 22 deletions

View File

@ -1,5 +1,4 @@
#ifndef effect_matrix_H #pragma once
#define effect_matrix_H
#include "prototypes.h" #include "prototypes.h"
#include "Effect.h" #include "Effect.h"
@ -8,10 +7,12 @@
#include "my_color_palettes.h" #include "my_color_palettes.h"
class MatrixEffectColumn { class MatrixEffectColumn {
private: protected:
int x, y;
int length;
Window* window; Window* window;
int x, y;
int length = 1;
virtual CRGB _getColor(uint8_t height);
private:
uint16_t speed; uint16_t speed;
boolean running; boolean running;
unsigned long last_move = 0; unsigned long last_move = 0;
@ -19,24 +20,47 @@ public:
MatrixEffectColumn(); MatrixEffectColumn();
MatrixEffectColumn(Window* win, int xPos); MatrixEffectColumn(Window* win, int xPos);
void start(); virtual void start();
void advance(); void advance();
void draw(); void draw();
void loop(); void loop();
}; };
class RainbowMatrixEffectColumn : public MatrixEffectColumn {
protected:
CRGB _getColor(uint8_t height) override;
public:
RainbowMatrixEffectColumn() : MatrixEffectColumn() {};
RainbowMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {};
};
class RandomMatrixEffectColumn : public MatrixEffectColumn {
protected:
uint8_t _hue = 42;
CRGB _getColor(uint8_t height) override;
public:
void start() override;
RandomMatrixEffectColumn() : MatrixEffectColumn() {};
RandomMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {};
};
class MatrixEffect : public Effect { class MatrixEffect : public Effect {
private: protected:
MatrixEffectColumn* _columns; MatrixEffectColumn** _columns;
public: public:
boolean can_be_shown_with_clock(); boolean can_be_shown_with_clock();
void start(); virtual void start();
void stop(); void stop();
MatrixEffect(); MatrixEffect();
void loop(); void loop();
}; };
#endif class RainbowMatrixEffect : public MatrixEffect {
public:
void start() override;
};
class RandomMatrixEffect : public MatrixEffect {
public:
void start() override;
};

View File

@ -26,12 +26,7 @@ void MatrixEffectColumn::advance() {
void MatrixEffectColumn::draw() { void MatrixEffectColumn::draw() {
for(int i=0; i<length; i++) { for(int i=0; i<length; i++) {
CRGB color; CRGB color = _getColor(i);
if (i==0) {
color = CRGB(255, 255, 255);
} else {
color = ColorFromPalette((CRGBPalette16)palette_matrix, 255 * (length - i) / length);
}
window->setPixel(x, y-i, &color); window->setPixel(x, y-i, &color);
} }
} }
@ -52,21 +47,79 @@ void MatrixEffectColumn::loop() {
} }
} }
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::start() {
MatrixEffectColumn::start();
_hue = random8();
}
boolean MatrixEffect::can_be_shown_with_clock() { return true; }; boolean MatrixEffect::can_be_shown_with_clock() { return true; };
MatrixEffect::MatrixEffect() { MatrixEffect::MatrixEffect() {
} }
void MatrixEffect::start() { void MatrixEffect::start() {
_columns = new MatrixEffectColumn[LED_WIDTH]; _columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<LED_WIDTH; i++) _columns[i] = MatrixEffectColumn(window, i); for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, i);
}
void RandomMatrixEffect::start() {
_columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, i);
}
void RainbowMatrixEffect::start() {
_columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, i);
} }
void MatrixEffect::stop() { void MatrixEffect::stop() {
for (int i=0; i<window->width; i++) {
delete _columns[i];
}
delete[] _columns; delete[] _columns;
} }
void MatrixEffect::loop() { void MatrixEffect::loop() {
window->clear(); window->clear();
for (int i=0; i<window->width; i++) _columns[i].loop(); for (int i=0; i<window->width; i++) _columns[i]->loop();
} }

View File

@ -35,6 +35,8 @@ AnimationEffect effect_anim_cake(&animation_cake, new CRGB(0x000000), 0, 0);
SingleDynamicEffect effect_single_dynamic; SingleDynamicEffect effect_single_dynamic;
MultiDynamicEffect effect_multi_dynamic; MultiDynamicEffect effect_multi_dynamic;
MatrixEffect effect_matrix; MatrixEffect effect_matrix;
RandomMatrixEffect effect_random_matrix;
RainbowMatrixEffect effect_rainbow_matrix;
CycleEffect effect_cycle; CycleEffect effect_cycle;
TwirlEffect effect_twirl; TwirlEffect effect_twirl;
ConfettiEffect effect_confetti; ConfettiEffect effect_confetti;
@ -60,6 +62,8 @@ void setup_effects() {
effects->add((EffectEntry){"single_dynamic", (Effect *)&effect_single_dynamic}); effects->add((EffectEntry){"single_dynamic", (Effect *)&effect_single_dynamic});
effects->add((EffectEntry){"multi_dynamic", (Effect *)&effect_multi_dynamic}); effects->add((EffectEntry){"multi_dynamic", (Effect *)&effect_multi_dynamic});
effects->add((EffectEntry){"matrix", (Effect *)&effect_matrix}); effects->add((EffectEntry){"matrix", (Effect *)&effect_matrix});
effects->add((EffectEntry){"rainbow_matrix", (Effect *)&effect_rainbow_matrix});
effects->add((EffectEntry){"random_matrix", (Effect *)&effect_random_matrix});
effects->add((EffectEntry){"cycle", (Effect *)&effect_cycle}); effects->add((EffectEntry){"cycle", (Effect *)&effect_cycle});
effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl}); effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl});
effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart}); effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart});