Zephyr Project API 4.2.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
17#ifndef ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
18#define ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
19
27#include <errno.h>
28#include <zephyr/types.h>
29#include <zephyr/device.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
41struct led_rgb {
42#ifdef CONFIG_LED_STRIP_RGB_SCRATCH
43 /*
44 * Pad/scratch space needed by some drivers. Users should
45 * ignore.
46 */
47 uint8_t scratch;
48#endif
55};
56
63typedef int (*led_api_update_rgb)(const struct device *dev,
64 struct led_rgb *pixels,
65 size_t num_pixels);
66
73typedef int (*led_api_update_channels)(const struct device *dev,
74 uint8_t *channels,
75 size_t num_channels);
76
83typedef size_t (*led_api_length)(const struct device *dev);
84
95
108static inline int led_strip_update_rgb(const struct device *dev,
109 struct led_rgb *pixels,
110 size_t num_pixels)
111{
112 const struct led_strip_driver_api *api =
113 (const struct led_strip_driver_api *)dev->api;
114
115 /* Allow for out-of-tree drivers that do not have this function for 2 Zephyr releases
116 * until making it mandatory, function added after Zephyr 3.6
117 */
118 if (api->length != NULL) {
119 /* Ensure supplied pixel size is valid for this device */
120 if (api->length(dev) < num_pixels) {
121 return -ERANGE;
122 }
123 }
124
125 return api->update_rgb(dev, pixels, num_pixels);
126}
127
143static inline int led_strip_update_channels(const struct device *dev,
144 uint8_t *channels,
145 size_t num_channels)
146{
147 const struct led_strip_driver_api *api =
148 (const struct led_strip_driver_api *)dev->api;
149
150 if (api->update_channels == NULL) {
151 return -ENOSYS;
152 }
153
154 return api->update_channels(dev, channels, num_channels);
155}
156
164static inline size_t led_strip_length(const struct device *dev)
165{
166 const struct led_strip_driver_api *api =
167 (const struct led_strip_driver_api *)dev->api;
168
169 return api->length(dev);
170}
171
172#ifdef __cplusplus
173}
174#endif
175
180#endif /* ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_ */
System error numbers.
int(* led_api_update_rgb)(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
Callback API for updating an RGB LED strip.
Definition led_strip.h:63
static int led_strip_update_rgb(const struct device *dev, struct led_rgb *pixels, size_t num_pixels)
Mandatory function to update an LED strip with the given RGB array.
Definition led_strip.h:108
static size_t led_strip_length(const struct device *dev)
Mandatory function to get chain length (in pixels) of an LED strip device.
Definition led_strip.h:164
static int led_strip_update_channels(const struct device *dev, uint8_t *channels, size_t num_channels)
Optional function to update an LED strip with the given channel array (each channel byte correspondin...
Definition led_strip.h:143
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:73
size_t(* led_api_length)(const struct device *dev)
Callback API for getting length of an LED strip.
Definition led_strip.h:83
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define ERANGE
Result too large.
Definition errno.h:72
#define NULL
Definition iar_missing_defs.h:20
Size of off_t must be equal or less than size of size_t
Definition retained_mem.h:29
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
Runtime device structure (in ROM) per driver instance.
Definition device.h:510
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:516
Color value for a single RGB LED.
Definition led_strip.h:41
uint8_t r
Red channel.
Definition led_strip.h:50
uint8_t g
Green channel.
Definition led_strip.h:52
uint8_t b
Blue channel.
Definition led_strip.h:54
LED strip driver API.
Definition led_strip.h:90
led_api_length length
Definition led_strip.h:93
led_api_update_channels update_channels
Definition led_strip.h:92
led_api_update_rgb update_rgb
Definition led_strip.h:91