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.

Committer:
rosienej
Date:
Wed Mar 11 14:25:18 2015 +0000
Revision:
2:b8dc9a84801c
Parent:
0:2fd584b4a9b8
Formatting and docs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rosienej 0:2fd584b4a9b8 1 #include "APA102.h"
rosienej 0:2fd584b4a9b8 2 #include "mbed.h"
rosienej 0:2fd584b4a9b8 3
rosienej 0:2fd584b4a9b8 4 APA102::APA102(PinName mosi,PinName miso,PinName sclk,int rate)
rosienej 0:2fd584b4a9b8 5 : _spi(mosi, miso, sclk)
rosienej 0:2fd584b4a9b8 6
rosienej 0:2fd584b4a9b8 7 {
rosienej 0:2fd584b4a9b8 8 // Setup the spi for 8 bit data, high steady state clock,
rosienej 0:2fd584b4a9b8 9 // second edge capture, with a 1MHz clock rate
rosienej 0:2fd584b4a9b8 10 _spi.format(8,3);
rosienej 0:2fd584b4a9b8 11 _spi.frequency(rate);
rosienej 0:2fd584b4a9b8 12 }
rosienej 0:2fd584b4a9b8 13
rosienej 0:2fd584b4a9b8 14 void APA102::SetBuffer(unsigned int Buffer[],int Rows,int Cols, int Stride,int Offset, bool ZigZag,bool Wrap)
rosienej 0:2fd584b4a9b8 15 {
rosienej 0:2fd584b4a9b8 16 Buf = Buffer;
rosienej 0:2fd584b4a9b8 17 NR = Rows;
rosienej 0:2fd584b4a9b8 18 NC = Cols;
rosienej 0:2fd584b4a9b8 19 NS = Stride;
rosienej 0:2fd584b4a9b8 20 off = Offset;
rosienej 0:2fd584b4a9b8 21 ZF = ZigZag;
rosienej 0:2fd584b4a9b8 22 WF = Wrap;
rosienej 0:2fd584b4a9b8 23 }
rosienej 0:2fd584b4a9b8 24
rosienej 0:2fd584b4a9b8 25 void APA102::Repaint()
rosienej 0:2fd584b4a9b8 26 {
rosienej 0:2fd584b4a9b8 27 int index;
rosienej 0:2fd584b4a9b8 28 unsigned int val;
rosienej 0:2fd584b4a9b8 29
rosienej 0:2fd584b4a9b8 30 _spi.write(0X00); // Start
rosienej 0:2fd584b4a9b8 31 _spi.write(0X00);
rosienej 0:2fd584b4a9b8 32 _spi.write(0X00);
rosienej 0:2fd584b4a9b8 33 _spi.write(0X00);
rosienej 0:2fd584b4a9b8 34
rosienej 0:2fd584b4a9b8 35 for(int r = 0;r<NR;r++)
rosienej 0:2fd584b4a9b8 36 {
rosienej 0:2fd584b4a9b8 37 for(int c = off;c<(NC+off);c++)
rosienej 0:2fd584b4a9b8 38 {
rosienej 0:2fd584b4a9b8 39 int cc = (WF)?(c%NS):((c<NS)?c:NS);
rosienej 0:2fd584b4a9b8 40 if (ZF)
rosienej 0:2fd584b4a9b8 41 if((r&0x01)>0)
rosienej 0:2fd584b4a9b8 42 index = r*NS + NC+off-cc;
rosienej 0:2fd584b4a9b8 43 else
rosienej 0:2fd584b4a9b8 44 index = r*NS + cc;
rosienej 0:2fd584b4a9b8 45 else
rosienej 0:2fd584b4a9b8 46 index = r*NS + cc;
rosienej 0:2fd584b4a9b8 47
rosienej 0:2fd584b4a9b8 48 val = Buf[index];
rosienej 0:2fd584b4a9b8 49 _spi.write((val>>24)&0xFF);
rosienej 0:2fd584b4a9b8 50 _spi.write((val>>16)&0xFF);
rosienej 0:2fd584b4a9b8 51 _spi.write((val>>8)&0xFF);
rosienej 0:2fd584b4a9b8 52 _spi.write(val&0xFF);
rosienej 0:2fd584b4a9b8 53 }
rosienej 0:2fd584b4a9b8 54 }
rosienej 0:2fd584b4a9b8 55 _spi.write(0XFF); // Stop
rosienej 0:2fd584b4a9b8 56 _spi.write(0XFF);
rosienej 0:2fd584b4a9b8 57 _spi.write(0XFF);
rosienej 0:2fd584b4a9b8 58 _spi.write(0XFF);
rosienej 0:2fd584b4a9b8 59
rosienej 0:2fd584b4a9b8 60 }