diff --git a/include/Animation/AnimationController.h b/include/Animation/AnimationController.h index 4f8e828..2cdb47c 100644 --- a/include/Animation/AnimationController.h +++ b/include/Animation/AnimationController.h @@ -4,6 +4,9 @@ #include "Animation.h" +#include +#include + class AnimationController { public: @@ -33,5 +36,7 @@ class AnimationController std::unique_ptr m_animation; std::unique_ptr m_nextAnimation; + SemaphoreHandle_t m_updateMutex; + uint64_t m_frame; }; diff --git a/include/Animation/FireAnimation.h b/include/Animation/FireAnimation.h index c526122..6a83502 100644 --- a/include/Animation/FireAnimation.h +++ b/include/Animation/FireAnimation.h @@ -17,11 +17,7 @@ class FireAnimation : public Animation m_stopping = true; } - void reset(void) override - { - m_stopping = false; - m_running = true; - } + void reset(void) override; private: static const constexpr unsigned MAX_NEW_ENERGY = 160; diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 15fd7f3..7b3b9e3 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -4,10 +4,14 @@ AnimationController::AnimationController(Fader *fader) : m_fader(fader), m_animation(nullptr), m_frame(0) -{} +{ + m_updateMutex = xSemaphoreCreateMutex(); +} void AnimationController::changeAnimation(std::unique_ptr anim, bool transition) { + xSemaphoreTake(m_updateMutex, portMAX_DELAY); + if(transition && m_animation) { m_nextAnimation = std::move(anim); m_animation->stop(); @@ -17,6 +21,8 @@ void AnimationController::changeAnimation(std::unique_ptr anim, bool m_animation = std::move(anim); m_nextAnimation.reset(nullptr); } + + xSemaphoreGive(m_updateMutex); } void AnimationController::changeAnimation(AnimationController::DefaultAnimation animation_id, bool transition) @@ -39,6 +45,8 @@ void AnimationController::changeAnimation(AnimationController::DefaultAnimation void AnimationController::loop(void) { + xSemaphoreTake(m_updateMutex, portMAX_DELAY); + if(m_animation && !m_animation->finished()) { m_animation->loop(m_frame); } @@ -52,6 +60,8 @@ void AnimationController::loop(void) m_animation.swap(m_nextAnimation); m_nextAnimation.reset(nullptr); } + + xSemaphoreGive(m_updateMutex); } void AnimationController::restart(void) diff --git a/src/Animation/FireAnimation.cpp b/src/Animation/FireAnimation.cpp index 7a441f6..3301c5d 100644 --- a/src/Animation/FireAnimation.cpp +++ b/src/Animation/FireAnimation.cpp @@ -24,7 +24,7 @@ FireAnimation::FireAnimation(Fader *fader, bool cold) m_colorMapColors.emplace_back(Fader::Color{255, 128, 0, 40}); } - m_fader->set_fadestep(10); + reset(); } uint8_t FireAnimation::interpolate(uint32_t energy, std::size_t start_x, std::size_t end_x, uint8_t start_c, uint8_t end_c) @@ -173,3 +173,11 @@ void FireAnimation::loop(uint64_t frame) m_running = false; } } + +void FireAnimation::reset(void) +{ + m_stopping = false; + m_running = true; + + m_fader->set_fadestep(10); +} diff --git a/src/main.cpp b/src/main.cpp index 39c5070..d8e9581 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,8 @@ #include #include +#include "coreids.h" + const uint32_t FRAME_INTERVAL_US = 16666; const uint32_t NUM_STRIPS = 8; const uint32_t NUM_LEDS = 16;