A library for SPI control of adafruit's neopixel ring and addressable LEDs.

Dependencies:   PixelArray

Dependents:   TI_NEOPIXEL_SPI_SAMPLE

Files at this revision

API Documentation at this revision

Comitter:
tichise
Date:
Sat Jan 04 02:53:13 2020 +0000
Parent:
2:0148ac5c90fa
Commit message:
switched back to PixelArray

Changed in this revision

TI_NEOPIXEL_SPI.cpp Show annotated file Show diff for this revision Revisions of this file
TI_NEOPIXEL_SPI.h Show annotated file Show diff for this revision Revisions of this file
diff -r 0148ac5c90fa -r 69bb2a2fa11f TI_NEOPIXEL_SPI.cpp
--- a/TI_NEOPIXEL_SPI.cpp	Sun Jul 28 00:13:03 2019 +0000
+++ b/TI_NEOPIXEL_SPI.cpp	Sat Jan 04 02:53:13 2020 +0000
@@ -20,17 +20,20 @@
     _ledStrip.update(colors, count);
 }
 
-void TI_NEOPIXEL_SPI::switchLightOn(int count,  int startCount, int endCount, rgbColor rgbColor)
+void TI_NEOPIXEL_SPI::switchLightOn(int count, int startCount, int endCount, rgbColor rgbColor)
 {
     neopixel::Pixel colors[count];
 
     for (int i = 0; i < count; i++)
     {
-        if (startCount <= i && i < endCount) {
+        if (startCount <= i && i < endCount)
+        {
             colors[i].red = rgbColor.red;
             colors[i].green = rgbColor.green;
             colors[i].blue = rgbColor.blue;
-        } else {
+        }
+        else
+        {
             colors[i].red = 0;
             colors[i].green = 0;
             colors[i].blue = 0;
@@ -46,11 +49,14 @@
 
     for (int i = 0; i < count; i++)
     {
-        if (startCount <= i && i < endCount) {
+        if (startCount <= i && i < endCount)
+        {
             colors[i].red = rgbColor.red;
             colors[i].green = rgbColor.green;
             colors[i].blue = rgbColor.blue;
-        } else {
+        }
+        else
+        {
             colors[i].red = 0;
             colors[i].green = 0;
             colors[i].blue = 0;
@@ -99,11 +105,14 @@
         {
             if (i <= j)
             {
-                if (startCount <= i && i < endCount) {
+                if (startCount <= i && i < endCount)
+                {
                     colors[i].red = rgbColor.red;
                     colors[i].green = rgbColor.green;
                     colors[i].blue = rgbColor.blue;
-                } else {
+                }
+                else
+                {
                     colors[i].red = 0;
                     colors[i].green = 0;
                     colors[i].blue = 0;
@@ -119,8 +128,16 @@
 
         _ledStrip.update(colors, count);
 
-        if (startCount <= j && j < endCount) {
-            wait(0.07);
+        if (startCount <= j && j < endCount)
+        {
+            if (count == 16)
+            {
+                wait(0.05);
+            }
+            else
+            {
+                wait(0.015);
+            }
         }
     }
 }
@@ -137,9 +154,7 @@
         colors[j].blue = c2.blue;
     }
 
-    _ledStrip.update(colors, virtualCount);
-
-    for (int j = 0; j < virtualCount + bufferCount; j++)
+    for (int j = 0; j < count; j++)
     {
         colors[j].red = c1.red;
         colors[j].green = c1.green;
@@ -150,11 +165,103 @@
         colors[j - bufferCount].blue = c2.blue;
 
         _ledStrip.update(colors, virtualCount);
-        wait(0.05);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.015);
+        }
     }
 }
 
-void TI_NEOPIXEL_SPI::chaseRainbow(int count)
+void TI_NEOPIXEL_SPI::chase2(int count, int bufferCount, rgbColor c1, rgbColor c2)
+{
+    for (int j = 0; j < count; j++)
+    {
+        neopixel::Pixel colors[count];
+
+        for (int i = 0; i < count; i++)
+        {
+            if (i <= j)
+            {
+                colors[i].red = c1.red;
+                colors[i].green = c1.green;
+                colors[i].blue = c1.blue;
+
+                // LEDを節約
+                colors[i - bufferCount].red = c2.red;
+                colors[i - bufferCount].green = c2.green;
+                colors[i - bufferCount].blue = c2.blue;
+            }
+            else
+            {
+                colors[i].red = c2.red;
+                colors[i].green = c2.green;
+                colors[i].blue = c2.blue;
+            }
+        }
+
+        _ledStrip.update(colors, count);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.015);
+        }
+    }
+}
+
+void TI_NEOPIXEL_SPI::chaseReverse(int count, int bufferCount, rgbColor c1, rgbColor c2)
+{
+    printf("chaseReverse \n");
+
+    neopixel::Pixel colors[count];
+
+    for (int j = 0; j <= count; j++)
+    {
+        colors[j].red = c2.red;
+        colors[j].green = c2.green;
+        colors[j].blue = c2.blue;
+    }
+
+    _ledStrip.update(colors, count);
+
+    for (int j = 0; j <= count; j++)
+    {
+        if (0 <= (count - j))
+        {
+            colors[count - j].red = c1.red;
+            colors[count - j].green = c1.green;
+            colors[count - j].blue = c1.blue;
+        }
+
+        if (bufferCount <= j)
+        {
+            colors[count + bufferCount - j].red = c2.red;
+            colors[count + bufferCount - j].green = c2.green;
+            colors[count + bufferCount - j].blue = c2.blue;
+        }
+
+        _ledStrip.update(colors, count);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.015);
+        }
+    }
+}
+
+void TI_NEOPIXEL_SPI::chaseRainbow(int count, int bufferCount)
 {
     for (int j = 0; j < count; j++)
     {
@@ -171,9 +278,9 @@
                 colors[i].blue = rgbColor.blue;
 
                 // LEDを節約
-                colors[i-4].red = 0;
-                colors[i-4].green = 0;
-                colors[i-4].blue = 0;
+                colors[i - bufferCount].red = 0;
+                colors[i - bufferCount].green = 0;
+                colors[i - bufferCount].blue = 0;
             }
             else
             {
@@ -184,7 +291,15 @@
         }
 
         _ledStrip.update(colors, count);
-        wait(0.06);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.015);
+        }
     }
 }
 
