Library to control NeoPixel strings of RGB leds

Dependencies:   PixelArray

Dependents:   NeoPixelI2cSlave NeoPixelI2cSlave

Files at this revision

API Documentation at this revision

Comitter:
dwini
Date:
Fri Apr 15 14:38:13 2016 +0000
Parent:
2:bb9ebad05691
Commit message:
Been to long since i coded this.

Changed in this revision

Effects/color_effect.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/color_effect.h Show annotated file Show diff for this revision Revisions of this file
Effects/effect.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/effect.h Show annotated file Show diff for this revision Revisions of this file
Effects/effects_manager.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/periodic_effect.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/periodic_effect.h Show annotated file Show diff for this revision Revisions of this file
Effects/shift_effect.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/shift_effect.h Show annotated file Show diff for this revision Revisions of this file
Effects/strobe_effect.cpp Show annotated file Show diff for this revision Revisions of this file
Effects/strobe_effect.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Effects/color_effect.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -0,0 +1,20 @@
+#include "color_effect.h"
+
+namespace Effects {
+
+    ColorEffect::ColorEffect(NeoPixelString * pixelstring, neopixel::Pixel color)
+        : Effect(pixelstring, color) {
+    }
+    
+    void ColorEffect::start(void) {
+        requestExecute();
+    }
+    
+    void ColorEffect::execute(void) {
+        if (shouldExecute()) {
+            clearExecute();
+            getPixelString()->update(getColor());
+        }
+    }
+
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Effects/color_effect.h	Fri Apr 15 14:38:13 2016 +0000
@@ -0,0 +1,17 @@
+#pragma once
+
+#include "effect.h"
+
+namespace Effects {
+    
+    class ColorEffect : public Effect {
+             
+        public:
+            ColorEffect(NeoPixelString * pixelstring, neopixel::Pixel color);
+        
+        public:
+            virtual void start(void);
+            virtual void execute(void);
+    };
+
+};
\ No newline at end of file
--- a/Effects/effect.cpp	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/effect.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -2,37 +2,38 @@
 
 namespace Effects {
     
-    Effect::Effect(NeoPixelString * pixelstring, int delay_ms) {
+    Effect::Effect(NeoPixelString * pixelstring, neopixel::Pixel color) {
         this->pixelstring = pixelstring;
-        this->delay_ms = delay_ms;
+        this->color = color;
+        doExecute = false;
+    }
     
-        execute_effect = false;
+    void Effect::start(void) {
+        doExecute = false;
+    }
+    
+    void Effect::stop(void) {
+        doExecute = false;
     }
     
     NeoPixelString * Effect::getPixelString(void) {
         return this->pixelstring;
     }
-    
-    void Effect::start(void) {
-        execute_effect = false;
-        ticker.attach(this, &Effect::tick, delay_ms/1000.0);
+
+    neopixel::Pixel Effect::getColor(void) {
+        return color;
     }
-    
-    void Effect::stop(void) {
-        ticker.detach();
-        execute_effect = false;
+
+    void Effect::requestExecute(void) {
+        doExecute = true;
     }
-    
-    void Effect::tick(void) {
-        execute_effect = true;
+
+    void Effect::clearExecute(void) {
+        doExecute = false;
     }
-    
-    void Effect::execute(void) {
-        execute_effect = false;
-    }
-    
-    bool Effect::needsExecutionTime(void) {
-        return execute_effect;
+
+    bool Effect::shouldExecute(void) {
+        return doExecute;
     }
 
 };
\ No newline at end of file
--- a/Effects/effect.h	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/effect.h	Fri Apr 15 14:38:13 2016 +0000
@@ -8,24 +8,21 @@
     
         private:
             NeoPixelString * pixelstring;
-            int delay_ms;
-            Ticker ticker;
-            bool execute_effect;
+            neopixel::Pixel color;
+            bool doExecute;
             
         public:
-            Effect(NeoPixelString * pixelstring, int delay_ms);
-        
-        public:
-            NeoPixelString * getPixelString(void);
-            void start(void);
-            void stop(void);
-            bool needsExecutionTime(void);
+            Effect(NeoPixelString * pixelstring, neopixel::Pixel color);
     
         public:
-            virtual void execute(void);
-    
-        private:
-            void tick(void);
+            virtual void start(void);
+            virtual void stop(void);
+            NeoPixelString * getPixelString(void);
+            neopixel::Pixel getColor(void);
+            void requestExecute(void);
+            void clearExecute(void);
+            bool shouldExecute(void);
+            virtual void execute(void) = 0;
     };
     
 };
\ No newline at end of file
--- a/Effects/effects_manager.cpp	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/effects_manager.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -8,17 +8,17 @@
     
     void EffectsManager::registerEffect(Effect * effect) {
         this->effect = effect;
-        this->effect->start();
+        effect->start();
     }
     
     void EffectsManager::execute(void) {
-        if (effect && effect->needsExecutionTime()) {
+        if (effect) {
             effect->execute();
         }
     }
     
     void EffectsManager::unregisterEffect(void) {
-        this->effect->stop();
+        effect->stop();
         effect = 0;
     }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Effects/periodic_effect.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -0,0 +1,25 @@
+#include "periodic_effect.h"
+
+namespace Effects {
+    
+    PeriodicEffect::PeriodicEffect(NeoPixelString * pixelstring, neopixel::Pixel color, int delay_ms)
+        : Effect(pixelstring, color) {
+
+        this->delay_ms = delay_ms;
+    }
+    
+    void PeriodicEffect::start(void) {
+        Effect::start();
+        ticker.attach(this, &PeriodicEffect::tick, delay_ms/1000.0);
+    }
+    
+    void PeriodicEffect::stop(void) {
+        Effect::stop();
+        ticker.detach();
+    }
+    
+    void PeriodicEffect::tick(void) {
+        requestExecute();
+    }
+
+};
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Effects/periodic_effect.h	Fri Apr 15 14:38:13 2016 +0000
@@ -0,0 +1,24 @@
+#pragma once
+
+#include "effect.h"
+
+namespace Effects {
+
+    class PeriodicEffect : public Effect {
+    
+        private:
+            int delay_ms;
+            Ticker ticker;
+            
+        public:
+            PeriodicEffect(NeoPixelString * pixelstring, neopixel::Pixel color, int delay_ms);
+        
+        public:
+            virtual void start(void);
+            virtual void stop(void);
+    
+        private:
+            void tick(void);
+    };
+    
+};
\ No newline at end of file
--- a/Effects/shift_effect.cpp	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/shift_effect.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -3,52 +3,52 @@
 
 namespace Effects {
 
-    ShiftEffect::ShiftEffect(NeoPixelString * pixelstring, int delay_ms, neopixel::Pixel color, int groupsize)
-        : Effect(pixelstring, delay_ms) {
+    ShiftEffect::ShiftEffect(NeoPixelString * pixelstring, neopixel::Pixel color, int delay_ms, int groupsize)
+        : PeriodicEffect(pixelstring, color, delay_ms) {
     
-        this->color = color;
         this->groupsize = groupsize;
         state = FIRST_TIME;
     }
     
     void ShiftEffect::execute(void) {
-        Effect::execute();
-
-        if (state == FIRST_TIME) {
-            for (unsigned i = 0; i < getPixelString()->getLength(); i++) {
-                if (i % groupsize == 0) {
-                    getPixelString()->setPixel(i, color);
-                } else {
-                    getPixelString()->setPixel(i, Colors::Black);
-                }
-            }
-
-            state = RUNNING;
-        } else {
-
-            unsigned int i = 0;
-            neopixel::Pixel pix;
-            neopixel::Pixel first;
-
-            while (i < getPixelString()->getLength()) {
-                unsigned int group = i / groupsize;
-
-                if (i % groupsize == 0) {
-                    first = getPixelString()->getPixel(i);
+        if (shouldExecute()) {
+            clearExecute();
+            if (state == FIRST_TIME) {
+                for (unsigned i = 0; i < getPixelString()->getLength(); i++) {
+                    if (i % groupsize == 0) {
+                        getPixelString()->setPixel(i, getColor());
+                    } else {
+                        getPixelString()->setPixel(i, Colors::Black);
+                    }
                 }
 
-                if (i == ((group+1)*groupsize)-1) { // last pix in group
-                    getPixelString()->setPixel(i, first);
-                } else {
-                    pix = getPixelString()->getPixel(i+1);
-                    getPixelString()->setPixel(i, pix);
+                state = RUNNING;
+            } else {
+
+                unsigned int i = 0;
+                neopixel::Pixel pix;
+                neopixel::Pixel first;
+
+                while (i < getPixelString()->getLength()) {
+                    unsigned int group = i / groupsize;
+
+                    if (i % groupsize == 0) {
+                        first = getPixelString()->getPixel(i);
+                    }
+
+                    if (i == ((group+1)*groupsize)-1) { // last pix in group
+                        getPixelString()->setPixel(i, first);
+                    } else {
+                        pix = getPixelString()->getPixel(i+1);
+                        getPixelString()->setPixel(i, pix);
+                    }
+
+                    i++;
                 }
 
-                i++;
             }
-
+            getPixelString()->update();
         }
-        getPixelString()->update();
     }
 
 };
\ No newline at end of file
--- a/Effects/shift_effect.h	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/shift_effect.h	Fri Apr 15 14:38:13 2016 +0000
@@ -1,20 +1,19 @@
 #pragma once
 
-#include "effect.h"
+#include "periodic_effect.h"
 
 namespace Effects {
 
     enum ShiftState { FIRST_TIME, RUNNING };
     
-    class ShiftEffect : public Effect {
+    class ShiftEffect : public PeriodicEffect {
     
         private:
             ShiftState state;
-            neopixel::Pixel color;
             int groupsize;      // Number of pixels to consider a group where the pixel is shifted in
             
         public:
-            ShiftEffect(NeoPixelString * pixelstring, int delay_ms, neopixel::Pixel color, int groupsize);
+            ShiftEffect(NeoPixelString * pixelstring, neopixel::Pixel color, int delay_ms, int groupsize);
         
         public:
             virtual void execute(void);
--- a/Effects/strobe_effect.cpp	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/strobe_effect.cpp	Fri Apr 15 14:38:13 2016 +0000
@@ -2,21 +2,22 @@
 
 namespace Effects {
 
-    StrobeEffect::StrobeEffect(NeoPixelString * pixelstring, int delay_ms, neopixel::Pixel initial_color)
-        : Effect(pixelstring, delay_ms) {
+    StrobeEffect::StrobeEffect(NeoPixelString * pixelstring, neopixel::Pixel initial_color, int delay_ms)
+        : PeriodicEffect(pixelstring, initial_color, delay_ms) {
     
-        this->initial_color = initial_color;
         current_state = OFF;
     }
     
     void StrobeEffect::execute(void) {
-        Effect::execute();
-        if (current_state == OFF) {
-            getPixelString()->update(initial_color);
-            current_state = ON;
-        } else {
-            getPixelString()->update(Colors::Black);
-            current_state = OFF;
+        if (shouldExecute()) {
+            clearExecute();
+            if (current_state == OFF) {
+                getPixelString()->update(getColor());
+                current_state = ON;
+            } else {
+                getPixelString()->update(Colors::Black);
+                current_state = OFF;
+            }
         }
     }
 
--- a/Effects/strobe_effect.h	Mon Nov 23 21:32:24 2015 +0000
+++ b/Effects/strobe_effect.h	Fri Apr 15 14:38:13 2016 +0000
@@ -1,19 +1,18 @@
 #pragma once
 
-#include "effect.h"
+#include "periodic_effect.h"
 
 namespace Effects {
 
     enum StrobeState { OFF, ON };
     
-    class StrobeEffect : public Effect {
+    class StrobeEffect : public PeriodicEffect {
     
         private:
-            neopixel::Pixel initial_color;
             StrobeState current_state;
             
         public:
-            StrobeEffect(NeoPixelString * pixelstring, int delay_ms, neopixel::Pixel initial_color);
+            StrobeEffect(NeoPixelString * pixelstring, neopixel::Pixel initial_color, int delay_ms);
         
         public:
             virtual void execute(void);