1 /*
2  * Copyright (c) 2023 Endor AG
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 
13 LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
14 
15 #define MAX_BRIGHTNESS	100
16 #define LAST_LED	15
17 
18 #define SLEEP_DELAY_MS			100
19 #define FADE_DELAY_MS			10
20 #define WRITE_CHANNELS_LEDS_COUNT	3
21 #define WRITE_CHANNELS_LED_START	2
22 
pulse_led(int led,const struct device * const dev)23 static void pulse_led(int led, const struct device *const dev)
24 {
25 	int status;
26 	uint8_t percent;
27 
28 	for (percent = 1 ; percent <= MAX_BRIGHTNESS ; percent++) {
29 		status = led_set_brightness(dev, led, percent);
30 		if (status) {
31 			LOG_ERR("Could not change brightness: %i", status);
32 			return;
33 		}
34 		k_msleep(FADE_DELAY_MS);
35 	}
36 	k_msleep(SLEEP_DELAY_MS);
37 	for (percent = MAX_BRIGHTNESS;
38 	     percent <= MAX_BRIGHTNESS; percent--) {
39 		status = led_set_brightness(dev, led, percent);
40 		if (status) {
41 			LOG_ERR("Could not change brightness: %i", status);
42 			return;
43 		}
44 		k_msleep(FADE_DELAY_MS);
45 	}
46 }
47 
pulse_leds(const struct device * const dev)48 static void pulse_leds(const struct device *const dev)
49 {
50 	int status;
51 	uint8_t brightness[WRITE_CHANNELS_LEDS_COUNT];
52 	uint8_t percent;
53 
54 	for (percent = 1; percent <= MAX_BRIGHTNESS; percent++) {
55 		memset(brightness, percent, sizeof(brightness));
56 		status = led_write_channels(dev, WRITE_CHANNELS_LED_START,
57 					    WRITE_CHANNELS_LEDS_COUNT,
58 					    brightness);
59 		if (status) {
60 			LOG_ERR("Could not change brightness: %i", status);
61 			return;
62 		}
63 		k_msleep(FADE_DELAY_MS);
64 	}
65 	k_msleep(SLEEP_DELAY_MS);
66 	for (percent = MAX_BRIGHTNESS;
67 	     percent <= MAX_BRIGHTNESS; percent--) {
68 		memset(brightness, percent, sizeof(brightness));
69 		status = led_write_channels(dev, WRITE_CHANNELS_LED_START,
70 					    WRITE_CHANNELS_LEDS_COUNT,
71 					    brightness);
72 		if (status) {
73 			LOG_ERR("Could not change brightness: %i", status);
74 			return;
75 		}
76 		k_msleep(FADE_DELAY_MS);
77 	}
78 }
79 
main(void)80 int main(void)
81 {
82 	int led;
83 	const struct device *const is31fl3216a =
84 		DEVICE_DT_GET_ANY(issi_is31fl3216a);
85 
86 	if (!is31fl3216a) {
87 		LOG_ERR("No device with compatible issi,is31fl3216a found");
88 		return 0;
89 	} else if (!device_is_ready(is31fl3216a)) {
90 		LOG_ERR("LED controller %s is not ready", is31fl3216a->name);
91 		return 0;
92 	}
93 
94 	LOG_INF("Found LED controller %s", is31fl3216a->name);
95 	for (;;) {
96 		LOG_INF("Pulsing single LED");
97 		for (led = 0 ; led <= LAST_LED ; led++) {
98 			pulse_led(led, is31fl3216a);
99 		}
100 		LOG_INF("Pulsing multiple LEDs");
101 		pulse_leds(is31fl3216a);
102 	}
103 
104 	return 0;
105 }
106