Routines to drive a chain of APA102 Leds.
Fork of APA102 by
A library to drive arrays of APA102 leds.
APA102a.cpp@3:bf8c63d10824, 2015-03-14 (annotated)
- Committer:
- rosienej
- Date:
- Sat Mar 14 18:17:53 2015 +0000
- Revision:
- 3:bf8c63d10824
- Child:
- 4:2afea45fce8f
Rename to avoid other published library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rosienej | 3:bf8c63d10824 | 1 | #include "APA102a.h" |
rosienej | 3:bf8c63d10824 | 2 | #include "mbed.h" |
rosienej | 3:bf8c63d10824 | 3 | |
rosienej | 3:bf8c63d10824 | 4 | APA102::APA102(PinName mosi,PinName miso,PinName sclk,int rate) |
rosienej | 3:bf8c63d10824 | 5 | : _spi(mosi, miso, sclk) |
rosienej | 3:bf8c63d10824 | 6 | |
rosienej | 3:bf8c63d10824 | 7 | { |
rosienej | 3:bf8c63d10824 | 8 | // Setup the spi for 8 bit data, high steady state clock, |
rosienej | 3:bf8c63d10824 | 9 | // second edge capture, with a 1MHz clock rate |
rosienej | 3:bf8c63d10824 | 10 | _spi.format(8,3); |
rosienej | 3:bf8c63d10824 | 11 | _spi.frequency(rate); |
rosienej | 3:bf8c63d10824 | 12 | } |
rosienej | 3:bf8c63d10824 | 13 | |
rosienej | 3:bf8c63d10824 | 14 | void APA102::SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap) |
rosienej | 3:bf8c63d10824 | 15 | { |
rosienej | 3:bf8c63d10824 | 16 | Buf = Buffer; |
rosienej | 3:bf8c63d10824 | 17 | NR = Rows; |
rosienej | 3:bf8c63d10824 | 18 | NC = Cols; |
rosienej | 3:bf8c63d10824 | 19 | NS = Stride; |
rosienej | 3:bf8c63d10824 | 20 | off = Offset; |
rosienej | 3:bf8c63d10824 | 21 | ZF = ZigZag; |
rosienej | 3:bf8c63d10824 | 22 | WF = Wrap; |
rosienej | 3:bf8c63d10824 | 23 | } |
rosienej | 3:bf8c63d10824 | 24 | |
rosienej | 3:bf8c63d10824 | 25 | void APA102::Repaint() |
rosienej | 3:bf8c63d10824 | 26 | { |
rosienej | 3:bf8c63d10824 | 27 | int index; |
rosienej | 3:bf8c63d10824 | 28 | unsigned int val; |
rosienej | 3:bf8c63d10824 | 29 | |
rosienej | 3:bf8c63d10824 | 30 | _spi.write(0X00); // Start |
rosienej | 3:bf8c63d10824 | 31 | _spi.write(0X00); |
rosienej | 3:bf8c63d10824 | 32 | _spi.write(0X00); |
rosienej | 3:bf8c63d10824 | 33 | _spi.write(0X00); |
rosienej | 3:bf8c63d10824 | 34 | |
rosienej | 3:bf8c63d10824 | 35 | for(int r = 0;r<NR;r++) |
rosienej | 3:bf8c63d10824 | 36 | { |
rosienej | 3:bf8c63d10824 | 37 | for(int c = off;c<(NC+off);c++) |
rosienej | 3:bf8c63d10824 | 38 | { |
rosienej | 3:bf8c63d10824 | 39 | int cc = (WF)?(c%NS):((c<NS)?c:NS); |
rosienej | 3:bf8c63d10824 | 40 | if (ZF) |
rosienej | 3:bf8c63d10824 | 41 | if((r&0x01)>0) |
rosienej | 3:bf8c63d10824 | 42 | index = r*NS + NC+off-cc; |
rosienej | 3:bf8c63d10824 | 43 | else |
rosienej | 3:bf8c63d10824 | 44 | index = r*NS + cc; |
rosienej | 3:bf8c63d10824 | 45 | else |
rosienej | 3:bf8c63d10824 | 46 | index = r*NS + cc; |
rosienej | 3:bf8c63d10824 | 47 | |
rosienej | 3:bf8c63d10824 | 48 | val = Buf[index]; |
rosienej | 3:bf8c63d10824 | 49 | _spi.write((val>>24)&0xFF); |
rosienej | 3:bf8c63d10824 | 50 | _spi.write((val>>16)&0xFF); |
rosienej | 3:bf8c63d10824 | 51 | _spi.write((val>>8)&0xFF); |
rosienej | 3:bf8c63d10824 | 52 | _spi.write(val&0xFF); |
rosienej | 3:bf8c63d10824 | 53 | } |
rosienej | 3:bf8c63d10824 | 54 | } |
rosienej | 3:bf8c63d10824 | 55 | _spi.write(0XFF); // Stop |
rosienej | 3:bf8c63d10824 | 56 | _spi.write(0XFF); |
rosienej | 3:bf8c63d10824 | 57 | _spi.write(0XFF); |
rosienej | 3:bf8c63d10824 | 58 | _spi.write(0XFF); |
rosienej | 3:bf8c63d10824 | 59 | |
rosienej | 3:bf8c63d10824 | 60 | } |