Routines to drive a chain of APA102 Leds.
Dependents: blink_led_threading
The APA102 is an LED with a built in pwm driver which can be drive by SPI.
An array of Intensity, Red, Green, Blue values packed in to integers is displayed on a strip of APA102 elements.
The array is characterized by Rows, (active) Columns, Offset, Stride, and flags which determine how to traverse the data.
To support scrolling messages, it is possible to allocate an array which is Rows x Stride in size, and use offset to "scroll" the message. The Wrap flag is used to allow the index into the array to wrap back to zero.
To support the physical construction of arrays, the zigzag flag is used to cause the display order of odd rows to be reversed. For a 3x3 array the values are displayed in the following order is zigzag is set.
1 2 3
6 5 4
7 8 9
This allows the 3rd element to be wired to the fourth element, shortening the length of the wires on the array.
Diff: APA102.h
- Revision:
- 0:2fd584b4a9b8
- Child:
- 1:ce2f23241f88
diff -r 000000000000 -r 2fd584b4a9b8 APA102.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/APA102.h Wed Mar 11 14:03:02 2015 +0000 @@ -0,0 +1,59 @@ +#ifndef MBED_APA102_H + +#define MBED_PING_H + +#include "mbed.h" + +/** Create an APA102_IRGB helper macro 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) + */ + + #define APA102_IRGB(I,R,G,B) ((0xE0 + 0x1F&I)<<24)|((0xFF&R)<<16)|((0xFF&G)<<8)|(0xFF&B) + + 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(); + + protected: + // SPI _spi(PTA16, PTA17, PTA15); // mosi, miso, sclk + 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