Zephyr API Documentation 4.4.0
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
60typedef int (*led_api_blink)(const struct device *dev, uint32_t led,
61 uint32_t delay_on, uint32_t delay_off);
62
68typedef int (*led_api_get_info)(const struct device *dev, uint32_t led,
69 const struct led_info **info);
70
76typedef int (*led_api_set_brightness)(const struct device *dev, uint32_t led,
77 uint8_t value);
83typedef int (*led_api_set_color)(const struct device *dev, uint32_t led,
84 uint8_t num_colors, const uint8_t *color);
85
91typedef int (*led_api_on)(const struct device *dev, uint32_t led);
92
98typedef int (*led_api_off)(const struct device *dev, uint32_t led);
99
105typedef int (*led_api_write_channels)(const struct device *dev,
106 uint32_t start_channel,
107 uint32_t num_channels,
108 const uint8_t *buf);
109
113__subsystem struct led_driver_api {
114 /* Mandatory callbacks, either on/off or set_brightness. */
118 /* Optional callbacks. */
123};
124
137__syscall int led_blink(const struct device *dev, uint32_t led,
138 uint32_t delay_on, uint32_t delay_off);
139
140static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
141 uint32_t delay_on, uint32_t delay_off)
142{
143 const struct led_driver_api *api =
144 (const struct led_driver_api *)dev->api;
145
146 if (api->blink == NULL) {
147 return -ENOSYS;
148 }
149 return api->blink(dev, led, delay_on, delay_off);
150}
151
162__syscall int led_get_info(const struct device *dev, uint32_t led,
163 const struct led_info **info);
164
165static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
166 const struct led_info **info)
167{
168 const struct led_driver_api *api =
169 (const struct led_driver_api *)dev->api;
170
171 if (api->get_info == NULL) {
172 *info = NULL;
173 return -ENOSYS;
174 }
175 return api->get_info(dev, led, info);
176}
177
194__syscall int led_set_brightness(const struct device *dev, uint32_t led,
195 uint8_t value);
196
197static inline int z_impl_led_set_brightness(const struct device *dev,
198 uint32_t led,
199 uint8_t value)
200{
201 const struct led_driver_api *api =
202 (const struct led_driver_api *)dev->api;
203
204 if (api->set_brightness == NULL) {
205 if (api->on == NULL || api->off == NULL) {
206 return -ENOSYS;
207 }
208 }
209
210 if (value > LED_BRIGHTNESS_MAX) {
211 return -EINVAL;
212 }
213
214 if (api->set_brightness == NULL) {
215 if (value) {
216 return api->on(dev, led);
217 } else {
218 return api->off(dev, led);
219 }
220 }
221
222 return api->set_brightness(dev, led, value);
223}
224
241__syscall int led_write_channels(const struct device *dev,
242 uint32_t start_channel,
243 uint32_t num_channels, const uint8_t *buf);
244
245static inline int
246z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
247 uint32_t num_channels, const uint8_t *buf)
248{
249 const struct led_driver_api *api =
250 (const struct led_driver_api *)dev->api;
251
252 if (api->write_channels == NULL) {
253 return -ENOSYS;
254 }
255 return api->write_channels(dev, start_channel, num_channels, buf);
256}
257
270__syscall int led_set_channel(const struct device *dev,
271 uint32_t channel, uint8_t value);
272
273static inline int z_impl_led_set_channel(const struct device *dev,
274 uint32_t channel, uint8_t value)
275{
276 return z_impl_led_write_channels(dev, channel, 1, &value);
277}
278
295__syscall int led_set_color(const struct device *dev, uint32_t led,
296 uint8_t num_colors, const uint8_t *color);
297
298static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
299 uint8_t num_colors, const uint8_t *color)
300{
301 const struct led_driver_api *api =
302 (const struct led_driver_api *)dev->api;
303
304 if (api->set_color == NULL) {
305 return -ENOSYS;
306 }
307 return api->set_color(dev, led, num_colors, color);
308}
309
322__syscall int led_on(const struct device *dev, uint32_t led);
323
324static inline int z_impl_led_on(const struct device *dev, uint32_t led)
325{
326 const struct led_driver_api *api =
327 (const struct led_driver_api *)dev->api;
328
329 if (api->set_brightness == NULL && api->on == NULL) {
330 return -ENOSYS;
331 }
332
333 if (api->on == NULL) {
334 return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
335 }
336
337 return api->on(dev, led);
338}
339
352__syscall int led_off(const struct device *dev, uint32_t led);
353
354static inline int z_impl_led_off(const struct device *dev, uint32_t led)
355{
356 const struct led_driver_api *api =
357 (const struct led_driver_api *)dev->api;
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_ */
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:68
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:98
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:105
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:83
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:91
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:60
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:76
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
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:519
LED driver API.
Definition led.h:113
led_api_off off
Definition led.h:116
led_api_set_color set_color
Definition led.h:121
led_api_get_info get_info
Definition led.h:120
led_api_set_brightness set_brightness
Definition led.h:117
led_api_on on
Definition led.h:115
led_api_write_channels write_channels
Definition led.h:122
led_api_blink blink
Definition led.h:119
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