Loading...
Searching...
No Matches
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
98extern "C" {
99#endif
100
104#define WS281X_BYTES_PER_DEVICE (3U)
105
109typedef struct {
115 uint8_t *buf;
116 uint16_t numof;
117 gpio_t pin;
119
123typedef struct {
125} ws281x_t;
126
127#if defined(WS281X_HAVE_INIT) || defined(DOXYGEN)
138int ws281x_init(ws281x_t *dev, const ws281x_params_t *params);
139#else
140static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) {
141 dev->params = *params;
142 return 0;
143}
144#endif
145
174void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size);
175
176#if defined(WS281X_HAVE_PREPARE_TRANSMISSION) || defined(DOXYGEN)
183#else
184static 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)
200#else
201static inline void ws281x_end_transmission(ws281x_t *dev)
202{
203 (void)dev;
205}
206#endif
207
218void ws281x_set_buffer(void *dest, uint16_t index, color_rgb_t color);
219
230static 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
243static inline void ws281x_write(ws281x_t *dev)
244{
247 (size_t)dev->params.numof * WS281X_BYTES_PER_DEVICE);
249}
250
251#ifdef __cplusplus
252}
253#endif
254
255#endif /* WS281X_H */
Headers for the color handling module.
Low-level GPIO peripheral driver interface definitions.
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
static void ws281x_write(ws281x_t *dev)
Writes the internal buffer to the LED chain.
Definition ws281x.h:243
#define WS281X_BYTES_PER_DEVICE
The number of bytes to allocate in the data buffer per LED.
Definition ws281x.h:104
void ws281x_end_transmission(ws281x_t *dev)
Ends the transmission to the WS2812/SK6812 LED chain.
int ws281x_init(ws281x_t *dev, const ws281x_params_t *params)
Initialize an WS281x RGB LED chain.
void ws281x_prepare_transmission(ws281x_t *dev)
Sets up everything needed to write data to the WS281X LED chain.
void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size)
Writes the color data of the user supplied 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.
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
Data-structure describing a RGB color.
Definition color.h:35
Struct to hold initialization parameters for a WS281x RGB LED.
Definition ws281x.h:109
gpio_t pin
GPIO connected to the data pin of the first LED.
Definition ws281x.h:117
uint8_t * buf
A statically allocated data buffer storing the state of the LEDs.
Definition ws281x.h:115
uint16_t numof
Number of chained RGB LEDs.
Definition ws281x.h:116
Device descriptor of a WS281x RGB LED chain.
Definition ws281x.h:123
ws281x_params_t params
Parameters of the LED chain.
Definition ws281x.h:124
Backend configuration for WS2812/SK6812 RGB LEDs.
Constants for WS2812/SK6812 RGB LEDs.
#define WS281X_T_END_US
Time in microseconds to pull the data line low to signal end of data.
xtimer interface definitions