1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018 Intel Corporation
4  * Copyright (c) 2024 TOKITA Hiroshi
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <errno.h>
10 #include <string.h>
11 
12 #define LOG_LEVEL 4
13 #include <zephyr/logging/log.h>
14 LOG_MODULE_REGISTER(main);
15 
16 #include <zephyr/kernel.h>
17 #include <zephyr/drivers/led_strip.h>
18 #include <zephyr/device.h>
19 #include <zephyr/drivers/spi.h>
20 #include <zephyr/sys/util.h>
21 
22 #define STRIP_NODE		DT_ALIAS(led_strip)
23 
24 #if DT_NODE_HAS_PROP(DT_ALIAS(led_strip), chain_length)
25 #define STRIP_NUM_PIXELS	DT_PROP(DT_ALIAS(led_strip), chain_length)
26 #else
27 #error Unable to determine length of LED strip
28 #endif
29 
30 #define DELAY_TIME K_MSEC(CONFIG_SAMPLE_LED_UPDATE_DELAY)
31 
32 #define RGB(_r, _g, _b) { .r = (_r), .g = (_g), .b = (_b) }
33 
34 static const struct led_rgb colors[] = {
35 	RGB(0x0f, 0x00, 0x00), /* red */
36 	RGB(0x00, 0x0f, 0x00), /* green */
37 	RGB(0x00, 0x00, 0x0f), /* blue */
38 };
39 
40 static struct led_rgb pixels[STRIP_NUM_PIXELS];
41 
42 static const struct device *const strip = DEVICE_DT_GET(STRIP_NODE);
43 
main(void)44 int main(void)
45 {
46 	size_t color = 0;
47 	int rc;
48 
49 	if (device_is_ready(strip)) {
50 		LOG_INF("Found LED strip device %s", strip->name);
51 	} else {
52 		LOG_ERR("LED strip device %s is not ready", strip->name);
53 		return 0;
54 	}
55 
56 	LOG_INF("Displaying pattern on strip");
57 	while (1) {
58 		for (size_t cursor = 0; cursor < ARRAY_SIZE(pixels); cursor++) {
59 			memset(&pixels, 0x00, sizeof(pixels));
60 			memcpy(&pixels[cursor], &colors[color], sizeof(struct led_rgb));
61 
62 			rc = led_strip_update_rgb(strip, pixels, STRIP_NUM_PIXELS);
63 			if (rc) {
64 				LOG_ERR("couldn't update strip: %d", rc);
65 			}
66 
67 			k_sleep(DELAY_TIME);
68 		}
69 
70 		color = (color + 1) % ARRAY_SIZE(colors);
71 	}
72 
73 	return 0;
74 }
75