diff --git a/keywords.txt b/keywords.txt index 75771ef..89144fd 100644 --- a/keywords.txt +++ b/keywords.txt @@ -117,6 +117,7 @@ StopAnimation KEYWORD2 RestartAnimation KEYWORD2 IsAnimationActive KEYWORD2 AnimationDuration KEYWORD2 +ChangeAnimationDuration KEYWORD2 UpdateAnimations KEYWORD2 IsPaused KEYWORD2 Pause KEYWORD2 diff --git a/src/NeoPixelAnimator.h b/src/NeoPixelAnimator.h index 21eb2ad..c49d9ec 100644 --- a/src/NeoPixelAnimator.h +++ b/src/NeoPixelAnimator.h @@ -109,6 +109,8 @@ public: return _animations[indexAnimation]._duration; } + void ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration); + void UpdateAnimations(); bool IsPaused() @@ -159,6 +161,11 @@ private: _remaining = 0; } + float CurrentProgress() + { + return (float)(_duration - _remaining) / (float)_duration; + } + uint16_t _duration; uint16_t _remaining; diff --git a/src/internal/NeoPixelAnimator.cpp b/src/internal/NeoPixelAnimator.cpp index db51a5d..d535316 100644 --- a/src/internal/NeoPixelAnimator.cpp +++ b/src/internal/NeoPixelAnimator.cpp @@ -142,7 +142,7 @@ void NeoPixelAnimator::UpdateAnimations() if (pAnim->_remaining > delta) { param.state = (pAnim->_remaining == pAnim->_duration) ? AnimationState_Started : AnimationState_Progress; - param.progress = (float)(pAnim->_duration - pAnim->_remaining) / (float)pAnim->_duration; + param.progress = pAnim->CurrentProgress(); fnUpdate(param); @@ -164,3 +164,33 @@ void NeoPixelAnimator::UpdateAnimations() } } } + +void NeoPixelAnimator::ChangeAnimationDuration(uint16_t indexAnimation, uint16_t newDuration) +{ + if (indexAnimation >= _countAnimations) + { + return; + } + + AnimationContext* pAnim = &_animations[indexAnimation]; + + // calc the current animation progress + float progress = pAnim->CurrentProgress(); + + // keep progress in range just in case + if (progress < 0.0f) + { + progress = 0.0f; + } + else if (progress > 1.0f) + { + progress = 1.0f; + } + + // change the duration + pAnim->_duration = newDuration; + + // _remaining time must also be reset after a duration change; + // use the progress to recalculate it + pAnim->_remaining = uint16_t(pAnim->_duration * (1.0f - progress)); +} \ No newline at end of file