add demo in linux

pull/5/head
kdurant 5 years ago
parent 22d0e6ec10
commit ab36a8d544

4
.gitignore vendored

@ -0,0 +1,4 @@
*.o
*.out
test
tmp

@ -9,7 +9,7 @@ MultiTimer 是一个软件定时器扩展模块,可无限扩展你所需的定
``` ```
struct Timer timer1; struct Timer timer1;
``` ```
2.初始化定时器对象,注册定时器回调处理函数,设置定时时间ms循环定时触发时间 2.初始化定时器对象,注册定时器回调处理函数,设置延迟启动时间ms循环定时触发时间
``` ```
timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat); timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat);

@ -0,0 +1,52 @@
#include <stdio.h>
#include <signal.h>
#include <sys/time.h>
#include "../multi_timer.h"
void signalHandler(int signo)
{
switch(signo)
{
case SIGALRM:
timer_ticks();
//printf("Caught the SIGALRM signal!\n");
break;
}
}
struct Timer timer1;
struct Timer timer2;
void timer1_callback()
{
printf("timer1 timeout!\r\n");
}
void timer2_callback()
{
printf("timer2 timeout!\r\n");
}
int main(void)
{
signal(SIGALRM, signalHandler);
struct itimerval new_value, old_value;
new_value.it_value.tv_sec = 1;
new_value.it_value.tv_usec = 0;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_usec = 1000;
setitimer(ITIMER_REAL, &new_value, &old_value);
timer_init(&timer1, timer1_callback, 4000, 1000); // start timer after 4s
timer_start(&timer1);
timer_init(&timer2, timer2_callback, 0, 2000);
timer_start(&timer2);
while(1)
{
timer_loop();
}
return 0;
}

@ -0,0 +1,31 @@
CC = gcc
CFLGAS += -std=c99
CFLAGS += -D TEST
CFLAGS += -g
OBJ_PATH = .
BIN_PATH = .
SRC_PATH = .
IDIR = .
INC += -I$(IDIR)
TARGET = $(BIN_PATH)/test
C_SRCS += ./examples/test_linux.c
C_SRCS += ./multi_timer.c
OBJ := $(patsubst %.c,%.o,$(filter %.c,$(C_SRCS)))
$(TARGET) : $(OBJ_PATH)/$(OBJ)
$(CC) $(INC) $(CFLAGS) $(OBJ) -o $(TARGET)
$(OBJ): %.o : %.c
$(CC) $(INC) $(CFLAGS) -c $< -o $@
.PHONY : clean
clean:
@-rm *.exe
@-rm *.o

@ -18,12 +18,12 @@ static uint32_t _timer_ticks = 0;
* @param repeat: repeat interval time. * @param repeat: repeat interval time.
* @retval None * @retval None
*/ */
void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat) void timer_init(struct Timer* handle, void (*timeout_cb)(), uint32_t timeout, uint32_t repeat)
{ {
// memset(handle, sizeof(struct Timer), 0); // memset(handle, sizeof(struct Timer), 0);
handle->timeout_cb = timeout_cb; handle->timeout_cb = timeout_cb;
handle->timeout = _timer_ticks + timeout; handle->timeout = _timer_ticks + timeout;
handle->repeat = repeat; handle->repeat = repeat;
} }
/** /**
@ -33,14 +33,16 @@ void timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uin
*/ */
int timer_start(struct Timer* handle) int timer_start(struct Timer* handle)
{ {
struct Timer* target = head_handle; struct Timer* target = head_handle;
while(target) { while(target)
if(target == handle) return -1; //already exist. {
target = target->next; if(target == handle)
} return -1; //already exist.
handle->next = head_handle; target = target->next;
head_handle = handle; }
return 0; handle->next = head_handle;
head_handle = handle;
return 0;
} }
/** /**
@ -50,15 +52,18 @@ int timer_start(struct Timer* handle)
*/ */
void timer_stop(struct Timer* handle) void timer_stop(struct Timer* handle)
{ {
struct Timer** curr; struct Timer** curr;
for(curr = &head_handle; *curr; ) { for(curr = &head_handle; *curr;)
struct Timer* entry = *curr; {
if (entry == handle) { struct Timer* entry = *curr;
*curr = entry->next; if(entry == handle)
// free(entry); {
} else *curr = entry->next;
curr = &entry->next; // free(entry);
} }
else
curr = &entry->next;
}
} }
/** /**
@ -68,17 +73,22 @@ void timer_stop(struct Timer* handle)
*/ */
void timer_loop() void timer_loop()
{ {
struct Timer* target; struct Timer* target;
for(target=head_handle; target; target=target->next) { for(target = head_handle; target; target = target->next)
if(_timer_ticks >= target->timeout) { {
if(target->repeat == 0) { if(_timer_ticks >= target->timeout)
timer_stop(target); {
} else { if(target->repeat == 0)
target->timeout = _timer_ticks + target->repeat; {
} timer_stop(target);
target->timeout_cb(); }
} else
} {
target->timeout = _timer_ticks + target->repeat;
}
target->timeout_cb();
}
}
} }
/** /**
@ -88,6 +98,6 @@ void timer_loop()
*/ */
void timer_ticks() void timer_ticks()
{ {
_timer_ticks++; _timer_ticks++;
} }

Loading…
Cancel
Save