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
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 = DEVICE_API_GET(led, dev);
144
145 if (api->blink == NULL) {
146 return -ENOSYS;
147 }
148 return api->blink(dev, led, delay_on, delay_off);
149}
150
161__syscall int led_get_info(const struct device *dev, uint32_t led,
162 const struct led_info **info);
163
164static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
165 const struct led_info **info)
166{
167 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
168
169 if (api->get_info == NULL) {
170 *info = NULL;
171 return -ENOSYS;
172 }
173 return api->get_info(dev, led, info);
174}
175
192__syscall int led_set_brightness(const struct device *dev, uint32_t led,
193 uint8_t value);
194
195static inline int z_impl_led_set_brightness(const struct device *dev,
196 uint32_t led,
197 uint8_t value)
198{
199 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
200
201 if (api->set_brightness == NULL) {
202 if (api->on == NULL || api->off == NULL) {
203 return -ENOSYS;
204 }
205 }
206
207 if (value > LED_BRIGHTNESS_MAX) {
208 return -EINVAL;
209 }
210
211 if (api->set_brightness == NULL) {
212 if (value) {
213 return api->on(dev, led);
214 } else {
215 return api->off(dev, led);
216 }
217 }
218
219 return api->set_brightness(dev, led, value);
220}
221
238__syscall int led_write_channels(const struct device *dev,
239 uint32_t start_channel,
240 uint32_t num_channels, const uint8_t *buf);
241
242static inline int
243z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
244 uint32_t num_channels, const uint8_t *buf)
245{
246 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
247
248 if (api->write_channels == NULL) {
249 return -ENOSYS;
250 }
251 return api->write_channels(dev, start_channel, num_channels, buf);
252}
253
266__syscall int led_set_channel(const struct device *dev,
267 uint32_t channel, uint8_t value);
268
269static inline int z_impl_led_set_channel(const struct device *dev,
270 uint32_t channel, uint8_t value)
271{
272 return z_impl_led_write_channels(dev, channel, 1, &value);
273}
274
291__syscall int led_set_color(const struct device *dev, uint32_t led,
292 uint8_t num_colors, const uint8_t *color);
293
294static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
295 uint8_t num_colors, const uint8_t *color)
296{
297 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
298
299 if (api->set_color == NULL) {
300 return -ENOSYS;
301 }
302 return api->set_color(dev, led, num_colors, color);
303}
304
317__syscall int led_on(const struct device *dev, uint32_t led);
318
319static inline int z_impl_led_on(const struct device *dev, uint32_t led)
320{
321 const struct led_driver_api *api = DEVICE_API_GET(led, dev);
322
323 if (api->set_brightness == NULL && api->on == NULL) {
324 return -ENOSYS;
325 }
326
327 if (api->on == NULL) {
328 return api->set_brightness(dev, led, LED_BRIGHTNESS_MAX);
329 }
330
331 return api->on(dev, led);
332}
333
346__syscall int led_off(const struct device *dev, uint32_t led);
347
348static inline int z_impl_led_off(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->off == NULL) {
353 return -ENOSYS;
354 }
355
356 if (api->off == NULL) {
357 return api->set_brightness(dev, led, 0);
358 }
359
360 return api->off(dev, led);
361}
362
363/*
364 * LED DT helpers.
365 */
366
377 const struct device *dev;
380};
381
391static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
392 uint8_t value)
393{
394 return led_set_brightness(spec->dev, spec->index, value);
395}
396
405static inline int led_on_dt(const struct led_dt_spec *spec)
406{
407 return led_on(spec->dev, spec->index);
408}
409
418static inline int led_off_dt(const struct led_dt_spec *spec)
419{
420 return led_off(spec->dev, spec->index);
421}
422
431static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
432{
433 return device_is_ready(spec->dev);
434}
435
472#define LED_DT_SPEC_GET(node_id) \
473 { \
474 .dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
475 .index = DT_NODE_CHILD_IDX(node_id), \
476 }
477
487#define LED_DT_SPEC_GET_OR(node_id, default_value) \
488 COND_CODE_1(DT_NODE_EXISTS(node_id), \
489 (LED_DT_SPEC_GET(node_id)), \
490 (default_value))
491
495
496#ifdef __cplusplus
497}
498#endif
499
500#include <zephyr/syscalls/led.h>
501
502#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:1425
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:418
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:431
static int led_on_dt(const struct led_dt_spec *spec)
Turn on an LED from a struct led_dt_spec.
Definition led.h:405
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:391
#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: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:375
uint32_t index
Index of the LED on the controller.
Definition led.h:379
const struct device * dev
LED device instance.
Definition led.h:377
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