feat(led): 使用CAF LEDs模块替代自定义LED驱动
- 移除src/modules/led_module.c中的自定义GPIO LED实现 - 在CMakeLists.txt中移除对led_module.c的引用 - 更新prj.conf配置,启用CAF LEDs相关功能 - 添加CONFIG_CAF_POWER_MANAGER配置项以支持电源管理 - 在main.c中集成LED事件,在系统就绪时触发启动LED效果 - 实现白色常亮LED效果作为系统启动指示
This commit is contained in:
@@ -3,5 +3,4 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
||||
|
||||
project(new_kbd)
|
||||
|
||||
target_sources(app PRIVATE src/main.c
|
||||
src/modules/led_module.c)
|
||||
target_sources(app PRIVATE src/main.c)
|
||||
|
||||
12
prj.conf
12
prj.conf
@@ -1,4 +1,14 @@
|
||||
CONFIG_CAF=y
|
||||
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
||||
CONFIG_LOG=y
|
||||
CONFIG_GPIO=y
|
||||
|
||||
CONFIG_LED=y
|
||||
CONFIG_LED_GPIO=y
|
||||
CONFIG_CAF_LEDS=y
|
||||
CONFIG_CAF_LEDS_GPIO=y
|
||||
CONFIG_CAF_LEDS_PM_EVENTS=y
|
||||
|
||||
CONFIG_CAF_POWER_MANAGER=y
|
||||
CONFIG_CAF_POWER_MANAGER_TIMEOUT=20
|
||||
CONFIG_CAF_POWER_MANAGER_ERROR_TIMEOUT=10
|
||||
CONFIG_REBOOT=y
|
||||
|
||||
19
src/main.c
19
src/main.c
@@ -3,14 +3,33 @@
|
||||
#define MODULE main
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include <caf/events/led_event.h>
|
||||
#include <caf/led_effect.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(MODULE);
|
||||
|
||||
/*
|
||||
* 通过 CAF leds 模块控制第 0 号 LED 常亮。
|
||||
* 颜色值在单色 LED 上会被折算为亮度,因此这里使用白色全亮。
|
||||
*/
|
||||
static const struct led_effect startup_led_effect =
|
||||
LED_EFFECT_LED_ON(LED_COLOR(255, 255, 255));
|
||||
|
||||
int main(void)
|
||||
{
|
||||
if (app_event_manager_init()) {
|
||||
LOG_ERR("Application Event Manager not initialized");
|
||||
} else {
|
||||
/*
|
||||
* 先提交 led_event,再上报 main ready。
|
||||
* CAF leds 模块会缓存 effect,并在收到 main ready 完成初始化后开始输出。
|
||||
*/
|
||||
struct led_event *event = new_led_event();
|
||||
|
||||
event->led_id = 0;
|
||||
event->led_effect = &startup_led_effect;
|
||||
APP_EVENT_SUBMIT(event);
|
||||
|
||||
module_set_state(MODULE_STATE_READY);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include <app_event_manager.h>
|
||||
#define MODULE led
|
||||
#include <caf/events/module_state_event.h>
|
||||
|
||||
#include <zephyr/drivers/gpio.h>
|
||||
#include <zephyr/logging/log.h>
|
||||
LOG_MODULE_REGISTER(MODULE);
|
||||
|
||||
static const struct gpio_dt_spec status_led = GPIO_DT_SPEC_GET(DT_PATH(led_0, chan0), gpios);
|
||||
|
||||
static void led_module_start(void)
|
||||
{
|
||||
if (!gpio_is_ready_dt(&status_led)) {
|
||||
LOG_ERR("LED GPIO controller is not ready");
|
||||
return;
|
||||
}
|
||||
|
||||
int err = gpio_pin_configure_dt(&status_led, GPIO_OUTPUT_ACTIVE);
|
||||
if (err) {
|
||||
LOG_ERR("Failed to turn on LED (err: %d)", err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_INF("LED is on");
|
||||
}
|
||||
|
||||
static bool app_event_handler(const struct app_event_header *aeh)
|
||||
{
|
||||
/* 仅处理 module_state_event,其他事件交给系统继续分发。 */
|
||||
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)) {
|
||||
led_module_start();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
APP_EVENT_LISTENER(led_module, app_event_handler);
|
||||
APP_EVENT_SUBSCRIBE(led_module, module_state_event);
|
||||
|
||||
Reference in New Issue
Block a user