driver for WS2812B LED, modified for better compatibility with LPC812 and LPC1549

Dependencies:   BurstSPI

Dependents:   RGB-balls cylon

Fork of wsDrive by Andy A

wsDrive.cpp

Committer:
AndyA
Date:
2014-11-05
Revision:
0:b3665f91bedc
Child:
3:3c48065d20ff

File content as of revision 0:b3665f91bedc:

#include "wsDrive.h"

wsDrive::wsDrive(PinName mosi, PinName miso, PinName clk) : BurstSPI(mosi,miso,clk)
{
    frequency(2400000);
    format(12);
    pixArray = NULL;
    pixelLen = 0;
}

void wsDrive::setData(pixelInfo *dataStart, uint16_t dataLen)
{
    pixArray = dataStart;
    pixelLen = dataLen;
}

void wsDrive::sendData()
{
    frequency(2400000);
    format(12);
    setFormat();


    uint16_t pixIndex = 0;
    while (pixIndex < pixelLen) {
        sendPixel(pixArray + 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;
        }
        dataToSend = dataToSend << 3;
        mask = mask >> 1;
    }
}

void wsDrive::sendPixel(pixelInfo *pixToSend)
{
    sendByte(pixToSend->G);
    sendByte(pixToSend->R);
    sendByte(pixToSend->B);
}