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:
2:1f20efb81649
--- a/wsDrive.h	Fri Nov 07 09:25:52 2014 +0000
+++ b/wsDrive.h	Wed Nov 12 16:43:33 2014 +0000
@@ -16,6 +16,27 @@
 
 /** A structure used to hold a single pixel or a pixel colour
 *
+* Each colour can be set to any value between about -32,768 and +32,767 (the 16 bit signed number range)
+* 0 or negative values mean that colour is off, 255 or more mean on at full brightness.
+* In between means partially on.
+* If you are never going to go outside the 0-255 range then use pixelInfo, it uses half the memory.
+* however if you are calculating pixel values when superimposing two images and don't want to worry about wrap around then use this version.
+*
+* Note, lots of LEDs on bright will use a lot of power, make sure your supply can cope.
+*
+* @param G The green component
+* @param R The red component
+* @param B The blue component
+*/
+typedef struct pixelInfo16 {
+    int16_t G;
+    int16_t R;
+    int16_t B;
+} pixelInfo16;
+
+
+/** A structure used to hold a single pixel or a pixel colour
+*
 * Each colour can be set to any value between 0 and 255.
 * 0 = off, 255 = full brightness.
 *
@@ -46,6 +67,15 @@
 *
 *  Also note that while all 3 SPI pins are specified only the MOSI pin is actually needed and should connect to the data in on the LEDs
 *
+*  Values for each LED are stored in either an array of pixelInfo or pixelInfo16 structures
+*  Each LED only supports 0-255 for each colour, the limits of the pixelInfo data type.
+*
+*  The 16 bit version is for when you are adding/subtracting values in order to avoid
+*  wraparound issues. When displayed the values outside the range are clamped to the limits.
+*  This allows tricks like having a point with negative brighnesses moving along the line to add a
+*  dark spot without having to worry about what happens if a number goes negative.
+*
+*
 *  Example code to run a single lit led along a chain
 *
 *  @code
@@ -144,12 +174,24 @@
 
     Before data can be sent the driver must be given a pointer to the pixel data to use.
     Setting this is normally a one time operation unless you want to switch between buffers.
+    Calling this function will replace any prior buffers including pixelInfo16 ones.
 
     @param dataStart The start of an array of pixelInfo items. This will be sent to the chain in order.
     @param dataLen   The length of the array.
     */
     void setData(pixelInfo *dataStart, uint16_t dataLen);
 
+    /** Set the data pointer
+
+    Before data can be sent the driver must be given a pointer to the pixel data to use.
+    Setting this is normally a one time operation unless you want to switch between buffers.
+    Calling this function will replace any prior buffers including pixelInfo ones.
+    
+    @param dataStart The start of an array of pixelInfo16 items. This will be sent to the chain in order.
+    @param dataLen   The length of the array.
+    */
+    void setData(pixelInfo16 *dataStart, uint16_t dataLen);
+
 
     /** Sends the data to the LEDs
     * setData() must be called prior to this.
@@ -160,8 +202,10 @@
 
     void sendByte(unsigned char value);
     void sendPixel(pixelInfo *pixToSend);
+    void sendPixel(pixelInfo16 *pixToSend);
 
     pixelInfo *pixArray;
+    pixelInfo16 *pixArray16;
     uint16_t pixelLen;
 
 };