用C语言实现的定时器库---库实现。
来源:互联网 发布:异星工厂信号网络 编辑:程序博客网 时间:2024/06/07 02:28
接上一篇。。。
用C语言实现的定时器库---库实现。 timer.c
用C语言实现的定时器库---库实现。 timer.c
#include "timer.h"
#include <stdlib.h>
static int timercount = 0;
static timer *alltimer = NULL;
static int nowpos = -1;
static const int increase = 10;
static timer_errors timer_error = timerOK;
static const char *error_msg[] =
{
"timer is ok.",
"no such timer, please check it.",
"initlize timer fail, beacuse of not enough memory.",
"a avoid distm_second, it should be a postive float.",
"no such error message"
};
timerID initlize_timer(float distm)
{
timer *tmp;
timer newtimer;
if(distm < 0)
{
timer_error = timerAvoidDistm;
return -1;
}
newtimer = (timer)malloc( sizeof(mytimer) );
if(newtimer == NULL)
{
timer_error = timerOutOfMem;
return -1;
}
newtimer->distm_second = distm;
newtimer->base_tm = clock();
nowpos++;
if(nowpos >= timercount)
{
tmp = (timer *)realloc((void*)alltimer, (unsigned int)(timercount+increase));
if(tmp == NULL)
{
timer_error = timerOutOfMem;
free(newtimer);
return -1;
}
alltimer = tmp;
timercount = timercount + increase;
}
alltimer[nowpos] = newtimer;
timer_error = timerOK;
return (timerID)nowpos;
}
int is_timer_over(timerID id)
{
timer p;
clock_t nowclock = clock();
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return -1;
}
p = alltimer[id];
if( (nowclock - p->base_tm) > ((p->distm_second)*CLK_TCK) )
{
p->base_tm = nowclock;
return 1;
}
else
return 0;
}
void reset_timer(timerID id)
{
timer p;
clock_t nowclock;
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return;
}
p = alltimer[id];
nowclock = clock();
p->base_tm = nowclock;
}
float get_timer_distm(timerID id)
{
timer p;
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return -1;
}
p = alltimer[id];
return p->distm_second;
}
void set_timer_distm(timerID id, float newdistm)
{
timer p;
if(newdistm < 0)
{
timer_error = timerAvoidDistm;
return;
}
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return;
}
p= alltimer[id];
p->distm_second = newdistm;
}
timer_errors timer_result(void)
{
return timer_error;
}
char * timer_msg(timer_errors err)
{
if((int)err < 0 ||(int)err >3)
err = timerNoSuchErrCode;
return (char *)error_msg[(int)err];
}
#include <stdlib.h>
static int timercount = 0;
static timer *alltimer = NULL;
static int nowpos = -1;
static const int increase = 10;
static timer_errors timer_error = timerOK;
static const char *error_msg[] =
{
"timer is ok.",
"no such timer, please check it.",
"initlize timer fail, beacuse of not enough memory.",
"a avoid distm_second, it should be a postive float.",
"no such error message"
};
timerID initlize_timer(float distm)
{
timer *tmp;
timer newtimer;
if(distm < 0)
{
timer_error = timerAvoidDistm;
return -1;
}
newtimer = (timer)malloc( sizeof(mytimer) );
if(newtimer == NULL)
{
timer_error = timerOutOfMem;
return -1;
}
newtimer->distm_second = distm;
newtimer->base_tm = clock();
nowpos++;
if(nowpos >= timercount)
{
tmp = (timer *)realloc((void*)alltimer, (unsigned int)(timercount+increase));
if(tmp == NULL)
{
timer_error = timerOutOfMem;
free(newtimer);
return -1;
}
alltimer = tmp;
timercount = timercount + increase;
}
alltimer[nowpos] = newtimer;
timer_error = timerOK;
return (timerID)nowpos;
}
int is_timer_over(timerID id)
{
timer p;
clock_t nowclock = clock();
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return -1;
}
p = alltimer[id];
if( (nowclock - p->base_tm) > ((p->distm_second)*CLK_TCK) )
{
p->base_tm = nowclock;
return 1;
}
else
return 0;
}
void reset_timer(timerID id)
{
timer p;
clock_t nowclock;
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return;
}
p = alltimer[id];
nowclock = clock();
p->base_tm = nowclock;
}
float get_timer_distm(timerID id)
{
timer p;
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return -1;
}
p = alltimer[id];
return p->distm_second;
}
void set_timer_distm(timerID id, float newdistm)
{
timer p;
if(newdistm < 0)
{
timer_error = timerAvoidDistm;
return;
}
if(id < 0 || id > nowpos)
{
timer_error = timerBadID;
return;
}
p= alltimer[id];
p->distm_second = newdistm;
}
timer_errors timer_result(void)
{
return timer_error;
}
char * timer_msg(timer_errors err)
{
if((int)err < 0 ||(int)err >3)
err = timerNoSuchErrCode;
return (char *)error_msg[(int)err];
}
- 用C语言实现的定时器库---库实现。
- 用C语言实现的定时器库---接口。
- C语言实现定时器
- C语言实现定时器
- C语言中高效定时器的实现_warmembrace
- 多媒体定时器 C的实现
- 黑马程序员---c语言 通讯录的实现
- 实时定时器C实现
- C语言伪泛型的实现--栈
- 模仿linux内核定时器代码,用python语言实现定时器
- clock程序-c语言实现
- SHA-1算法C语言实现
- 一个简单定时器的实现(C++)
- 一个简单定时器的实现(C++)
- muduo网络库定时器的实现
- muduo网络库定时器的实现
- 中断和定时器实验--c语言实现中断嵌套
- ARM7定时器模块简介及实现(C语言)
- Eclipse+ATF的安装
- 软件项目开发过程中的几点体会
- 像老板一样思考
- java记事本
- Eclipse远程调试Weblogic运行的源代码
- 用C语言实现的定时器库---库实现。
- Java套接字实现网络编程之基础篇1
- Java套接字实现网络编程之基础篇2
- Java网络编程精解之ServerSocket用法详解一1
- ASP.NET 2.0 中的数据访问
- Java网络编程精解之ServerSocket用法详解一2
- Tomcat绑定二级域名的配置过程
- Java网络编程精解之ServerSocket用法详解二1
- 危险警告!!!微软正全力追踪锁定电脑