feat: 迁移主应用程序以使用CAF架构
- 在prj.conf中启用CAF、堆内存池、日志和断言配置 - 将main.c重构为使用应用事件管理器和模块状态事件 - 移除GPIO LED闪烁功能,改为初始化事件管理系统 - 添加日志模块注册和错误处理
This commit is contained in:
5
prj.conf
5
prj.conf
@@ -1 +1,4 @@
|
|||||||
CONFIG_GPIO=y
|
CONFIG_CAF=y
|
||||||
|
CONFIG_HEAP_MEM_POOL_SIZE=2048
|
||||||
|
CONFIG_LOG=y
|
||||||
|
CONFIG_ASSERT=y
|
||||||
|
|||||||
48
src/main.c
48
src/main.c
@@ -1,48 +1,18 @@
|
|||||||
/*
|
#include <app_event_manager.h>
|
||||||
* Copyright (c) 2016 Intel Corporation
|
|
||||||
*
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <stdio.h>
|
#define MODULE main
|
||||||
#include <zephyr/kernel.h>
|
#include <caf/events/module_state_event.h>
|
||||||
#include <zephyr/drivers/gpio.h>
|
|
||||||
|
|
||||||
/* 1000 msec = 1 sec */
|
#include <zephyr/logging/log.h>
|
||||||
#define SLEEP_TIME_MS 1000
|
LOG_MODULE_REGISTER(MODULE);
|
||||||
|
|
||||||
/* The devicetree node identifier for the "led0" alias. */
|
|
||||||
#define LED0_NODE DT_ALIAS(led0)
|
|
||||||
|
|
||||||
/*
|
|
||||||
* A build error on this line means your board is unsupported.
|
|
||||||
* See the sample documentation for information on how to fix this.
|
|
||||||
*/
|
|
||||||
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED0_NODE, gpios);
|
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
int ret;
|
if (app_event_manager_init()) {
|
||||||
bool led_state = true;
|
LOG_ERR("Application Event Manager not initialized");
|
||||||
|
} else {
|
||||||
if (!gpio_is_ready_dt(&led)) {
|
module_set_state(MODULE_STATE_READY);
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
|
|
||||||
if (ret < 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (1) {
|
|
||||||
ret = gpio_pin_toggle_dt(&led);
|
|
||||||
if (ret < 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
led_state = !led_state;
|
|
||||||
printf("LED state: %s\n", led_state ? "ON" : "OFF");
|
|
||||||
k_msleep(SLEEP_TIME_MS);
|
|
||||||
}
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user