2026-04-13 15:23:46 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
#include <app_event_manager.h>
|
|
|
|
|
|
|
|
|
|
#define MODULE led_strip_module
|
2026-04-13 15:56:45 +08:00
|
|
|
#include <caf/events/button_event.h>
|
2026-04-13 15:23:46 +08:00
|
|
|
#include <caf/events/module_state_event.h>
|
|
|
|
|
#include <caf/events/power_event.h>
|
2026-04-13 15:56:45 +08:00
|
|
|
#include <caf/key_id.h>
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
#include <zephyr/device.h>
|
|
|
|
|
#include <zephyr/drivers/gpio.h>
|
|
|
|
|
#include <zephyr/drivers/led_strip.h>
|
|
|
|
|
#include <zephyr/kernel.h>
|
|
|
|
|
#include <zephyr/logging/log.h>
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
#include "led_effect/led_effect.h"
|
2026-04-13 15:23:46 +08:00
|
|
|
#include "led_strip_en_event.h"
|
2026-04-13 16:43:17 +08:00
|
|
|
#include "theme_rgb_update_event.h"
|
2026-04-13 15:56:45 +08:00
|
|
|
#include "theme_color.h"
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(MODULE, LOG_LEVEL_INF);
|
|
|
|
|
|
|
|
|
|
#define LED_STRIP_NODE DT_CHOSEN(zephyr_led_strip)
|
|
|
|
|
#define LED_STRIP_NUM_PIXELS DT_PROP(LED_STRIP_NODE, chain_length)
|
2026-04-13 15:56:45 +08:00
|
|
|
#define LED_STRIP_TICK_INTERVAL K_MSEC(20)
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
BUILD_ASSERT(DT_NODE_HAS_STATUS(LED_STRIP_NODE, okay),
|
|
|
|
|
"Missing zephyr,led-strip chosen node");
|
|
|
|
|
BUILD_ASSERT(DT_NODE_HAS_PROP(LED_STRIP_NODE, supply_gpios),
|
|
|
|
|
"Missing supply-gpios on zephyr,led-strip node");
|
2026-04-13 15:56:45 +08:00
|
|
|
BUILD_ASSERT(LED_STRIP_NUM_PIXELS == 17U,
|
|
|
|
|
"LED strip key map expects 17 pixels");
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
static const struct device *const strip = DEVICE_DT_GET(LED_STRIP_NODE);
|
|
|
|
|
static const struct gpio_dt_spec strip_en =
|
|
|
|
|
GPIO_DT_SPEC_GET(LED_STRIP_NODE, supply_gpios);
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static const struct led_key_map led_key_map[] = {
|
|
|
|
|
{ KEY_ID(0, 1), 0U },
|
|
|
|
|
{ KEY_ID(1, 1), 1U },
|
|
|
|
|
{ KEY_ID(2, 1), 2U },
|
|
|
|
|
{ KEY_ID(3, 1), 3U },
|
|
|
|
|
{ KEY_ID(0, 2), 4U },
|
|
|
|
|
{ KEY_ID(1, 2), 5U },
|
|
|
|
|
{ KEY_ID(2, 2), 6U },
|
|
|
|
|
{ KEY_ID(0, 3), 7U },
|
|
|
|
|
{ KEY_ID(1, 3), 8U },
|
|
|
|
|
{ KEY_ID(2, 3), 9U },
|
|
|
|
|
{ KEY_ID(3, 3), 10U },
|
|
|
|
|
{ KEY_ID(0, 4), 11U },
|
|
|
|
|
{ KEY_ID(1, 4), 12U },
|
|
|
|
|
{ KEY_ID(2, 4), 13U },
|
|
|
|
|
{ KEY_ID(0, 5), 14U },
|
|
|
|
|
{ KEY_ID(1, 5), 15U },
|
|
|
|
|
{ KEY_ID(3, 5), 16U },
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static const struct led_effect_config effect_cfg = {
|
|
|
|
|
.key_map = led_key_map,
|
|
|
|
|
.key_map_len = ARRAY_SIZE(led_key_map),
|
|
|
|
|
.pixel_count = LED_STRIP_NUM_PIXELS,
|
|
|
|
|
.default_brightness = 0xFFU,
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
static struct led_rgb pixels[LED_STRIP_NUM_PIXELS];
|
|
|
|
|
static struct k_work_delayable effect_work;
|
2026-04-13 15:56:45 +08:00
|
|
|
static struct led_effect *effect;
|
|
|
|
|
static struct theme_rgb current_theme = {
|
|
|
|
|
.r = BLINKY_THEME_DEFAULT_R,
|
|
|
|
|
.g = BLINKY_THEME_DEFAULT_G,
|
|
|
|
|
.b = BLINKY_THEME_DEFAULT_B,
|
|
|
|
|
};
|
2026-04-13 15:23:46 +08:00
|
|
|
static bool initialized;
|
|
|
|
|
static bool running;
|
|
|
|
|
static bool enabled = true;
|
|
|
|
|
|
|
|
|
|
static void clear_pixels(void)
|
|
|
|
|
{
|
|
|
|
|
memset(pixels, 0, sizeof(pixels));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int submit_frame(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = led_strip_update_rgb(strip, pixels, ARRAY_SIZE(pixels));
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("led_strip_update_rgb failed (%d)", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int set_strip_power(bool on)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = gpio_pin_set_dt(&strip_en, on ? 1 : 0);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_WRN("LED strip EN set failed (%d)", err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static int refresh_idle_frame(void)
|
2026-04-13 15:23:46 +08:00
|
|
|
{
|
2026-04-13 15:56:45 +08:00
|
|
|
clear_pixels();
|
|
|
|
|
return submit_frame();
|
2026-04-13 15:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static void stop_effect_work(void)
|
2026-04-13 15:23:46 +08:00
|
|
|
{
|
2026-04-13 15:56:45 +08:00
|
|
|
k_work_cancel_delayable(&effect_work);
|
|
|
|
|
}
|
2026-04-13 15:23:46 +08:00
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static void schedule_effect_tick(k_timeout_t delay)
|
|
|
|
|
{
|
|
|
|
|
k_work_reschedule(&effect_work, delay);
|
2026-04-13 15:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static int enable_strip_output(void)
|
2026-04-13 15:23:46 +08:00
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
err = set_strip_power(true);
|
|
|
|
|
if (err) {
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
return refresh_idle_frame();
|
2026-04-13 15:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static void disable_strip_output(void)
|
2026-04-13 15:23:46 +08:00
|
|
|
{
|
2026-04-13 15:56:45 +08:00
|
|
|
stop_effect_work();
|
2026-04-13 15:23:46 +08:00
|
|
|
clear_pixels();
|
|
|
|
|
(void)submit_frame();
|
|
|
|
|
(void)set_strip_power(false);
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static void effect_work_handler(struct k_work *work)
|
|
|
|
|
{
|
|
|
|
|
bool keep_running;
|
|
|
|
|
|
|
|
|
|
ARG_UNUSED(work);
|
|
|
|
|
|
|
|
|
|
if (!running || !enabled || (effect == NULL)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
keep_running = effect->ops->tick(effect, k_ticks_to_ms_floor32(
|
|
|
|
|
LED_STRIP_TICK_INTERVAL.ticks),
|
|
|
|
|
pixels, ARRAY_SIZE(pixels));
|
|
|
|
|
(void)submit_frame();
|
|
|
|
|
|
|
|
|
|
if (keep_running) {
|
|
|
|
|
schedule_effect_tick(LED_STRIP_TICK_INTERVAL);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
static int module_init(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (!device_is_ready(strip)) {
|
|
|
|
|
LOG_ERR("LED strip device %s not ready", strip->name);
|
|
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!gpio_is_ready_dt(&strip_en)) {
|
2026-04-13 15:56:45 +08:00
|
|
|
LOG_ERR("LED strip supply GPIO not ready");
|
2026-04-13 15:23:46 +08:00
|
|
|
return -ENODEV;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = gpio_pin_configure_dt(&strip_en, GPIO_OUTPUT_INACTIVE);
|
|
|
|
|
if (err) {
|
2026-04-13 15:56:45 +08:00
|
|
|
LOG_ERR("Failed to configure LED strip supply GPIO (%d)", err);
|
2026-04-13 15:23:46 +08:00
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
k_work_init_delayable(&effect_work, effect_work_handler);
|
|
|
|
|
clear_pixels();
|
2026-04-13 15:56:45 +08:00
|
|
|
|
|
|
|
|
effect = led_effect_get_mutable(LED_EFFECT_ID_KEY_FADE);
|
|
|
|
|
if ((effect == NULL) || (effect->ops == NULL)) {
|
|
|
|
|
LOG_ERR("LED effect not available");
|
|
|
|
|
return -ENOENT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = effect->ops->init(effect, &effect_cfg);
|
|
|
|
|
if (err) {
|
|
|
|
|
LOG_ERR("LED effect init failed (%d)", err);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
effect->ops->set_theme(effect, ¤t_theme);
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(strip));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int module_start(void)
|
|
|
|
|
{
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (running) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
|
|
if (!enabled) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
err = enable_strip_output();
|
2026-04-13 15:23:46 +08:00
|
|
|
if (err) {
|
|
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void module_pause(void)
|
|
|
|
|
{
|
|
|
|
|
if (!running) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
if ((effect != NULL) && (effect->ops != NULL)) {
|
|
|
|
|
effect->ops->reset(effect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disable_strip_output();
|
2026-04-13 15:23:46 +08:00
|
|
|
running = false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:56:45 +08:00
|
|
|
static bool handle_button_event(const struct button_event *event)
|
|
|
|
|
{
|
|
|
|
|
if (!running || !enabled || (effect == NULL) || !event->pressed) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
effect->ops->on_key_press(effect, event->key_id);
|
|
|
|
|
schedule_effect_tick(K_NO_WAIT);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
|
|
|
|
{
|
|
|
|
|
enabled = event->enabled;
|
|
|
|
|
|
|
|
|
|
if (!running) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (enabled) {
|
2026-04-13 15:56:45 +08:00
|
|
|
int err = enable_strip_output();
|
2026-04-13 15:23:46 +08:00
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
2026-04-13 15:56:45 +08:00
|
|
|
return false;
|
2026-04-13 15:23:46 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
2026-04-13 15:56:45 +08:00
|
|
|
if ((effect != NULL) && (effect->ops != NULL)) {
|
|
|
|
|
effect->ops->reset(effect);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
disable_strip_output();
|
2026-04-13 15:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 16:43:17 +08:00
|
|
|
static bool handle_theme_rgb_update_event(const struct theme_rgb_update_event *event)
|
|
|
|
|
{
|
|
|
|
|
current_theme = event->theme;
|
|
|
|
|
|
|
|
|
|
if ((effect != NULL) && (effect->ops != NULL)) {
|
|
|
|
|
effect->ops->set_theme(effect, ¤t_theme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (running && enabled && (effect != NULL) && effect->ops->is_active(effect)) {
|
|
|
|
|
schedule_effect_tick(K_NO_WAIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
static bool app_event_handler(const struct app_event_header *aeh)
|
|
|
|
|
{
|
2026-04-13 15:56:45 +08:00
|
|
|
if (is_button_event(aeh)) {
|
|
|
|
|
return handle_button_event(cast_button_event(aeh));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
if (is_led_strip_en_event(aeh)) {
|
|
|
|
|
return handle_led_strip_en_event(cast_led_strip_en_event(aeh));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 16:43:17 +08:00
|
|
|
if (is_theme_rgb_update_event(aeh)) {
|
|
|
|
|
return handle_theme_rgb_update_event(cast_theme_rgb_update_event(aeh));
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-13 15:23:46 +08:00
|
|
|
if (is_module_state_event(aeh)) {
|
|
|
|
|
const struct module_state_event *event = cast_module_state_event(aeh);
|
|
|
|
|
|
|
|
|
|
if (check_state(event, MODULE_ID(main), MODULE_STATE_READY)) {
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (!initialized) {
|
|
|
|
|
err = module_init();
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = module_start();
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_power_down_event(aeh)) {
|
|
|
|
|
if (initialized) {
|
|
|
|
|
module_pause();
|
|
|
|
|
module_set_state(MODULE_STATE_STANDBY);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (is_wake_up_event(aeh)) {
|
|
|
|
|
if (initialized) {
|
|
|
|
|
int err = module_start();
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
module_set_state(MODULE_STATE_ERROR);
|
|
|
|
|
} else {
|
|
|
|
|
module_set_state(MODULE_STATE_READY);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
2026-04-13 15:56:45 +08:00
|
|
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, button_event);
|
2026-04-13 15:23:46 +08:00
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, led_strip_en_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
2026-04-13 16:43:17 +08:00
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, theme_rgb_update_event);
|
2026-04-13 15:23:46 +08:00
|
|
|
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
|
|
|
|
APP_EVENT_SUBSCRIBE(MODULE, wake_up_event);
|