File WS2812B.h
File List > driver > Inc > WS2812B.h
Go to the documentation of this file
#pragma once
#include "stm32xx_hal.h"
#include "FreeRTOS.h"
#include "semphr.h"
#define MAX_RGB_VAL 255
#define NUMBER_PWM_DATA_ELEMENTS 4
#define WS2812B_TIMER_PERIOD_TICKS 90
#define WS2812_HIGH (2*WS2812B_TIMER_PERIOD_TICKS)/3
#define WS2812_LOW WS2812B_TIMER_PERIOD_TICKS/3
#define WS2812_RESET_TIME 50
typedef enum{
WS2812B_OK, // WS2812B transaction completed successfully
WS2812B_NULL_ERROR, // parameter is NULL
WS2812B_ERROR, // an error occured
WS2812B_BUSY // a shared resource is busy
}ws2812b_status_t;
typedef struct{
uint8_t red; // red value
uint8_t green; // green value
uint8_t blue; // blue value
}ws2812b_color_t;
#define WS2812B_SOLID_GREEN ((ws2812b_color_t){ .red = 0, .green = 255, .blue = 0 })
#define WS2812B_SOLID_RED ((ws2812b_color_t){ .red = 255, .green = 0, .blue = 0 })
#define WS2812B_SOLID_BLUE ((ws2812b_color_t){ .red = 0, .green = 0, .blue = 255 })
#define WS2812B_SOLID_YELLOW ((ws2812b_color_t){ .red = 255, .green = 255, .blue = 0 })
#define WS2812B_SOLID_BURNT_ORANGE ((ws2812b_color_t){ .red = 204, .green = 85, .blue = 0 })
#define WS2812B_SOLID_PURPLE ((ws2812b_color_t){ .red = 128, .green = 0, .blue = 128 })
#define WS2812B_SOLID_OFF ((ws2812b_color_t){ .red = 0, .green = 0, .blue = 0 })
// Represents a string of LEDs
typedef struct{
uint8_t (*ledData)[NUMBER_PWM_DATA_ELEMENTS]; // Represents the colors contained in the strip: [LED][LEDNUM, G, R, B]
uint16_t *pwmBuffer; // PWM bitstream of duty cycles, this is what is passed to DMA
TIM_HandleTypeDef *timerHandle; // The timer handle used to generate PWM
uint32_t channel; // The channel associated with the pin's timer
uint8_t numberLeds; // the number of LEDs in the string
SemaphoreHandle_t mutex; // protects multiple threads from writting to the handle
StaticSemaphore_t mutexBuf; // static buffer for the mutex
volatile uint8_t dmaActive; // indicates when a dma transmission is active
SemaphoreHandle_t framePendingSem; // indicates that there's a new rgb frame to send
StaticSemaphore_t framePendingBuf; // static buffer to store the semaphore
}ws2812b_handle_t;
ws2812b_status_t ws2812b_init(ws2812b_handle_t *ledHandler, uint8_t ledData[][NUMBER_PWM_DATA_ELEMENTS], uint16_t *pwmData, TIM_HandleTypeDef *timerHandle, uint32_t channel, uint8_t numberLeds);
ws2812b_status_t ws2812b_set_color(ws2812b_handle_t *ledHandler, uint8_t led_num, ws2812b_color_t color, TickType_t delay_ticks);
void ws2812b_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim, ws2812b_handle_t *ledHandler, BaseType_t *xHigherPriorityTaskWoken);
ws2812b_status_t ws2812b_set_all_leds(ws2812b_handle_t *ledHandler, ws2812b_color_t color, TickType_t delay_ticks);
ws2812b_status_t ws2812b_set_led_range(ws2812b_handle_t *ledHandler, uint8_t start, uint8_t end, ws2812b_color_t color, TickType_t delay_ticks);
ws2812b_status_t ws2812b_load_colors(ws2812b_handle_t *ledHandler, const ws2812b_color_t colors[], uint8_t start, uint8_t numColors, TickType_t delay_ticks);