1 /*
2 * Copyright (c) 2020 Seagate Technology LLC
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/device.h>
8 #include <zephyr/devicetree.h>
9 #include <errno.h>
10 #include <zephyr/drivers/led.h>
11 #include <zephyr/sys/util.h>
12 #include <zephyr/kernel.h>
13
14 #include <zephyr/logging/log.h>
15 LOG_MODULE_REGISTER(main, CONFIG_LOG_DEFAULT_LEVEL);
16
17 #define LED_PWM_NODE_ID DT_COMPAT_GET_ANY_STATUS_OKAY(pwm_leds)
18
19 const char *led_label[] = {
20 DT_FOREACH_CHILD_SEP_VARGS(LED_PWM_NODE_ID, DT_PROP_OR, (,), label, NULL)
21 };
22
23 const int num_leds = ARRAY_SIZE(led_label);
24
25 #define MAX_BRIGHTNESS 100
26
27 #define FADE_DELAY_MS 10
28 #define FADE_DELAY K_MSEC(FADE_DELAY_MS)
29
30 /**
31 * @brief Run tests on a single LED using the LED API syscalls.
32 *
33 * @param led_pwm LED PWM device.
34 * @param led Number of the LED to test.
35 */
run_led_test(const struct device * led_pwm,uint8_t led)36 static void run_led_test(const struct device *led_pwm, uint8_t led)
37 {
38 int err;
39 uint16_t level;
40
41 LOG_INF("Testing LED %d - %s", led, led_label[led] ? : "no label");
42
43 /* Turn LED on. */
44 err = led_on(led_pwm, led);
45 if (err < 0) {
46 LOG_ERR("err=%d", err);
47 return;
48 }
49 LOG_INF(" Turned on");
50 k_sleep(K_MSEC(1000));
51
52 /* Turn LED off. */
53 err = led_off(led_pwm, led);
54 if (err < 0) {
55 LOG_ERR("err=%d", err);
56 return;
57 }
58 LOG_INF(" Turned off");
59 k_sleep(K_MSEC(1000));
60
61 /* Increase LED brightness gradually up to the maximum level. */
62 LOG_INF(" Increasing brightness gradually");
63 for (level = 0; level <= MAX_BRIGHTNESS; level++) {
64 err = led_set_brightness(led_pwm, led, level);
65 if (err < 0) {
66 LOG_ERR("err=%d brightness=%d\n", err, level);
67 return;
68 }
69 k_sleep(FADE_DELAY);
70 }
71 k_sleep(K_MSEC(1000));
72
73 /* Set LED blinking (on: 0.1 sec, off: 0.1 sec) */
74 err = led_blink(led_pwm, led, 100, 100);
75 if (err < 0) {
76 LOG_ERR("err=%d", err);
77 return;
78 }
79 LOG_INF(" Blinking on: 0.1 sec, off: 0.1 sec");
80 k_sleep(K_MSEC(5000));
81
82 /* Enable LED blinking (on: 1 sec, off: 1 sec) */
83 err = led_blink(led_pwm, led, 1000, 1000);
84 if (err < 0) {
85 LOG_ERR("err=%d", err);
86 LOG_INF(" Cycle period not supported - on: 1 sec, off: 1 sec");
87 } else {
88 LOG_INF(" Blinking on: 1 sec, off: 1 sec");
89 }
90 k_sleep(K_MSEC(5000));
91
92 /* Turn LED off. */
93 err = led_off(led_pwm, led);
94 if (err < 0) {
95 LOG_ERR("err=%d", err);
96 return;
97 }
98 LOG_INF(" Turned off, loop end");
99 }
100
main(void)101 int main(void)
102 {
103 const struct device *led_pwm;
104 uint8_t led;
105
106 led_pwm = DEVICE_DT_GET(LED_PWM_NODE_ID);
107 if (!device_is_ready(led_pwm)) {
108 LOG_ERR("Device %s is not ready", led_pwm->name);
109 return 0;
110 }
111
112 if (!num_leds) {
113 LOG_ERR("No LEDs found for %s", led_pwm->name);
114 return 0;
115 }
116
117 do {
118 for (led = 0; led < num_leds; led++) {
119 run_led_test(led_pwm, led);
120 }
121 } while (true);
122 return 0;
123 }
124