1 /*
2  * Copyright (c) 2018 Savoir-Faire Linux.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/device.h>
8 #include <errno.h>
9 #include <zephyr/drivers/led.h>
10 #include <zephyr/sys/util.h>
11 #include <zephyr/kernel.h>
12 
13 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(main);
16 
17 #define NUM_LEDS 4
18 #define MAX_BRIGHTNESS 100
19 #define HALF_BRIGHTNESS (MAX_BRIGHTNESS / 2)
20 #define BLINK_DELAY_ON 500
21 #define BLINK_DELAY_OFF 500
22 #define DELAY_TIME_MS 1000
23 #define DELAY_TIME K_MSEC(DELAY_TIME_MS)
24 
main(void)25 int main(void)
26 {
27 	const struct device *const led_dev = DEVICE_DT_GET_ANY(nxp_pca9633);
28 	int i, ret;
29 
30 	if (!led_dev) {
31 		LOG_ERR("No devices with compatible nxp,pca9633 found");
32 		return 0;
33 	} else if (!device_is_ready(led_dev)) {
34 		LOG_ERR("LED device %s is not ready", led_dev->name);
35 		return 0;
36 	} else {
37 		LOG_INF("Found LED device %s", led_dev->name);
38 	}
39 
40 	LOG_INF("Testing 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 < 0) {
47 				return 0;
48 			}
49 
50 			k_sleep(DELAY_TIME);
51 		}
52 
53 		/* Turn off LEDs one by one */
54 		for (i = 0; i < NUM_LEDS; i++) {
55 			ret = led_off(led_dev, i);
56 			if (ret < 0) {
57 				return 0;
58 			}
59 
60 			k_sleep(DELAY_TIME);
61 		}
62 
63 		/* Set the brightness to half max of LEDs one by one */
64 		for (i = 0; i < NUM_LEDS; i++) {
65 			ret = led_set_brightness(led_dev, i, HALF_BRIGHTNESS);
66 			if (ret < 0) {
67 				return 0;
68 			}
69 
70 			k_sleep(DELAY_TIME);
71 		}
72 
73 		/* Turn off LEDs one by one */
74 		for (i = 0; i < NUM_LEDS; i++) {
75 			ret = led_off(led_dev, i);
76 			if (ret < 0) {
77 				return 0;
78 			}
79 
80 			k_sleep(DELAY_TIME);
81 		}
82 
83 		/* Test the blinking of LEDs one by one */
84 		for (i = 0; i < NUM_LEDS; i++) {
85 			ret = led_blink(led_dev, i, BLINK_DELAY_ON,
86 					BLINK_DELAY_OFF);
87 			if (ret < 0) {
88 				return 0;
89 			}
90 
91 			k_sleep(DELAY_TIME);
92 		}
93 
94 		/* Wait a few blinking before turning off the LEDs */
95 		k_msleep(DELAY_TIME_MS * 10);
96 
97 		/* Turn off LEDs one by one */
98 		for (i = 0; i < NUM_LEDS; i++) {
99 			ret = led_off(led_dev, i);
100 			if (ret < 0) {
101 				return 0;
102 			}
103 
104 			k_sleep(DELAY_TIME);
105 		}
106 
107 	}
108 	return 0;
109 }
110