#include "ws2812.h"
#include <stdlib.h>
#define BYTES_FOR_LED_BYTE 4
#define NB_COLORS 3
#define BYTES_FOR_LED BYTES_FOR_LED_BYTE*NB_COLORS
#define DATA_SIZE BYTES_FOR_LED*NB_LEDS
#define RESET_SIZE 200
#define PREAMBLE_SIZE 4
#define WS2812_SPI SPID2
#define NB_LEDS RGBLED_NUM
#define LED_SPIRAL 1
static uint8_t txbuf[PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE];
static uint8_t get_protocol_eq(uint8_t data, int pos);
static THD_WORKING_AREA(LEDS_THREAD_WA, 128);
static THD_FUNCTION(ledsThread, arg) {
(void) arg;
while(1){
spiSend(&WS2812_SPI, PREAMBLE_SIZE + DATA_SIZE + RESET_SIZE, txbuf);
}
}
static const SPIConfig spicfg = {
false,
NULL,
PORT_WS2812,
PIN_WS2812,
SPI_CR1_BR_1|SPI_CR1_BR_0 };
void leds_init(void){
palSetPadMode(PORT_WS2812, PIN_WS2812, PAL_MODE_STM32_ALTERNATE_PUSHPULL);
for(int i = 0; i < RESET_SIZE; i++)
txbuf[DATA_SIZE+i] = 0x00;
for (int i=0; i<PREAMBLE_SIZE; i++)
txbuf[i] = 0x00;
spiAcquireBus(&WS2812_SPI);
spiStart(&WS2812_SPI, &spicfg);
spiSelect(&WS2812_SPI);
chThdCreateStatic(LEDS_THREAD_WA, sizeof(LEDS_THREAD_WA),NORMALPRIO, ledsThread, NULL);
}
static uint8_t get_protocol_eq(uint8_t data, int pos){
uint8_t eq = 0;
if (data & (1 << (2*(3-pos))))
eq = 0b1110;
else
eq = 0b1000;
if (data & (2 << (2*(3-pos))))
eq += 0b11100000;
else
eq += 0b10000000;
return eq;
}
void WS2812_init(void) {
leds_init();
}
void ws2812_setleds(LED_TYPE *ledarray, uint16_t number_of_leds) {
uint8_t i = 0;
while (i < number_of_leds) {
set_led_color_rgb(ledarray[i], i);
i++;
}
}
void set_led_color_rgb(LED_TYPE color, int pos){
for(int j = 0; j < 4; j++)
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + j] = get_protocol_eq(color.g, j);
for(int j = 0; j < 4; j++)
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE+j] = get_protocol_eq(color.r, j);
for(int j = 0; j < 4; j++)
txbuf[PREAMBLE_SIZE + BYTES_FOR_LED*pos + BYTES_FOR_LED_BYTE*2+j] = get_protocol_eq(color.b, j);
}
void set_leds_color_rgb(LED_TYPE color){
for(int i = 0; i < NB_LEDS; i++)
set_led_color_rgb(color, i);
}
void ws2812_setleds_rgbw(LED_TYPE *ledarray, uint16_t number_of_leds) {
}