nRF Connect SDK API 2.8.99
Loading...
Searching...
No Matches
led_effect.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019-2021 Nordic Semiconductor ASA
3 *
4 * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
5 */
6
7#ifndef _LED_EFFECT_H_
8#define _LED_EFFECT_H_
9
16#include <zephyr/types.h>
17#include <zephyr/sys/util.h>
18
19#ifdef __cplusplus
20extern "C" {
21#endif
22
23#define _CAF_LED_COLOR_CHANNEL_COUNT 3
24
27struct led_color {
29 uint8_t c[_CAF_LED_COLOR_CHANNEL_COUNT];
30};
31
41
43 uint16_t substep_count;
44
46 uint16_t substep_time;
47};
48
51struct led_effect {
53 const struct led_effect_step *steps;
54
56 uint16_t step_count;
57
60};
61
68#define COLOR_BRIGHTNESS_TO_PCT(_val) ((_val * 100) / UINT8_MAX)
69
83#define LED_COLOR_ARG_PASS(...) __VA_ARGS__
84
96#define LED_COLOR(_r, _g, _b) { \
97 .c = { \
98 COLOR_BRIGHTNESS_TO_PCT(_r), \
99 COLOR_BRIGHTNESS_TO_PCT(_g), \
100 COLOR_BRIGHTNESS_TO_PCT(_b) \
101 } \
102}
103
106#define LED_NOCOLOR() { \
107 .c = {0, 0, 0} \
108}
109
116#define LED_EFFECT_LED_ON(_color) \
117 { \
118 .steps = ((const struct led_effect_step[]) { \
119 { \
120 .color = _color, \
121 .substep_count = 1, \
122 .substep_time = 0, \
123 }, \
124 }), \
125 .step_count = 1, \
126 .loop_forever = false, \
127 }
128
129
132#define LED_EFFECT_LED_OFF() LED_EFFECT_LED_ON(LED_NOCOLOR())
133
143#define LED_EFFECT_LED_ON_GO_OFF(_color, _on_time, _off_delay) \
144 { \
145 .steps = ((const struct led_effect_step[]) { \
146 { \
147 .color = _color, \
148 .substep_count = 1, \
149 .substep_time = 0, \
150 }, \
151 { \
152 .color = _color, \
153 .substep_count = 1, \
154 .substep_time = (_on_time), \
155 }, \
156 { \
157 .color = LED_NOCOLOR(), \
158 .substep_count = (_off_delay)/10, \
159 .substep_time = 10, \
160 }, \
161 }), \
162 .step_count = 3, \
163 .loop_forever = false, \
164 }
165
178#define LED_EFFECT_LED_BLINK2(_period_on, _period_off, _color) \
179 { \
180 .steps = ((const struct led_effect_step[]) { \
181 { \
182 .color = _color, \
183 .substep_count = 1, \
184 .substep_time = (_period_off), \
185 }, \
186 { \
187 .color = LED_NOCOLOR(), \
188 .substep_count = 1, \
189 .substep_time = (_period_on), \
190 }, \
191 }), \
192 .step_count = 2, \
193 .loop_forever = true, \
194 }
195
207#define LED_EFFECT_LED_BLINK(_period, _color) \
208 LED_EFFECT_LED_BLINK2(_period, _period, LED_COLOR_ARG_PASS(_color))
209
214#define _BREATH_SUBSTEPS 15
215
224#define LED_EFFECT_LED_BREATH(_period, _color) \
225 { \
226 .steps = ((const struct led_effect_step[]) { \
227 { \
228 .color = _color, \
229 .substep_count = _BREATH_SUBSTEPS, \
230 .substep_time = ((_period + (_BREATH_SUBSTEPS - 1)) \
231 / _BREATH_SUBSTEPS), \
232 }, \
233 { \
234 .color = _color, \
235 .substep_count = 1, \
236 .substep_time = _period, \
237 }, \
238 { \
239 .color = LED_NOCOLOR(), \
240 .substep_count = _BREATH_SUBSTEPS, \
241 .substep_time = ((_period + (_BREATH_SUBSTEPS - 1)) \
242 / _BREATH_SUBSTEPS), \
243 }, \
244 { \
245 .color = LED_NOCOLOR(), \
246 .substep_count = 1, \
247 .substep_time = _period, \
248 }, \
249 }), \
250 .step_count = 4, \
251 .loop_forever = true, \
252 }
253
259#define LED_CLOCK_BLINK_PERIOD 200
260
265#define LED_CLOCK_SLEEP_PERIOD 1000
266
275#define _LED_CLOCK_TIK(i, ...) \
276 { \
277 .color = __VA_ARGS__, \
278 .substep_count = 1, \
279 .substep_time = LED_CLOCK_BLINK_PERIOD, \
280 }, \
281 { \
282 .color = LED_NOCOLOR(), \
283 .substep_count = 1, \
284 .substep_time = LED_CLOCK_BLINK_PERIOD, \
285 }
286
298#define LED_EFFECT_LED_CLOCK(_ticks, _color) \
299 { \
300 .steps = ((const struct led_effect_step[]) { \
301 { \
302 .color = LED_NOCOLOR(), \
303 .substep_count = 1, \
304 .substep_time = LED_CLOCK_SLEEP_PERIOD, \
305 }, \
306 LISTIFY(_ticks, _LED_CLOCK_TIK, (,), _color) \
307 }), \
308 .step_count = (2 * _ticks + 1), \
309 .loop_forever = true, \
310 }
311
312#ifdef __cplusplus
313}
314#endif
315
320#endif /* _LED_EFFECT_H_ */
uint8_t c[3]
Definition led_effect.h:29
Color of LED.
Definition led_effect.h:27
struct led_color color
Definition led_effect.h:40
uint16_t substep_count
Definition led_effect.h:43
uint16_t substep_time
Definition led_effect.h:46
Single step of a LED effect.
Definition led_effect.h:38
uint16_t step_count
Definition led_effect.h:56
const struct led_effect_step * steps
Definition led_effect.h:53
bool loop_forever
Definition led_effect.h:59
Single LED effect.
Definition led_effect.h:51