feat: 添加模块生命周期管理框架并重构现有模块
添加了模块生命周期管理头文件 module_lifecycle.h,定义了完整的生命周期状态机, 包括初始化、运行、停止、挂起和错误状态。同时将电池模块、BLE BAS模块、BLE HID 模块和BLE NUS模块重构为使用新的生命周期框架进行状态管理。 提升日志缓冲区大小以支持更详细的调试信息记录。
This commit is contained in:
@@ -18,6 +18,7 @@
|
||||
|
||||
#include "led_effect/led_effect.h"
|
||||
#include "led_strip_en_event.h"
|
||||
#include "module_lifecycle.h"
|
||||
#include "theme_rgb_update_event.h"
|
||||
#include "theme_color.h"
|
||||
|
||||
@@ -34,9 +35,16 @@ BUILD_ASSERT(DT_NODE_HAS_PROP(LED_STRIP_NODE, supply_gpios),
|
||||
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);
|
||||
struct led_strip_module_ctx {
|
||||
struct module_lifecycle_ctx lc;
|
||||
const struct device *strip;
|
||||
struct gpio_dt_spec strip_en;
|
||||
struct led_rgb pixels[LED_STRIP_NUM_PIXELS];
|
||||
struct k_work_delayable effect_work;
|
||||
struct led_effect *effect;
|
||||
struct theme_rgb current_theme;
|
||||
bool enabled;
|
||||
};
|
||||
|
||||
static const struct led_key_map led_key_map[] = {
|
||||
{ KEY_ID(0, 1), 0U },
|
||||
@@ -65,28 +73,47 @@ static const struct led_effect_config effect_cfg = {
|
||||
.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 int do_init(void);
|
||||
static int do_start(void);
|
||||
static int do_stop(void);
|
||||
|
||||
static const struct module_lifecycle_cfg lifecycle_cfg = {
|
||||
.mode = ML_MODE_POWER,
|
||||
.stopped_state = MODULE_STATE_STANDBY,
|
||||
};
|
||||
|
||||
static const struct module_lifecycle_ops lifecycle_ops = {
|
||||
.do_init = do_init,
|
||||
.do_start = do_start,
|
||||
.do_stop = do_stop,
|
||||
};
|
||||
|
||||
static struct led_strip_module_ctx ctx = {
|
||||
.lc = {
|
||||
.state = LC_UNINIT,
|
||||
.cfg = &lifecycle_cfg,
|
||||
.ops = &lifecycle_ops,
|
||||
},
|
||||
.strip = DEVICE_DT_GET(LED_STRIP_NODE),
|
||||
.strip_en = GPIO_DT_SPEC_GET(LED_STRIP_NODE, supply_gpios),
|
||||
.current_theme = {
|
||||
.r = BLINKY_THEME_DEFAULT_R,
|
||||
.g = BLINKY_THEME_DEFAULT_G,
|
||||
.b = BLINKY_THEME_DEFAULT_B,
|
||||
},
|
||||
.enabled = true,
|
||||
};
|
||||
static bool initialized;
|
||||
static bool running;
|
||||
static bool enabled = true;
|
||||
|
||||
static void clear_pixels(void)
|
||||
{
|
||||
memset(pixels, 0, sizeof(pixels));
|
||||
memset(ctx.pixels, 0, sizeof(ctx.pixels));
|
||||
}
|
||||
|
||||
static int submit_frame(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = led_strip_update_rgb(strip, pixels, ARRAY_SIZE(pixels));
|
||||
err = led_strip_update_rgb(ctx.strip, ctx.pixels, ARRAY_SIZE(ctx.pixels));
|
||||
if (err) {
|
||||
LOG_WRN("led_strip_update_rgb failed (%d)", err);
|
||||
}
|
||||
@@ -98,7 +125,7 @@ static int set_strip_power(bool on)
|
||||
{
|
||||
int err;
|
||||
|
||||
err = gpio_pin_set_dt(&strip_en, on ? 1 : 0);
|
||||
err = gpio_pin_set_dt(&ctx.strip_en, on ? 1 : 0);
|
||||
if (err) {
|
||||
LOG_WRN("LED strip EN set failed (%d)", err);
|
||||
}
|
||||
@@ -114,12 +141,12 @@ static int refresh_idle_frame(void)
|
||||
|
||||
static void stop_effect_work(void)
|
||||
{
|
||||
k_work_cancel_delayable(&effect_work);
|
||||
k_work_cancel_delayable(&ctx.effect_work);
|
||||
}
|
||||
|
||||
static void schedule_effect_tick(k_timeout_t delay)
|
||||
{
|
||||
k_work_reschedule(&effect_work, delay);
|
||||
k_work_reschedule(&ctx.effect_work, delay);
|
||||
}
|
||||
|
||||
static int enable_strip_output(void)
|
||||
@@ -148,13 +175,15 @@ static void effect_work_handler(struct k_work *work)
|
||||
|
||||
ARG_UNUSED(work);
|
||||
|
||||
if (!running || !enabled || (effect == NULL)) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc) || !ctx.enabled ||
|
||||
(ctx.effect == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
keep_running = effect->ops->tick(effect, k_ticks_to_ms_floor32(
|
||||
LED_STRIP_TICK_INTERVAL.ticks),
|
||||
pixels, ARRAY_SIZE(pixels));
|
||||
keep_running = ctx.effect->ops->tick(ctx.effect,
|
||||
k_ticks_to_ms_floor32(
|
||||
LED_STRIP_TICK_INTERVAL.ticks),
|
||||
ctx.pixels, ARRAY_SIZE(ctx.pixels));
|
||||
(void)submit_frame();
|
||||
|
||||
if (keep_running) {
|
||||
@@ -162,90 +191,85 @@ static void effect_work_handler(struct k_work *work)
|
||||
}
|
||||
}
|
||||
|
||||
static int module_init(void)
|
||||
static int do_init(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (!device_is_ready(strip)) {
|
||||
LOG_ERR("LED strip device %s not ready", strip->name);
|
||||
if (!device_is_ready(ctx.strip)) {
|
||||
LOG_ERR("LED strip device %s not ready", ctx.strip->name);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
if (!gpio_is_ready_dt(&strip_en)) {
|
||||
if (!gpio_is_ready_dt(&ctx.strip_en)) {
|
||||
LOG_ERR("LED strip supply GPIO not ready");
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
err = gpio_pin_configure_dt(&strip_en, GPIO_OUTPUT_INACTIVE);
|
||||
err = gpio_pin_configure_dt(&ctx.strip_en, GPIO_OUTPUT_INACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to configure LED strip supply GPIO (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
k_work_init_delayable(&effect_work, effect_work_handler);
|
||||
k_work_init_delayable(&ctx.effect_work, effect_work_handler);
|
||||
clear_pixels();
|
||||
|
||||
effect = led_effect_get_mutable(LED_EFFECT_ID_KEY_FADE);
|
||||
if ((effect == NULL) || (effect->ops == NULL)) {
|
||||
ctx.effect = led_effect_get_mutable(LED_EFFECT_ID_KEY_FADE);
|
||||
if ((ctx.effect == NULL) || (ctx.effect->ops == NULL)) {
|
||||
LOG_ERR("LED effect not available");
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
err = effect->ops->init(effect, &effect_cfg);
|
||||
err = ctx.effect->ops->init(ctx.effect, &effect_cfg);
|
||||
if (err) {
|
||||
LOG_ERR("LED effect init failed (%d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
effect->ops->set_theme(effect, ¤t_theme);
|
||||
ctx.effect->ops->set_theme(ctx.effect, &ctx.current_theme);
|
||||
|
||||
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(strip));
|
||||
LOG_INF("LED strip ready len:%u", (uint32_t)led_strip_length(ctx.strip));
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int module_start(void)
|
||||
static int do_start(void)
|
||||
{
|
||||
int err;
|
||||
|
||||
if (running) {
|
||||
if (module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
running = true;
|
||||
|
||||
if (!enabled) {
|
||||
if (!ctx.enabled) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
err = enable_strip_output();
|
||||
if (err) {
|
||||
running = false;
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
static void module_pause(void)
|
||||
static int do_stop(void)
|
||||
{
|
||||
if (!running) {
|
||||
return;
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
if ((ctx.effect != NULL) && (ctx.effect->ops != NULL)) {
|
||||
ctx.effect->ops->reset(ctx.effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
running = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool handle_button_event(const struct button_event *event)
|
||||
{
|
||||
if (!running || !enabled || (effect == NULL) || !event->pressed) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc) || !ctx.enabled ||
|
||||
(ctx.effect == NULL) || !event->pressed) {
|
||||
return false;
|
||||
}
|
||||
|
||||
effect->ops->on_key_press(effect, event->key_id);
|
||||
ctx.effect->ops->on_key_press(ctx.effect, event->key_id);
|
||||
schedule_effect_tick(K_NO_WAIT);
|
||||
|
||||
return false;
|
||||
@@ -253,22 +277,23 @@ static bool handle_button_event(const struct button_event *event)
|
||||
|
||||
static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
{
|
||||
enabled = event->enabled;
|
||||
ctx.enabled = event->enabled;
|
||||
|
||||
if (!running) {
|
||||
if (!module_lifecycle_is_running(&ctx.lc)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (enabled) {
|
||||
if (ctx.enabled) {
|
||||
int err = enable_strip_output();
|
||||
|
||||
if (err) {
|
||||
module_set_state(MODULE_STATE_ERROR);
|
||||
LOG_WRN("LED strip enable request failed (%d)", err);
|
||||
ctx.enabled = false;
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->reset(effect);
|
||||
if ((ctx.effect != NULL) && (ctx.effect->ops != NULL)) {
|
||||
ctx.effect->ops->reset(ctx.effect);
|
||||
}
|
||||
|
||||
disable_strip_output();
|
||||
@@ -279,13 +304,14 @@ static bool handle_led_strip_en_event(const struct led_strip_en_event *event)
|
||||
|
||||
static bool handle_theme_rgb_update_event(const struct theme_rgb_update_event *event)
|
||||
{
|
||||
current_theme = event->theme;
|
||||
ctx.current_theme = event->theme;
|
||||
|
||||
if ((effect != NULL) && (effect->ops != NULL)) {
|
||||
effect->ops->set_theme(effect, ¤t_theme);
|
||||
if ((ctx.effect != NULL) && (ctx.effect->ops != NULL)) {
|
||||
ctx.effect->ops->set_theme(ctx.effect, &ctx.current_theme);
|
||||
}
|
||||
|
||||
if (running && enabled && (effect != NULL) && effect->ops->is_active(effect)) {
|
||||
if (module_lifecycle_is_running(&ctx.lc) && ctx.enabled &&
|
||||
(ctx.effect != NULL) && ctx.effect->ops->is_active(ctx.effect)) {
|
||||
schedule_effect_tick(K_NO_WAIT);
|
||||
}
|
||||
|
||||
@@ -310,47 +336,23 @@ static bool app_event_handler(const struct app_event_header *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);
|
||||
}
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if (is_power_down_event(aeh)) {
|
||||
if (initialized) {
|
||||
module_pause();
|
||||
module_set_state(MODULE_STATE_STANDBY);
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_STOPPED);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
if (module_lifecycle_is_initialized(&ctx.lc)) {
|
||||
(void)module_set_lifecycle(&ctx.lc, LC_RUNNING);
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user