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