Yet another WS2812 driver, uses the BusrtSPI library. Less features than the PixelArray library but I felt like making my own version.

Dependencies:   BurstSPI

Dependents:   WS2812Text cylon

An alternative WS2811/2812 (NeoPixel) driver using the BusrtSPI library.

Credit for the inspiration goes to Jacob Bramley for his pixelArray library that can be found here: http://developer.mbed.org/users/JacobBramley/code/PixelArray/

This version was written mainly to help me understand what was going on rather than to overcome any shortcomings in the other library and as such it lacks some features (800kHz only, no callback on each pixel etc...)

Connect the SPI output to the LED data input, other SPI pins are unused.

Note: The voltage thresholds between the LEDs and mbed devices are on paper incompatible. The datasheet for the WS2812 indicated that running at 5V it requires 4V on Din to count it as a high (the threshold is 0.8*the supply voltage). Most mbeds are lucky to output much over 3.1V. In reality things normally work OK but it depends on the mBed and batch to batch variations in the LEDs, I've seen some combinations that start to fail at an LED supply voltage of 4.4V or more. If something odd is going on try dropping the LED power supply voltage, they run fine down to 4V.

Revision:
3:3c48065d20ff
Parent:
0:b3665f91bedc
--- a/wsDrive.cpp	Fri Nov 07 09:25:52 2014 +0000
+++ b/wsDrive.cpp	Wed Nov 12 16:43:33 2014 +0000
@@ -5,6 +5,7 @@
     frequency(2400000);
     format(12);
     pixArray = NULL;
+    pixArray16 = NULL;
     pixelLen = 0;
 }
 
@@ -12,6 +13,14 @@
 {
     pixArray = dataStart;
     pixelLen = dataLen;
+    pixArray16 = NULL;
+}
+
+void wsDrive::setData(pixelInfo16 *dataStart, uint16_t dataLen)
+{
+    pixArray16 = dataStart;
+    pixelLen = dataLen;
+    pixArray = NULL;
 }
 
 void wsDrive::sendData()
@@ -22,23 +31,29 @@
 
 
     uint16_t pixIndex = 0;
-    while (pixIndex < pixelLen) {
-        sendPixel(pixArray + pixIndex++);
-    }  
+    if (pixArray) {
+        while (pixIndex < pixelLen) {
+            sendPixel(pixArray + pixIndex++);
+        }
+    } else if (pixArray16) {
+        while (pixIndex < pixelLen) {
+            sendPixel(pixArray16 + pixIndex++);
+        }
+    }
 }
 
 // each bytes sent as two 12 bit messages (3 bits of data per LED bit).
 void wsDrive::sendByte(unsigned char value)
 {
-    
+
     uint16_t dataToSend = 0;
-    
+
     uint8_t mask = 0x80;
     while (mask) {
         dataToSend += (value & mask)?0x06:0x4; // 100 for a 0 or  110 for a 1
         if (mask & 0x11) {                     // trans
-          fastWrite(dataToSend); 
-          dataToSend = 0;
+            fastWrite(dataToSend);
+            dataToSend = 0;
         }
         dataToSend = dataToSend << 3;
         mask = mask >> 1;
@@ -52,3 +67,27 @@
     sendByte(pixToSend->B);
 }
 
+void wsDrive::sendPixel(pixelInfo16 *pixToSend)
+{
+    if (pixToSend->G > 0xff)
+        sendByte(0xff);
+    else if (pixToSend->G < 0)
+        sendByte(0xff);
+    else
+        sendByte((unsigned char)pixToSend->G);
+
+    if (pixToSend->R > 0xff)
+        sendByte(0xff);
+    else if (pixToSend->R < 0)
+        sendByte(0xff);
+    else
+        sendByte((unsigned char)pixToSend->R);
+
+    if (pixToSend->B > 0xff)
+        sendByte(0xff);
+    else if (pixToSend->B < 0)
+        sendByte(0xff);
+    else
+        sendByte((unsigned char)pixToSend->B);
+
+}