@@ -13,6 +13,7 @@ public class CANdleLEDStrip extends LEDStrip {
1313    private  static  CANdle  CANDLE ;
1414    private  static  int  LAST_CREATED_LED_STRIP_ANIMATION_SLOT  = 0 ;
1515    private  final  int  animationSlot ;
16+     private  boolean  shouldRunPeriodically  = false ;
1617
1718    /** 
1819     * Sets the CANdle instance to be used for controlling the LED strips. Must be set before using any LED strips. Should only be called once. 
@@ -51,7 +52,8 @@ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) {
5152
5253    @ Override 
5354    public  void  periodic () {
54-         currentAnimation .run ();
55+         if  (shouldRunPeriodically )
56+             currentAnimation .run ();
5557    }
5658
5759    @ Override 
@@ -61,6 +63,7 @@ protected void clearLEDColors() {
6163
6264    @ Override 
6365    protected  void  blink (Color  color , double  speed ) {
66+         shouldRunPeriodically  = false ;
6467        CANDLE .animate (
6568                new  SingleFadeAnimation (
6669                        (int ) (color .red  * 255 ),
@@ -77,6 +80,7 @@ protected void blink(Color color, double speed) {
7780
7881    @ Override 
7982    protected  void  staticColor (Color  color ) {
83+         shouldRunPeriodically  = false ;
8084        CANDLE .setLEDs (
8185                ((int ) color .red  * 255 ),
8286                ((int ) color .green  * 255 ),
@@ -89,6 +93,7 @@ protected void staticColor(Color color) {
8993
9094    @ Override 
9195    protected  void  breathe (Color  color , int  numberOfBreathingLEDs , double  speed , boolean  inverted , LarsonAnimation .BounceMode  bounceMode ) {
96+         shouldRunPeriodically  = false ;
9297        CANDLE .animate (
9398                new  LarsonAnimation (
9499                        (int ) (color .red  * 255 ),
@@ -107,6 +112,7 @@ protected void breathe(Color color, int numberOfBreathingLEDs, double speed, boo
107112
108113    @ Override 
109114    protected  void  alternateColor (Color  firstColor , Color  secondColor ) {
115+         shouldRunPeriodically  = false ;
110116        for  (int  i  = 0 ; i  < numberOfLEDs ; i ++)
111117            CANDLE .setLEDs (
112118                    (int ) ((isEven (i ) ? firstColor .red  : secondColor .red ) * 255 ),
@@ -120,6 +126,7 @@ protected void alternateColor(Color firstColor, Color secondColor) {
120126
121127    @ Override 
122128    protected  void  colorFlow (Color  color , double  speed , boolean  inverted ) {
129+         shouldRunPeriodically  = false ;
123130        final  boolean  correctedInverted  = this .inverted  != inverted ;
124131        CANDLE .animate (
125132                new  ColorFlowAnimation (
@@ -138,6 +145,7 @@ protected void colorFlow(Color color, double speed, boolean inverted) {
138145
139146    @ Override 
140147    protected  void  rainbow (double  brightness , double  speed , boolean  inverted ) {
148+         shouldRunPeriodically  = false ;
141149        final  boolean  correctedInverted  = this .inverted  != inverted ;
142150        CANDLE .animate (
143151                new  RainbowAnimation (
@@ -153,6 +161,7 @@ protected void rainbow(double brightness, double speed, boolean inverted) {
153161
154162    @ Override 
155163    protected  void  sectionColor (Supplier <Color >[] colors ) {
164+         shouldRunPeriodically  = true ;
156165        final  int  ledsPerSection  = (int ) Math .floor ((double ) numberOfLEDs  / colors .length );
157166        setSectionColor (colors .length , ledsPerSection , colors );
158167    }
0 commit comments