ws281x.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 Marian Buschsieweke
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
86 #ifndef WS281X_H
87 #define WS281X_H
88 
89 #include <stdint.h>
90 
91 #include "color.h"
92 #include "periph/gpio.h"
93 #include "ws281x_backend.h"
94 #include "ws281x_constants.h"
95 #include "xtimer.h"
96 
97 #ifdef __cplusplus
98 extern "C" {
99 #endif
100 
104 #define WS281X_BYTES_PER_DEVICE (3U)
105 
109 typedef struct {
115  uint8_t *buf;
116  uint16_t numof;
117  gpio_t pin;
119 
123 typedef struct {
125 } ws281x_t;
126 
127 #if defined(WS281X_HAVE_INIT) || defined(DOXYGEN)
128 
138 int ws281x_init(ws281x_t *dev, const ws281x_params_t *params);
139 #else
140 static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) {
141  dev->params = *params;
142  return 0;
143 }
144 #endif
145 
174 void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size);
175 
176 #if defined(WS281X_HAVE_PREPARE_TRANSMISSION) || defined(DOXYGEN)
177 
183 #else
184 static inline void ws281x_prepare_transmission(ws281x_t *dev)
185 {
186  (void)dev;
187 }
188 #endif
189 
190 #if defined(WS281X_HAVE_END_TRANSMISSION) || defined(DOXYGEN)
191 
200 #else
201 static inline void ws281x_end_transmission(ws281x_t *dev)
202 {
203  (void)dev;
205 }
206 #endif
207 
218 void ws281x_set_buffer(void *dest, uint16_t index, color_rgb_t color);
219 
230 static inline void ws281x_set(ws281x_t *dev, uint16_t index, color_rgb_t color)
231 {
232  ws281x_set_buffer(dev->params.buf, index, color);
233 }
234 
243 static inline void ws281x_write(ws281x_t *dev)
244 {
246  ws281x_write_buffer(dev, dev->params.buf,
247  (size_t)dev->params.numof * WS281X_BYTES_PER_DEVICE);
249 }
250 
251 #ifdef __cplusplus
252 }
253 #endif
254 
255 #endif /* WS281X_H */
256 
ws281x_init
int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
Initialize an WS281x RGB LED chain.
ws281x_prepare_transmission
void ws281x_prepare_transmission(ws281x_t *dev)
Sets up everything needed to write data to the WS281X LED chain.
ws281x_t
Device descriptor of a WS281x RGB LED chain.
Definition: ws281x.h:123
ws281x_t::params
ws281x_params_t params
Parameters of the LED chain.
Definition: ws281x.h:124
ws281x_params_t::buf
uint8_t * buf
A statically allocated data buffer storing the state of the LEDs.
Definition: ws281x.h:115
ws281x_write
static void ws281x_write(ws281x_t *dev)
Writes the internal buffer to the LED chain.
Definition: ws281x.h:243
ws281x_constants.h
Constants for WS2812/SK6812 RGB LEDs.
ws281x_write_buffer
void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size)
Writes the color data of the user supplied buffer.
ws281x_params_t::numof
uint16_t numof
Number of chained RGB LEDs.
Definition: ws281x.h:116
ws281x_params_t
Struct to hold initialization parameters for a WS281x RGB LED.
Definition: ws281x.h:109
xtimer_usleep
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
ws281x_set
static void ws281x_set(ws281x_t *dev, uint16_t index, color_rgb_t color)
Sets the color of an LED in the chain in the internal buffer.
Definition: ws281x.h:230
ws281x_set_buffer
void ws281x_set_buffer(void *dest, uint16_t index, color_rgb_t color)
Sets the color of an LED in the given data buffer.
WS281X_BYTES_PER_DEVICE
#define WS281X_BYTES_PER_DEVICE
The number of bytes to allocate in the data buffer per LED.
Definition: ws281x.h:104
gpio.h
Low-level GPIO peripheral driver interface definitions.
ws281x_params_t::pin
gpio_t pin
GPIO connected to the data pin of the first LED.
Definition: ws281x.h:117
ws281x_backend.h
Backend configuration for WS2812/SK6812 RGB LEDs.
color.h
Headers for the color handling module.
color_rgb_t
Data-structure describing a RGB color.
Definition: color.h:35
WS281X_T_END_US
#define WS281X_T_END_US
Time in microseconds to pull the data line low to signal end of data.
Definition: ws281x_constants.h:53
xtimer.h
xtimer interface definitions
ws281x_end_transmission
void ws281x_end_transmission(ws281x_t *dev)
Ends the transmission to the WS2812/SK6812 LED chain.