1 /*
2  * Copyright (c) 2017 Linaro Limited
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 #include <string.h>
9 
10 #define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
11 #include <logging/log.h>
12 LOG_MODULE_REGISTER(main);
13 
14 #include <zephyr.h>
15 #include <drivers/led_strip.h>
16 #include <device.h>
17 #include <drivers/spi.h>
18 #include <sys/util.h>
19 
20 /*
21  * Number of RGB LEDs in the LED strip, adjust as needed.
22  */
23 #define STRIP_NUM_LEDS 32
24 
25 #define DELAY_TIME K_MSEC(40)
26 
27 static const struct led_rgb colors[] = {
28 	{ .r = 0xff, .g = 0x00, .b = 0x00, }, /* red */
29 	{ .r = 0x00, .g = 0xff, .b = 0x00, }, /* green */
30 	{ .r = 0x00, .g = 0x00, .b = 0xff, }, /* blue */
31 };
32 
33 static const struct led_rgb black = {
34 	.r = 0x00,
35 	.g = 0x00,
36 	.b = 0x00,
37 };
38 
39 struct led_rgb strip_colors[STRIP_NUM_LEDS];
40 
41 static const struct device *strip = DEVICE_DT_GET_ANY(greeled_lpd8806);
42 
color_at(size_t time,size_t i)43 const struct led_rgb *color_at(size_t time, size_t i)
44 {
45 	size_t rgb_start = time % STRIP_NUM_LEDS;
46 
47 	if (rgb_start <= i && i < rgb_start + ARRAY_SIZE(colors)) {
48 		return &colors[i - rgb_start];
49 	} else {
50 		return &black;
51 	}
52 }
53 
main(void)54 void main(void)
55 {
56 	size_t i, time;
57 
58 	if (!strip) {
59 		LOG_ERR("LED strip device not found");
60 		return;
61 	} else if (!device_is_ready(strip)) {
62 		LOG_INF("LED strip device %s is not ready", strip->name);
63 		return;
64 	}
65 	LOG_INF("Found LED strip device %s", strip->name);
66 
67 	/*
68 	 * Display a pattern that "walks" the three primary colors
69 	 * down the strip until it reaches the end, then starts at the
70 	 * beginning.
71 	 */
72 	LOG_INF("Displaying pattern on strip");
73 	time = 0;
74 	while (1) {
75 		for (i = 0; i < STRIP_NUM_LEDS; i++) {
76 			memcpy(&strip_colors[i], color_at(time, i),
77 			       sizeof(strip_colors[i]));
78 		}
79 		led_strip_update_rgb(strip, strip_colors, STRIP_NUM_LEDS);
80 		k_sleep(DELAY_TIME);
81 		time++;
82 	}
83 }
84