时驱函数的进阶:定时器链表

时驱函数的进阶:定时器链表

前一篇帖子我写了时驱函数的设计本质是一种前后台程序,本文依旧如故。
后台程序放到定时器里面,周期性的调用。这个程序程序是扫描一个链表,链表中每一个成员 噢 应该说没一个节点都设计好数据的结构体,到达设定的时间就执行回调函数。
功能:完成约定时间调用回调函数。

头文件
#ifndef _TIMELIST_H_
#define _TIMELIST_H_
#include "sys.h"
//自己部分 可以放在.C 不对别人提供
typedef struct  _time
{
        void        *next;//指向后一个
        uint8_t     handle;//自己的ID号
        uint8_t     start;//开关 1开0关
        uint32_t    cnt;//累计次数的变量
        uint32_t    time_out;//设定次数的常量
        void        (*fun)(void);//计数达到以后做啥的函数
}time_type;
typedef void    (*time_call_back)(void);//计数达到以后做啥的函数

void timer_isr( void );//这个是关键  我自己部分是如何实现的 就是靠它了



//别人部分 这就是给别人的方法 创建 后面的停止可开始 其实就是在上面寻找节点然后start开关
typedef struct
{
    uint8_t (*creat) (  uint32_t time_out ,uint8_t start, time_call_back call_back);
    uint8_t (*stop)  (  uint8_t handle);
    uint8_t (*start) (  uint8_t handle);
}time_ops_type;
extern time_ops_type   timer;



#endif

.c


#include "timelist.h"
#include "malloc.h"        
#define NULL 0 
enum
{
	false,
	true
};
uint8_t        timerTaskId = 0;//全局变量 每个节点的ID号从0开始
time_type      *time = NULL; //链表的头 第一个节点


//这个时驱函数就是在扫描链表 有活儿干 就进去
void timer_isr( void )
{
    time_type  *priv = time;

    while( priv != NULL )
    {
        if( priv->start)//这个节点是开的 就进去 否则pass
        {
            if( ++priv->cnt >= priv->time_out)//进来累计一次 直达到达约定设定值
            {
                priv->cnt = 0;
                if(priv->fun != NULL)        
                priv->fun();
            }
        }
        priv = priv->next;      
    }
}

//只有malloc没有free 我没有释放
//返回void*

void *timer_malloc(int size)
{
     return mymalloc(SRAMIN,sizeof(time_type));
}

uint8_t timer_stop_time(uint8_t handle)
{
    time_type *priv = time;
        
    while( priv != NULL )
    {
        if( priv->handle == handle)
        {
            priv->start = false;//找到 然后关闭
            priv->cnt = 0;
            return true;
        }
        priv = priv->next;     
    }  
    
    return false;
}

uint8_t timer_start_time(uint8_t handle)
{
    time_type *priv = time;
        
    while( priv != NULL )
    {
        if( priv->handle == handle)
        {
            priv->start = true;//找到 然后打开
            return true;
        }
        priv = priv->next;     
    }  
    
    return false;
}

uint8_t timer_register_isr(  uint32_t time_out ,uint8_t start, time_call_back call_back)
{
        time_type *priv;
        time_type *this;

        this = (time_type *)timer_malloc(sizeof(time_type));
        if( this != NULL)
        {

                this->cnt = 0;
                this->start = start;
                this->handle = timerTaskId++;
                this->time_out = time_out;
                this->fun = call_back;
                this->next = NULL;
				if( time == NULL)
                {
                         time = this;
                }
                else
                {
                        priv = time;
                        while( priv->next != NULL )        priv = priv->next;
                        priv->next = this;
                }    
        }
        else
        {
                return 0xFF;
        }


    return (this->handle);

}

//KEIL不支持该写法??
//time_ops_type  timer =
//{
//    .creat = timer_register_isr,
//    .stop  = timer_stop_time , 
//    .start = timer_start_time,
//};


time_ops_type  timer =
{
    timer_register_isr,
    timer_stop_time , 
    timer_start_time,
};

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值