40 lines
824 B
C
40 lines
824 B
C
#ifndef APA102_H
|
|
#define APA102_H
|
|
|
|
#include <avr/io.h>
|
|
#include "mystd.h"
|
|
#include "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(0x00);
|
|
|
|
// End frame: 8+8*(leds >> 4) clock cycles
|
|
for (uint8_t i = 0; i < length; i += 16) {
|
|
spi_write(0x00); // 8 more clock cycles
|
|
}
|
|
}
|
|
|
|
static inline void apa102_set_color(rgb col, uint8_t brightness) {
|
|
apa102_set_colors(&col, &brightness, 1);
|
|
}
|
|
|
|
#endif /* APA102_H */
|