Zephyr Project API 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
59
65typedef int (*led_api_blink)(const struct device *dev, uint32_t led,
66 uint32_t delay_on, uint32_t delay_off);
67
73typedef int (*led_api_get_info)(const struct device *dev, uint32_t led,
74 const struct led_info **info);
75
81typedef int (*led_api_set_brightness)(const struct device *dev, uint32_t led,
82 uint8_t value);
88typedef int (*led_api_set_color)(const struct device *dev, uint32_t led,
89 uint8_t num_colors, const uint8_t *color);
90
96typedef int (*led_api_on)(const struct device *dev, uint32_t led);
97
103typedef int (*led_api_off)(const struct device *dev, uint32_t led);
104
110typedef int (*led_api_write_channels)(const struct device *dev,
111 uint32_t start_channel,
112 uint32_t num_channels,
113 const uint8_t *buf);
114
151
153
166__syscall int led_blink(const struct device *dev, uint32_t led,
167 uint32_t delay_on, uint32_t delay_off);
168
169static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
170 uint32_t delay_on, uint32_t delay_off)
171{
172 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
173
174 if (api->blink == NULL) {
175 return -ENOSYS;
176 }
177 return api->blink(dev, led, delay_on, delay_off);
178}
179
190__syscall int led_get_info(const struct device *dev, uint32_t led,
191 const struct led_info **info);
192
193static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
194 const struct led_info **info)
195{
196 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
197
198 if (api->get_info == NULL) {
199 *info = NULL;
200 return -ENOSYS;
201 }
202 return api->get_info(dev, led, info);
203}
204
221__syscall int led_set_brightness(const struct device *dev, uint32_t led,
222 uint8_t value);
223
224static inline int z_impl_led_set_brightness(const struct device *dev,
225 uint32_t led,
226 uint8_t value)
227{
228 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
229
230 if (api->set_brightness == NULL) {
231 if (api->on == NULL || api->off == NULL) {
232 return -ENOSYS;
233 }
234 }
235
236 if (value > LED_BRIGHTNESS_MAX) {
237 return -EINVAL;
238 }
239
240 if (api->set_brightness == NULL) {
241 if (value) {
242 return api->on(dev, led);
243 } else {
244 return api->off(dev, led);
245 }
246 }
247
248 return api->set_brightness(dev, led, value);
249}
250
267__syscall int led_write_channels(const struct device *dev,
268 uint32_t start_channel,
269 uint32_t num_channels, const uint8_t *buf);
270
271static inline int
272z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
273 uint32_t num_channels, const uint8_t *buf)
274{
275 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
276
277 if (api->write_channels == NULL) {
278 return -ENOSYS;
279 }
280 return api->write_channels(dev, start_channel, num_channels, buf);
281}
282
295__syscall int led_set_channel(const struct device *dev,
296 uint32_t channel, uint8_t value);
297
298static inline int z_impl_led_set_channel(const struct device *dev,
299 uint32_t channel, uint8_t value)
300{
301 return z_impl_led_write_channels(dev, channel, 1, &value);
302}
303
320__syscall int led_set_color(const struct device *dev, uint32_t led,
321 uint8_t num_colors, const uint8_t *color);
322
323static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
324 uint8_t num_colors, const uint8_t *color)
325{
326 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
327
328 if (api->set_color == NULL) {
329 return -ENOSYS;
330 }
331 return api->set_color(dev, led, num_colors, color);
332}
333
346__syscall int led_on(const struct device *dev, uint32_t led);
347
348static inline int z_impl_led_on(const struct device *dev, uint32_t led)
349{
350 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
351
352 if (api->set_brightness == NULL && api->on == NULL) {
353 return -ENOSYS;
354 }
355
356 if (api->on == NULL) {
357 return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
358 }
359
360 return api->on(dev, led);
361}
362
375__syscall int led_off(const struct device *dev, uint32_t led);
376
377static inline int z_impl_led_off(const struct device *dev, uint32_t led)
378{
379 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
380
381 if (api->set_brightness == NULL && api->off == NULL) {
382 return -ENOSYS;
383 }
384
385 if (api->off == NULL) {
386 return api->set_brightness(dev, led, 0);
387 }
388
389 return api->off(dev, led);
390}
391
392/*
393 * LED DT helpers.
394 */
395
406 const struct device *dev;
409};
410
421static inline int led_blink_dt(const struct led_dt_spec *spec, uint32_t delay_on,
422 uint32_t delay_off)
423{
424 return led_blink(spec->dev, spec->index, delay_on, delay_off);
425}
426
436static inline int led_get_info_dt(const struct led_dt_spec *spec, const struct led_info **info)
437{
438 return led_get_info(spec->dev, spec->index, info);
439}
440
450static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
451 uint8_t value)
452{
453 return led_set_brightness(spec->dev, spec->index, value);
454}
455
468static inline int led_set_color_dt(const struct led_dt_spec *spec, uint8_t num_colors,
469 const uint8_t *color)
470{
471 return led_set_color(spec->dev, spec->index, num_colors, color);
472}
473
482static inline int led_on_dt(const struct led_dt_spec *spec)
483{
484 return led_on(spec->dev, spec->index);
485}
486
495static inline int led_off_dt(const struct led_dt_spec *spec)
496{
497 return led_off(spec->dev, spec->index);
498}
499
508static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
509{
510 return device_is_ready(spec->dev);
511}
512
549#define LED_DT_SPEC_GET(node_id) \
550 { \
551 .dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
552 .index = DT_NODE_CHILD_IDX(node_id), \
553 }
554
564#define LED_DT_SPEC_GET_OR(node_id, default_value) \
565 COND_CODE_1(DT_NODE_EXISTS(node_id), \
566 (LED_DT_SPEC_GET(node_id)), \
567 (default_value))
568
572
573#ifdef __cplusplus
574}
575#endif
576
577#include <zephyr/syscalls/led.h>
578
579#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:1449
System error numbers.
bool device_is_ready(const struct device *dev)
Verify that a device is ready for use.
static int led_get_info_dt(const struct led_dt_spec *spec, const struct led_info **info)
Get LED information from a led_dt_spec.
Definition led.h:436
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:73
static int led_set_color_dt(const struct led_dt_spec *spec, uint8_t num_colors, const uint8_t *color)
Set LED color from a struct led_dt_spec.
Definition led.h:468
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:103
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:110
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:495
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:88
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:508
static int led_on_dt(const struct led_dt_spec *spec)
Turn on an LED from a struct led_dt_spec.
Definition led.h:482
static int led_blink_dt(const struct led_dt_spec *spec, uint32_t delay_on, uint32_t delay_off)
Blink an LED from a led_dt_spec.
Definition led.h:421
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)
@def_driverbackendgroup{LED,led_interface}
Definition led.h:65
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:81
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:450
#define EINVAL
Invalid argument.
Definition errno.h:61
#define ENOSYS
Function not implemented.
Definition errno.h:83
#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
@driver_ops{LED}
Definition led.h:122
led_api_off off
@driver_ops_optional Turn off an LED.
Definition led.h:134
led_api_set_color set_color
@driver_ops_optional Set LED color.
Definition led.h:147
led_api_get_info get_info
@driver_ops_optional Get LED information.
Definition led.h:145
led_api_set_brightness set_brightness
@driver_ops_optional Set LED brightness.
Definition led.h:141
led_api_on on
@driver_ops_optional Turn on an LED.
Definition led.h:128
led_api_write_channels write_channels
@driver_ops_optional Write/update a strip of LED channels.
Definition led.h:149
led_api_blink blink
@driver_ops_optional Blink an LED.
Definition led.h:143
Container for an LED information specified in devicetree.
Definition led.h:404
uint32_t index
Index of the LED on the controller.
Definition led.h:408
const struct device * dev
LED device instance.
Definition led.h:406
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