Routines to drive a chain of APA102 Leds.
Fork of APA102 by
A library to drive arrays of APA102 leds.
Revision 3:bf8c63d10824, committed 2015-03-14
- Comitter:
- rosienej
- Date:
- Sat Mar 14 18:17:53 2015 +0000
- Parent:
- 2:b8dc9a84801c
- Child:
- 4:2afea45fce8f
- Commit message:
- Rename to avoid other published library
Changed in this revision
--- a/APA102.cpp Wed Mar 11 14:25:18 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,60 +0,0 @@ -#include "APA102.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
--- a/APA102.h Wed Mar 11 14:25:18 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -#ifndef MBED_APA102_H - -#define MBED_PING_H - -#include "mbed.h" - - - -/** Create an APA102 Object - */ - class APA102{ - public: -/** Create an APA102 object connected to the specified mosi,miso,sclk pins - * - * @param mosi : SPI Master Out Slave In pin - * @param miso : SPI Master In Slave Out pin (ignored) - * @param sclk : SPI Clock - * @param rate : SPI Rate - */ - APA102(PinName mosi,PinName miso,PinName sclk,int rate); - -/** Set the Buffer - * - * @param buffer[] : a buffer of unsigned integers (4 bytes) *Rows*Stride in size - * @param Rows : Number of Rows - * @param Cols : Number of Columns - * @param Stride : The actual number of columns (useful for data alignment) - * @param Offset : The offset into a row - * @param ZigZag : A boolean, do we alternate count up/ count down per row? - * @param Wrap : A boolean, do we wrap if (Offset+Cols) > Stride? (handy for scrolling messages) - */ - void SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap); - -/** Repaint the Strip - * - * @param none - */ - void Repaint(); - -/** Create an IRGB helper function to construct a 4 byte LED Frame - * - * @param I : 5 bits of intensity (0,1,2,...,31) - * @param R : 8 bits of Red (0,1,...,255) - * @param G : 8 bits of Green (0,1,...,255) - * @param B : 8 bits of Blue (0,1,...,255) - */ - -int IRGB(unsigned char I,unsigned char R,unsigned char G,unsigned char B) { - return ((0xE0 + 0x1F&I)<<24)|((0xFF&R)<<16)|((0xFF&G)<<8)|(0xFF&B);} - - protected: - - SPI _spi; - private: - int NR; - int NC; - int NS; - int off; - bool ZF; - bool WF; - unsigned int * Buf; - }; - -#endif \ No newline at end of file
--- /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
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/APA102a.h Sat Mar 14 18:17:53 2015 +0000 @@ -0,0 +1,64 @@ +#ifndef MBED_APA102a_H + +#define MBED_APA102a_H + +#include "mbed.h" + + + +/** Create an APA102 Object + */ + class APA102{ + public: +/** Create an APA102 object connected to the specified mosi,miso,sclk pins + * + * @param mosi : SPI Master Out Slave In pin + * @param miso : SPI Master In Slave Out pin (ignored) + * @param sclk : SPI Clock + * @param rate : SPI Rate + */ + APA102(PinName mosi,PinName miso,PinName sclk,int rate); + +/** Set the Buffer + * + * @param buffer[] : a buffer of unsigned integers (4 bytes) *Rows*Stride in size + * @param Rows : Number of Rows + * @param Cols : Number of Columns + * @param Stride : The actual number of columns (useful for data alignment) + * @param Offset : The offset into a row + * @param ZigZag : A boolean, do we alternate count up/ count down per row? + * @param Wrap : A boolean, do we wrap if (Offset+Cols) > Stride? (handy for scrolling messages) + */ + void SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap); + +/** Repaint the Strip + * Sends to the Buffer to the Strip. + * @param none + */ + void Repaint(); + +/** Create an IRGB helper function to construct a 4 byte LED Frame + * + * @param I : 5 bits of intensity (0,1,2,...,31) + * @param R : 8 bits of Red (0,1,...,255) + * @param G : 8 bits of Green (0,1,...,255) + * @param B : 8 bits of Blue (0,1,...,255) + */ + +int IRGB(unsigned char I,unsigned char R,unsigned char G,unsigned char B) { + return ((0xE0 + 0x1F&I)<<24)|((0xFF&R)<<16)|((0xFF&G)<<8)|(0xFF&B);} + + protected: + + SPI _spi; + private: + int NR; + int NC; + int NS; + int off; + bool ZF; + bool WF; + unsigned int * Buf; + }; + +#endif \ No newline at end of file