From ab36a8d544e919f3094a4db781734240a09675b0 Mon Sep 17 00:00:00 2001 From: kdurant Date: Tue, 8 Dec 2020 15:46:45 +0800 Subject: [PATCH] add demo in linux --- .gitignore | 4 +++ README.md | 2 +- examples/test_linux.c | 52 +++++++++++++++++++++++++++++ makefile | 31 +++++++++++++++++ multi_timer.c | 78 ++++++++++++++++++++++++------------------- 5 files changed, 132 insertions(+), 35 deletions(-) create mode 100644 .gitignore create mode 100644 examples/test_linux.c create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..167bed0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.out +test +tmp diff --git a/README.md b/README.md index e0b0e76..124562d 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ MultiTimer 是一个软件定时器扩展模块,可无限扩展你所需的定 ``` struct Timer timer1; ``` -2.初始化定时器对象,注册定时器回调处理函数,设置定时时间(ms),循环定时触发时间 +2.初始化定时器对象,注册定时器回调处理函数,设置延迟启动时间(ms),循环定时触发时间 ``` timer_init(struct Timer* handle, void(*timeout_cb)(), uint32_t timeout, uint32_t repeat); diff --git a/examples/test_linux.c b/examples/test_linux.c new file mode 100644 index 0000000..85ff2ae --- /dev/null +++ b/examples/test_linux.c @@ -0,0 +1,52 @@ +#include +#include +#include +#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; +} diff --git a/makefile b/makefile new file mode 100644 index 0000000..aa54ccd --- /dev/null +++ b/makefile @@ -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 diff --git a/multi_timer.c b/multi_timer.c index 76e811a..8b48f2c 100644 --- a/multi_timer.c +++ b/multi_timer.c @@ -18,12 +18,12 @@ static uint32_t _timer_ticks = 0; * @param repeat: repeat interval time. * @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); - handle->timeout_cb = timeout_cb; - handle->timeout = _timer_ticks + timeout; - handle->repeat = repeat; + // memset(handle, sizeof(struct Timer), 0); + handle->timeout_cb = timeout_cb; + handle->timeout = _timer_ticks + timeout; + 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) { - struct Timer* target = head_handle; - while(target) { - if(target == handle) return -1; //already exist. - target = target->next; - } - handle->next = head_handle; - head_handle = handle; - return 0; + struct Timer* target = head_handle; + while(target) + { + if(target == handle) + return -1; //already exist. + target = target->next; + } + 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) { - struct Timer** curr; - for(curr = &head_handle; *curr; ) { - struct Timer* entry = *curr; - if (entry == handle) { - *curr = entry->next; -// free(entry); - } else - curr = &entry->next; - } + struct Timer** curr; + for(curr = &head_handle; *curr;) + { + struct Timer* entry = *curr; + if(entry == handle) + { + *curr = entry->next; + // free(entry); + } + else + curr = &entry->next; + } } /** @@ -68,17 +73,22 @@ void timer_stop(struct Timer* handle) */ void timer_loop() { - struct Timer* target; - for(target=head_handle; target; target=target->next) { - if(_timer_ticks >= target->timeout) { - if(target->repeat == 0) { - timer_stop(target); - } else { - target->timeout = _timer_ticks + target->repeat; - } - target->timeout_cb(); - } - } + struct Timer* target; + for(target = head_handle; target; target = target->next) + { + if(_timer_ticks >= target->timeout) + { + if(target->repeat == 0) + { + timer_stop(target); + } + else + { + target->timeout = _timer_ticks + target->repeat; + } + target->timeout_cb(); + } + } } /** @@ -88,6 +98,6 @@ void timer_loop() */ void timer_ticks() { - _timer_ticks++; + _timer_ticks++; }