feat: 实现事件驱动应用框架,支持前台/后台分离、事件优先级和CPU占用控制

This commit is contained in:
2026-07-10 11:30:48 +08:00
parent 00c00fb60e
commit b4fabf85b5
11 changed files with 2487 additions and 541 deletions

121
App/event.c Normal file
View File

@@ -0,0 +1,121 @@
#include "event.h"
/* =========================================================================
* 事件队列内部变量
* ========================================================================= */
// 环形缓冲区存储数组
static SystemEvent event_queue[EVENT_QUEUE_DEPTH];
// 队列读写指针
static u8 queue_head = 0; // 读指针:指向即将读取的位置
static u8 queue_tail = 0; // 写指针:指向即将写入的位置
static u8 queue_count = 0; // 当前队列中的事件数量
// 队列锁标志(用于中断保护)
static bit queue_locked = 0;
/* =========================================================================
* 事件队列核心实现
* ========================================================================= */
void EventQueue_Init(void)
{
queue_head = 0;
queue_tail = 0;
queue_count = 0;
queue_locked = 0;
}
bit EventQueue_IsEmpty(void)
{
return (queue_count == 0);
}
bit EventQueue_IsFull(void)
{
return (queue_count >= EVENT_QUEUE_DEPTH);
}
u8 EventQueue_GetCount(void)
{
return queue_count;
}
bit EventQueue_Push(SystemEvent evt)
{
// 队列已满,丢弃事件
if (EventQueue_IsFull())
{
return 0;
}
// 禁用中断保护队列操作
EA = 0;
// 将事件写入环形缓冲区
event_queue[queue_tail] = evt;
// 更新写指针(循环递增)
queue_tail++;
if (queue_tail >= EVENT_QUEUE_DEPTH)
{
queue_tail = 0;
}
// 更新事件计数
queue_count++;
// 恢复中断
EA = 1;
return 1;
}
SystemEvent EventQueue_Pop(void)
{
SystemEvent evt;
// 初始化返回的空事件
evt.key_event = KEY_EVENT_NONE;
evt.priority = EVENT_PRIORITY_LOW;
evt.extra_data = 0;
evt.extra_size = 0;
// 队列为空,返回空事件
if (EventQueue_IsEmpty())
{
return evt;
}
// 禁用中断保护队列操作
EA = 0;
// 从队列头部读取事件
evt = event_queue[queue_head];
// 更新读指针(循环递增)
queue_head++;
if (queue_head >= EVENT_QUEUE_DEPTH)
{
queue_head = 0;
}
// 更新事件计数
queue_count--;
// 恢复中断
EA = 1;
return evt;
}
// 外部声明:应用管理器的事件分发函数
extern void AppManager_DispatchEvent(SystemEvent evt);
void EventQueue_EmergencyDispatch(SystemEvent evt)
{
// 紧急事件直接调用应用管理器分发,跳过队列
// 这种方式用于SOS等最高优先级事件确保立即响应
AppManager_DispatchEvent(evt);
}