用c語言實現的定時器庫—-介面(定義的結構和函式宣告)和庫實現

來源:互聯網
上載者:User

可實現多個計時器
以下的代碼是定時器庫的介面: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];
}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.