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
9#pragma once
10
87
88#include <stdint.h>
89
90#include "color.h"
91#include "periph/gpio.h"
92#include "ws281x_backend.h"
93#include "ws281x_constants.h"
94#include "xtimer.h"
95
96#ifdef __cplusplus
97extern "C" {
98#endif
99
103#define WS281X_BYTES_PER_DEVICE (3U)
104
108typedef struct {
114 uint8_t *buf;
115 uint16_t numof;
116 gpio_t pin;
118
122typedef struct {
124} ws281x_t;
125
126#if defined(WS281X_HAVE_INIT) || defined(DOXYGEN)
137int ws281x_init(ws281x_t *dev, const ws281x_params_t *params);
138#else
139static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) {
140 dev->params = *params;
141 return 0;
142}
143#endif
144
173void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size);
174
175#if defined(WS281X_HAVE_PREPARE_TRANSMISSION) || defined(DOXYGEN)
182#else
183static inline void ws281x_prepare_transmission(ws281x_t *dev)
184{
185 (void)dev;
186}
187#endif
188
189#if defined(WS281X_HAVE_END_TRANSMISSION) || defined(DOXYGEN)
199#else
200static inline void ws281x_end_transmission(ws281x_t *dev)
201{
202 (void)dev;
204}
205#endif
206
217void ws281x_set_buffer(void *dest, uint16_t index, color_rgb_t color);
218
229static inline void ws281x_set(ws281x_t *dev, uint16_t index, color_rgb_t color)
230{
231 ws281x_set_buffer(dev->params.buf, index, color);
232}
233
242static inline void ws281x_write(ws281x_t *dev)
243{
246 (size_t)dev->params.numof * WS281X_BYTES_PER_DEVICE);
248}
249
250#ifdef __cplusplus
251}
252#endif
253
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:229
static void ws281x_write(ws281x_t *dev)
Writes the internal buffer to the LED chain.
Definition ws281x.h:242
#define WS281X_BYTES_PER_DEVICE
The number of bytes to allocate in the data buffer per LED.
Definition ws281x.h:103
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:34
Struct to hold initialization parameters for a WS281x RGB LED.
Definition ws281x.h:108
gpio_t pin
GPIO connected to the data pin of the first LED.
Definition ws281x.h:116
uint8_t * buf
A statically allocated data buffer storing the state of the LEDs.
Definition ws281x.h:114
uint16_t numof
Number of chained RGB LEDs.
Definition ws281x.h:115
Device descriptor of a WS281x RGB LED chain.
Definition ws281x.h:122
ws281x_params_t params
Parameters of the LED chain.
Definition ws281x.h:123
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