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