1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

drivers/ws281x: Add saul support

This commit is contained in:
MrKevinWeiss 2024-04-09 15:38:52 +02:00
parent 5a4d55a66a
commit 0a96e36fb5
No known key found for this signature in database
GPG Key ID: C26684F1C0767FFF
6 changed files with 153 additions and 0 deletions

View File

@ -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 <kevin.weiss@haw-hamburg.de>
*
* @}
*/
#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]));
}
}

View File

@ -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();
}
}

View File

@ -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

View File

@ -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

View File

@ -20,6 +20,8 @@
#define WS281X_PARAMS_H
#include "board.h"
#include "saul_reg.h"
#include "ws281x.h"
#ifdef __cplusplus
@ -105,6 +107,21 @@ static const ws281x_params_t ws281x_params[] =
#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

View File

@ -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 <kevin.weiss@haw-hamburg.de>
*
* @}
*/
#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);
}
ws281x_write(ws281x);
return 1;
}
const saul_driver_t ws281x_saul_driver = {
.read = saul_read_notsup,
.write = set_rgb_led,
.type = SAUL_ACT_LED_RGB,
};