Merge pull request #5 from kdurant/master

add demo in linux
master
Bin 5 years ago committed by GitHub
commit 402cef258d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

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

@ -34,8 +34,10 @@ 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. {
if(target == handle)
return -1; //already exist.
target = target->next; target = target->next;
} }
handle->next = head_handle; handle->next = head_handle;
@ -51,12 +53,15 @@ 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; struct Timer* entry = *curr;
if (entry == handle) { if(entry == handle)
{
*curr = entry->next; *curr = entry->next;
// free(entry); // free(entry);
} else }
else
curr = &entry->next; curr = &entry->next;
} }
} }
@ -69,11 +74,16 @@ 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)
{
if(target->repeat == 0)
{
timer_stop(target); timer_stop(target);
} else { }
else
{
target->timeout = _timer_ticks + target->repeat; target->timeout = _timer_ticks + target->repeat;
} }
target->timeout_cb(); target->timeout_cb();

Loading…
Cancel
Save