Effects now use Constructor and Destructor to initialize or delete their data, instead of using start() and stop().

This commit is contained in:
Fabian Schlenz 2019-06-18 18:09:05 +02:00
parent 72cdb46451
commit 1912772da3
13 changed files with 58 additions and 60 deletions

View File

@ -9,6 +9,7 @@ class Effect {
protected: protected:
Window* window = Window::getFullWindow(); // Use a full screen window per default. Window* window = Window::getFullWindow(); // Use a full screen window per default.
public: public:
virtual ~Effect() {};
virtual void loop() = 0; virtual void loop() = 0;
boolean supports_window = false; boolean supports_window = false;
virtual boolean can_be_shown_with_clock() { return false; }; virtual boolean can_be_shown_with_clock() { return false; };
@ -16,8 +17,6 @@ public:
void setWindow(Window* win) { void setWindow(Window* win) {
window = win; window = win;
}; };
virtual void start() {}
virtual void stop() {}
}; };
#endif #endif

View File

@ -6,13 +6,13 @@
class CycleEffect : public Effect { class CycleEffect : public Effect {
private: private:
Effect* effect; Effect* effect = NULL;
uint16_t effect_id = -1; uint16_t effect_id = -1;
unsigned long effectSince = 0; unsigned long effectSince = 0;
public: public:
CycleEffect();
~CycleEffect();
void changeEffect(); void changeEffect();
void start();
void stop();
boolean can_be_shown_with_clock(); boolean can_be_shown_with_clock();
boolean clock_as_mask(); boolean clock_as_mask();

View File

@ -13,7 +13,7 @@ private:
static CRGBPalette16 palette; static CRGBPalette16 palette;
public: public:
void start(); FireEffect();
void stop(); ~FireEffect();
void loop(); void loop();
}; };

View File

@ -16,8 +16,7 @@ private:
public: public:
GolEffect(); GolEffect();
void start(); ~GolEffect();
void stop();
void loop(); void loop();
bool can_be_shown_with_clock(); bool can_be_shown_with_clock();
}; };

View File

@ -12,15 +12,14 @@ protected:
int x, y; int x, y;
int length = 1; int length = 1;
virtual CRGB _getColor(uint8_t height); virtual CRGB _getColor(uint8_t height);
virtual void restart();
private: private:
uint16_t speed; uint16_t speed;
boolean running; boolean running;
unsigned long last_move = 0; unsigned long last_move = 0;
public: public:
MatrixEffectColumn();
MatrixEffectColumn(Window* win, int xPos); MatrixEffectColumn(Window* win, int xPos);
virtual ~MatrixEffectColumn() {};
virtual void start();
void advance(); void advance();
void draw(); void draw();
void loop(); void loop();
@ -30,7 +29,6 @@ class RainbowMatrixEffectColumn : public MatrixEffectColumn {
protected: protected:
CRGB _getColor(uint8_t height) override; CRGB _getColor(uint8_t height) override;
public: public:
RainbowMatrixEffectColumn() : MatrixEffectColumn() {};
RainbowMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {}; RainbowMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {};
}; };
@ -38,29 +36,28 @@ class RandomMatrixEffectColumn : public MatrixEffectColumn {
protected: protected:
uint8_t _hue = 42; uint8_t _hue = 42;
CRGB _getColor(uint8_t height) override; CRGB _getColor(uint8_t height) override;
void restart() override;
public: public:
void start() override;
RandomMatrixEffectColumn() : MatrixEffectColumn() {};
RandomMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {}; RandomMatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn(win, xPos) {};
}; };
class MatrixEffect : public Effect { class MatrixEffect : public Effect {
protected: protected:
MatrixEffectColumn** _columns; MatrixEffectColumn** _columns;
virtual void _init();
public: public:
boolean can_be_shown_with_clock(); boolean can_be_shown_with_clock();
virtual void start();
void stop();
MatrixEffect(); MatrixEffect();
virtual ~MatrixEffect();
void loop(); void loop();
}; };
class RainbowMatrixEffect : public MatrixEffect { class RainbowMatrixEffect : public MatrixEffect {
public: private:
void start() override; void _init() override;
}; };
class RandomMatrixEffect : public MatrixEffect { class RandomMatrixEffect : public MatrixEffect {
public: private:
void start() override; void _init() override;
}; };

