首页 > 其他分享 >简易版-软件定时器

简易版-软件定时器

时间:2023-11-01 16:24:41浏览次数:32  
标签:count 定时器 list void sw timer 简易版 软件 uint32

main.c

#include <stdio.h>
#include <stdlib.h>
#include "sw_timer.h"
#include "windows.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void timer1(void *parameter)
{
	sw_timer_t *timer = parameter;
	printf("timer1 run %s ...\r\n",timer->user_data);
}



void timer2(void *parameter)
{
	sw_timer_t *timer = parameter;
	printf("timer2 run %s ...\r\n",timer->user_data);
}



void timer3(void *parameter)
{
	sw_timer_t *timer = parameter;
	printf("timer3 run %s ...\r\n",timer->user_data);
	sw_timer_set_pause(timer);
}

void timer4(void *parameter)
{
	sw_timer_t *timer = parameter;
	printf("timer4 run %s ...\r\n",timer->user_data);
	sw_timer_del(timer);
}


int main(int argc, char *argv[])
{
	sw_timer_t * timer;
	setbuf(stdout, NULL);
	timer = sw_timer_create(timer1,1000,6,"timer1");
	printf("timer = %p\r\n",timer);
	sw_timer_set_repeat_count(timer,10);
	timer = sw_timer_create(timer2,2000,3,"timer2");
	printf("timer = %p\r\n",timer);
	timer = sw_timer_create(timer3,3000,2,"timer3");
	printf("timer = %p\r\n",timer);
	timer = sw_timer_create(timer4,4000,8,"timer4");
	printf("timer = %p\r\n",timer);


	while(1)
	{
		sw_timer_handle();
		Sleep(10);
		sw_timer_tick(10);
		//printf("...\r\n");
	}
	
	return 0;
}

  

sw_timer.h

#ifndef __SW_TIMER_H__
#define __SW_TIMER_H__

#include "stdint.h"

#define TIMER_LIST_NUM_MAX      10


typedef void (*sw_timer_cb_t)(void *);

typedef struct
{
    uint32_t used;		/*  */
	uint32_t period; /**< How often the timer should run*/
    uint32_t priority;  /* 优先级 越小优先级越高*/
    uint32_t last_run; /**< next time the timer ran*/
    sw_timer_cb_t timer_cb; /**< Timer function*/
    void * user_data; /**< Custom user data*/
    int32_t repeat_count; /**< 1: One time;  -1 : infinity;  n>0: residual times*/
    uint32_t paused : 1;
}sw_timer_t;





sw_timer_t *sw_timer_create(sw_timer_cb_t timer_xcb, uint32_t period, uint32_t priority, void * user_data);
int sw_timer_del(sw_timer_t *timer);
int sw_get_timer_count(void);
void sw_timer_handle(void);

void sw_timer_set_ready(sw_timer_t * timer, void *user_data);
void sw_timer_set_pause(sw_timer_t *timer);
void sw_timer_set_repeat_count(sw_timer_t * timer, int32_t repeat_count);

void sw_timer_tick(uint32_t ms);



#endif //__SW_LIST_H__

  

sw_timer.c

#include "sw_timer.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"

#define timer_malloc(x)         malloc(x)
#define timer_free(p)           free(p)
#define timer_tick_get			sw_tick_get

#define T_LOG_USER         		printf
#define T_LOG_ERROR          	printf

#define TIMER_LIST_NUM_MAX		10


static sw_timer_t timer_list[TIMER_LIST_NUM_MAX] = {0};



static uint32_t time_space(uint32_t new,uint32_t old)
{
    uint32_t ret = 0;
    if(new > old)
    {
        ret = new -old;
    }
    else
    {
        ret = 0xFFFFFFFFU - old;
        ret += new;
    }
    return ret;
}

static uint32_t timer_tick = 0;
void sw_timer_tick(uint32_t ms)
{
	timer_tick += ms;
}

static uint32_t sw_tick_get(void)
{
	return timer_tick;
}

int sw_get_timer_count(void)
{
	int i=0;
	int cnt = 0;
	for(i=0;i<TIMER_LIST_NUM_MAX;i++)
	{
		if(timer_list[i].used == 1)
		{
			cnt++; 
		}
	}
	return cnt;
}


sw_timer_t *sw_timer_create(sw_timer_cb_t timer_xcb, uint32_t period, uint32_t priority, void * user_data)
{
	int i = 0,j = 0;
	if(sw_get_timer_count() >= TIMER_LIST_NUM_MAX)
	{
		T_LOG_ERROR("timer count >= TIMER_LIST_NUM_MAX");
		return NULL;
	}
	for(i=0;i<TIMER_LIST_NUM_MAX;i++)
	{
		if(timer_list[i].used == 1)
		{
			if(timer_list[i].priority > priority)
			{
				for(j=TIMER_LIST_NUM_MAX-2;j>=i;j--)
				{
					memcpy(&timer_list[j+1],&timer_list[j],sizeof(sw_timer_t));
				}
				break;
			}
		}
		else
		{
			break;
		}
	}
	timer_list[i].period = period;
	timer_list[i].priority = priority;
	timer_list[i].timer_cb = timer_xcb;
	timer_list[i].repeat_count = -1;
	timer_list[i].paused = 0;
	timer_list[i].last_run = timer_tick_get();
	timer_list[i].user_data = user_data;
	timer_list[i].used = 1;
	printf("timer[i] = %d\r\n",i);
	return &timer_list[i];
}

