整的有点复杂,不习惯的可以简化
nw_time.c
#include "nw_define.h"
#include "Vmsys.h"
NW_TIMER_DATA nw_timer;
#define NW_HTTP_TIMER_ID 125
#define NW_TIMER_SUPPORT_MAX (16)
#define NW_TIMER_MAIN_LOOP (100)
void nw_http_timer_loop(void);
void nw_http_main_timer_start()
{
StartTimer(NW_HTTP_TIMER_ID, 100, nw_http_timer_loop);
}
void nw_http_main_timer_stop()
{
StopTimer(NW_HTTP_TIMER_ID);
}
nw_uint32 nw_timer_get_interval()
{
return NW_TIMER_MAIN_LOOP; //100毫秒
}
void nw_http_timer_loop(void)
{
if (nw_timer.callback_func != NULL)
{
if (nw_timer.remain_time <= nw_timer_get_interval())
{
NW_TIMER_CALLBACK timer_func = nw_timer.callback_func;
nw_timer.callback_func = NULL;
timer_func(nw_timer.nw_timer_param_i,nw_timer.nw_timer_param_w);
}
else
{
nw_timer.remain_time -= nw_timer_get_interval();
}
}
}
nw_uint16 nw_http_timer_start(nw_int8 app_index, nw_uint32 delay, NW_TIMER_CALLBACK callback_func, NW_TIMER_TYPE type)
{
if(nw_timer.used == 0)
{
nw_timer.timer_id = 1;
nw_timer.remain_time= delay;
nw_timer.callback_func = callback_func;
nw_timer.soc_id= app_index;
nw_timer.nw_timer_param_i = (void*)nw_timer.soc_id;
nw_timer.used = 1;
nw_timer.type = type;
nw_timer.nw_timer_param_w = (void*)nw_timer.type;
return (nw_timer.timer_id);
}
return 0;
}
nw_uint16 nw_http_get_timer_id_by_app_index(nw_int8 app_index , NW_TIMER_TYPE type)
{
if((nw_timer.soc_id == app_index) && (nw_timer.type == type))
{
return (nw_timer.timer_id);
}
return 0;
}
nw_uint16 nw_http_timer_stop(nw_uint16 timer_id)
{
if(timer_id == nw_timer.timer_id)
{
nw_timer.timer_id = 0;
nw_timer.remain_time= 0;
nw_timer.callback_func = 0;
nw_timer.soc_id= 0;
}
return 0;
}