View File

@ -9,8 +9,7 @@ private:
CRGB* _color_minutes; CRGB* _color_minutes;
public: public:
PixelClockEffect(); PixelClockEffect();
void start(); ~PixelClockEffect();
void stop();
void loop(); void loop();
bool can_be_shown_with_clock(); bool can_be_shown_with_clock();
}; };

View File

@ -1,24 +1,21 @@
#include "effect_animation.h" #include "effect_animation.h"
#include "functions.h" #include "functions.h"
AnimationEffect::AnimationEffect(AnimationData* anim, CRGB* bg, int x, int y) { AnimationEffect::AnimationEffect(const char* name, CRGB* bg, int x, int y) {
this->animation_data = anim;
this->bg_color = bg; this->bg_color = bg;
this->xOffset = x; this->xOffset = x;
this->yOffset = y; this->yOffset = y;
}
void AnimationEffect::start() { this->animation = new Animation(name, window);
this->animation = new Animation(this->animation_data);
this->animation->setBgColor(this->bg_color); this->animation->setBgColor(this->bg_color);
this->animation->setOffsets(this->xOffset, this->yOffset); this->animation->setOffsets(this->xOffset, this->yOffset);
} }
void AnimationEffect::stop() { AnimationEffect::~AnimationEffect() {
delete this->animation; delete this->animation;
} }
void AnimationEffect::loop() { void AnimationEffect::loop() {
this->animation->drawFrame(this->window); this->animation->drawFrame();
this->animation->advance(); this->animation->advance();
} }

View File

@ -4,13 +4,13 @@
#include "my_fastled.h" #include "my_fastled.h"
#include "functions.h" #include "functions.h"
void FireEffect::start() { FireEffect::FireEffect() {
this->data = new uint8_t[this->window->width * this->window->height]; this->data = new uint8_t[this->window->width * this->window->height];
for (int i=0; i<(this->window->width * this->window->height); i++) this->data[i]=0; for (int i=0; i<(this->window->width * this->window->height); i++) this->data[i]=0;
for (int i=0; i<this->window->width; i++) this->data[i]=this->spark_temp(); for (int i=0; i<this->window->width; i++) this->data[i]=this->spark_temp();
} }
void FireEffect::stop() { FireEffect::~FireEffect() {
delete [] this->data; delete [] this->data;
} }

View File

