可實現多個計時器
以下的代碼是定時器庫的介面:timer.h
#ifndef TIMER_H
#define TIMER_H
#include <time.h>
typedef struct mytimer mytimer;
typedef struct mytimer* timer;
struct mytimer
{
clock_t base_tm;
float distm_second;
};
typedef int timerID;
typedef enum timer_errors timer_errors;
enum timer_errors
{
timerOK,
timerBadID,
timerOutOfMem,
timerAvoidDistm,
timerNoSuchErrCode
};
/****************************
* initlize a new timer.
* input: distm: the timer's limitation.
* output: timer's ID.
* used:
* *****************************/
timerID initlize_timer(float distm);
/****************************
* judge the timer is over or not.
* input: id: the timer.
* output: 1 if the timer is overtimer otheiwise 0.
* used:
* *****************************/
int is_timer_over(timerID id);
/****************************
* reset the timer , the timer begin to time again.
* input: id:the timer.
* output: nothing.
* used: ...
* *****************************/
void reset_timer(timerID id);
/****************************
* get the timer's distm_second.
* input: id:the timer.
* output: the timer's ditm_second.
* used:...
* *****************************/
float get_timer_distm(timerID id);
/****************************
* set the timer's distm_second.
* input: id:the timer.
* output: nothing.
* used:...
* *****************************/
void set_timer_distm(timerID id, float newdistm);
/****************************
* ger the recent timer's result.
* input: nothing.
* output: timer_errors's code.
* used:...
* *****************************/
timer_errors timer_result(void);
/****************************
* get the recent timer's message .
* input: nothing
* output: a char poniter to a const string.
* used:....
* *****************************/
char * timer_msg(timer_errors err);
#endif
庫實現 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)*CLOCKS_PER_SEC) )
{
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];
}