Skip to content

Commit 8fdf3ed

Browse files
committed
LED strip support
1 parent bd30430 commit 8fdf3ed

1 file changed

Lines changed: 43 additions & 11 deletions

File tree

src/system/led.c

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ K_THREAD_DEFINE(led_thread_id, 512, led_thread, NULL, NULL, NULL, 6, 0, 0);
1616

1717
#define ZEPHYR_USER_NODE DT_PATH(zephyr_user)
1818

19+
#if CONFIG_LED_STRIP
20+
#define LED_STRIP_EXISTS true
21+
#include <zephyr/drivers/led_strip.h>
22+
#define STRIP_NODE DT_ALIAS(led_strip)
23+
static const struct device *const strip = DEVICE_DT_GET(STRIP_NODE);
24+
#endif
25+
1926
#if DT_NODE_HAS_PROP(ZEPHYR_USER_NODE, led_gpios)
2027
#define LED_EXISTS true
2128
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(ZEPHYR_USER_NODE, led_gpios);
@@ -30,8 +37,9 @@ static const struct gpio_dt_spec led0 = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
3037
#endif
3138
#endif
3239
#ifndef LED_EXISTS
40+
#ifndef LED_STRIP_EXISTS
3341
#warning "LED GPIO does not exist"
34-
//static const struct gpio_dt_spec led = {0};
42+
#endif
3543
#endif
3644
#if DT_NODE_EXISTS(DT_ALIAS(led1))
3745
#define LED1_EXISTS true
@@ -50,8 +58,10 @@ static const struct gpio_dt_spec led3 = GPIO_DT_SPEC_GET(DT_ALIAS(led3), gpios);
5058
#define PWM_LED_EXISTS true
5159
static const struct pwm_dt_spec pwm_led = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led0));
5260
#else
61+
#ifndef LED_STRIP_EXISTS
5362
#warning "PWM LED node does not exist"
5463
#endif
64+
#endif
5565
#if DT_NODE_EXISTS(DT_ALIAS(pwm_led1))
5666
#define PWM_LED1_EXISTS true
5767
static const struct pwm_dt_spec pwm_led1 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led1));
@@ -64,15 +74,17 @@ static const struct pwm_dt_spec pwm_led2 = PWM_DT_SPEC_GET(DT_ALIAS(pwm_led2));
6474
static enum sys_led_pattern current_led_pattern;
6575
static int current_priority;
6676

67-
#if LED_EXISTS
77+
#if LED_EXISTS || LED_STRIP_EXISTS
6878
static enum sys_led_pattern led_patterns[SYS_LED_PATTERN_DEPTH] = {[0 ... (SYS_LED_PATTERN_DEPTH - 1)] = SYS_LED_PATTERN_OFF};
6979
static int led_pattern_state;
7080