@ -3,11 +3,7 @@
GolEffect::GolEffect() { GolEffect::GolEffect() {
this->window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6); this->window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6);
}
bool GolEffect::can_be_shown_with_clock() { return true; }
void GolEffect::start() {
_data = new uint8_t[this->window->count]; _data = new uint8_t[this->window->count];
_old = new uint8_t[this->window->count]; _old = new uint8_t[this->window->count];
for(uint16_t i=0; i<this->window->count; i++) { for(uint16_t i=0; i<this->window->count; i++) {
@ -16,6 +12,8 @@ void GolEffect::start() {
_initialize(); _initialize();
} }
bool GolEffect::can_be_shown_with_clock() { return true; }
void GolEffect::_initialize() { void GolEffect::_initialize() {
for(uint16_t i=0; i<this->window->count; i++) { for(uint16_t i=0; i<this->window->count; i++) {
_data[i] = random8() < EFFECT_GOL_START_PERCENTAGE ? 1 : 0; _data[i] = random8() < EFFECT_GOL_START_PERCENTAGE ? 1 : 0;
@ -25,7 +23,7 @@ void GolEffect::_initialize() {
_step = 0; _step = 0;
} }
void GolEffect::stop() { GolEffect::~GolEffect() {
delete[] _data; delete[] _data;
delete[] _old; delete[] _old;
} }

View File

@ -2,17 +2,14 @@
#include "my_color_palettes.h" #include "my_color_palettes.h"
#include "functions.h" #include "functions.h"
MatrixEffectColumn::MatrixEffectColumn() { MatrixEffectColumn::MatrixEffectColumn(Window* win, int xPos) {
}
MatrixEffectColumn::MatrixEffectColumn(Window* win, int xPos) : MatrixEffectColumn() {
window = win; window = win;
x = xPos; x = xPos;
start(); restart();
y = random8(0, window->height); y = random8(0, window->height);
} }
void MatrixEffectColumn::start() { void MatrixEffectColumn::restart() {
y=-1; y=-1;
length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX); length = random8(EFFECT_MATRIX_LENGTH_MIN, EFFECT_MATRIX_LENGTH_MAX);
running = true; running = true;
@ -35,7 +32,7 @@ void MatrixEffectColumn::loop() {
if (!running) { if (!running) {
if (random8() < 20) { if (random8() < 20) {
// Start the column again. // Start the column again.
start(); restart();
} }
} else { } else {
if (millis() - last_move > speed) { if (millis() - last_move > speed) {
@ -78,8 +75,8 @@ CRGB RandomMatrixEffectColumn::_getColor(uint8_t i) {
return color; return color;
} }
void RandomMatrixEffectColumn::start() { void RandomMatrixEffectColumn::restart() {
MatrixEffectColumn::start(); MatrixEffectColumn::restart();
_hue = random8(); _hue = random8();
} }
@ -95,24 +92,23 @@ void RandomMatrixEffectColumn::start() {
boolean MatrixEffect::can_be_shown_with_clock() { return true; }; boolean MatrixEffect::can_be_shown_with_clock() { return true; };
MatrixEffect::MatrixEffect() { MatrixEffect::MatrixEffect() {
_columns = new MatrixEffectColumn* [window->width];
_init();
} }
void MatrixEffect::start() { void MatrixEffect::_init() {
_columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, i); for (int i=0; i<window->width; i++) _columns[i] = new MatrixEffectColumn(window, i);
} }
void RandomMatrixEffect::start() { void RandomMatrixEffect::_init() {
_columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, i); for (int i=0; i<window->width; i++) _columns[i] = new RandomMatrixEffectColumn(window, i);
} }
void RainbowMatrixEffect::start() { void RainbowMatrixEffect::_init() {
_columns = new MatrixEffectColumn* [window->width];
for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, i); for (int i=0; i<window->width; i++) _columns[i] = new RainbowMatrixEffectColumn(window, i);
} }
void MatrixEffect::stop() { MatrixEffect::~MatrixEffect() {
for (int i=0; i<window->width; i++) { for (int i=0; i<window->width; i++) {
delete _columns[i]; delete _columns[i];
} }

View File

@ -3,14 +3,11 @@
PixelClockEffect::PixelClockEffect() { PixelClockEffect::PixelClockEffect() {
window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6); window = new Window(0, 0, LED_WIDTH, LED_HEIGHT-6);
}
void PixelClockEffect::start() {
_color_seconds = new CRGB(0x00FF00); _color_seconds = new CRGB(0x00FF00);
_color_minutes = new CRGB(0xFFFF00); _color_minutes = new CRGB(0xFFFF00);
} }
void PixelClockEffect::stop() { PixelClockEffect::~PixelClockEffect() {
delete _color_seconds; delete _color_seconds;
delete _color_minutes; delete _color_minutes;
} }

View File

@ -44,8 +44,6 @@ void setup() {
mqtt_setup(); mqtt_setup();
#endif #endif
LOGln("Core * Setup complete"); LOGln("Core * Setup complete");
current_effect->start();
} }
void loop() { void loop() {

18
test.txt Normal file
View File

@ -0,0 +1,18 @@
Original:
DATA: [==== ] 40.5% (used 33164 bytes from 81920 bytes)
PROGRAM: [==== ] 36.0% (used 375664 bytes from 1044464 bytes)
lots of compares:
DATA: [==== ] 38.6% (used 31640 bytes from 81920 bytes)
PROGRAM: [==== ] 35.6% (used 371740 bytes from 1044464 bytes)
crc32:
DATA: [==== ] 38.5% (used 31532 bytes from 81920 bytes)
PROGRAM: [==== ] 35.6% (used 371456 bytes from 1044464 bytes)
original:
DATA: [==== ] 39.4% (used 32256 bytes from 81920 bytes) │pitrix_dev/log MQTT * Received data for topic pitrix_dev/uptime with payload 391
PROGRAM: [==== ] 37.4% (used 390380 bytes from 1044464 bytes)
DATA: [==== ] 37.4% (used 30608 bytes from 81920 bytes) │pitrix_dev/free_heap 43624
PROGRAM: [==== ] 37.4% (used 390556 bytes from 1044464 bytes)