Zephyr Project API 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
led_strip.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2017 Linaro Limited
3 * Copyright (c) 2024 Jamie McCrae
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
16
17#ifndef ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
18#define ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
19
33
34#include <errno.h>
35#include <zephyr/types.h>
36#include <zephyr/device.h>
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
48struct led_rgb {
49#ifdef CONFIG_LED_STRIP_RGB_SCRATCH
50 /*
51 * Pad/scratch space needed by some drivers. Users should
52 * ignore.
53 */
54 uint8_t scratch;
55#endif
62};
63
68
74typedef int (*led_api_update_rgb)(const struct device *dev,
75 struct led_rgb *pixels,
76 size_t num_pixels);
77
83typedef int (*led_api_update_channels)(const struct device *dev,
84 uint8_t *channels,
85 size_t num_channels);
86
92typedef size_t (*led_api_length)(const struct device *dev);
93
111
114
127static inline int led_strip_update_rgb(const struct device *dev,
128 struct led_rgb *pixels,
129 size_t num_pixels)
130{
131 const struct led_strip_driver_api *api = DEVICE_API_GET(led_strip, dev);
132
133 /* Allow for out-of-tree drivers that do not have this function for 2 Zephyr releases
134 * until making it mandatory, function added after Zephyr 3.6
135 */
136 if (api->length != NULL) {
137 /* Ensure supplied pixel size is valid for this device */
138 if (api->length(dev) < num_pixels) {
139 return -ERANGE;
140 }
141 }
142
143 return api->update_rgb(dev, pixels, num_pixels);
144}
145
161static inline int led_strip_update_channels(const struct device *dev,
162 uint8_t *channels,
163 size_t num_channels)
164{
165 const struct led_strip_driver_api *api = DEVICE_API_GET(led_strip, dev);
166
167 if (api->update_channels == NULL) {
168 return -ENOSYS;
169 }
170
171 return api->update_channels(dev, channels, num_channels);
172}
173
181static inline size_t led_strip_length(const struct device *dev)
182{
183 return DEVICE_API_GET(led_strip, dev)->length(dev);
184}
185
186#ifdef __cplusplus
187}
188#endif
189
193
194#endif /* ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1449
System error numbers.
size_t(* led_api_length)(const struct device *dev)
Callback API for getting length of an LED strip.
Definition led_strip.h:92
static int led_strip_update_rgb(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
Update an LED strip with the given RGB array.
Definition led_strip.h:127
static size_t led_strip_length(const struct device *dev)
Get chain length (in pixels) of an LED strip device.
Definition led_strip.h:181
static int led_strip_update_channels(const struct device *dev, uint8_t *channels, size_t num_channels)
Update an LED strip with the given channel array.
Definition led_strip.h:161
int(* led_api_update_rgb)(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
@def_driverbackendgroup{LED Strip,led_strip_interface}
Definition led_strip.h:74
int(* led_api_update_channels)(const struct device *dev, uint8_t *channels, size_t num_channels)
Callback API for updating channels without an RGB interpretation.
Definition led_strip.h:83
#define ENOSYS
Function not implemented.
Definition errno.h:83
#define ERANGE
Result too large.
Definition errno.h:73
#define NULL
Definition iar_missing_defs.h:20
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:513
Color value for a single RGB LED.
Definition led_strip.h:48
uint8_t r
Red channel.
Definition led_strip.h:57
uint8_t g
Green channel.
Definition led_strip.h:59
uint8_t b
Blue channel.
Definition led_strip.h:61
@driver_ops{LED Strip}
Definition led_strip.h:97
led_api_length length
@driver_ops_mandatory Get chain length (in pixels) of an LED strip device.
Definition led_strip.h:105
led_api_update_channels update_channels
@driver_ops_optional Update an LED strip with the given channel array.
Definition led_strip.h:109
led_api_update_rgb update_rgb
@driver_ops_mandatory Update an LED strip with the given RGB array.
Definition led_strip.h:101