Loading...
Searching...
No Matches
ws281x.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2019 Marian Buschsieweke
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
84
85#include <stdint.h>
86
87#include "color.h"
88#include "periph/gpio.h"
89#include "ws281x_backend.h"
90#include "ws281x_constants.h"
91#include "xtimer.h"
92
93#ifdef __cplusplus
94extern "C" {
95#endif
96
97#ifndef WS281X_BYTES_PER_DEVICE
101# define WS281X_BYTES_PER_DEVICE (3U)
102#endif
103
107#if WS281X_BYTES_PER_DEVICE == 4
109#else
111#endif
112
116typedef struct {
122 uint8_t *buf;
123 uint16_t numof;
124 gpio_t pin;
126
130typedef struct {
132} ws281x_t;
133
134#if defined(WS281X_HAVE_INIT) || defined(DOXYGEN)
145int ws281x_init(ws281x_t *dev, const ws281x_params_t *params);
146#else
147static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) {
148 dev->params = *params;
149 return 0;
150}
151#endif
152
181void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size);
182
183#if defined(WS281X_HAVE_PREPARE_TRANSMISSION) || defined(DOXYGEN)
190#else
191static inline void ws281x_prepare_transmission(ws281x_t *dev)
192{
193 (void)dev;
194}
195#endif
196
197#if defined(WS281X_HAVE_END_TRANSMISSION) || defined(DOXYGEN)
207#else
208static inline void ws281x_end_transmission(ws281x_t *dev)
209{
210 (void)dev;
212}
213#endif
214
225void ws281x_set_buffer(void *dest, uint16_t index, ws281x_pixel_t color);
226
237static inline void ws281x_set(ws281x_t *dev, uint16_t index, ws281x_pixel_t color)
238{
239 ws281x_set_buffer(dev->params.buf, index, color);
240}
241
250static inline void ws281x_write(ws281x_t *dev)
251{
254 (size_t)dev->params.numof * WS281X_BYTES_PER_DEVICE);
256}
257
258#ifdef __cplusplus
259}
260#endif
261
Headers for the color handling module.
Low-level GPIO peripheral driver interface definitions.
static void ws281x_write(ws281x_t *dev)
Writes the internal buffer to the LED chain.
Definition ws281x.h:250
void ws281x_set_buffer(void *dest, uint16_t index, ws281x_pixel_t color)
Sets the color of an LED in the given data buffer.
#define WS281X_BYTES_PER_DEVICE
The number of bytes to allocate in the data buffer per LED.
Definition ws281x.h:101
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.
color_rgb_t ws281x_pixel_t
Pixel type representing a single LED.
Definition ws281x.h:110
static void ws281x_set(ws281x_t *dev, uint16_t index, ws281x_pixel_t color)
Sets the color of an LED in the chain in the internal buffer.
Definition ws281x.h:237
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.
static void xtimer_usleep(uint32_t microseconds)
Pause the execution of a thread for some microseconds.
Data-structure describing an RGB color.
Definition color.h:34
Data-structure describing an RGBW color.
Definition color.h:43
Struct to hold initialization parameters for a WS281x RGB LED.
Definition ws281x.h:116
gpio_t pin
GPIO connected to the data pin of the first LED.
Definition ws281x.h:124
uint8_t * buf
A statically allocated data buffer storing the state of the LEDs.
Definition ws281x.h:122
uint16_t numof
Number of chained RGB LEDs.
Definition ws281x.h:123
Device descriptor of a WS281x RGB LED chain.
Definition ws281x.h:130
ws281x_params_t params
Parameters of the LED chain.
Definition ws281x.h:131
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