Zephyr API Documentation 4.0.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#ifndef ZEPHYR_INCLUDE_DRIVERS_LED_H_
13#define ZEPHYR_INCLUDE_DRIVERS_LED_H_
14
24#include <errno.h>
25
26#include <zephyr/types.h>
27#include <zephyr/device.h>
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
48
55typedef int (*led_api_blink)(const struct device *dev, uint32_t led,
56 uint32_t delay_on, uint32_t delay_off);
57
64typedef int (*led_api_get_info)(const struct device *dev, uint32_t led,
65 const struct led_info **info);
66
73typedef int (*led_api_set_brightness)(const struct device *dev, uint32_t led,
74 uint8_t value);
81typedef int (*led_api_set_color)(const struct device *dev, uint32_t led,
82 uint8_t num_colors, const uint8_t *color);
83
90typedef int (*led_api_on)(const struct device *dev, uint32_t led);
91
98typedef int (*led_api_off)(const struct device *dev, uint32_t led);
99
106typedef int (*led_api_write_channels)(const struct device *dev,
107 uint32_t start_channel,
108 uint32_t num_channels,
109 const uint8_t *buf);
110
125
138__syscall int led_blink(const struct device *dev, uint32_t led,
139 uint32_t delay_on, uint32_t delay_off);
140
141static inline int z_impl_led_blink(const struct device *dev, uint32_t led,
142 uint32_t delay_on, uint32_t delay_off)
143{
144 const struct led_driver_api *api =
145 (const struct led_driver_api *)dev->api;
146
147 if (api->blink == NULL) {
148 return -ENOSYS;
149 }
150 return api->blink(dev, led, delay_on, delay_off);
151}
152
163__syscall int led_get_info(const struct device *dev, uint32_t led,
164 const struct led_info **info);
165
166static inline int z_impl_led_get_info(const struct device *dev, uint32_t led,
167 const struct led_info **info)
168{
169 const struct led_driver_api *api =
170 (const struct led_driver_api *)dev->api;
171
172 if (api->get_info == NULL) {
173 *info = NULL;
174 return -ENOSYS;
175 }
176 return api->get_info(dev, led, info);
177}
178
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 return -ENOSYS;
206 }
207 return api->set_brightness(dev, led, value);
208}
209
226__syscall int led_write_channels(const struct device *dev,
227 uint32_t start_channel,
228 uint32_t num_channels, const uint8_t *buf);
229
230static inline int
231z_impl_led_write_channels(const struct device *dev, uint32_t start_channel,
232 uint32_t num_channels, const uint8_t *buf)
233{
234 const struct led_driver_api *api =
235 (const struct led_driver_api *)dev->api;
236
237 if (api->write_channels == NULL) {
238 return -ENOSYS;
239 }
240 return api->write_channels(dev, start_channel, num_channels, buf);
241}
242
255__syscall int led_set_channel(const struct device *dev,
256 uint32_t channel, uint8_t value);
257
258static inline int z_impl_led_set_channel(const struct device *dev,
259 uint32_t channel, uint8_t value)
260{
261 return z_impl_led_write_channels(dev, channel, 1, &value);
262}
263
280__syscall int led_set_color(const struct device *dev, uint32_t led,
281 uint8_t num_colors, const uint8_t *color);
282
283static inline int z_impl_led_set_color(const struct device *dev, uint32_t led,
284 uint8_t num_colors, const uint8_t *color)
285{
286 const struct led_driver_api *api =
287 (const struct led_driver_api *)dev->api;
288
289 if (api->set_color == NULL) {
290 return -ENOSYS;
291 }
292 return api->set_color(dev, led, num_colors, color);
293}
294
304__syscall int led_on(const struct device *dev, uint32_t led);
305
306static inline int z_impl_led_on(const struct device *dev, uint32_t led)
307{
308 const struct led_driver_api *api =
309 (const struct led_driver_api *)dev->api;
310
311 return api->on(dev, led);
312}
313
323__syscall int led_off(const struct device *dev, uint32_t led);
324
325static inline int z_impl_led_off(const struct device *dev, uint32_t led)
326{
327 const struct led_driver_api *api =
328 (const struct led_driver_api *)dev->api;
329
330 return api->off(dev, led);
331}
332
333/*
334 * LED DT helpers.
335 */
336
347 const struct device *dev;
350};
351
361static inline int led_set_brightness_dt(const struct led_dt_spec *spec,
362 uint8_t value)
363{
364 return led_set_brightness(spec->dev, spec->index, value);
365}
366
375static inline int led_on_dt(const struct led_dt_spec *spec)
376{
377 return led_on(spec->dev, spec->index);
378}
379
388static inline int led_off_dt(const struct led_dt_spec *spec)
389{
390 return led_off(spec->dev, spec->index);
391}
392
401static inline bool led_is_ready_dt(const struct led_dt_spec *spec)
402{
403 return device_is_ready(spec->dev);
404}
405
442#define LED_DT_SPEC_GET(node_id) \
443 { \
444 .dev = DEVICE_DT_GET(DT_PARENT(node_id)), \
445 .index = DT_NODE_CHILD_IDX(node_id), \
446 }
447
457#define LED_DT_SPEC_GET_OR(node_id, default_value) \
458 COND_CODE_1(DT_NODE_EXISTS(node_id), \
459 (LED_DT_SPEC_GET(node_id)), \
460 (default_value))
461
466#ifdef __cplusplus
467}
468#endif
469
470#include <zephyr/syscalls/led.h>
471
472#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:64
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:106
int led_set_channel(const struct device *dev, uint32_t channel, uint8_t value)
Set a single LED channel.
static int led_off_dt(const struct led_dt_spec *spec)
Turn off an LED from a struct led_dt_spec.
Definition led.h:388
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:81
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:401
static int led_on_dt(const struct led_dt_spec *spec)
Turn on an LED from a struct led_dt_spec.
Definition led.h:375
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:90
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:55
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:73
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:361
#define ENOSYS
Function not implemented.
Definition errno.h:82
__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:412
const void * api
Address of the API structure exposed by the device instance.
Definition device.h:418
LED driver API.
Definition led.h:114
led_api_off off
Definition led.h:117
led_api_set_color set_color
Definition led.h:122
led_api_get_info get_info
Definition led.h:120
led_api_set_brightness set_brightness
Definition led.h:121
led_api_on on
Definition led.h:116
led_api_write_channels write_channels
Definition led.h:123
led_api_blink blink
Definition led.h:119
Container for an LED information specified in devicetree.
Definition led.h:345
uint32_t index
Index of the LED on the controller.
Definition led.h:349
const struct device * dev
LED device instance.
Definition led.h:347
LED information structure.
Definition led.h:38
const char * label
LED label.
Definition led.h:40
uint32_t index
Index of the LED on the controller.
Definition led.h:42
const uint8_t * color_mapping
Mapping of the LED colors.
Definition led.h:46
uint8_t num_colors
Number of colors per LED.
Definition led.h:44