Zephyr API Documentation 4.2.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
led.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Linaro Limited
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
12
13#ifndef ZEPHYR_INCLUDE_DRIVERS_LED_H_
14#define ZEPHYR_INCLUDE_DRIVERS_LED_H_
15
24
25#include <errno.h>
26
27#include <zephyr/types.h>
28#include <zephyr/device.h>
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
37#define LED_BRIGHTNESS_MAX 100u
38
54
61typedef int (*led_api_blink)(const struct device *dev, uint32_t led,
62 uint32_t delay_on, uint32_t delay_off);
63
70typedef int (*led_api_get_info)(const struct device *dev, uint32_t led,
71 const struct led_info **info);
72
79typedef int (*led_api_set_brightness)(const struct device *dev, uint32_t led,
80 uint8_t value);
87typedef int (*led_api_set_color)(const struct device *dev, uint32_t led,
88 uint8_t num_colors, const uint8_t *color);
89
96typedef int (*led_api_on)(const struct device *dev, uint32_t led);
97
104typedef int (*led_api_off)(const struct device *dev, uint32_t led);
105
112typedef int (*led_api_write_channels)(const struct device *dev,
113 uint32_t start_channel,
114 uint32_t num_channels,
115 const uint8_t *buf);
116
120__subsystem struct led_driver_api {
121 /* Mandatory callbacks, either on/off or set_brightness. */
125 /* Optional callbacks. */
130};
131
144__syscall int led_blink(const struct device *dev, uint32_t led,
145 uint32_t delay_on, uint32_t delay_off);
146
147static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
148 uint32_t delay_on, uint32_t delay_off)
149{
150 const struct led_driver_api *api =
151 (const struct led_driver_api *)dev->api;
152
153 if (api->blink == NULL) {
154 return -ENOSYS;
155 }
156 return api->blink(dev, led, delay_on, delay_off);
157}
158
169__syscall int led_get_info(const struct device *dev, uint32_t led,
170 const struct led_info **info);
171
172static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
173 const struct led_info **info)
174{
175 const struct led_driver_api *api =
176 (const struct led_driver_api *)dev->api;
177
178 if (api->get_info == NULL) {
179 *info = NULL;
180 return -ENOSYS;
181 }
182 return api->get_info(dev, led, info);
183}
184
201__syscall int led_set_brightness(const struct device *dev, uint32_t led,
202 uint8_t value);
203
204static inline int z_impl_led_set_brightness(const struct device *dev,
205 uint32_t led,
206 uint8_t value)
207{
208 const struct led_driver_api *api =
209 (const struct led_driver_api *)dev->api;
210
211 if (api->set_brightness == NULL) {
212 if (api->on == NULL || api->off == NULL) {
213 return -ENOSYS;
214 }
215 }
216
217 if (value > LED_BRIGHTNESS_MAX) {
218 return -EINVAL;
219 }
220
221 if (api->set_brightness == NULL) {
222 if (value) {
223 return api->on(dev, led);
224 } else {
225 return api->off(dev, led);
226 }
227 }
228
229 return api->set_brightness(dev, led, value);
230}
231
248__syscall int led_write_channels(const struct device *dev,
249 uint32_t start_channel,
250 uint32_t num_channels, const uint8_t *buf);
251
252static inline int
253z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
254 uint32_t num_channels, const uint8_t *buf)
255{
256 const struct led_driver_api *api =
257 (const struct led_driver_api *)dev->api;
258
259 if (api->write_channels == NULL) {
260 return -ENOSYS;
261 }
262 return api->write_channels(dev, start_channel, num_channels, buf);
263}
264
277__syscall int led_set_channel(const struct device *dev,
278 uint32_t channel, uint8_t value);
279
280static inline int z_impl_led_set_channel(const struct device *dev,
281 uint32_t channel, uint8_t value)
282{
283 return z_impl_led_write_channels(dev, channel, 1, &value);
284}
285
302__syscall int led_set_color(const struct device *dev, uint32_t led,
303 uint8_t num_colors, const uint8_t *color);
304
305static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
306 uint8_t num_colors, const uint8_t *color)
307{
308 const struct led_driver_api *api =
309 (const struct led_driver_api *)dev->api;
310
311 if (api->set_color == NULL) {
312 return -ENOSYS;
313 }
314 return api->set_color(dev, led, num_colors, color);
315}
316
329__syscall int led_on(const struct device *dev, uint32_t led);
330
331static inline int z_impl_led_on(const struct device *dev, uint32_t led)
332{
333 const struct led_driver_api *api =
334 (const struct led_driver_api *)dev->api;
335
336 if (api->set_brightness == NULL && api->on == NULL) {
337 return -ENOSYS;
338 }
339
340 if (api->on == NULL) {
341 return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
342 }
343
344 return api->on(dev, led);
345}
346
359__syscall int led_off(const struct device *dev, uint32_t led);
360
361static inline int z_impl_led_off(const struct device *dev, uint32_t led)
362{
363 const struct led_driver_api *api =
364 (const struct led_driver_api *)dev->api;
365
366 if (api->set_brightness == NULL && api->off == NULL) {
367 return -ENOSYS;
368 }
369
370 if (api->off == NULL) {
371 return api->set_brightness(dev, led, 0);
372 }
373
374 return api->off(dev, led);
375}
376
377/*
378 * LED DT helpers.
379 */
380
391 const struct device *dev;
394};
395
405static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
406 uint8_t value)
407{
408 return led_set_brightness(spec->dev, spec->index, value);
409}
410
419static inline int led_on_dt(const struct led_dt_spec *spec)
420{
421 return led_on(spec->dev, spec->index);
422}
423
432static inline int led_off_dt(const struct led_dt_spec *spec)
433{
434 return led_off(spec->dev, spec->index);
435}
436
445static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
446{
447 return device_is_ready(spec->dev);
448}
449
486#define LED_DT_SPEC_GET(node_id) \
487 { \
488 .dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
489 .index = DT_NODE_CHILD_IDX(node_id), \
490 }
491
501#define LED_DT_SPEC_GET_OR(node_id, default_value) \
502 COND_CODE_1(DT_NODE_EXISTS(node_id), \
503 (LED_DT_SPEC_GET(node_id)), \
504 (default_value))
505
509
510#ifdef __cplusplus
511}
512#endif
513
514#include <zephyr/syscalls/led.h>
515
516#endif /* ZEPHYR_INCLUDE_DRIVERS_LED_H_ */
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
int led_off(const struct device *dev, uint32_t led)
Turn off an LED.
int led_write_channels(const struct device *dev, uint32_t start_channel, uint32_t num_channels, const uint8_t *buf)
Write/update a strip of LED channels.
int(* led_api_get_info)(const struct device *dev, uint32_t led, const struct led_info **info)
Optional API callback to get LED information.
Definition led.h:70
int led_blink(const struct device *dev, uint32_t led, uint32_t delay_on, uint32_t delay_off)
Blink an LED.
int(* led_api_off)(const struct device *dev, uint32_t led)
Callback API for turning off an LED.
Definition led.h:104
int(* led_api_write_channels)(const struct device *dev, uint32_t start_channel, uint32_t num_channels, const uint8_t *buf)
Callback API for writing a strip of LED channels.
Definition led.h:112
int led_set_channel(const struct device *dev, uint32_t channel, uint8_t value)
Set a single LED channel.
#define LED_BRIGHTNESS_MAX
Maximum brightness level, range is 0 to 100.
Definition led.h:37
static int led_off_dt(const struct led_dt_spec *spec)
Turn off an LED from a struct led_dt_spec.
Definition led.h:432
int led_set_color(const struct device *dev, uint32_t led, uint8_t num_colors, const uint8_t *color)
Set LED color.
int(* led_api_set_color)(const struct device *dev, uint32_t led, uint8_t num_colors, const uint8_t *color)
Optional API callback to set the colors of a LED.
Definition led.h:87
int led_get_info(const struct device *dev, uint32_t led, const struct led_info **info)
Get LED information.
static bool led_is_ready_dt(const struct led_dt_spec *spec)
Validate that the LED device is ready.
Definition led.h:445
static int led_on_dt(const struct led_dt_spec *spec)
Turn on an LED from a struct led_dt_spec.
Definition led.h:419
int led_set_brightness(const struct device *dev, uint32_t led, uint8_t value)
Set LED brightness.
int(* led_api_on)(const struct device *dev, uint32_t led)
Callback API for turning on an LED.
Definition led.h:96
int(* led_api_blink)(const struct device *dev, uint32_t led, uint32_t delay_on, uint32_t delay_off)
Callback API for blinking an LED.
Definition led.h:61
int led_on(const struct device *dev, uint32_t led)
Turn on an LED.
int(* led_api_set_brightness)(const struct device *dev, uint32_t led, uint8_t value)
Callback API for setting brightness of an LED.
Definition led.h:79
static int led_set_brightness_dt(const struct led_dt_spec *spec, uint8_t value)
Set LED brightness from a led_dt_spec.
Definition led.h:405
#define EINVAL
Invalid argument.
Definition errno.h:60
#define ENOSYS
Function not implemented.
Definition errno.h:82
#define NULL
Definition iar_missing_defs.h:20
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__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
LED driver API.
Definition led.h:120
led_api_off off
Definition led.h:123
led_api_set_color set_color
Definition led.h:128
led_api_get_info get_info
Definition led.h:127
led_api_set_brightness set_brightness
Definition led.h:124
led_api_on on
Definition led.h:122
led_api_write_channels write_channels
Definition led.h:129
led_api_blink blink
Definition led.h:126
Container for an LED information specified in devicetree.
Definition led.h:389
uint32_t index
Index of the LED on the controller.
Definition led.h:393
const struct device * dev
LED device instance.
Definition led.h:391
LED information structure.
Definition led.h:44
const char * label
LED label.
Definition led.h:46
uint32_t index
Index of the LED on the controller.
Definition led.h:48
const uint8_t * color_mapping
Mapping of the LED colors.
Definition led.h:52
uint8_t num_colors
Number of colors per LED.
Definition led.h:50