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.
APA102.h@2:b8dc9a84801c, 2015-03-11 (annotated)
- Committer:
- rosienej
- Date:
- Wed Mar 11 14:25:18 2015 +0000
- Revision:
- 2:b8dc9a84801c
- Parent:
- 1:ce2f23241f88
Formatting and docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rosienej | 0:2fd584b4a9b8 | 1 | #ifndef MBED_APA102_H |
rosienej | 0:2fd584b4a9b8 | 2 | |
rosienej | 0:2fd584b4a9b8 | 3 | #define MBED_PING_H |
rosienej | 0:2fd584b4a9b8 | 4 | |
rosienej | 0:2fd584b4a9b8 | 5 | #include "mbed.h" |
rosienej | 0:2fd584b4a9b8 | 6 | |
rosienej | 2:b8dc9a84801c | 7 | |
rosienej | 2:b8dc9a84801c | 8 | |
rosienej | 2:b8dc9a84801c | 9 | /** Create an APA102 Object |
rosienej | 2:b8dc9a84801c | 10 | */ |
rosienej | 0:2fd584b4a9b8 | 11 | class APA102{ |
rosienej | 0:2fd584b4a9b8 | 12 | public: |
rosienej | 1:ce2f23241f88 | 13 | /** Create an APA102 object connected to the specified mosi,miso,sclk pins |
rosienej | 0:2fd584b4a9b8 | 14 | * |
rosienej | 0:2fd584b4a9b8 | 15 | * @param mosi : SPI Master Out Slave In pin |
rosienej | 0:2fd584b4a9b8 | 16 | * @param miso : SPI Master In Slave Out pin (ignored) |
rosienej | 0:2fd584b4a9b8 | 17 | * @param sclk : SPI Clock |
rosienej | 0:2fd584b4a9b8 | 18 | * @param rate : SPI Rate |
rosienej | 0:2fd584b4a9b8 | 19 | */ |
rosienej | 0:2fd584b4a9b8 | 20 | APA102(PinName mosi,PinName miso,PinName sclk,int rate); |
rosienej | 0:2fd584b4a9b8 | 21 | |
rosienej | 1:ce2f23241f88 | 22 | /** Set the Buffer |
rosienej | 0:2fd584b4a9b8 | 23 | * |
rosienej | 0:2fd584b4a9b8 | 24 | * @param buffer[] : a buffer of unsigned integers (4 bytes) *Rows*Stride in size |
rosienej | 0:2fd584b4a9b8 | 25 | * @param Rows : Number of Rows |
rosienej | 0:2fd584b4a9b8 | 26 | * @param Cols : Number of Columns |
rosienej | 0:2fd584b4a9b8 | 27 | * @param Stride : The actual number of columns (useful for data alignment) |
rosienej | 0:2fd584b4a9b8 | 28 | * @param Offset : The offset into a row |
rosienej | 0:2fd584b4a9b8 | 29 | * @param ZigZag : A boolean, do we alternate count up/ count down per row? |
rosienej | 0:2fd584b4a9b8 | 30 | * @param Wrap : A boolean, do we wrap if (Offset+Cols) > Stride? (handy for scrolling messages) |
rosienej | 0:2fd584b4a9b8 | 31 | */ |
rosienej | 0:2fd584b4a9b8 | 32 | void SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap); |
rosienej | 0:2fd584b4a9b8 | 33 | |
rosienej | 1:ce2f23241f88 | 34 | /** Repaint the Strip |
rosienej | 0:2fd584b4a9b8 | 35 | * |
rosienej | 0:2fd584b4a9b8 | 36 | * @param none |
rosienej | 0:2fd584b4a9b8 | 37 | */ |
rosienej | 0:2fd584b4a9b8 | 38 | void Repaint(); |
rosienej | 0:2fd584b4a9b8 | 39 | |
rosienej | 2:b8dc9a84801c | 40 | /** Create an IRGB helper function to construct a 4 byte LED Frame |
rosienej | 2:b8dc9a84801c | 41 | * |
rosienej | 2:b8dc9a84801c | 42 | * @param I : 5 bits of intensity (0,1,2,...,31) |
rosienej | 2:b8dc9a84801c | 43 | * @param R : 8 bits of Red (0,1,...,255) |
rosienej | 2:b8dc9a84801c | 44 | * @param G : 8 bits of Green (0,1,...,255) |
rosienej | 2:b8dc9a84801c | 45 | * @param B : 8 bits of Blue (0,1,...,255) |
rosienej | 2:b8dc9a84801c | 46 | */ |
rosienej | 2:b8dc9a84801c | 47 | |
rosienej | 2:b8dc9a84801c | 48 | int IRGB(unsigned char I,unsigned char R,unsigned char G,unsigned char B) { |
rosienej | 2:b8dc9a84801c | 49 | return ((0xE0 + 0x1F&I)<<24)|((0xFF&R)<<16)|((0xFF&G)<<8)|(0xFF&B);} |
rosienej | 2:b8dc9a84801c | 50 | |
rosienej | 0:2fd584b4a9b8 | 51 | protected: |
rosienej | 2:b8dc9a84801c | 52 | |
rosienej | 0:2fd584b4a9b8 | 53 | SPI _spi; |
rosienej | 0:2fd584b4a9b8 | 54 | private: |
rosienej | 0:2fd584b4a9b8 | 55 | int NR; |
rosienej | 0:2fd584b4a9b8 | 56 | int NC; |
rosienej | 0:2fd584b4a9b8 | 57 | int NS; |
rosienej | 0:2fd584b4a9b8 | 58 | int off; |
rosienej | 0:2fd584b4a9b8 | 59 | bool ZF; |
rosienej | 0:2fd584b4a9b8 | 60 | bool WF; |
rosienej | 0:2fd584b4a9b8 | 61 | unsigned int * Buf; |
rosienej | 0:2fd584b4a9b8 | 62 | }; |
rosienej | 0:2fd584b4a9b8 | 63 | |
rosienej | 0:2fd584b4a9b8 | 64 | #endif |