diff --git a/CMakeLists.txt b/CMakeLists.txt index a0fb22b..4541ccc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,4 +3,5 @@ find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(new_kbd) -target_sources(app PRIVATE src/main.c) +target_sources(app PRIVATE src/main.c + src/modules/led_module.c) diff --git a/app.overlay b/app.overlay new file mode 100644 index 0000000..010064b --- /dev/null +++ b/app.overlay @@ -0,0 +1,16 @@ +/{}; +&gpio0 { + status = "okay"; +}; + +&gpio1 { + status = "okay"; +}; + +&gpiote { + status = "okay"; +}; + +&led_0 { + status = "okay"; +}; \ No newline at end of file diff --git a/prj.conf b/prj.conf index cce25cf..4c27504 100644 --- a/prj.conf +++ b/prj.conf @@ -1,3 +1,4 @@ CONFIG_CAF=y CONFIG_HEAP_MEM_POOL_SIZE=2048 -CONFIG_LOG=y \ No newline at end of file +CONFIG_LOG=y +CONFIG_GPIO=y diff --git a/src/modules/led_module.c b/src/modules/led_module.c new file mode 100644 index 0000000..5f2c33c --- /dev/null +++ b/src/modules/led_module.c @@ -0,0 +1,43 @@ +#include +#define MODULE led +#include + +#include +#include +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); +