From 127b8d6dca90d1619276346b794f442c8e04f624 Mon Sep 17 00:00:00 2001 From: Thomas Kolb Date: Mon, 16 Dec 2019 23:29:05 +0100 Subject: [PATCH] Animation robustness improvements - Check if animation is valid in all methods of AnimationController - Fixed timing (+crash) for quick loops of LED task --- include/Animation/AnimationController.h | 2 +- src/Animation/AnimationController.cpp | 9 +++++++-- src/main.cpp | 20 +++++++++++++++++--- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/include/Animation/AnimationController.h b/include/Animation/AnimationController.h index c03eaad..edf58f5 100644 --- a/include/Animation/AnimationController.h +++ b/include/Animation/AnimationController.h @@ -37,7 +37,7 @@ class AnimationController bool isIdle(void) { - return m_animation->finished() && !m_nextAnimation; + return !m_animation || (m_animation->finished() && !m_nextAnimation); } private: diff --git a/src/Animation/AnimationController.cpp b/src/Animation/AnimationController.cpp index 683bc2c..125b1a1 100644 --- a/src/Animation/AnimationController.cpp +++ b/src/Animation/AnimationController.cpp @@ -75,11 +75,16 @@ void AnimationController::loop(void) void AnimationController::stop(void) { - m_animation->stop(); + if(m_animation) { + m_animation->stop(); + } } void AnimationController::restart(void) { - m_animation->reset(); + if(m_animation) { + m_animation->reset(); + } + m_frame = 0; } diff --git a/src/main.cpp b/src/main.cpp index c2c3f83..059fd16 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -187,6 +187,18 @@ static void ledFSM(void) stateEntered = (lastState != ledState); } +static void fineDelay(uint32_t us) +{ + uint32_t ms = us / 1000; + us -= ms * 1000; + + if(ms) { + delay(ms); + } + + delayMicroseconds(us); +} + static void ledTask( void * parameter ) { uint64_t frame = 0; @@ -227,16 +239,18 @@ static void ledTask( void * parameter ) uint32_t now = micros(); uint32_t duration = now - start_time; - if(frame % 50 == 0) { + if(frame % 60 == 0) { uint32_t load = 100 * duration / FRAME_INTERVAL_US; - Serial.print("CPU Load: "); + Serial.print("Duration: "); + Serial.print(duration); + Serial.print("μs → CPU Load: "); Serial.print(load); Serial.println(" %"); } if(duration < FRAME_INTERVAL_US) { - delayMicroseconds(FRAME_INTERVAL_US - duration); + fineDelay(FRAME_INTERVAL_US - duration); } } /* delete a task when finish,