Routines to drive a chain of APA102 Leds.
Fork of APA102a by
Diff: APA102a.cpp
- Revision:
- 3:bf8c63d10824
- Child:
- 4:2afea45fce8f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/APA102a.cpp Sat Mar 14 18:17:53 2015 +0000 @@ -0,0 +1,60 @@ +#include "APA102a.h" +#include "mbed.h" + +APA102::APA102(PinName mosi,PinName miso,PinName sclk,int rate) + : _spi(mosi, miso, sclk) + + { + // Setup the spi for 8 bit data, high steady state clock, + // second edge capture, with a 1MHz clock rate + _spi.format(8,3); + _spi.frequency(rate); + } + +void APA102::SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap) +{ + Buf = Buffer; + NR = Rows; + NC = Cols; + NS = Stride; + off = Offset; + ZF = ZigZag; + WF = Wrap; + } + +void APA102::Repaint() +{ + int index; + unsigned int val; + + _spi.write(0X00); // Start + _spi.write(0X00); + _spi.write(0X00); + _spi.write(0X00); + + for(int r = 0;r<NR;r++) + { + for(int c = off;c<(NC+off);c++) + { + int cc = (WF)?(c%NS):((c<NS)?c:NS); + if (ZF) + if((r&0x01)>0) + index = r*NS + NC+off-cc; + else + index = r*NS + cc; + else + index = r*NS + cc; + + val = Buf[index]; + _spi.write((val>>24)&0xFF); + _spi.write((val>>16)&0xFF); + _spi.write((val>>8)&0xFF); + _spi.write(val&0xFF); + } + } + _spi.write(0XFF); // Stop + _spi.write(0XFF); + _spi.write(0XFF); + _spi.write(0XFF); + +} \ No newline at end of file