Optimised fork of bikeNomad's WS2811 LED control library. Supports KL25Z and KL46Z

Dependents:   CubicHand

Fork of Multi_WS2811 by Ned Konz

Optimised to use far less RAM than the original.

Capable of running up to 8 strings of 240 LEDs each with plenty of RAM to spare on the KL46Z.

Should run at least three strings of 240 LEDs on the KL25Z (RAM limited)

Revision:
7:58623ad7f310
Parent:
0:a8535703f23b
--- a/LedStrip.cpp	Wed Apr 02 12:16:42 2014 +0000
+++ b/LedStrip.cpp	Wed Apr 02 13:22:25 2014 +0000
@@ -1,9 +1,9 @@
 #include "LedStrip.h"
 
-LedStrip::LedStrip(int n)
+LedStrip::LedStrip(uint16_t pixelCount) :
+    numLEDs(pixelCount)
 {
-   // Allocate 3 bytes per pixel:
-    numLEDs = n;
+    // Allocate 3 bytes per pixel:
     pixels = (uint8_t *)malloc(numPixelBytes());
     if (pixels) {
         memset(pixels, 0x00, numPixelBytes()); // Init to RGB 'off' state
@@ -14,8 +14,8 @@
 {
     free(pixels);
 }
- 
-uint32_t LedStrip::total_luminance(void)
+
+uint32_t LedStrip::total_luminance()
 {
     uint32_t running_total;
     running_total = 0;
@@ -25,56 +25,53 @@
 }
 
 // Convert R,G,B to combined 32-bit color
-uint32_t LedStrip::Color(uint8_t r, uint8_t g, uint8_t b)
+uint32_t LedStrip::Color(uint8_t red, uint8_t green, uint8_t blue)
 {
-    // Take the lowest 7 bits of each value and append them end to end
-    // We have the top bit set high (its a 'parity-like' bit in the protocol
-    // and must be set!)
-    return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b;
+    return ((uint32_t)green << 16) | ((uint32_t)red << 8) | (uint32_t)blue;
 }
 
-// store the rgb component in our array
-void LedStrip::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)
+// Store the rgb component in our array
+void LedStrip::setPixelColor(uint16_t pixNum, uint8_t red, uint8_t green, uint8_t blue)
 {
-    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
-
-    pixels[n*3  ] = g;
-    pixels[n*3+1] = r;
-    pixels[n*3+2] = b;
+    if (pixNum < numLEDs) {
+        pixels[pixNum*3  ] = green;
+        pixels[pixNum*3+1] = red;
+        pixels[pixNum*3+2] = blue;
+    }
 }
 
-void LedStrip::setPixelR(uint16_t n, uint8_t r)
+void LedStrip::setPixelR(uint16_t pixNum, uint8_t red)
 {
-    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
-
-    pixels[n*3+1] = r;
+    if (pixNum < numLEDs) {
+        pixels[pixNum*3+1] = red;
+    }
 }
 
-void LedStrip::setPixelG(uint16_t n, uint8_t g)
+void LedStrip::setPixelG(uint16_t pixNum, uint8_t green)
 {
-    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
-
-    pixels[n*3] = g;
+    if (pixNum < numLEDs) {
+        pixels[pixNum*3] = green;
+    }
 }
 
-void LedStrip::setPixelB(uint16_t n, uint8_t b)
+void LedStrip::setPixelB(uint16_t pixNum, uint8_t blue)
 {
-    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
-
-    pixels[n*3+2] = b;
+    if (pixNum < numLEDs) {
+        pixels[pixNum*3+2] = blue;
+    }
 }
 
-void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t n)
+void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t count)
 {
-    if (n >= numLEDs) return;
-    memcpy(pixels, buffer, (size_t) (n*3));
+    if (count > numLEDs) return;
+    memcpy(pixels, buffer, (size_t) (count*3));
 }
 
-void LedStrip::setPixelColor(uint16_t n, uint32_t c)
+void LedStrip::setPixelColor(uint16_t pixNum, uint32_t color)
 {
-    if (n >= numLEDs) return; // '>=' because arrays are 0-indexed
-
-    pixels[n*3  ] = (c >> 16);
-    pixels[n*3+1] = (c >>  8);
-    pixels[n*3+2] =  c;
+    if (pixNum < numLEDs) {
+        pixels[pixNum*3  ] = (color >> 16);
+        pixels[pixNum*3+1] = (color >>  8);
+        pixels[pixNum*3+2] =  color;
+    }
 }