Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers/ws281x: Add saul support #20562

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boards/feather-nrf52840-sense/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += lsm6dsxx
USEMODULE += saul_gpio
USEMODULE += sht3x
USEMODULE += ws281x
endif

# include common nrf52 dependencies
Expand Down
3 changes: 3 additions & 0 deletions boards/feather-nrf52840-sense/include/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,11 @@
* @{
*/
#ifndef WS281X_PARAM_PIN
#define WS281X_PARAM_PIN GPIO_PIN(0, 16) /**< GPIO pin connected to the data pin of the first LED */

Check warning on line 98 in boards/feather-nrf52840-sense/include/board.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
#endif
#ifndef WS281X_PARAM_NUMOF
#define WS281X_PARAM_NUMOF (1U) /**< Number of LEDs chained */
#endif
/** @} */

#ifdef __cplusplus
Expand Down
71 changes: 71 additions & 0 deletions drivers/saul/init_devs/auto_init_ws281x.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (C) 2024 HAW Hamburg
*
* 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 sys_auto_init_saul
* @{
*
* @file
* @brief Auto initialization of ws281x RGB LED devices
*
* @author Kevin Weiss <[email protected]>
*
* @}
*/

#include "assert.h"
#include "log.h"
#include "saul_reg.h"

#include "ws281x.h"
#include "ws281x_params.h"

/**
* @brief Define the number of configured sensors
*/
#define WS281X_NUM ARRAY_SIZE(ws281x_params)

/**
* @brief Allocate memory for the device descriptors
*/
static ws281x_t ws281x_devs[WS281X_NUM];

/**
* @brief Memory for the SAUL registry entries
*/
static saul_reg_t saul_entries[WS281X_NUM];

/**
* @brief Define the number of saul info
*/
#define WS281X_INFO_NUM ARRAY_SIZE(ws281x_saul_info)

/**
* @brief Reference the driver struct
*/
extern const saul_driver_t ws281x_saul_driver;

void auto_init_ws281x(void)
{
assert(WS281X_NUM == WS281X_INFO_NUM);

for (unsigned i = 0; i < WS281X_NUM; i++) {
LOG_DEBUG("[auto_init_saul] initializing WS281x #%u: ", i);

if (ws281x_init(&ws281x_devs[i], &ws281x_params[i]) != 0) {
LOG_ERROR("ERROR\n");
continue;
}
LOG_DEBUG("SUCCESS\n");
saul_entries[i].dev = &(ws281x_devs[i]);
saul_entries[i].name = ws281x_saul_info[i].name;
saul_entries[i].driver = &ws281x_saul_driver;
saul_reg_add(&(saul_entries[i]));
Teufelchen1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
4 changes: 4 additions & 0 deletions drivers/saul/init_devs/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,8 @@ void saul_init_devs(void)
extern void auto_init_servo(void);
auto_init_servo();
}
if (IS_USED(MODULE_WS281X)) {
extern void auto_init_ws281x(void);
auto_init_ws281x();
}
}
11 changes: 11 additions & 0 deletions drivers/ws281x/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
SRC := ws281x.c # All other sources files provide ws281x_write as submodule
SUBMODULES := 1

# Since we have submodules we need to manually handle saul instead of
# including driver_with_saul.mk
MODULE ?= $(shell basename $(CURDIR))
SAUL_INTERFACE ?= $(MODULE)_saul.c

# only include <module>_saul.c if saul module is used
ifneq (,$(filter saul,$(USEMODULE)))
SRC += $(SAUL_INTERFACE)
endif

include $(RIOTBASE)/Makefile.base
3 changes: 3 additions & 0 deletions drivers/ws281x/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ endif
ifneq (,$(filter ws281x_timer_gpio_ll,$(USEMODULE)))
FEATURES_REQUIRED += periph_gpio_ll periph_timer periph_timer_poll
endif

# It would seem xtimer is always required as it is used in the header...
USEMODULE += xtimer
17 changes: 17 additions & 0 deletions drivers/ws281x/include/ws281x_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
#define WS281X_PARAMS_H

#include "board.h"
#include "saul_reg.h"

#include "ws281x.h"

#ifdef __cplusplus
Expand All @@ -31,7 +33,7 @@
* @{
*/
#ifndef WS281X_PARAM_PIN
#define WS281X_PARAM_PIN (GPIO_PIN(0, 0)) /**< GPIO pin connected to the data pin of the first LED */

Check warning on line 36 in drivers/ws281x/include/ws281x_params.h

View workflow job for this annotation

GitHub Actions / static-tests

line is longer than 100 characters
#endif
#ifndef WS281X_PARAM_NUMOF
#define WS281X_PARAM_NUMOF (8U) /**< Number of LEDs chained */
Expand Down Expand Up @@ -105,6 +107,21 @@
#define WS281X_TIMER_FREQ 16000000
#endif

/**
* @brief SAUL info
*/
#ifndef WS281X_SAUL_INFO
#define WS281X_SAUL_INFO { .name = "WS281X RGB LED" }
#endif

/**
* @brief Additional meta information to keep in the SAUL registry
*/
static const saul_reg_info_t ws281x_saul_info[] =
{
WS281X_SAUL_INFO
};

#ifdef __cplusplus
}
#endif
Expand Down
47 changes: 47 additions & 0 deletions drivers/ws281x/ws281x_saul.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright (C) 2024 HAW Hamburg
*
* 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 NEOPixel/ws281x adaption to SAUL
*
* @author Kevin Weiss <[email protected]>
*
* @}
*/

