feat(led): 添加LED按键淡入淡出效果并重构LED条模块
添加了完整的LED效果系统架构,包括: - 新增主题颜色定义文件theme_color.h - 实现key fade LED效果算法,支持按键触发的渐变效果 - 创建LED效果注册机制和通用接口 - 配置17个LED像素与按键映射关系 - 将原有简单的周期性效果替换为基于按键事件的动态效果 CMakeLists.txt中添加了新的源文件路径和实现文件。 BREAKING CHANGE: LED效果从固定的周期性变化改为响应按键事件的动态效果。
This commit is contained in:
@@ -5,8 +5,10 @@
|
||||
#include <app_event_manager.h>
|
||||
|
||||
#define MODULE led_strip_module
|
||||
#include <caf/events/button_event.h>
|
||||
#include <caf/events/module_state_event.h>
|
||||
#include <caf/events/power_event.h>
|
||||
#include <caf/key_id.h>
|
||||
|
||||
#include <zephyr/device.h>
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
@@ -14,44 +16,71 @@
|
||||
#include <zephyr/kernel.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
#include "led_effect/led_effect.h"
|
||||
#include "led_strip_en_event.h"
|
||||
#include "theme_color.h"
|
||||
|
||||
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)
|
||||
#define LED_STRIP_EFFECT_INTERVAL K_MSEC(400)
|
||||
#define LED_STRIP_TICK_INTERVAL K_MSEC(20)
|
||||
|
||||
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");
|
||||
BUILD_ASSERT(LED_STRIP_NUM_PIXELS == 17U,
|
||||
"LED strip key map expects 17 pixels");
|
||||
|
||||
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);
|
||||
|
||||
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,
|
||||
};
|
||||
|
||||
static struct led_rgb pixels[LED_STRIP_NUM_PIXELS];
|
||||
static struct k_work_delayable effect_work;
|
||||
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,
|
||||
};
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool enabled = true;
|
||||
static uint8_t effect_step;
|
||||
|
||||
static void clear_pixels(void)
|
||||
{
|
||||
memset(pixels, 0, sizeof(pixels));
|
||||
}
|
||||
|
||||
static void fill_pixels(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
for (size_t i = 0; i < ARRAY_SIZE(pixels); i++) {
|
||||
pixels[i].r = r;
|
||||
pixels[i].g = g;
|
||||
pixels[i].b = b;
|
||||
}
|
||||
}
|
||||
|
||||
static int submit_frame(void)
|
||||
{
|
||||
int err;
|
||||
@@ -76,38 +105,23 @@ static int set_strip_power(bool on)
|
||||
return err;
|
||||
}
|
||||
|
||||
static void render_effect_step(void)
|
||||
static int refresh_idle_frame(void)
|
||||
{
|
||||
switch (effect_step) {
|
||||
case 0U:
|
||||
fill_pixels(0x40, 0x00, 0x00);
|
||||
break;
|
||||
|
||||
case 1U:
|
||||
fill_pixels(0x00, 0x40, 0x00);
|
||||
break;
|
||||
|
||||
default:
|
||||
fill_pixels(0x00, 0x00, 0x40);
|
||||
break;
|
||||
}
|
||||
clear_pixels();
|
||||
return submit_frame();
|
||||
}
|
||||
|
||||
static void effect_work_handler(struct k_work *work)
|
||||
static void stop_effect_work(void)
|
||||
{
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !enabled) {
|
||||
return;
|
||||
}
|
||||
|
||||
render_effect_step();
|
||||
(void)submit_frame();
|
||||
effect_step = (uint8_t)((effect_step + 1U) % 3U);
|
||||
k_work_reschedule(&effect_work, LED_STRIP_EFFECT_INTERVAL);
|
||||
k_work_cancel_delayable(&effect_work);
|
||||
}
|
||||
|
||||
static int enable_effect_output(void)
|
||||
static void schedule_effect_tick(k_timeout_t delay)
|
||||
{
|
||||
k_work_reschedule(&effect_work, delay);
|
||||
}
|
||||
|
||||
static int enable_strip_output(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
@@ -116,24 +130,37 @@ static int enable_effect_output(void)
|
||||
return err;
|
||||
}
|
||||
|
||||
render_effect_step();
|
||||
err = submit_frame();
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_reschedule(&effect_work, LED_STRIP_EFFECT_INTERVAL);
|
||||
return 0;
|
||||
return refresh_idle_frame();
|
||||
}
|
||||
|
||||
static void disable_effect_output(void)
|
||||
static void disable_strip_output(void)
|
||||
{
|
||||
k_work_cancel_delayable(&effect_work);
|
||||
stop_effect_work();
|
||||
clear_pixels();
|
||||
(void)submit_frame();
|
||||
(void)set_strip_power(false);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
{
|
||||
int err;
|
||||
@@ -144,19 +171,32 @@ static int module_init(void)
|
||||
}
|
||||
|
||||
if (!gpio_is_ready_dt(&strip_en)) {
|
||||
LOG_ERR("LED strip EN GPIO not ready");
|
||||
LOG_ERR("LED strip supply GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure_dt(&strip_en, GPIO_OUTPUT_INACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to configure LED strip EN (%d)", err);
|
||||
LOG_ERR("Failed to configure LED strip supply GPIO (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&effect_work, effect_work_handler);
|
||||
clear_pixels();
|
||||
effect_step = 0U;
|
||||
|
||||
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);
|
||||
|
||||
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(strip));
|
||||
return 0;
|
||||
@@ -176,7 +216,7 @@ static int module_start(void)
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = enable_effect_output();
|
||||
err = enable_strip_output();
|
||||
if (err) {
|
||||
running = false;
|
||||
}
|
||||
@@ -190,10 +230,26 @@ static void module_pause(void)
|
||||
return;
|
||||
}
|
||||
|
||||
disable_effect_output();
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
running = false;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
{
|
||||
enabled = event->enabled;
|
||||
@@ -203,13 +259,18 @@ static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
int err = enable_effect_output();
|
||||
int err = enable_strip_output();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
disable_effect_output();
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -217,6 +278,10 @@ static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
if (is_button_event(aeh)) {
|
||||
return handle_button_event(cast_button_event(aeh));
|
||||
}
|
||||
|
||||
if (is_led_strip_en_event(aeh)) {
|
||||
return handle_led_strip_en_event(cast_led_strip_en_event(aeh));
|
||||
}
|
||||
@@ -275,6 +340,7 @@ static bool app_event_handler(const struct app_event_header *aeh)
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(MODULE, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, button_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, led_strip_en_event);
|
||||
APP_EVENT_SUBSCRIBE(MODULE, module_state_event);
|
||||
APP_EVENT_SUBSCRIBE_EARLY(MODULE, power_down_event);
|
||||
|
||||
Reference in New Issue
Block a user