7181
static int led_pin_init(void)
7282
{
7383
LOG_DBG("led_pin_init");
84+
#if LED_EXISTS
7485
gpio_pin_configure_dt(&led, GPIO_OUTPUT);
7586
gpio_pin_set_dt(&led, 0);
87+
#endif
7688
#if LED0_EXISTS
7789
gpio_pin_configure_dt(&led0, GPIO_OUTPUT);
7890
gpio_pin_set_dt(&led0, 0);
@@ -97,7 +109,9 @@ SYS_INIT(led_pin_init, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY);
97109
static void led_pin_reset(void)
98110
{
99111
LOG_DBG("led_pin_reset");
112+
#if LED_EXISTS
100113
gpio_pin_configure_dt(&led, GPIO_DISCONNECTED);
114+
#endif
101115
#if LED0_EXISTS
102116
gpio_pin_configure_dt(&led0, GPIO_DISCONNECTED);
103117
#endif
@@ -124,11 +138,13 @@ static void led_resume(void)
124138
led_pin_init();
125139
}
126140

141+
#if LED_STRIP_EXISTS
142+
#define LED_RGB_COLOR
143+
#else
127144
#ifdef CONFIG_LED_RGB_COLOR
128145
#define LED_RGB_COLOR
129146
#define LED_RG_COLOR
130147
#endif
131-
132148
#if PWM_LED_EXISTS && PWM_LED1_EXISTS && PWM_LED2_EXISTS
133149
#define LED_TRI_COLOR
134150
#else
@@ -141,41 +157,42 @@ static void led_resume(void)
141157
#undef LED_DUAL_COLOR
142158
#endif
143159
#endif
160+
#endif
144161

145162
#ifdef LED_RGB_COLOR
146-
static int led_pwm_period[5][3] = {
163+
static const int led_pwm_period[5][3] = {
147164
{CONFIG_LED_DEFAULT_COLOR_R, CONFIG_LED_DEFAULT_COLOR_G, CONFIG_LED_DEFAULT_COLOR_B}, // Default
148165
{0, 10000, 0}, // Success
149166
{10000, 0, 0}, // Error
150167
{8000, 2000, 0}, // Charging
151168
{0, 0, 10000}, // Pairing
152169
};
153170
#elif defined(LED_TRI_COLOR)
154-
static int led_pwm_period[5][3] = {
171+
static const int led_pwm_period[5][3] = {
155172
{0, 0, 10000}, // Default
156173
{0, 10000, 0}, // Success
157174
{10000, 0, 0}, // Error
158175
{6000, 4000, 0}, // Charging
159176
{0, 0, 10000}, // Pairing
160177
};
161178
#elif defined(LED_RG_COLOR)
162-
static int led_pwm_period[5][2] = {
179+
static const int led_pwm_period[5][2] = {
163180
{CONFIG_LED_DEFAULT_COLOR_R, CONFIG_LED_DEFAULT_COLOR_G}, // Default
164181
{0, 10000}, // Success
165182
{10000, 0}, // Error
166183
{8000, 2000}, // Charging
167184
{4000, 6000}, // Pairing
168185
};
169186
#elif defined(LED_DUAL_COLOR)
170-
static int led_pwm_period[5][2] = {
187+
static const int led_pwm_period[5][2] = {
171188
{0, 10000}, // Default
172189
{0, 10000}, // Success
173190
{10000, 0}, // Error
174191
{6000, 4000}, // Charging
175192
{0, 10000}, // Pairing
176193
};
177194
#else
178-
static int led_pwm_period[5][1] = {
195+
static const int led_pwm_period[5][1] = {
179196
{10000}, // Default
180197
{10000}, // Success
181198
{10000}, // Error
@@ -197,7 +214,14 @@ static void led_pin_set(enum sys_led_color color, int brightness_pptt, int value
197214
value_pptt = 0;
198215
else if (value_pptt > 10000)
199216
value_pptt = 10000;
200-
#if PWM_LED_EXISTS
217+
#if LED_STRIP_EXISTS
218+
static struct led_rgb pixel[1];
219+
value_pptt = value_pptt * brightness_pptt / 10000;
220+
pixel[0].r = 255 * (led_pwm_period[color][0] * value_pptt / 10000) / 10000;
221+
pixel[0].g = 255 * (led_pwm_period[color][1] * value_pptt / 10000) / 10000;
222+
pixel[0].b = 255 * (led_pwm_period[color][2] * value_pptt / 10000) / 10000;
223+
led_strip_update_rgb(strip, pixel, 1);
224+
#elif PWM_LED_EXISTS
201225
value_pptt = value_pptt * brightness_pptt / 10000;
202226
// only supporting color if PWM is supported
203227
pwm_set_pulse_dt(&pwm_led, pwm_led.period / 10000 * (led_pwm_period[color][0] * value_pptt / 10000));
@@ -217,7 +241,7 @@ void set_led(enum sys_led_pattern led_pattern, int priority)
217241
{
218242
LOG_DBG("set_led: current_led_pattern %d, current_priority %d", current_led_pattern, current_priority);
219243
LOG_DBG("set_led: pattern %d, priority %d", led_pattern, priority);
220-
#if LED_EXISTS
244+
#if LED_EXISTS || LED_STRIP_EXISTS
221245
if (led_pattern <= SYS_LED_PATTERN_OFF && k_current_get() == led_thread_id)
222246
led_patterns[current_priority] = led_pattern;
223247
else
@@ -238,29 +262,36 @@ void set_led(enum sys_led_pattern led_pattern, int priority)
238262
{
239263
led_suspend();
240264
k_thread_suspend(led_thread_id);
265+
LOG_DBG("set_led: suspended led_thread_id");
241266
}
242267
else if (k_current_get() != led_thread_id) // do not suspend if called from thread
243268
{
244269
k_thread_suspend(led_thread_id);
270+
LOG_DBG("set_led: suspended led_thread_id");
245271
led_resume();
246272
k_thread_resume(led_thread_id);
273+
k_wakeup(led_thread_id);
274+
LOG_DBG("set_led: resumed led_thread_id");
247275
}
248276
else
249277
{
250278
led_resume();
251279
k_thread_resume(led_thread_id);
280+
k_wakeup(led_thread_id);
281+
LOG_DBG("set_led: resumed led_thread_id");
252282
}
253283
#endif
254284
}
255285

256286
static void led_thread(void)
257287
{
258-
#if !LED_EXISTS
288+
#if !LED_EXISTS && !LED_STRIP_EXISTS
259289
LOG_WRN("LED GPIO does not exist");
260290
return;
261291
#else
262292
while (1)
263293
{
294+
LOG_DBG("led_thread: current_led_pattern %d", current_led_pattern);
264295
switch (current_led_pattern)
265296
{
266297
case SYS_LED_PATTERN_ON:
@@ -373,6 +404,7 @@ static void led_thread(void)
373404
break;
374405

375406
default:
407+
LOG_DBG("led_thread: suspending led_thread_id");
376408
k_thread_suspend(led_thread_id);
377409
}
378410
}

0 commit comments

Comments
 (0)