Effects are now constructed when needed instead of having global objects of every effect flying around.

This commit is contained in:
Fabian Schlenz 2019-06-18 18:14:59 +02:00
parent 5a02050680
commit 0941ad8349
4 changed files with 66 additions and 95 deletions

View File

@ -2,16 +2,17 @@
#define effects_H #define effects_H
#include <SimpleList.h> #include <SimpleList.h>
#include "EffectEntry.h"
#include "Effect.h" #include "Effect.h"
#include "effect_clock.h" #include "effect_clock.h"
extern SimpleList<EffectEntry>* effects; extern const char* cycle_effects[];
extern SimpleList<Effect*>* cycle_effects; extern uint8_t cycle_effects_count;
extern Effect* current_effect; extern Effect* current_effect;
extern ClockEffect effect_clock; extern ClockEffect effect_clock;
Effect* string_to_effect(String s);
bool change_current_effect(String s);
void setup_effects(); void setup_effects();
#endif #endif

View File

@ -2,18 +2,6 @@
#include <Arduino.h> #include <Arduino.h>
typedef struct {
uint8_t *colors;
uint8_t *data;
uint16_t *offsets;
uint16_t *delays;
boolean individual_delays;
int color_count;
int frame_count;
int w;
int h;
} AnimationData;
typedef struct { typedef struct {
uint8_t width; uint8_t width;
uint8_t height; uint8_t height;

View File

@ -1,32 +1,33 @@
#include "effect_cycle.h" #include "effect_cycle.h"
#include "effects.h"
CycleEffect::CycleEffect() {
changeEffect();
}
CycleEffect::~CycleEffect() {
delete effect;
}
void CycleEffect::changeEffect() { void CycleEffect::changeEffect() {
int new_id; int new_id;
if (EFFECT_CYCLE_RANDOM) { if (EFFECT_CYCLE_RANDOM) {
do { do {
new_id = random8(cycle_effects->size()); new_id = random8(cycle_effects_count);
} while (new_id == effect_id); } while (new_id == effect_id);
} else { } else {
new_id = (effect_id + 1) % cycle_effects->size(); new_id = (effect_id + 1) % cycle_effects_count;
} }
LOG("CycleEffect * Changing effect from #"); LOG(effect_id); LOG(" to #"); LOGln(new_id); LOGln("CycleEffect * Changing effect from #%d to #%d", effect_id, new_id);
if (effect) effect->stop(); delay(25);
effect = cycle_effects->get(new_id); if (effect) delete effect;
effect->start(); LOGln("CycleEffect * Searching for new effect '%s'", cycle_effects[new_id]);
delay(25);
effect = string_to_effect(cycle_effects[new_id]);
effect_id = new_id; effect_id = new_id;
effectSince = millis(); effectSince = millis();
} }
void CycleEffect::start() {
if (!effect) changeEffect();
else effect->start();
}
void CycleEffect::stop() {
effect_id = -1;
if (effect) effect->stop();
}
boolean CycleEffect::can_be_shown_with_clock() { boolean CycleEffect::can_be_shown_with_clock() {
return effect->can_be_shown_with_clock(); return effect->can_be_shown_with_clock();
}; };

View File

@ -1,7 +1,6 @@
#include "effects.h" #include "effects.h"
#include "animations.h"
#include "my_fastled.h" #include "my_fastled.h"
#include "EffectEntry.h" #include <ErriezCRC32.h>
#include "effect_bell.h" #include "effect_bell.h"
#include "effect_sinematrix3.h" #include "effect_sinematrix3.h"
#include "effect_big_clock.h" #include "effect_big_clock.h"
@ -19,69 +18,51 @@
#include "effect_gol.h" #include "effect_gol.h"
#include "effect_pixelclock.h" #include "effect_pixelclock.h"
SimpleList<EffectEntry>* effects;
SimpleList<Effect*>* cycle_effects;
ClockEffect effect_clock;
Sinematrix3Effect effect_sinematrix3;
BigClockEffect effect_big_clock;
//ClockEffect effect_clock; <-- generated as global variable in effects.h
BellEffect effect_bell;
StaticEffect effect_off(CRGB(0x000000));
AnimationEffect effect_anim_koopa(&animation_koopa, new CRGB(0x000000), 0, 0);
AnimationEffect effect_anim_couple_rain(&animation_couple_rain, new CRGB(0x000000), -8, -16);
AnimationEffect effect_anim_heart(&animation_heart, new CRGB(0x000000), 0, 0);
AnimationEffect effect_anim_cake(&animation_cake, new CRGB(0x000000), 0, 0);
SingleDynamicEffect effect_single_dynamic;
MultiDynamicEffect effect_multi_dynamic;
MatrixEffect effect_matrix;
RandomMatrixEffect effect_random_matrix;
RainbowMatrixEffect effect_rainbow_matrix;
CycleEffect effect_cycle;
TwirlEffect effect_twirl;
ConfettiEffect effect_confetti;
SnakeEffect effect_snake;
FireEffect effect_fire;
FireworkEffect effect_firework;
GolEffect effect_gol;
PixelClockEffect effect_pixelclock;
Effect* current_effect; Effect* current_effect;
void setup_effects() { ClockEffect effect_clock;
effects = new SimpleList<EffectEntry>();
cycle_effects = new SimpleList<Effect*>();
effects->add((EffectEntry){"sinematrix3", (Effect *)&effect_sinematrix3}); Effect* string_to_effect(String name) {
effects->add((EffectEntry){"big_clock", (Effect *)&effect_big_clock}); uint32_t crc = crc32String(name.c_str());
effects->add((EffectEntry){"clock", (Effect *)&effect_clock}); switch (crc) {
effects->add((EffectEntry){"bell", (Effect *)&effect_bell}); // use e.g. https://crccalc.com/ for the conversion of name to crc.
effects->add((EffectEntry){"off", (Effect *)&effect_off}); case 0xD682E3C8 /* sinematrix3 */ : return new Sinematrix3Effect();
effects->add((EffectEntry){"koopa", (Effect *)&effect_anim_koopa}); case 0x90A887DA /* big_clock */ : return new BigClockEffect();
effects->add((EffectEntry){"couple_rain", (Effect *)&effect_anim_couple_rain}); case 0xBE7BBE92 /* clock */ : return new ClockEffect();
effects->add((EffectEntry){"single_dynamic", (Effect *)&effect_single_dynamic}); case 0x733BE087 /* bell */ : return new BellEffect();
effects->add((EffectEntry){"multi_dynamic", (Effect *)&effect_multi_dynamic}); case 0x2BBC5D43 /* off */ : return new StaticEffect(0x000000);
effects->add((EffectEntry){"matrix", (Effect *)&effect_matrix}); case 0x1D84F231 /* koopa */ : return new AnimationEffect("/koopa.pia", new CRGB(0x000000), 0, 0);
effects->add((EffectEntry){"rainbow_matrix", (Effect *)&effect_rainbow_matrix}); case 0xAC43BCF1 /* couple_rain */ : return new AnimationEffect("/couple_rain.pia", new CRGB(0x000000), -8, -16);
effects->add((EffectEntry){"random_matrix", (Effect *)&effect_random_matrix}); case 0xF1B117F7 /* single_dynamic */ : return new SingleDynamicEffect();
effects->add((EffectEntry){"cycle", (Effect *)&effect_cycle}); case 0xF52F2804 /* multi_dynamic */ : return new MultiDynamicEffect();
effects->add((EffectEntry){"twirl", (Effect *)&effect_twirl}); case 0xF83341CF /* matrix */ : return new MatrixEffect();
effects->add((EffectEntry){"heart", (Effect*)&effect_anim_heart}); case 0xD2B79DD0 /* rainbow_matrix */ : return new RainbowMatrixEffect();
effects->add((EffectEntry){"confetti", (Effect *)&effect_confetti}); case 0xE8DD3433 /* random_matrix */ : return new RandomMatrixEffect();
effects->add((EffectEntry){"snake", (Effect *)&effect_snake}); case 0xB086D193 /* cycle */ : return new CycleEffect();
effects->add((EffectEntry){"fire", (Effect *)&effect_fire}); case 0x2293EF9F /* twirl */ : return new TwirlEffect();
effects->add((EffectEntry){"firework", (Effect *)&effect_firework}); case 0x60ECC3E6 /* heart */ : return new AnimationEffect("/heart.pia", new CRGB(0x000000), 0, 0);
effects->add((EffectEntry){"gol", (Effect *)&effect_gol}); case 0x42090A49 /* confetti */ : return new ConfettiEffect();
effects->add((EffectEntry){"cake", (Effect *)&effect_anim_cake}); case 0x516D6B9E /* snake */ : return new SnakeEffect();
effects->add((EffectEntry){"pixel_clock", (Effect *)&effect_pixelclock}); case 0x58DE09CF /* fire */ : return new FireEffect();
case 0x08BA9C08 /* firework */ : return new FireworkEffect();
cycle_effects->add(&effect_sinematrix3); case 0x14B85EAC /* gol */ : return new GolEffect();
cycle_effects->add(&effect_multi_dynamic); case 0xFA13015D /* cake */ : return new AnimationEffect("/cake.pia", new CRGB(0x000000), 0, 0);
cycle_effects->add(&effect_matrix); case 0xA2B0D68B /* pixel_clock */ : return new PixelClockEffect();
cycle_effects->add(&effect_confetti); default : return NULL;
cycle_effects->add(&effect_single_dynamic); };
cycle_effects->add(&effect_snake); }
cycle_effects->add(&effect_gol);
bool change_current_effect(String name) {
current_effect = &effect_cycle; Effect* new_effect = string_to_effect(name);
if (new_effect == NULL) return false;
delete current_effect;
current_effect = new_effect;
return true;
}
const char* cycle_effects[] = {"sinematrix3", "multi_dynamic", "matrix", "confetti", "single_dynamic", "snake", "gol"};
uint8_t cycle_effects_count = 7;
void setup_effects() {
current_effect = new CycleEffect();
} }