#include <string.h>
#include <stdio.h>

#include "saul.h"
#include "ws281x.h"

static int set_rgb_led(const void *dev, const phydat_t *res)
{
ws281x_t *ws281x = (ws281x_t *)dev;
color_rgb_t color = {
.r = res->val[0],
.g = res->val[1],
.b = res->val[2]
};
for (unsigned idx = 0; idx < ws281x->params.numof; idx++) {
puts("Setting LED");
ws281x_set(ws281x, idx, color);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just from a perspective of practicality: Would it make sense to set the idx as an additional vector component (or address each LED as their own SAUL device)? It seems to me very restrictive that only one color can be set for a whole LED strip using SAUL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think an extra vector component would be weird; strand-as-a-whole is at least consistent with the data types and write mode.

A per-LED mode would make sense, possibly also a group-of-LEDs mode – but maybe that can grow from this as a starting point?

Copy link
Member

@miri64 miri64 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[…] but maybe that can grow from this as a starting point?

Fine by me, just wanted to give a possible discussion starter.

Maybe we should even distinguish from a single RGB LED and a LED strip in SAUL type. In any way it should be clarified (in the documentation) and unify that at the moment SAUL_ACT_LED_RGB sets just everything in the SAUL device to one color. Currently, it seems to be used for PWM-based RGB LEDs (where it is used to set the color of a single LED associated to a single PWM device) and by the Grove LED bar, that currently just seems to set the red compontent.

static int write_rgb(const void *dev, const phydat_t *state)
{
const saul_pwm_rgb_params_t *p = dev;
int factor, shiftback, max;
int err = extract_scaling(state, &factor, &shiftback, &max);
if (err < 0) {
return err;
}
for (int i = 0; i < 3; ++i) {
if (state->val[i] < 0 || state->val[i] > max) {
return -ECANCELED;
}
}
for (int i = 0; i < 3; ++i) {
setchan(&p->channels[i], (state->val[i] * factor) >> shiftback);
}
return 3;
}

static int set_ledbar(const void *dev, const phydat_t *res)
{
uint8_t lvl = (uint8_t)res->val[0];
grove_ledbar_set((grove_ledbar_t *)dev, lvl);
return 1;
}

Copy link
Member

@chrysn chrysn Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And a good starter it is indeed.

I don't think the SAUL type is the right place, [edit, added:] if visible the distinction belongs in the descriptive text. Single LED, LED strip or group of LEDs are just what RIOT sees, and may not correspond to the physical reality. (For example, on a PWM port there could be a strip of non-addressable LEDs, or all the many LEDs of a WS281x may be hidden behind a larger diffusor).

It is weird that in the Grove bar it uses one component -- anything monochromatic should be SAUL_ACT_DIMMER (and read one channel), and anything RGBish should be SAUL_ACT_LED_RGB and read the 3 components. (And I do not yet know how to best work with RGBW or RGBA LEDs in SAUL, but one step at a time).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we say this is for a different PR? This issues is already documented in the PR description.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think yes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes!

}
ws281x_write(ws281x);
return 1;
benpicco marked this conversation as resolved.
Show resolved Hide resolved
}

const saul_driver_t ws281x_saul_driver = {
.read = saul_read_notsup,
.write = set_rgb_led,
.type = SAUL_ACT_LED_RGB,
};
2 changes: 1 addition & 1 deletion examples/rust-gcoap/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/rust-hello-world/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sys/rust_riotmodules_standalone/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/rust_minimal/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading