#ifndef _MODEL_SCHEDULE_H_
#define _MODEL_SCHEDULE_H_
#define MODEL_SCHEDULE_IDLE 0x00000000
#define MODEL_SCHEDULE_OCCUPY 0x5A555A55
#define MODEL_SCHEDULE_NUM 10
typedef struct model_schedule_linked_list_unit
void (*pfun)(void); // 事件内容
struct model_schedule_linked_list_unit *next; // 下一个事件
} model_schedule_linked_list_unit;
model_schedule_linked_list_unit *head;
model_schedule_linked_list_unit linked_list_space[MODEL_SCHEDULE_NUM]; // 下一个事件
} model_schedule_linked_list;
bool model_schedule_push(model_schedule_linked_list *pLinked_list, void (*Pfun)(void), int delayTime);
void model_schedule_check(model_schedule_linked_list *pLinked_list);
#include "model_schedule.h"
/// @param pLinked_list 事件容器
/// @param delayTime 定时时间
bool model_schedule_push(model_schedule_linked_list *pLinked_list, void (*Pfun)(void), int delayTime)
for(index=0;index<MODEL_SCHEDULE_NUM;index++) //寻找空闲空间
if(pLinked_list->linked_list_space[index].idleFlag!=MODEL_SCHEDULE_OCCUPY) break; //找到空闲空间
if(index>=MODEL_SCHEDULE_NUM) return FALSE; //未找到空闲空间,返回失败
pLinked_list->linked_list_space[index].idleFlag = MODEL_SCHEDULE_OCCUPY; //占用资源
pLinked_list->linked_list_space[index].pfun = Pfun; //添加事件
pLinked_list->linked_list_space[index].timeNode = g_GlobalReferenceClock+delayTime; //添加时间节点
model_schedule_linked_list_unit **ppCurrent_unit = &pLinked_list->head; //定义临时变量
if((*ppCurrent_unit)==NULL) //如果链表为空
(*ppCurrent_unit) = &pLinked_list->linked_list_space[index];
if(pLinked_list->linked_list_space[index].timeNode < pLinked_list->head->timeNode) //如果插入值为最小值
pLinked_list->linked_list_space[index].next = (*ppCurrent_unit);
(*ppCurrent_unit) = &pLinked_list->linked_list_space[index];
if((*ppCurrent_unit)->next==NULL) //经过比较后遇到链表尾巴
(*ppCurrent_unit)->next = &pLinked_list->linked_list_space[index];
if(pLinked_list->linked_list_space[index].timeNode < (*ppCurrent_unit)->next->timeNode) //插入中间值
pLinked_list->linked_list_space[index].next = (*ppCurrent_unit)->next;
(*ppCurrent_unit)->next = &pLinked_list->linked_list_space[index];
ppCurrent_unit = &(*ppCurrent_unit)->next; //指向下一个节点
void model_schedule_check(model_schedule_linked_list *pLinked_list)
while (pLinked_list->head!=NULL) //如果表头不为空
if(g_GlobalReferenceClock > pLinked_list->head->timeNode)
if(pLinked_list->head->pfun!=NULL) //事件不为空
pLinked_list->head->pfun(); //定时事件执行
model_schedule_linked_list_unit *pCurrent_unit = pLinked_list->head; //保留原有列表地址,
pLinked_list->head=pLinked_list->head->next; //指向下一个事件
memset(pCurrent_unit, 0, sizeof(model_schedule_linked_list_unit)); //释放资源
void model_schedule_printf_timeNode(model_schedule_linked_list *pLinked_list)
model_schedule_linked_list_unit *pCurrent_unit = pLinked_list->head;
while (pCurrent_unit!=NULL)
printf("timeValue:%d\r\n", pCurrent_unit->timeNode-g_GlobalReferenceClock);
pCurrent_unit = pCurrent_unit->next;