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
100#ifndef WS281X_BYTES_PER_DEVICE
104# define WS281X_BYTES_PER_DEVICE (3U)
105#endif
106
110#if WS281X_BYTES_PER_DEVICE == 4
112#else
114#endif
115
119typedef struct {
125 uint8_t *buf;
126 uint16_t numof;
127 gpio_t pin;
129
133typedef struct {
135} ws281x_t;
136
137#if defined(WS281X_HAVE_INIT) || defined(DOXYGEN)
148int ws281x_init(ws281x_t *dev, const ws281x_params_t *params);
149#else
150static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) {
151 dev->params = *params;
152 return 0;
153}
154#endif
155
184void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size);
185
186#if defined(WS281X_HAVE_PREPARE_TRANSMISSION) || defined(DOXYGEN)
193#else
194static inline void ws281x_prepare_transmission(ws281x_t *dev)
195{
196 (void)dev;
197}
198#endif
199
200#if defined(WS281X_HAVE_END_TRANSMISSION) || defined(DOXYGEN)
210#else
211static inline void ws281x_end_transmission(ws281x_t *dev)
212{
213 (void)dev;
215}
216#endif
217
228void ws281x_set_buffer(void *dest, uint16_t index, ws281x_pixel_t color);
229
240static inline void ws281x_set(ws281x_t *dev, uint16_t index, ws281x_pixel_t color)
241{
242 ws281x_set_buffer(dev->params.buf, index, color);
243}
244
253static inline void ws281x_write(ws281x_t *dev)
254{
257 (size_t)dev->params.numof * WS281X_BYTES_PER_DEVICE);
259}
260
261#ifdef __cplusplus
262}
263#endif
264
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:253
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: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.
color_rgb_t ws281x_pixel_t
Pixel type representing a single LED.
Definition ws281x.h:113
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:240
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:119
gpio_t pin
GPIO connected to the data pin of the first LED.
Definition ws281x.h:127
uint8_t * buf
A statically allocated data buffer storing the state of the LEDs.
Definition ws281x.h:125
uint16_t numof
Number of chained RGB LEDs.
Definition ws281x.h:126
Device descriptor of a WS281x RGB LED chain.
Definition ws281x.h:133
ws281x_params_t params
Parameters of the LED chain.
Definition ws281x.h:134
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