167 lines
2.4 KiB
C
167 lines
2.4 KiB
C
|
|
#include "elec_task_slot.h"
|
|
#include "board.h"
|
|
#include "stdlib.h"
|
|
#include "string.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
此文件定义动态任务插入的机制
|
|
|
|
*/
|
|
|
|
|
|
#define ERR_TABLE_SIZE 10
|
|
|
|
typedef struct _task_node{
|
|
task_def task;
|
|
struct _task_node *next;
|
|
}task_node;
|
|
|
|
|
|
|
|
|
|
typedef struct{
|
|
int node_num;
|
|
task_node *head;
|
|
task_node *current;
|
|
int err_num;
|
|
uint8_t err_table[ERR_TABLE_SIZE];
|
|
}task_slot_def;
|
|
|
|
|
|
|
|
static task_slot_def *g_task_slot;
|
|
|
|
void task_slot_delete(void)
|
|
{
|
|
//task_slot_def *t=app_variable("task_slot",0,0);
|
|
task_slot_def *t=g_task_slot;
|
|
if(t){
|
|
task_node *n=t->head;
|
|
task_node *old=0;
|
|
while(n){
|
|
old=n;
|
|
n=n->next;
|
|
free(old);
|
|
}
|
|
free(t);
|
|
}
|
|
g_task_slot=0;
|
|
}
|
|
|
|
|
|
// 初始化
|
|
void *task_slot_init(void)
|
|
{
|
|
//task_slot_def *t=app_variable("task_slot",0,0);
|
|
task_slot_def *t=g_task_slot;
|
|
if(t==0){
|
|
t=calloc(1,sizeof(task_slot_def));
|
|
// app_variable("task_slot",t,0);
|
|
}
|
|
t->current=t->head;
|
|
g_task_slot=t;
|
|
return t;
|
|
}
|
|
|
|
|
|
|
|
// 添加一个任务
|
|
int task_slot_add_item(void *context,task_def *item)
|
|
{
|
|
task_slot_def *t=context;
|
|
if(t==0){
|
|
return -1;
|
|
}
|
|
task_node **n=&t->head;
|
|
task_node *tail;
|
|
tail=calloc(1,sizeof(task_node));
|
|
memcpy(&tail->task,item,sizeof(task_def));
|
|
while(*n){
|
|
n=&(*n)->next;
|
|
}
|
|
(*n)=tail;
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
// 找到下一个slot
|
|
task_def *task_slot_next(void *context,uint8_t slot_index)
|
|
{
|
|
task_slot_def *t=context;
|
|
if(t==0){
|
|
return 0;
|
|
}
|
|
task_def *n;
|
|
while(n=task_slot_next_item(t),n!=0){
|
|
if(n->slot_index==slot_index){
|
|
return n;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
// 找到下一个
|
|
task_def *task_slot_next_item(void *context)
|
|
{
|
|
task_slot_def *t=context;
|
|
if(t==0){
|
|
return 0;
|
|
}
|
|
task_node *n=0;
|
|
if(t->current){
|
|
n=t->current;
|
|
t->current=n->next;
|
|
return &n->task;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
int task_slot_add_err(void *context,uint8_t err)
|
|
{
|
|
task_slot_def *t=context;
|
|
if(t==0){
|
|
return -1;
|
|
}
|
|
int index=-1;
|
|
for(int i=0;i<ERR_TABLE_SIZE;i++){
|
|
if(t->err_table[i]==0){
|
|
if(index<0) index=i;
|
|
}
|
|
if(t->err_table[i]==err){
|
|
return 0;
|
|
}
|
|
}
|
|
if(index>=0){
|
|
t->err_table[index]=err;
|
|
t->err_num++;
|
|
return 0;
|
|
}
|
|
return -2;
|
|
}
|
|
|
|
|
|
|
|
// 获取异常代码表
|
|
int task_slot_err_table(void *context,uint8_t **table)
|
|
{
|
|
task_slot_def *t=context;
|
|
if(t==0){
|
|
return 0;
|
|
}
|
|
if(table) *table=t->err_table;
|
|
return t->err_num;
|
|
}
|
|
|
|
|
|
|