快速入门
EventOS Nano非常简单,除源码外,只需要实现三个代码,就可以使用EventOS Nano编写程序
- main.c main函数,初始化和启动EventOS
- eos_port.c 如EventOS在特定平台上的接口实现,就是EventOS Nano移植的相关代码
- eos_led.c LED的闪烁状态机。LED灯闪烁,类似于单片机界的hello world
如下对每一个文件进行详细说明:
1.main.c
EventOS初始化->订阅表初始化->LED状态机初始化->EventOS启动
/* include ------------------------------------------------------------------ */
#include "eventos.h" // EventOS Nano头文件
#include "event_def.h" // 事件主题的枚举
#include "eos_led.h" // LED灯闪烁状态机
/* define ------------------------------------------------------------------- */
static eos_u32_t eos_sub_table[Event_Max]; // 订阅表数据空间
/* main function ------------------------------------------------------------ */
int main(void)
{
if (SysTick_Config(SystemCoreClock / 1000) != 0)
while (1);
eos_init(); // EventOS初始化
#if (EOS_USE_PUB_SUB != 0)
eos_sub_init(eos_sub_table, Event_Max); // 订阅表初始化
#endif
#if (EOS_USE_SM_MODE != 0)
eos_sm_led_init(); // 状态机模式demo初始化
#endif
eos_reactor_led_init(); // reactor模式demo初始化
eos_run(); // EventOS启动
return 0;
}
在QP和EventOS Nano中,事件主题的定义是使用枚举来定义的。但反观很多事件驱动和消息驱动的应用,如ROS和MQTT的实现,其事件(消息)的定义,是使用字符串的。二者各有利弊,枚举的好处是节省RAM和ROM,适合资源受限的场合,字符串的好处是,使用方便,不产生耦合,利于实现分布式。
事件主题event_def.h的定义如下:
#include "eventos.h"
enum {
Event_Test = Event_User, // 事件主题的定义从Event_User开始,小于Event_User的是系统事件。
Event_Time_500ms,
Event_Time_1000ms,
Event_ActEnd,
Event_Max
};
2.eos_port.c
eos_port.c是硬件或平台依赖文件
#include "eventos.h"
// 进入临界区,关闭全局中断
void eos_port_critical_enter(void)
{
__disable_irq(); // 当IDE为MDK时,面向ARM Cortex-M系列,可以参考实现
}
// 退出临界区,开启全局中断
void eos_port_critical_exit(void)
{
__enable_irq(); // 当IDE为MDK时,面向ARM Cortex-M系列,可以参考实现
}
// 断言接口,EventOS Nano推崇防御式编程,内部运用了大量的断言检查程序运行的合法性
eos_u32_t eos_error_id = 0;
void eos_port_assert(eos_u32_t error_id)
{
// 断言信息的打印。
SEGGER_RTT_printf(0, "------------------------------------\n");
SEGGER_RTT_printf(0, "ASSERT >>> Module: EventOS Nano, ErrorId: %d.\n", error_id);
SEGGER_RTT_printf(0, "------------------------------------\n");
eos_error_id = error_id;
while (1)
{
/* User code */
}
}
// 空闲回调函数:当EventOS Nano没有任何事件需要处理的时候,会调用此函数
// 可以实现:硬件设备的轮训、随机数的计算等,如果没有,实现一个空函数即可
void eos_hook_idle(void)
{
}
// 启动回调函数
// 在EventOS Nano初始化完毕,且状态机运行之前,会调用这个回调函数。这里可以对硬件设备进行初始化、对功能模块或者中间件进行初始化等。
void eos_hook_start(void)
{
// 硬件初始化
/* User code */
// 功能模块初始化
/* User code */
}
// 停止回调函数
// 在EventOS Nano进入断言后,或者用户调用eventos_stop函数将EventOS停止后,此回调函数会被调用
// 这个函数主要用于有序关闭敏感设备(关闭电机运行),或者向外界提供信息(打开报警器,显示报警界面,或者打印报警信息等)。
void eos_hook_stop(void)
{
// 有序关闭敏感设备,如果没有可以注释掉
motor_stop(Motor_Left);
motor_stop(Motor_Right);
// 显示必要信息
led_set_status(LedStatus_Stop);
}
3.eos_led_sm.c和eos_led_sm.h
eos_led.c中描述了状态机模式是如何使用的,eos_sm_start->state_init->state_off->state_on,state_on和state_off根据Event_Time_500ms发布时机切换状态。
/* include ------------------------------------------------------------------ */
#include "eos_led.h" // 模块头文件
#include "eventos.h" // EventOS头文件
#include "event_def.h" // 事件定义头文件
#include // 标准输入输出库
/* data structure ----------------------------------------------------------- */
typedef struct eos_led_tag { // LED类
eos_sm_t super;
eos_bool_t status;
} eos_led_t;
static eos_led_t led; // led对象,单例模式
/* static state function ---------------------------------------------------- */
// 初始状态
static eos_ret_t state_init(eos_led_t * const me, eos_event_t const * const e);
// Led的ON状态
static eos_ret_t state_on(eos_led_t * const me, eos_event_t const * const e);
// Led的Off状态
static eos_ret_t state_off(eos_led_t * const me, eos_event_t const * const e);
/* api ---------------------------------------------------- */
void eos_led_init(void)
{
static eos_u32_t queue[32]; // 事件队列
eos_sm_init(&led.super, 1, queue, 32); // 状态机初始化
// 状态机启动,以state_init作为初始状态。
eos_sm_start(&led.super, EOS_STATE_CAST(state_init));
led.status = 0;
}
/* static state function ---------------------------------------------------- */
static eos_ret_t state_init(eos_led_t * const me, eos_event_t const * const e)
{
// 将Event_Time_500ms型事件加入到订阅表中,即订阅Event_Time_500ms事件
EOS_EVENT_SUB(Event_Time_500ms);
// 发布Event_Time_500ms周期事件,并设置发布周期为500ms
eos_event_pub_period(Event_Time_500ms, 500);
// 跳转到state_off函数
return EOS_TRAN(state_off);
}
/*每500ms,订阅的500ms事件会发布一次,切换on/off状态,并进入对应函数的Event_Enter状态执行*/
static eos_ret_t state_on(eos_led_t * const me, eos_event_t const * const e)
{
switch (e->topic) {
case Event_Enter: // 状态state_on的进入事件
printf("State On!\n");
me->status = 1;
return EOS_Ret_Handled;
case Event_Time_500ms: // 收到Event_Time_500ms,跳转到state_off
return EOS_TRAN(state_off);
default:
return EOS_SUPER(eos_state_top);
}
}
static eos_ret_t state_off(eos_led_t * const me, eos_event_t const * const e)
{
switch (e->topic) {
case Event_Enter: // 状态state_off的进入事件
printf("State Off!\n");
me->status = 0;
return EOS_Ret_Handled;
case Event_Time_500ms: // 收到Event_Time_500ms,跳转到state_on
return EOS_TRAN(state_on);
default:
return EOS_SUPER(eos_state_top);
}
}
4.eos_led_reactor.c和eos_led_reactor.h
描述reacotor模型的使用方法
/* include ------------------------------------------------------------------ */
#include "eos_led.h"
#include "eventos.h"
#include "event_def.h"
#include
/* data structure ----------------------------------------------------------- */
typedef struct eos_reactor_led_tag {
eos_reactor_t super;
eos_u8_t status;
} eos_reactor_led_t;
eos_reactor_led_t actor_led;
/* static event handler ----------------------------------------------------- */
static void led_e_handler(eos_reactor_led_t * const me, eos_event_t const * const e);
/* api ---------------------------------------------------- */
void eos_reactor_led_init(void)
{
eos_reactor_init(&actor_led.super, 2, EOS_NULL);
eos_reactor_start(&actor_led.super, EOS_HANDLER_CAST(led_e_handler)); // 绑定led_e_handler句柄
actor_led.status = 0;
#if (EOS_USE_PUB_SUB != 0)
eos_event_sub((eos_actor_t *)(&actor_led), Event_Time_1000ms); // 订阅Event_Time_1000ms事件,即将Event_Time_1000ms加入到订阅表中
#endif
#if (EOS_USE_TIME_EVENT != 0)
eos_event_pub_period(Event_Time_1000ms, 1000); // 发布Event_Time_1000ms period 事件,设置发布周期为100ms
#endif
}
/* static state function ---------------------------------------------------- */
static void led_e_handler(eos_reactor_led_t * const me, eos_event_t const * const e)
{
if (e->topic == Event_Time_1000ms) {
// 只要Event_Time_1000ms到来,me->status切换状态
me->status = (me->status == 0) ? 1 : 0;
}
}
Original: https://www.cnblogs.com/Wangzx000/p/16491863.html
Author: _Wangzx
Title: 【EventOS Nano】EventOS Nano使用快速入门
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607509/
转载文章受原作者版权保护。转载请注明原作者出处!