From d6faa0a3e23f6e8690c8d9062fdec6996714385a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20O=2E=20Sel=C3=A5sdal?= Date: Wed, 20 Feb 2013 20:41:25 +0100 Subject: [PATCH] Added timers.c demo for uc_timers --- test/timers.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 test/timers.c diff --git a/test/timers.c b/test/timers.c new file mode 100644 index 0000000..355b98f --- /dev/null +++ b/test/timers.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + + +struct MyStruct { + char *str; + struct UCTimer timer; +}; + +void t1_cb(struct UCTimers *timers, struct UCTimer *timer) +{ + TRACEF("T1 expired\n"); +} + +void t2_cb(struct UCTimers *timers, struct UCTimer *timer) +{ + TRACEF("T2 expired\n"); +} + +void mystruct_cb(struct UCTimers *timers, struct UCTimer *timer) +{ + struct MyStruct *mystruct = CONTAINER_OF(timer, struct MyStruct, timer); + + TRACEF("MyStruct timer: %s\n", mystruct->str); + uc_timer_add(timers, timer, 1, 0); +} + +int main(int argc, char *argv[]) +{ + struct UCTimers timers; + struct UCTimer t1 = { + .callback = t1_cb, + }; + struct UCTimer t2 = { + .callback = t2_cb, + }; + struct MyStruct my_struct = { + .str = "Hello", + .timer = { + .callback = mystruct_cb, + }, + }; + + uc_timers_init(&timers); + uc_timer_add(&timers, &t1, 2, 0); + uc_timer_add(&timers, &t2, 5, 0); + uc_timer_add(&timers, &my_struct.timer, 10, 0); + + for(;;) { + struct timeval now; + gettimeofday(&now, NULL); + uc_timers_run(&timers, &now); +#ifndef FULL_SPEED + usleep(1000); +#endif + } + + return 0; +}