forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
led_strip.h
142 lines (126 loc) · 3.35 KB
/
led_strip.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2017 Linaro Limited
*
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @file
* @brief Public API for controlling linear strips of LEDs.
*
* This library abstracts the chipset drivers for individually
* addressable strips of LEDs.
*/
#ifndef ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
#define ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_
/**
* @brief LED Strip Interface
* @defgroup led_strip_interface LED Strip Interface
* @ingroup io_interfaces
* @{
*/
#include <zephyr/types.h>
#include <device.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Color value for a single RGB LED.
*
* Individual strip drivers may ignore lower-order bits if their
* resolution in any channel is less than a full byte.
*/
struct led_rgb {
#ifdef CONFIG_LED_STRIP_RGB_SCRATCH
/*
* Pad/scratch space needed by some drivers. Users should
* ignore.
*/
uint8_t scratch;
#endif
/** Red channel */
uint8_t r;
/** Green channel */
uint8_t g;
/** Blue channel */
uint8_t b;
};
/**
* @typedef led_api_update_rgb
* @brief Callback API for updating an RGB LED strip
*
* @see led_strip_update_rgb() for argument descriptions.
*/
typedef int (*led_api_update_rgb)(const struct device *dev,
struct led_rgb *pixels,
size_t num_pixels);
/**
* @typedef led_api_update_channels
* @brief Callback API for updating channels without an RGB interpretation.
*
* @see led_strip_update_channels() for argument descriptions.
*/
typedef int (*led_api_update_channels)(const struct device *dev,
uint8_t *channels,
size_t num_channels);
/**
* @brief LED strip driver API
*
* This is the mandatory API any LED strip driver needs to expose.
*/
struct led_strip_driver_api {
led_api_update_rgb update_rgb;
led_api_update_channels update_channels;
};
/**
* @brief Update an LED strip made of RGB pixels
*
* Important:
* This routine may overwrite @a pixels.
*
* This routine immediately updates the strip display according to the
* given pixels array.
*
* @param dev LED strip device
* @param pixels Array of pixel data
* @param num_pixels Length of pixels array
* @return 0 on success, negative on error
* @warning May overwrite @a pixels
*/
static inline int led_strip_update_rgb(const struct device *dev,
struct led_rgb *pixels,
size_t num_pixels) {
const struct led_strip_driver_api *api =
(const struct led_strip_driver_api *)dev->api;
return api->update_rgb(dev, pixels, num_pixels);
}
/**
* @brief Update an LED strip on a per-channel basis.
*
* Important:
* This routine may overwrite @a channels.
*
* This routine immediately updates the strip display according to the
* given channels array. Each channel byte corresponds to an
* individually addressable color channel or LED. Channels
* are updated linearly in strip order.
*
* @param dev LED strip device
* @param channels Array of per-channel data
* @param num_channels Length of channels array
* @return 0 on success, negative on error
* @warning May overwrite @a channels
*/
static inline int led_strip_update_channels(const struct device *dev,
uint8_t *channels,
size_t num_channels) {
const struct led_strip_driver_api *api =
(const struct led_strip_driver_api *)dev->api;
return api->update_channels(dev, channels, num_channels);
}
#ifdef __cplusplus
}
#endif
/**
* @}
*/
#endif /* ZEPHYR_INCLUDE_DRIVERS_LED_STRIP_H_ */