From 17070842993cf1b17682204aef84c6076898297c Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Wed, 29 Apr 2020 20:17:34 +0200 Subject: [PATCH] Added AnimationEffect::Blinker to quickly create a blinking sprite effect from an image file. --- include/effect_animation.h | 8 +++++--- src/effect_animation.cpp | 21 +++++++++++++++++++++ src/effects.cpp | 4 ++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/include/effect_animation.h b/include/effect_animation.h index 82d91db..81cacf9 100644 --- a/include/effect_animation.h +++ b/include/effect_animation.h @@ -11,12 +11,14 @@ private: const char* name; uint16_t xOffset; uint16_t yOffset; + unsigned long _last_blink_at; + uint16_t _blink_freq; public: - AnimationEffect(const char* name) : AnimationEffect(name, 0x000000, 0, 0) {} - AnimationEffect(const char* name, uint32_t bg_color) : AnimationEffect(name, bg_color, 0, 0) {} - AnimationEffect(const char* name, uint32_t bg_color, int x, int y); + AnimationEffect(const char* name, uint32_t bg_color=0x000000, int x=0, int y=0); + static AnimationEffect* Blinker(const char* name, uint16_t interval, uint32_t color, uint32_t background_color=0x000000); ~AnimationEffect(); AnimationEffect* setFgColor(uint32_t c); + AnimationEffect* setBlinkFrequency(uint16_t); void loop(uint16_t ms); String get_name() override; }; diff --git a/src/effect_animation.cpp b/src/effect_animation.cpp index cb4c8a3..92484ae 100644 --- a/src/effect_animation.cpp +++ b/src/effect_animation.cpp @@ -9,6 +9,8 @@ AnimationEffect::AnimationEffect(const char* name, uint32_t bg, int x, int y) { this->animation = new Animation(name, window); this->animation->setBgColor(bg); this->animation->setOffsets(this->xOffset, this->yOffset); + + _last_blink_at = millis(); } AnimationEffect* AnimationEffect::setFgColor(uint32_t c) { @@ -16,11 +18,23 @@ AnimationEffect* AnimationEffect::setFgColor(uint32_t c) { return this; } +AnimationEffect* AnimationEffect::setBlinkFrequency(uint16_t ms) { + _blink_freq = ms; + return this; +} + AnimationEffect::~AnimationEffect() { delete this->animation; } void AnimationEffect::loop(uint16_t ms) { + if (_blink_freq > 0) { + unsigned long mil = millis(); + if (mil < _last_blink_at || _last_blink_at + _blink_freq <= mil) { + this->animation->invert(); + _last_blink_at = mil; + } + } this->animation->drawFrame(); this->animation->advance(); } @@ -30,3 +44,10 @@ String AnimationEffect::get_name() { s += this->name; return s; } + +AnimationEffect* AnimationEffect::Blinker(const char* name, uint16_t interval, uint32_t color, uint32_t background_color) { + AnimationEffect* anim = new AnimationEffect(name, background_color); + anim->setFgColor(color); + anim->setBlinkFrequency(interval); + return anim; +} diff --git a/src/effects.cpp b/src/effects.cpp index 80d27d7..d1a80cb 100644 --- a/src/effects.cpp +++ b/src/effects.cpp @@ -31,11 +31,11 @@ ClockEffect effect_clock; TimerEffect effect_timer; // We're using 0 instead of false to get a better visual difference between true and false. -const EffectEntry effects[] = { +EffectEntry effects[] = { /* 0 */ {"sinematrix3", true, [](){ return new Sinematrix3Effect(); }}, /* 1 */ {"big_clock", true, [](){ return new BigClockEffect(); }}, /* 2 */ {"clock", 0, [](){ return new ClockEffect(); }}, - /* 3 */ {"bell", 0, [](){ return new BellEffect(); }}, + /* 3 */ {"bell", 0, [](){ return AnimationEffect::Blinker("/bell.pia", 300, 0xFFFF00); }}, /* 4 */ {"off", 0, [](){ return new StaticEffect(0x000000); }}, /* 5 */ {"single_dynamic", true, [](){ return new SingleDynamicEffect(); }}, /* 6 */ {"multi_dynamic", true, [](){ return new MultiDynamicEffect(); }},