帮我写一段C语言程序,单片机stm32f429 ucosiii,动作执行器,以面向对象的形式完成代码,提供C和H文件,
有以下功能:每个动作执行器包含一个ucosiii定时器对象,动作执行状态,动作总数,已执行动作数量,动作组数据,每个动作有一个执行时间点,到时间即执行动作,要注意一点,有些动作可能执行时间相同(这部分要在代码中标出注释),你需要想一个数据结构来管理动作组数据。动作组结构体包含执行的动作类型(类型包括开关量操作、模拟量输出、自定义函数指针等),动作执行时间点单位秒,执行时的参数指针。还要有动作执行器操作函数,比如开始执行,暂停执行,继续执行,停止执行函数,获取执行器状态。编码时状态要用枚举型表示。ucosiii定时器每100ms调度一次。其他区要求:不要每次调度动作都遍历一遍动作数组,要想办法减少运行时间。时间单位统一使用毫秒(ms)
// action_executor.h
#ifndef ACTION_EXECUTOR_H
#define ACTION_EXECUTOR_H
#include "os.h"
// 执行器状态枚举
typedef enum {
EXECUTOR_IDLE = 0, // 空闲
EXECUTOR_RUNNING, // 运行中
EXECUTOR_PAUSED, // 暂停
EXECUTOR_COMPLETED // 完成
} ExecutorState;
// 动作类型枚举
typedef enum {
ACTION_DIGITAL = 0, // 开关量操作
ACTION_ANALOG, // 模拟量输出
ACTION_CUSTOM // 自定义函数
} ActionType;
// 动作结构体
typedef struct {
ActionType type; // 动作类型
uint32_t executeTime; // 执行时间点(ms)
void* params; // 参数指针
void (*customFunc)(void*); // 自定义函数指针
} Action;
// 动作组结构体
typedef struct {
Action* actions; // 动作数组
uint16_t totalActions; // 动作总数
uint16_t executedActions; // 已执行动作数
uint16_t currentIndex; // 当前执行索引
OS_TMR* timer; // ucosiii定时器
ExecutorState state; // 执行器状态
uint32_t currentTime; // 当前时间(ms)
} ActionExecutor;
// 初始化执行器
ActionExecutor* ActionExecutor_Create(Action* actions, uint16_t actionCount);
// 开始执行
void ActionExecutor_Start(ActionExecutor* executor);
// 暂停执行
void ActionExecutor_Pause(ActionExecutor* executor);
// 继续执行
void ActionExecutor_Resume(ActionExecutor* executor);
// 停止执行
void ActionExecutor_Stop(ActionExecutor* executor);
// 获取执行器状态
ExecutorState ActionExecutor_GetState(ActionExecutor* executor);
// 销毁执行器
void ActionExecutor_Destroy(ActionExecutor* executor);
#endif // ACTION_EXECUTOR_H
// action_executor.c
#include "action_executor.h"
#include <stdlib.h>
#define TIMER_PERIOD_MS 100 // 定时器周期(ms)
// 定时器回调函数
static void TimerCallback(void* p_tmr, void* p_arg) {
ActionExecutor* executor = (ActionExecutor*)p_arg;
if (executor->state != EXECUTOR_RUNNING) {
return;
}
executor->currentTime += TIMER_PERIOD_MS;
// 检查当前索引的动作是否需要执行
while (executor->currentIndex < executor->totalActions) {
Action* currentAction = &executor->actions[executor->currentIndex];
// 如果当前动作的执行时间未到,退出循环
if (currentAction->executeTime > executor->currentTime) {
break;
}
// 注意:这里可能有多个动作的执行时间相同
// 所以使用while循环而不是if语句
// 根据动作类型执行相应操作
switch (currentAction->type) {
case ACTION_DIGITAL:
{
uint8_t* value = (uint8_t*)currentAction->params;
// 执行数字输出操作
// TODO: 添加具体的硬件操作代码
}
break;
case ACTION_ANALOG:
{
float* value = (float*)currentAction->params;
// 执行模拟量输出操作
// TODO: 添加具体的硬件操作代码
}
break;
case ACTION_CUSTOM:
if (currentAction->customFunc) {
currentAction->customFunc(currentAction->params);
}
break;
}
executor->executedActions++;
executor->currentIndex++;
// 检查是否所有动作都已执行完成
if (executor->executedActions >= executor->totalActions) {
executor->state = EXECUTOR_COMPLETED;
break;
}
}
}
ActionExecutor* ActionExecutor_Create(Action* actions, uint16_t actionCount) {
if (!actions || actionCount == 0) {
return NULL;
}
ActionExecutor* executor = (ActionExecutor*)malloc(sizeof(ActionExecutor));
if (!executor) {
return NULL;
}
executor->actions = actions;
executor->totalActions = actionCount;
executor->executedActions = 0;
executor->currentIndex = 0;
executor->state = EXECUTOR_IDLE;
executor->currentTime = 0;
OS_ERR err;
executor->timer = OSTmrCreate(0, // 初始延时
TIMER_PERIOD_MS, // 周期
OS_OPT_TMR_PERIODIC, // 周期性定时器
TimerCallback, // 回调函数
executor, // 回调参数
"ActionExecutorTmr", // 定时器名称
&err); // 错误码
if (err != OS_ERR_NONE) {
free(executor);
return NULL;
}
return executor;
}
void ActionExecutor_Start(ActionExecutor* executor) {
if (!executor || executor->state != EXECUTOR_IDLE) {
return;
}
OS_ERR err;
OSTmrStart(executor->timer, &err);
if (err == OS_ERR_NONE) {
executor->state = EXECUTOR_RUNNING;
}
}
void ActionExecutor_Pause(ActionExecutor* executor) {
if (!executor || executor->state != EXECUTOR_RUNNING) {
return;
}
OS_ERR err;
OSTmrStop(executor->timer, OS_OPT_TMR_NONE, NULL, &err);
if (err == OS_ERR_NONE) {
executor->state = EXECUTOR_PAUSED;
}
}
void ActionExecutor_Resume(ActionExecutor* executor) {
if (!executor || executor->state != EXECUTOR_PAUSED) {
return;
}
OS_ERR err;
OSTmrStart(executor->timer, &err);
if (err == OS_ERR_NONE) {
executor->state = EXECUTOR_RUNNING;
}
}
void ActionExecutor_Stop(ActionExecutor* executor) {
if (!executor || executor->state == EXECUTOR_IDLE) {
return;
}
OS_ERR err;
OSTmrStop(executor->timer, OS_OPT_TMR_NONE, NULL, &err);
if (err == OS_ERR_NONE) {
executor->state = EXECUTOR_IDLE;
executor->currentIndex = 0;
executor->executedActions = 0;
executor->currentTime = 0;
}
}
ExecutorState ActionExecutor_GetState(ActionExecutor* executor) {
return executor ? executor->state : EXECUTOR_IDLE;
}
void ActionExecutor_Destroy(ActionExecutor* executor) {
if (!executor) {
return;
}
OS_ERR err;
OSTmrDel(executor->timer, &err);
free(executor);
}
用例如下
// 创建动作数组
Action actions[] = {
{ACTION_DIGITAL, 1000, &digitalValue1, NULL}, // 1秒
{ACTION_ANALOG, 1000, &analogValue1, NULL}, // 1秒,与上一个动作同时执行
{ACTION_CUSTOM, 2500, customParam1, CustomFunction1}, // 2.5秒
// ... 更多动作
};
// 创建执行器
ActionExecutor* executor = ActionExecutor_Create(actions, sizeof(actions)/sizeof(Action));