From a7d956209a4d5d9682acfb8139747e6280ba2e27 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Fri, 31 May 2019 05:46:17 +0200 Subject: [PATCH] Added `Effect::start()` and `Effect::stop()` to allow effects to reserve memory on start and free it afterwards. --- include/Effect.h | 2 ++ include/effect_cycle.h | 2 ++ src/effect_cycle.cpp | 11 +++++++++++ src/mqtt.cpp | 2 ++ src/pitrix.cpp | 2 ++ 5 files changed, 19 insertions(+) diff --git a/include/Effect.h b/include/Effect.h index 8c2b230..5232fd2 100644 --- a/include/Effect.h +++ b/include/Effect.h @@ -16,6 +16,8 @@ class Effect { void setWindow(Window win) { window = win; }; + virtual void start() {} + virtual void stop() {} }; #endif diff --git a/include/effect_cycle.h b/include/effect_cycle.h index ad154c7..b68c9c0 100644 --- a/include/effect_cycle.h +++ b/include/effect_cycle.h @@ -10,6 +10,8 @@ class CycleEffect : public Effect { unsigned long effectSince = 0; public: void changeEffect(); + void start(); + void stop(); boolean can_be_shown_with_clock(); boolean clock_as_mask(); diff --git a/src/effect_cycle.cpp b/src/effect_cycle.cpp index 1626091..00ac92b 100644 --- a/src/effect_cycle.cpp +++ b/src/effect_cycle.cpp @@ -6,10 +6,21 @@ void CycleEffect::changeEffect() { int new_id = random8(cycle_effects->size()); new_effect = cycle_effects->get(new_id); } while (&new_effect == &effect); + if (effect) effect->stop(); effect = new_effect; + effect->start(); effectSince = millis(); } +void CycleEffect::start() { + if (!effect) changeEffect(); + else effect->start(); +} + +void CycleEffect::stop() { + if (effect) effect->stop(); +} + boolean CycleEffect::can_be_shown_with_clock() { return effect->can_be_shown_with_clock(); }; diff --git a/src/mqtt.cpp b/src/mqtt.cpp index cd24519..4164f47 100644 --- a/src/mqtt.cpp +++ b/src/mqtt.cpp @@ -22,7 +22,9 @@ void mqtt_callback(char* complete_topic, byte* pl, unsigned int length) { EffectEntry e = effects->get(i); if (strcmp(e.name, payload)==0) { //Serial.printf("Effect found in mqtt_callback: %p\n", (void *)&e->effect); + current_effect->stop(); current_effect = e.effect; + current_effect->start(); clear(); return; } diff --git a/src/pitrix.cpp b/src/pitrix.cpp index f01aeb0..f5b9aaa 100644 --- a/src/pitrix.cpp +++ b/src/pitrix.cpp @@ -31,6 +31,8 @@ void setup() { ntpClient.begin(); mqtt_setup(); LOGln("Core * Setup complete"); + + current_effect->start(); } void loop() {