From bd2c85a78cc9626c0cba7bdd2eb634a66b9427ba Mon Sep 17 00:00:00 2001 From: Bruno Date: Sun, 25 Nov 2018 18:21:42 +0100 Subject: [PATCH] Fix sequence logic --- library.properties | 2 +- src/AlaLed.cpp | 17 +++++++++++++---- src/AlaLed.h | 3 ++- src/AlaLedRgb.cpp | 29 ++++++++++++++++++++++------- src/AlaLedRgb.h | 6 ++++-- 5 files changed, 42 insertions(+), 15 deletions(-) diff --git a/library.properties b/library.properties index 0c9017e..d9e6069 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ALA -version=2.3.7 +version=2.3.8 author=bportaluri maintainer=Bruno Portaluri sentence=Arduino Light Animation (ALA) library diff --git a/src/AlaLed.cpp b/src/AlaLed.cpp index bc27d8a..50494aa 100644 --- a/src/AlaLed.cpp +++ b/src/AlaLed.cpp @@ -71,7 +71,7 @@ void AlaLed::setRefreshRate(int refreshRate) } -void AlaLed::setAnimation(int animation, long speed) +void AlaLed::setAnimation(int animation, long speed, bool isSeq) { if (this->animation == animation && this->speed == speed) return; @@ -79,7 +79,9 @@ void AlaLed::setAnimation(int animation, long speed) this->animation = animation; this->speed = speed; - setAnimationFunc(animation); + if (!isSeq) + animSeqLen=0; + setAnimationFunc(animation); animStartTime = millis(); } @@ -95,9 +97,16 @@ void AlaLed::setAnimation(AlaSeq animSeq[]) animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; } animSeqStartTime = millis(); - setAnimation(animSeq[0].animation, animSeq[0].speed); + setAnimation(animSeq[0].animation, animSeq[0].speed, true); } +void AlaLed::setSpeed(long speed) +{ + this->speed = speed; + animStartTime = millis(); +} + + int AlaLed::getAnimation() { return animation; @@ -128,7 +137,7 @@ bool AlaLed::runAnimation() { if (t>=c && t<(c+animSeq[i].duration)) { - setAnimation(animSeq[i].animation, animSeq[i].speed); + setAnimation(animSeq[i].animation, animSeq[i].speed, true); break; } c = c + animSeq[i].duration; diff --git a/src/AlaLed.h b/src/AlaLed.h index f395ff2..44c00ff 100644 --- a/src/AlaLed.h +++ b/src/AlaLed.h @@ -59,8 +59,9 @@ class AlaLed int getRefreshRate(); - void setAnimation(int animation, long speed); + void setAnimation(int animation, long speed, bool isSeq=false); void setAnimation(AlaSeq animSeq[]); + void setSpeed(long speed); int getAnimation(); bool runAnimation(); diff --git a/src/AlaLedRgb.cpp b/src/AlaLedRgb.cpp index d874128..8c89ddc 100644 --- a/src/AlaLedRgb.cpp +++ b/src/AlaLedRgb.cpp @@ -99,7 +99,7 @@ int AlaLedRgb::getCurrentRefreshRate() } -void AlaLedRgb::setAnimation(int animation, long speed, AlaColor color) +void AlaLedRgb::setAnimation(int animation, long speed, AlaColor color, bool isSeq) { // is there any change? if (this->animation == animation && this->speed == speed && this->palette.numColors == 1 && this->palette.colors[0] == color) @@ -115,15 +115,18 @@ void AlaLedRgb::setAnimation(int animation, long speed, AlaColor color) this->speed = speed; this->palette.numColors = 1; - this->palette.colors = (AlaColor*)malloc(3); + // TODO is this a memory leak? + this->palette.colors = (AlaColor*)malloc(3); this->palette.colors[0] = color; - animSeqLen=0; + if (!isSeq) + animSeqLen=0; setAnimationFunc(animation); animStartTime = millis(); } -void AlaLedRgb::setAnimation(int animation, long speed, AlaPalette palette) + +void AlaLedRgb::setAnimation(int animation, long speed, AlaPalette palette, bool isSeq) { // is there any change? if (this->animation == animation && this->speed == speed && this->palette == palette) @@ -139,7 +142,8 @@ void AlaLedRgb::setAnimation(int animation, long speed, AlaPalette palette) this->speed = speed; this->palette = palette; - animSeqLen=0; + if (!isSeq) + animSeqLen=0; setAnimationFunc(animation); animStartTime = millis(); } @@ -156,7 +160,18 @@ void AlaLedRgb::setAnimation(AlaSeq animSeq[]) animSeqDuration = animSeqDuration + animSeq[animSeqLen].duration; } animSeqStartTime = millis(); - setAnimation(animSeq[0].animation, animSeq[0].speed, animSeq[0].palette); + setAnimation(animSeq[0].animation, animSeq[0].speed, animSeq[0].palette, true); +} + +void AlaLedRgb::setSpeed(long speed) +{ + this->speed = speed; + animStartTime = millis(); +} + +void AlaLedRgb::setColor(AlaColor color) +{ + this->palette.colors[0] = color; } int AlaLedRgb::getAnimation() @@ -189,7 +204,7 @@ bool AlaLedRgb::runAnimation() { if (t>=c && t<(c+animSeq[i].duration)) { - setAnimation(animSeq[i].animation, animSeq[i].speed, animSeq[i].palette); + setAnimation(animSeq[i].animation, animSeq[i].speed, animSeq[i].palette, true); break; } c = c + animSeq[i].duration; diff --git a/src/AlaLedRgb.h b/src/AlaLedRgb.h index c876053..35e0042 100644 --- a/src/AlaLedRgb.h +++ b/src/AlaLedRgb.h @@ -42,9 +42,11 @@ class AlaLedRgb int getCurrentRefreshRate(); - void setAnimation(int animation, long speed, AlaColor color); - void setAnimation(int animation, long speed, AlaPalette palette); + void setAnimation(int animation, long speed, AlaColor color, bool isSeq=false); + void setAnimation(int animation, long speed, AlaPalette palette, bool isSeq=false); void setAnimation(AlaSeq animSeq[]); + void setSpeed(long speed); + void setColor(AlaColor color); int getAnimation(); bool runAnimation();