diff --git a/drivers/Makefile.dep b/drivers/Makefile.dep index c6d63a087a..25c564640e 100644 --- a/drivers/Makefile.dep +++ b/drivers/Makefile.dep @@ -693,11 +693,6 @@ ifneq (,$(filter ws281x,$(USEMODULE))) USEMODULE += xtimer endif -ifneq (,$(filter ws281x_vt100,$(USEMODULE))) - CFLAGS += -DWS281X_HAVE_PREPARE_TRANSMISSION - CFLAGS += -DWS281X_HAVE_END_TRANSMISSION -endif - ifneq (,$(filter xbee,$(USEMODULE))) FEATURES_REQUIRED += periph_uart FEATURES_REQUIRED += periph_gpio diff --git a/drivers/include/ws281x.h b/drivers/include/ws281x.h index 86d659dfaf..608ee82c89 100644 --- a/drivers/include/ws281x.h +++ b/drivers/include/ws281x.h @@ -59,6 +59,7 @@ #include "color.h" #include "periph/gpio.h" +#include "ws281x_backend.h" #include "ws281x_constants.h" #include "xtimer.h" @@ -92,6 +93,7 @@ typedef struct { ws281x_params_t params; /**< Parameters of the LED chain */ } ws281x_t; +#if defined(WS281X_HAVE_INIT) || defined(DOXYGEN) /** * @brief Initialize an WS281x RGB LED chain * @@ -103,6 +105,12 @@ typedef struct { * @retval -EIO Failed to initialize the data GPIO pin */ int ws281x_init(ws281x_t *dev, const ws281x_params_t *params); +#else +static inline int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) { + dev->params = *params; + return 0; +} +#endif /** * @brief Writes the color data of the user supplied buffer diff --git a/drivers/ws281x/atmega.c b/drivers/ws281x/atmega.c index f3076fa8b5..b549d39812 100644 --- a/drivers/ws281x/atmega.c +++ b/drivers/ws281x/atmega.c @@ -203,3 +203,19 @@ void ws281x_write_buffer(ws281x_t *dev, const void *buf, size_t size) #error "No low level WS281x implementation for ATmega CPUs for your CPU clock" #endif } + +int ws281x_init(ws281x_t *dev, const ws281x_params_t *params) +{ + if (!dev || !params || !params->buf) { + return -EINVAL; + } + + memset(dev, 0, sizeof(ws281x_t)); + dev->params = *params; + + if (gpio_init(dev->params.pin, GPIO_OUT)) { + return -EIO; + } + + return 0; +} diff --git a/drivers/ws281x/include/ws281x_backend.h b/drivers/ws281x/include/ws281x_backend.h new file mode 100644 index 0000000000..eb396ca743 --- /dev/null +++ b/drivers/ws281x/include/ws281x_backend.h @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2019 Marian Buschsieweke + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * @ingroup drivers_ws281x + * + * @{ + * @file + * @brief Backend configuration for WS2812/SK6812 RGB LEDs + * + * @author Marian Buschsieweke + */ + +#ifndef WS281X_BACKEND_H +#define WS281X_BACKEND_H + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name Properties of the ATmega backend. + * @{ + */ +#ifdef MODULE_WS281X_ATMEGA +#define WS281X_HAVE_INIT (1) +#endif +/** @} */ + +/** + * @name Properties of the VT100 terminal backend. + * @{ + */ +#ifdef MODULE_WS281X_VT100 +#define WS281X_HAVE_PREPARE_TRANSMISSION (1) +#define WS281X_HAVE_END_TRANSMISSION (1) +#endif +/** @} */ + +#ifdef __cplusplus +} +#endif + +#endif /* WS281X_BACKEND_H */ +/** @} */ diff --git a/drivers/ws281x/ws281x.c b/drivers/ws281x/ws281x.c index 9624d2395f..ef388bc5ea 100644 --- a/drivers/ws281x/ws281x.c +++ b/drivers/ws281x/ws281x.c @@ -31,25 +31,6 @@ /* Default buffer used in ws281x_params.h. Will be optimized out if unused */ uint8_t ws281x_buf[WS281X_PARAM_NUMOF * WS281X_BYTES_PER_DEVICE]; -/* Some backend will need a custom init function. Declaring this as weak symbol - * allows them to provide their own. */ -int __attribute__((weak)) ws281x_init(ws281x_t *dev, - const ws281x_params_t *params) -{ - if (!dev || !params || !params->buf) { - return -EINVAL; - } - - memset(dev, 0, sizeof(ws281x_t)); - dev->params = *params; - - if (gpio_init(dev->params.pin, GPIO_OUT)) { - return -EIO; - } - - return 0; -} - void ws281x_set_buffer(void *_dest, uint16_t n, color_rgb_t c) { uint8_t *dest = _dest;