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
#include <SimpleList.h>
#include "EffectEntry.h"
#include "Effect.h"
#include "effect_clock.h"
extern SimpleList<EffectEntry>* effects;
extern SimpleList<Effect*>* cycle_effects;
extern const char* cycle_effects[];
extern uint8_t cycle_effects_count;
extern Effect* current_effect;
extern ClockEffect effect_clock;
Effect* string_to_effect(String s);
bool change_current_effect(String s);
void setup_effects();
#endif

View File

@ -2,18 +2,6 @@
#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 {
uint8_t width;
uint8_t height;

View File

@ -1,32 +1,33 @@
#include "effect_cycle.h"
#include "effects.h"
CycleEffect::CycleEffect() {
changeEffect();
}
CycleEffect::~CycleEffect() {
delete effect;
}
void CycleEffect::changeEffect() {
int new_id;
if (EFFECT_CYCLE_RANDOM) {
do {
new_id = random8(cycle_effects->size());
new_id = random8(cycle_effects_count);
} while (new_id == effect_id);
} 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);
if (effect) effect->stop();
effect = cycle_effects->get(new_id);
effect->start();
LOGln("CycleEffect * Changing effect from #%d to #%d", effect_id, new_id);
delay(25);
if (effect) delete effect;
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;
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() {
return effect->can_be_shown_with_clock();
};

View File

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