#include <SPI.h>
#define leds 10
#define SpiBytes 9
#define Reset 30
#define Buffer ((leds * SpiBytes) + Reset)
uint8_t spi_buffer[Buffer];
void Colors(uint8_t color, uint8_t *dest) {
uint32_t pattern = 0;
for (int i = 7; i >= 0; i--) {
pattern <<= 3;
if (color & (1 << i)) {
pattern |= 0b110;
}
else {
pattern |= 0b100;
}
}
dest[0] = (pattern >> 16) & 0xFF;
dest[1] = (pattern >> 8) & 0xFF;
dest[2] = pattern & 0xFF;
}
void setLedColor(uint8_t index, uint8_t r, uint8_t g, uint8_t b) {
if (index >= leds) return;
uint16_t start = index * SpiBytes;
Colors(g, &spi_buffer[start]);
Colors(r, &spi_buffer[start + 3]);
Colors(b, &spi_buffer[start + 6]);
}
void showLeds() {
SPI.beginTransaction(SPISettings(2400000, MSBFIRST, SPI_MODE0));
for (int i = 0; i < Buffer; i++) {
SPI.transfer(spi_buffer[i]);
}
SPI.endTransaction();
}
void setup() {
SPI.begin();
for (int i = leds * SpiBytes; i < Buffer; i++) {
spi_buffer[i] = 0x00;
}
}
void loop() {
// Fill the 10 LEDs with a moving color effect or solid test pattern
for (int i = 0; i < leds; i++) {
setLedColor(i, 0, 0, 255); // Set to solid blue
}
showLeds();
delay(500);
for (int i = 0; i < leds; i++) {
setLedColor(i, 255, 0, 0); // Set to solid red
}
showLeds();
delay(500);
}
Loading
st-nucleo-c031c6
st-nucleo-c031c6