Zephyr API Documentation 4.4.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 = DEVICE_API_GET(led, dev);
151
152 if (api->blink == NULL) {
153 return -ENOSYS;
154 }
155 return api->blink(dev, led, delay_on, delay_off);
156}
157
168__syscall int led_get_info(const struct device *dev, uint32_t led,
169 const struct led_info **info);
170
171static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
172 const struct led_info **info)
173{
174 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
175
176 if (api->get_info == NULL) {
177 *info = NULL;
178 return -ENOSYS;
179 }
180 return api->get_info(dev, led, info);
181}
182
199__syscall int led_set_brightness(const struct device *dev, uint32_t led,
200 uint8_t value);
201
202static inline int z_impl_led_set_brightness(const struct device *dev,
203 uint32_t led,
204 uint8_t value)
205{
206 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
207
208 if (api->set_brightness == NULL) {
209 if (api->on == NULL || api->off == NULL) {
210 return -ENOSYS;
211 }
212 }
213
214 if (value > LED_BRIGHTNESS_MAX) {
215 return -EINVAL;
216 }
217
218 if (api->set_brightness == NULL) {
219 if (value) {
220 return api->on(dev, led);
221 } else {
222 return api->off(dev, led);
223 }
224 }
225
226 return api->set_brightness(dev, led, value);
227}
228
245__syscall int led_write_channels(const struct device *dev,
246 uint32_t start_channel,
247 uint32_t num_channels, const uint8_t *buf);
248
249static inline int
250z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
251 uint32_t num_channels, const uint8_t *buf)
252{
253 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
254
255 if (api->write_channels == NULL) {
256 return -ENOSYS;
257 }
258 return api->write_channels(dev, start_channel, num_channels, buf);
259}
260
273__syscall int led_set_channel(const struct device *dev,
274 uint32_t channel, uint8_t value);
275
276static inline int z_impl_led_set_channel(const struct device *dev,
277 uint32_t channel, uint8_t value)
278{
279 return z_impl_led_write_channels(dev, channel, 1, &value);
280}
281
298__syscall int led_set_color(const struct device *dev, uint32_t led,
299 uint8_t num_colors, const uint8_t *color);
300
301static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
302 uint8_t num_colors, const uint8_t *color)
303{
304 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
305
306 if (api->set_color == NULL) {
307 return -ENOSYS;
308 }
309 return api->set_color(dev, led, num_colors, color);
310}
311
324__syscall int led_on(const struct device *dev, uint32_t led);
325
326static inline int z_impl_led_on(const struct device *dev, uint32_t led)
327{
328 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
329
330 if (api->set_brightness == NULL && api->on == NULL) {
331 return -ENOSYS;
332 }
333
334 if (api->on == NULL) {
335 return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
336 }
337
338 return api->on(dev, led);
339}
340
353__syscall int led_off(const struct device *dev, uint32_t led);
354
355static inline int z_impl_led_off(const struct device *dev, uint32_t led)
356{
357 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
358
359 if (api->set_brightness == NULL && api->off == NULL) {
360 return -ENOSYS;
361 }
362
363 if (api->off == NULL) {
364 return api->set_brightness(dev, led, 0);
365 }
366
367 return api->off(dev, led);
368}
369
370/*
371 * LED DT helpers.
372 */
373
384 const struct device *dev;
387};
388
398static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
399 uint8_t value)
400{
401 return led_set_brightness(spec->dev, spec->index, value);
402}
403
412static inline int led_on_dt(const struct led_dt_spec *spec)
413{
414 return led_on(spec->dev, spec->index);
415}
416
425static inline int led_off_dt(const struct led_dt_spec *spec)
426{
427 return led_off(spec->dev, spec->index);
428}
429
438static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
439{
440 return device_is_ready(spec->dev);
441}
442
479#define LED_DT_SPEC_GET(node_id) \
480 { \
481 .dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
482 .index = DT_NODE_CHILD_IDX(node_id), \
483 }
484
494#define LED_DT_SPEC_GET_OR(node_id, default_value) \
495 COND_CODE_1(DT_NODE_EXISTS(node_id), \
496 (LED_DT_SPEC_GET(node_id)), \
497 (default_value))
498
502
503#ifdef __cplusplus
504}
505#endif
506
507#include <zephyr/syscalls/led.h>
508
509#endif /* ZEPHYR_INCLUDE_DRIVERS_LED_H_ */
#define DEVICE_API_GET(_class, _dev)
Expands to the pointer of a device's API for a given class.
Definition device.h:1375
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:425
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:438
static int led_on_dt(const struct led_dt_spec *spec)
Turn on an LED from a struct led_dt_spec.
Definition led.h:412
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:398
#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:513
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:382
uint32_t index
Index of the LED on the controller.
Definition led.h:386
const struct device * dev
LED device instance.
Definition led.h:384
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