int sw_timer_del(sw_timer_t *timer)
{
	int i = 0,j = 0;
	for(i=0;i<TIMER_LIST_NUM_MAX;i++)
	{
		if(timer_list[i].used != 1)
		{
			return -1;
		}	
		if(&timer_list[i] == timer)
		{
			for(j=i;j<TIMER_LIST_NUM_MAX-1;j++)
			{
				memcpy(&timer_list[j],&timer_list[j+1],sizeof(sw_timer_t));
			}
			return 1;
		}
	}
	return -1;
}

void sw_timer_set_pause(sw_timer_t *timer)
{
	timer->paused = 1;
}

void sw_timer_set_repeat_count(sw_timer_t * timer, int32_t repeat_count)
{
	timer->repeat_count = repeat_count;
}

void sw_timer_set_ready(sw_timer_t * timer, void *user_data)
{
	timer->user_data = user_data;
	timer->paused = 0;
	timer->last_run = timer_tick_get() - timer->period - 1;
}


void sw_timer_handle(void)
{
	int i = 0;
	for(i=0;i<TIMER_LIST_NUM_MAX;i++)
	{
		if(timer_list[i].used != 1)
		{
			break;
		}
		if(timer_list[i].paused != 0)
		{
			continue;
		}
		if(timer_list[i].repeat_count == 0)
		{
			continue;
		}
		
		if(time_space(timer_tick_get(),timer_list[i].last_run) >= timer_list[i].period)
		{
			if(timer_list[i].repeat_count > 0)
			{
				timer_list[i].repeat_count --;
			}
			timer_list[i].timer_cb(&timer_list[i]);
			timer_list[i].last_run = timer_tick_get();
		}	
	}
}

  

标签:count,定时器,list,void,sw,timer,简易版,软件,uint32
From: https://www.cnblogs.com/star-water/p/17803410.html

相关文章

  • 没有可用软件包 gitlab-jh。
    一问题安装gitlab时,提示“没有可用软件包gitlab-jh” 二解决1、yum没有找到对应依赖包,更新epel第三方软件库,运行命令:yuminstall-yepel-release更新完epel第三方软件库后,再次尝试使用yum命令安装对应的软件包2、如果还不行yumupdate更新,时间长一些  ......
  • CRM软件助力企业科学决策
     我们常说“选择大于努力”,这对于企业发展同样适用。每一家企业管理者在日常工作中都要做大量决策,员工只是将决策落地,而这些决策往往决定了公司大大小小项目实施的顺利与否。因此,采用CRM软件助力企业科学决策显得十分关键。越来越多企业发展的过程中都出现了数字化转型的需求......
  • 开源软件:释放创新的力量,改变数字世界的游戏规则
    在充满活力的技术领域,创新是至高无上的,有一种方法已获得显著的吸引力——开源软件。开源软件凭借其透明、协作和无限可能性的精神,彻底改变了我们开发、共享和定制应用程序的方式。从操作系统到数据分析工具,其影响跨越了多个领域。本文将带您深入了解开源软件的历史、优势和充满活......
  • 软件测试与嵌入式测试的异同
    ​ 软件嵌入式测试 一、软件测试和嵌入式测试的定义(一)软件测试是一种评估软件质量和功能的过程,它是为了验证软件系统是否符合要求,发现可能存在的bug并及时修复和改进的过程。(二)嵌入式软件测试(cross-test):是一种进行测试的方法和活动,针对的是嵌入式系统中的软件。1、嵌入式......
  • 软件设计实验4:抽象工厂模式
    实验4:抽象工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解抽象工厂模式的动机,掌握该模式的结构;2、能够利用抽象工厂模式解决实际问题。 [实验任务一]:人与肤色使用抽象工厂模式,完成下述产品等级结构: 实验要求:1. 画出对应的类图; 2. 提交源代......
  • 软件设计实验5:建造者模式
    实验5:建造者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解建造者模式的动机,掌握该模式的结构;2、能够利用建造者模式解决实际问题。 [实验任务一]:计算机组装使用建造者模式,完成下述任务:计算机组装工厂可以将CPU、内存、硬盘、主机等硬件设备组装在一起......
  • 软件设计实验1:UML与面向对象程序设计原则
    实验1:UML与面向对象程序设计原则本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、掌握面向对象程序设计中类与类之间的关系以及对应的UML类图;2、理解面向对象程序设计原则。 [实验任务一]:UML复习阅读教材第一章复习UML,回答下述问题:面向对象程序设计中类与类的关......
  • 软件设计实验2:简单工厂模式
    实验2:简单工厂模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解简单工厂模式的动机,掌握该模式的结构;2、能够利用简单工厂模式解决实际问题。 [实验任务一]:女娲造人使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参......
  • 软件设计实验3:工厂方法模式
    实验3:工厂方法模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解工厂方法模式的动机,掌握该模式的结构;2、能够利用工厂方法模式解决实际问题。 [实验任务一]:加密算法目前常用的加密算法有DES(DataEncryptionStandard)和IDEA(InternationalDataEncryption......
  • 1020. 【软件认证】任务调度算法
    题目描述某分布式任务调度系统有taskNum个任务(编号从1到taskNum)需要调度,调度策略:任务之间可能存在依赖关系,且无循环依赖,如任务1依赖任务2,那么要等待任务2执行完才能执行任务1;如果任务之间没有依赖关系,则可以并发执行(假设并发所需资源是充足的)。现给出任务间的依赖关系,并......