• 通用定时器设计(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

  • 相关阅读:
    Oracle日期函数大全
    Android Permission(授权)大全
    澳大利亚项目VBA部分简略代码
    Android模拟器安装程序及上传音乐并播放
    更改电脑背景颜色,保护您的眼睛
    编写高效Excel VBA代码的最佳实践(一)
    VBA复制粘贴效率问题
    编写高效Excel VBA代码的最佳实践(二)
    编写高效Excel VBA代码的最佳实践(二)
    Android新浪星座运势程序开发
  • 原文地址:https://www.cnblogs.com/dong1/p/6810849.html
Copyright © 2020-2023  润新知