From 917627c0d0a46c6d97d7524068ac42b7aeeeb99f Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Tue, 10 Dec 2019 22:41:11 +0100 Subject: [PATCH] Smoother transitions between UDP and animation - stop() animation before changing state and wait for completion - additionally fade to black (in case animation does not stop at black or when UDP stream ends) - reset() the animation when changing back to the animation state --- include/Animation/AnimationController.h | 1 + src/Animation/AnimationController.cpp | 5 +++++ src/Animation/FireAnimation.cpp | 4 ++++ src/main.cpp | 21 ++++++++++++++++++--- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/Animation/AnimationController.h b/include/Animation/AnimationController.h index 2cdb47c..c4f87aa 100644 --- a/include/Animation/AnimationController.h +++ b/include/Animation/AnimationController.h @@ -24,6 +24,7 @@ class AnimationController void loop(void); + void stop(void); void restart(void); bool isIdle(void) diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 7b3b9e3..7f7d9fa 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -64,6 +64,11 @@ void AnimationController::loop(void) xSemaphoreGive(m_updateMutex); } +void AnimationController::stop(void) +{ + m_animation->stop(); +} + void AnimationController::restart(void) { m_animation->reset(); diff --git a/src/Animation/FireAnimation.cpp b/src/Animation/FireAnimation.cpp index 3301c5d..193f9b2 100644 --- a/src/Animation/FireAnimation.cpp +++ b/src/Animation/FireAnimation.cpp @@ -180,4 +180,8 @@ void FireAnimation::reset(void) m_running = true; m_fader->set_fadestep(10); + + for(auto &energy: m_energy) { + energy = 0; + } } diff --git a/src/main.cpp b/src/main.cpp index aa8be13..86bc8ee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -90,7 +90,8 @@ bool initLEDs() enum LEDState { UDP, ANIMATION, - TRANSITION + TRANSITION, + TRANSITION_FADE2BLACK }; static void ledFSM(void) @@ -135,16 +136,30 @@ static void ledFSM(void) if(udpProto.loop()) { lastUDPUpdate = millis(); - ledFader.update(); } else if(millis() - lastUDPUpdate > 3000) { // seems like no more UDP data arrives -> transition to ANIMATION state nextState = ANIMATION; ledState = TRANSITION; } + + ledFader.update(); break; case TRANSITION: - Serial.print("T"); + Serial.print("S"); + if(stateEntered) { + animController.stop(); + } + + if(animController.isIdle()) { + ledState = TRANSITION_FADE2BLACK; + } else { + animController.loop(); + } + break; + + case TRANSITION_FADE2BLACK: + Serial.print("F"); if(stateEntered) { ledFader.set_fadestep(2); ledFader.fade_color(Fader::Color{0, 0, 0, 0});