1 /*
2  * Copyright (c) 2023 Phytec Messtechnik GmbH
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <zephyr/drivers/led.h>
9 #include <zephyr/kernel.h>
10 
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(app, CONFIG_LED_LOG_LEVEL);
13 
14 #define NUM_LEDS 9
15 #define DELAY_TIME_ON		K_MSEC(1000)
16 #define DELAY_TIME_BREATHING	K_MSEC(20)
17 
main(void)18 int main(void)
19 {
20 	const struct device *const led_dev = DEVICE_DT_GET_ANY(ti_lp5569);
21 	uint8_t ch_buf[9] = {0};
22 	int i, ret;
23 
24 	if (!led_dev) {
25 		LOG_ERR("No device with compatible ti,lp5569 found");
26 		return 0;
27 	} else if (!device_is_ready(led_dev)) {
28 		LOG_ERR("LED device %s not ready", led_dev->name);
29 		return 0;
30 	}
31 
32 	LOG_INF("Found LED device %s", led_dev->name);
33 
34 	/*
35 	 * Display a continuous pattern that turns on 9 LEDs at 1 s one by
36 	 * one until it reaches the end and fades off all LEDs.
37 	 * Afterwards, all LEDs get blinked once at the same time.
38 	 */
39 
40 	LOG_INF("Testing 9 LEDs ..");
41 
42 	while (1) {
43 		/* Turn on LEDs one by one */
44 		for (i = 0; i < NUM_LEDS; i++) {
45 			ret = led_on(led_dev, i);
46 			if (ret) {
47 				return ret;
48 			}
49 
50 			k_sleep(DELAY_TIME_ON);
51 		}
52 
53 		/* Turn all LEDs off slowly to demonstrate set_brightness */
54 		for (i = 0; i <= 100; i++) {
55 			for (int j = 0; j < NUM_LEDS; j++) {
56 				ret = led_set_brightness(led_dev, j, 100 - i);
57 				if (ret) {
58 					return ret;
59 				}
60 			}
61 
62 			k_sleep(DELAY_TIME_BREATHING);
63 		}
64 
65 		k_sleep(DELAY_TIME_ON);
66 
67 		/* Turn on all LEDs at once to demonstrate write_channels */
68 		memset(ch_buf, 255, ARRAY_SIZE(ch_buf));
69 		ret = led_write_channels(led_dev, 0, ARRAY_SIZE(ch_buf), ch_buf);
70 		if (ret) {
71 			return ret;
72 		}
73 
74 		k_sleep(DELAY_TIME_ON);
75 
76 		/* Turn off all LEDs at once to demonstrate write_channels */
77 		memset(ch_buf, 0, ARRAY_SIZE(ch_buf));
78 		ret = led_write_channels(led_dev, 0, ARRAY_SIZE(ch_buf), ch_buf);
79 		if (ret) {
80 			return ret;
81 		}
82 
83 		k_sleep(DELAY_TIME_ON);
84 	}
85 	return 0;
86 }
87