And that does nothing else then combining the RGB components into on 32bit number and placing that integer in a pixel buffer at position px
.
Just like this
#define LEDCOUNT 300
uint32_t pxBuffer[LEDCOUNT];
// actually for the APA102 protocoll you need some extra bytes for leadin/leadout
//uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)
bool needRefresh;
volatile bool canRefresh = true;
uint32_t setAPA102Color(int px, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness = 31) {
uint32_t color = 0;
// due to big endianness in revers order
color |= (brighntess << 0) | 0xE0; //top 3 bits must be 111
color |= (r << 8);
color |= (g << 16);
color |= (b << 24);
pxBuffer[px] = color;
return color;
}
void refreshDone() {
canRefresh = true;
}
void setup() {
SPI.begin();
SPI.setClockSpeed(8, MHZ);
//SPI.setDataMode(0);
SPI.setBitOrder(MSBFIRST);
}
void loop() {
// setup your pixels
if (needRefresh && canRefresh) {
canRefresh =
needRefresh = false;
SPI.transfer(pxBuffer, NULL, sizeof(pxBuffer), refreshDone);
}
}
This is all you need to work with APA102 strips - actually it's already a bit more to also illustrate use of DMA and guard against messing up data while still being sent out in the background.