@@ -213,7 +328,78 @@
         }
 
         _ledStrip.update(colors, count);
-        wait(0.06);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.015);
+        }
+    }
+}
+
+void TI_NEOPIXEL_SPI::moon(int count, int startIndex, int stopIndex, rgbColor c1, rgbColor c2)
+{
+
+    neopixel::Pixel colors[count];
+
+    for (int j = 0; j < count; j++)
+    {
+        if (0 <= j && j <= count)
+        {
+            colors[j].red = c2.red;
+            colors[j].green = c2.green;
+            colors[j].blue = c2.blue;
+        }
+    }
+
+    int loopCount = (stopIndex - startIndex) / 2;
+    int middlePoint = startIndex + loopCount;
+
+    for (int i = 0; i <= loopCount - 1; i++)
+    {
+        colors[middlePoint + i].red = c1.red;
+        colors[middlePoint + i].green = c1.green;
+        colors[middlePoint + i].blue = c1.blue;
+
+        colors[middlePoint - i].red = c1.red;
+        colors[middlePoint - i].green = c1.green;
+        colors[middlePoint - i].blue = c1.blue;
+
+        _ledStrip.update(colors, count);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.04);
+        }
+    }
+
+    for (int i = 0; i <= loopCount - 1; i++)
+    {
+        colors[startIndex + i].red = c2.red;
+        colors[startIndex + i].green = c2.green;
+        colors[startIndex + i].blue = c2.blue;
+
+        colors[stopIndex - i].red = c2.red;
+        colors[stopIndex - i].green = c2.green;
+        colors[stopIndex - i].blue = c2.blue;
+
+        _ledStrip.update(colors, count);
+
+        if (count == 16)
+        {
+            wait(0.05);
+        }
+        else
+        {
+            wait(0.04);
+        }
     }
 }
 
diff -r 0148ac5c90fa -r 69bb2a2fa11f TI_NEOPIXEL_SPI.h
--- a/TI_NEOPIXEL_SPI.h	Sun Jul 28 00:13:03 2019 +0000
+++ b/TI_NEOPIXEL_SPI.h	Sat Jan 04 02:53:13 2020 +0000
@@ -15,7 +15,7 @@
 {
 public:
   TI_NEOPIXEL_SPI(PinName input);
-  
+
   void switchLightOff(int count);
   void switchLightOn(int count, int startCount, int endCount, rgbColor rgbColor);
 
@@ -24,8 +24,14 @@
 
   void circle(int count, int startCount, int endCount, rgbColor rgbColor);
   void chase(int count, int bufferCount, rgbColor c1, rgbColor c2);
-  void chaseRainbow(int count);
+  void chase2(int count, int bufferCount, rgbColor c1, rgbColor c2);
+  void chaseReverse(int count, int bufferCount, rgbColor c1, rgbColor c2);
+
+  void chaseRainbow(int count, int bufferCount);
   void circleRainbow(int count);
+
+  void moon(int count, int startIndex, int stopIndex, rgbColor c1, rgbColor c2);
+
   rgbColor convertHsvToRgb(float h, float s, float v);
 
 private: