zoukankan      html  css  js  c++  java
  • 通用定时器设计(2)

    上一篇随笔,【嵌入式程序设计】——通用定时器设计(1),使用的链表来管理定时器,稍显复杂。

    现在直接将链表简化成数组,清爽很多了,简洁就是美。

    main.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    #include <stdint.h>
    #include <stddef.h>
    
    #define  OS_TIMER_MAX  32
    
    struct timer_t
    {
        void (*pfun)(void* para);
        unsigned int delay;
        unsigned int period;
        bool run;
        void* para;
    };
    struct timer_t timer_list[OS_TIMER_MAX];
    
    typedef uint8_t timer_id;
    
    void timer_init(void)
    {
        uint8_t index;
    
        for (index = 0; index < OS_TIMER_MAX; index++)
        {
            timer_list[index].pfun = NULL;
        }
    }
    
    timer_id timer_creat(void(*pFunction)(void* para),
                         const unsigned int delay,
                         const unsigned int period,
                         bool run,
                         void* para)
    {
        uint8_t index = 0;
    
        while ((timer_list[index].pfun != NULL) && (index < OS_TIMER_MAX))
        {
            index++;
        }
    
        printf("time index %d
    ",index);
        timer_list[index].pfun  = pFunction;
        timer_list[index].delay  = delay;
        timer_list[index].period  = period;
        timer_list[index].run  = run;
        timer_list[index].para = para;
        return index;
    }
    
    bool timer_delete(const timer_id index)
    {
        if(index >= OS_TIMER_MAX) return false;
    
        if (timer_list[index].pfun == NULL)
        {
            return false;
        }
        timer_list[index].pfun   = NULL;
        timer_list[index].delay  = 0;
        timer_list[index].period = 0;
        timer_list[index].run    = false;
        timer_list[index].para   = NULL;
        return true;
    }
    
    void timer_start(const timer_id index)
    {
        if(index >= OS_TIMER_MAX) return;
    
        timer_list[index].run    = true;
    }
    
    void timer_stop(const timer_id index)
    {
        if(index >= OS_TIMER_MAX) return;
    
        timer_list[index].run    = false;
    }
    
    void timer_sched(void)
    {
        uint8_t index;
        for (index = 0; index < OS_TIMER_MAX; index++)
        {
            if(timer_list[index].delay == 0)
            {
                if (timer_list[index].run)
                {
                    (*timer_list[index].pfun)(timer_list[index].para);
                    if (timer_list[index].period == 0)
                    {
                        timer_delete(index);
                    }
                    else timer_list[index].delay = timer_list[index].period;
                }
            }
            else
            {
                timer_list[index].delay -= 1;
            }
        }
    }
    
    void f1(void* para)
    {}
    
    void f2(void* para)
    {}
    
    void f3(void* para)
    {}
    
    
    int main()
    {
        printf("Hello world!
    ");
    
        timer_init();
    
        timer_id t1 = timer_creat(f1, 0, 100, false, NULL);
        timer_id t2 = timer_creat(f2, 10, 200, false, NULL);
        timer_id t3 = timer_creat(f3, 20, 400, false, NULL);
    
        timer_sched();
    
        timer_start(t1);
        timer_stop(t1);
    
        timer_delete(t1);
        timer_delete(t2);
        timer_delete(t3);
    
        return 0;
    }

    https://github.com/zhoudd1/timer

  • 相关阅读:
    [resource]23个python的机器学习包
    [resource]Python机器学习库
    [resource-]Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
    过滤垃圾评论
    IIS Express start introduction and applicationHost modification
    [algothrim] url pattern mining within same domain site
    API网关的优缺点
    API网关特性
    部署到IIS的两种方式
    .Net Core有三种部署方式
  • 原文地址:https://www.cnblogs.com/dong1/p/6810849.html
Copyright © 2011-2022 走看看