day09/ex05/include/lib/apa102.h
2026-04-27 15:42:00 +02:00

47 lines
998 B
C

#ifndef APA102_H
#define APA102_H
#include <avr/io.h>
#include "lib/mystd.h"
#include "lib/spi.h"
typedef struct rgb {
uint8_t r;
uint8_t g;
uint8_t b;
} rgb;
static inline void apa102_set_colors(rgb col[], uint8_t brightness[], uint8_t length) {
// start frame
for (uint8_t i = 0; i < 4; i++)
spi_write(0x00);
// led data
for (uint8_t i = 0; i < length; i++) {
spi_write(0xE0 + (brightness[i] & 31));
spi_write(col[i].b);
spi_write(col[i].g);
spi_write(col[i].r);
}
// reset frame
for (uint8_t i = 0; i < 4; i++)
spi_write(0xFF);
// End frame: 8+8*(leds >> 4) clock cycles
for (uint8_t i = 0; i < length; i += 16) {
spi_write(0xFF); // 8 more clock cycles
}
}
static inline void apa102_set_color(rgb col, uint8_t brightness) {
apa102_set_colors(&col, &brightness, 1);
}
static inline void apa102_set_all_color(rgb col)
{
rgb cols[3] = {col, col, col};
uint8_t brightness[3] = {31, 31, 31};
apa102_set_colors(cols, brightness, 3);
}
#endif /* APA102_H */