From c835e37d00b837a3f1ce7800c494dd8d33f67b6c Mon Sep 17 00:00:00 2001 From: lukas Date: Mon, 7 Mar 2022 09:48:36 +0100 Subject: [PATCH] pinganimator --- src/Clock.cpp | 10 ++++++---- src/Clock.h | 2 +- src/LoadAnimationBase.h | 26 ++++++++++++++++++++++++++ src/LoadAnimator.cpp | 2 +- src/LoadAnimator.h | 10 +++++----- src/PingLoadAnimator.cpp | 22 ++++++++++++++++++++++ src/PingLoadAnimator.h | 25 +++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 11 deletions(-) create mode 100644 src/LoadAnimationBase.h create mode 100644 src/PingLoadAnimator.cpp create mode 100644 src/PingLoadAnimator.h diff --git a/src/Clock.cpp b/src/Clock.cpp index 7d0f2a8..634dee3 100644 --- a/src/Clock.cpp +++ b/src/Clock.cpp @@ -5,7 +5,9 @@ #include "Types.h" #include "Clock.h" -Clock::Clock() : strip(NUMPIXELS, STRIPPIN, NEO_GRB + NEO_KHZ800), animator(), refreshTicker() { +Clock::Clock() : strip(NUMPIXELS, STRIPPIN, NEO_GRB + NEO_KHZ800), refreshTicker() { + LoadAnimator an = LoadAnimator(); + animator = &an; } void Clock::init() { @@ -54,12 +56,12 @@ void Clock::refreshTime() { // if not time is set (only seconds since start) start animation if (now <= 604800) { - animator.startLoadAnimation(&strip); + animator->startLoadAnimation(&strip); return; } - if (animator.animationActive()) { - animator.stopLoadAnimation(); + if (animator->animationActive()) { + animator->stopLoadAnimation(); strip.clear(); // only add on first time iteration diff --git a/src/Clock.h b/src/Clock.h index 3c2f37d..49d10c4 100644 --- a/src/Clock.h +++ b/src/Clock.h @@ -22,7 +22,7 @@ class Clock { private: Adafruit_NeoPixel strip{}; - LoadAnimator animator; + LoadAnimationBase *animator; Ticker refreshTicker; Ticker transitionTicker; diff --git a/src/LoadAnimationBase.h b/src/LoadAnimationBase.h new file mode 100644 index 0000000..3de8334 --- /dev/null +++ b/src/LoadAnimationBase.h @@ -0,0 +1,26 @@ +// +// Created by lukas on 01.02.22. +// + +#ifndef LEDSTRIPINTERFACE_LOADANIMATIONBASE_H +#define LEDSTRIPINTERFACE_LOADANIMATIONBASE_H + +#include "Adafruit_NeoPixel.h" + +class LoadAnimationBase { + public: + /** + * start the animation if not already runnint + * @param strip pointer to strip object + */ + virtual void startLoadAnimation(Adafruit_NeoPixel *strip) = 0; + + /** + * stop the animation + */ + virtual void stopLoadAnimation() = 0; + + virtual bool animationActive() = 0; +}; + +#endif // LEDSTRIPINTERFACE_LOADANIMATIONBASE_H diff --git a/src/LoadAnimator.cpp b/src/LoadAnimator.cpp index 28f53ba..34c7d27 100644 --- a/src/LoadAnimator.cpp +++ b/src/LoadAnimator.cpp @@ -9,7 +9,7 @@ void LoadAnimator::startLoadAnimation(Adafruit_NeoPixel* strip) { this->strip = strip; if (!timer.active()) - timer.attach_ms(100, std::bind(&LoadAnimator::animationStep, this)); + timer.attach_ms(100, [this] { animationStep(); }); } void LoadAnimator::stopLoadAnimation() { diff --git a/src/LoadAnimator.h b/src/LoadAnimator.h index 5d3af0f..8d44fcb 100644 --- a/src/LoadAnimator.h +++ b/src/LoadAnimator.h @@ -6,9 +6,9 @@ #define LEDSTRIPINTERFACE_LOADANIMATOR_H #include -#include "Adafruit_NeoPixel.h" +#include "LoadAnimationBase.h" -class LoadAnimator { +class LoadAnimator : public LoadAnimationBase { private: Ticker timer; uint8_t x, y, nro, nroo, nrooo; @@ -26,14 +26,14 @@ class LoadAnimator { * start the animation if not already runnint * @param strip pointer to strip object */ - void startLoadAnimation(Adafruit_NeoPixel *strip); + void startLoadAnimation(Adafruit_NeoPixel *strip) override; /** * stop the animation */ - void stopLoadAnimation(); + void stopLoadAnimation() override; - bool animationActive(); + bool animationActive() override; }; #endif // LEDSTRIPINTERFACE_LOADANIMATOR_H diff --git a/src/PingLoadAnimator.cpp b/src/PingLoadAnimator.cpp new file mode 100644 index 0000000..b0ed1b0 --- /dev/null +++ b/src/PingLoadAnimator.cpp @@ -0,0 +1,22 @@ +// +// Created by lukas on 01.02.22. +// + +#include "PingLoadAnimator.h" +void PingLoadAnimator::startLoadAnimation(Adafruit_NeoPixel* strip) { + this->strip = strip; + + if (!timer.active()) + timer.attach_ms(100, [this] { animationStep(); }); +} + +void PingLoadAnimator::stopLoadAnimation() { + timer.detach(); +} + +bool PingLoadAnimator::animationActive() { + return timer.active(); +} +void PingLoadAnimator::animationStep() { + +} diff --git a/src/PingLoadAnimator.h b/src/PingLoadAnimator.h new file mode 100644 index 0000000..3764c0a --- /dev/null +++ b/src/PingLoadAnimator.h @@ -0,0 +1,25 @@ +// +// Created by lukas on 01.02.22. +// + +#ifndef LEDSTRIPINTERFACE_PINGLOADANIMATOR_H +#define LEDSTRIPINTERFACE_PINGLOADANIMATOR_H + +#include +#include "LoadAnimationBase.h" +class PingLoadAnimator : LoadAnimationBase { + private: + Ticker timer; + Adafruit_NeoPixel *strip; + + /** + * one animation step to process the animation + */ + void animationStep(); + public: + void startLoadAnimation(Adafruit_NeoPixel* strip) override; + void stopLoadAnimation() override; + bool animationActive() override; +}; + +#endif // LEDSTRIPINTERFACE_PINGLOADANIMATOR_H