feat(keyboard): 添加LED模块支持
- 在CMakeLists.txt中添加src/modules/led_module.c源文件 - 创建app.overlay设备树配置文件,启用GPIO0、GPIO1、GPIOTE和LED_0 - 在prj.conf中启用CONFIG_GPIO配置选项 - 实现led_module.c,包含LED GPIO控制逻辑和CAF事件处理
This commit is contained in:
@@ -3,4 +3,5 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
|
|||||||
|
|
||||||
project(new_kbd)
|
project(new_kbd)
|
||||||
|
|
||||||
target_sources(app PRIVATE src/main.c)
|
target_sources(app PRIVATE src/main.c
|
||||||
|
src/modules/led_module.c)
|
||||||
|
|||||||
16
app.overlay
Normal file
16
app.overlay
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/{};
|
||||||
|
&gpio0 {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&gpio1 {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&gpiote {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
|
|
||||||
|
&led_0 {
|
||||||
|
status = "okay";
|
||||||
|
};
|
||||||
3
prj.conf
3
prj.conf
@@ -1,3 +1,4 @@
|
|||||||
CONFIG_CAF=y
|
CONFIG_CAF=y
|
||||||
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
||||||
CONFIG_LOG=y
|
CONFIG_LOG=y
|
||||||
|
CONFIG_GPIO=y
|
||||||
|
|||||||
43
src/modules/led_module.c
Normal file
43
src/modules/led_module.c
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#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