131 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			131 lines
		
	
	
		
			1.8 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;
 | 
						|
 | 
						|
 | 
						|
 | 
						|
void task_slot_delete(void)
 | 
						|
{
 | 
						|
  task_slot_def *t=app_variable("task_slot",0,0);
 | 
						|
  if(t){
 | 
						|
    task_node *n=t->head;
 | 
						|
    task_node *old=0;
 | 
						|
    while(n){
 | 
						|
      old=n;
 | 
						|
      n=n->next;
 | 
						|
      free(old);
 | 
						|
    }
 | 
						|
    free(t);
 | 
						|
  }
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
// 初始化
 | 
						|
void *task_slot_init(void)
 | 
						|
{
 | 
						|
  task_slot_def *t=app_variable("task_slot",0,0);
 | 
						|
  if(t==0){
 | 
						|
    t=calloc(1,sizeof(task_slot_def));
 | 
						|
    app_variable("task_slot",t,0);
 | 
						|
  }
 | 
						|
  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,item,sizeof(task_node));
 | 
						|
  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_node *n;
 | 
						|
  while(t->current){
 | 
						|
    n=t->current;
 | 
						|
    t->current=n->next;
 | 
						|
    if(n->task.slot_index==slot_index){
 | 
						|
      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;
 | 
						|
}
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 |