test code.
parent
6208dc71fc
commit
242dffa9dd
@ -1,34 +0,0 @@
|
|||||||
#include "multi_timer.h"
|
|
||||||
|
|
||||||
struct Timer timer1;
|
|
||||||
struct Timer timer2;
|
|
||||||
|
|
||||||
void timer1_callback(void *arg)
|
|
||||||
{
|
|
||||||
printf("timer1 timeout! arg: %p\r\n", arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void timer2_callback(void *arg)
|
|
||||||
{
|
|
||||||
printf("timer2 timeout! arg: %p\r\n", arg);
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
timer_init(&timer1, timer1_callback, 1000, 1000, NULL); //1s loop
|
|
||||||
timer_start(&timer1);
|
|
||||||
|
|
||||||
timer_init(&timer2, timer2_callback, 50, 0, NULL); //50ms delay
|
|
||||||
timer_start(&timer2);
|
|
||||||
|
|
||||||
while(1) {
|
|
||||||
timer_loop();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void HAL_SYSTICK_Callback(void)
|
|
||||||
{
|
|
||||||
timer_ticks(); //1ms ticks
|
|
||||||
}
|
|
||||||
@ -1,92 +1,45 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <signal.h>
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <stdarg.h>
|
#include "../MultiTimer.h"
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "../multi_timer.h"
|
MultiTimer timer1;
|
||||||
|
MultiTimer timer2;
|
||||||
|
MultiTimer timer3;
|
||||||
|
|
||||||
#define LOG_TIMESTAMP_FORMAT "[%Y-%m-%d %H:%M:%S]"
|
uint64_t PlatformTicksGetFunc(void)
|
||||||
#define LOG_TIMESTAMP_LEN 21
|
|
||||||
#define MS_TIMESTAMP_LEN 5
|
|
||||||
|
|
||||||
int32_t printf_timestamp(const char *msg, ...)
|
|
||||||
{
|
{
|
||||||
char content[1024] = {0};
|
struct timespec current_time;
|
||||||
time_t time_write;
|
clock_gettime(CLOCK_MONOTONIC, ¤t_time);
|
||||||
struct tm tm_Log;
|
return (uint64_t)((current_time.tv_sec * 1000) + (current_time.tv_nsec / 1000000));
|
||||||
struct timeval t;
|
|
||||||
uint32_t len = 0;
|
|
||||||
va_list vl_list;
|
|
||||||
|
|
||||||
time_write = time(NULL);
|
|
||||||
localtime_r(&time_write, &tm_Log);
|
|
||||||
strftime((char *)&content[len], sizeof(content) - len - 1, LOG_TIMESTAMP_FORMAT, &tm_Log);
|
|
||||||
len = strlen(&content[len]);
|
|
||||||
|
|
||||||
/* .msec] */
|
|
||||||
gettimeofday(&t, NULL);
|
|
||||||
len--; // min a ']' char
|
|
||||||
snprintf((char*)&content[len], sizeof(content) - len, ".%03d]", (int)(t.tv_usec / 1000));
|
|
||||||
len += MS_TIMESTAMP_LEN;
|
|
||||||
|
|
||||||
va_start(vl_list, msg);
|
|
||||||
vsnprintf((char *)&content[len], sizeof(content) - len - 1, (const char *)msg, vl_list);
|
|
||||||
va_end(vl_list);
|
|
||||||
|
|
||||||
printf("%s", content);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void signalHandler(int signo)
|
void exampleTimer1Callback(MultiTimer* timer, void *userData)
|
||||||
{
|
{
|
||||||
switch(signo)
|
printf("[T:%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
|
||||||
{
|
MultiTimerStart(timer, 1000, exampleTimer1Callback, userData);
|
||||||
case SIGALRM:
|
|
||||||
timer_ticks();
|
|
||||||
//printf_timestamp("Caught the SIGALRM signal every 1ms !\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Timer timer1;
|
void exampleTimer2Callback(MultiTimer* timer, void *userData)
|
||||||
struct Timer timer2;
|
|
||||||
|
|
||||||
void timer1_callback(void *arg)
|
|
||||||
{
|
{
|
||||||
printf_timestamp("timer1 timeout! arg: %p\r\n", arg);
|
printf("[T:%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
|
||||||
}
|
}
|
||||||
|
|
||||||
void timer2_callback(void *arg)
|
void exampleTimer3Callback(MultiTimer* timer, void *userData)
|
||||||
{
|
{
|
||||||
printf_timestamp("timer2 timeout! arg: %p\r\n", arg);
|
printf("[T:%012ld] Timer:%p callback-> %s.\r\n", PlatformTicksGetFunc(), timer, (char*)userData);
|
||||||
|
MultiTimerStart(timer, 4567, exampleTimer3Callback, userData);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
printf_timestamp("%s start ...\r\n", __func__);
|
MultiTimerInstall(PlatformTicksGetFunc);
|
||||||
|
|
||||||
signal(SIGALRM, signalHandler);
|
|
||||||
|
|
||||||
struct itimerval new_value, old_value;
|
|
||||||
new_value.it_value.tv_sec = 0; // should be 0x00 !!!
|
|
||||||
new_value.it_value.tv_usec = 1; // non-zero is OK !!!
|
|
||||||
new_value.it_interval.tv_sec = 0;
|
|
||||||
new_value.it_interval.tv_usec = 1000 * CFG_TIMER_1_TICK_N_MS; // 1ms
|
|
||||||
setitimer(ITIMER_REAL, &new_value, &old_value);
|
|
||||||
|
|
||||||
timer_init(&timer1, timer1_callback, 4000, 1000, &timer1); // start timer after 4s
|
MultiTimerStart(&timer1, 1000, exampleTimer1Callback, "1000ms CYCLE timer");
|
||||||
timer_start(&timer1);
|
MultiTimerStart(&timer2, 5000, exampleTimer2Callback, "5000ms ONCE timer");
|
||||||
|
MultiTimerStart(&timer3, 3456, exampleTimer3Callback, "3456ms delay start, 4567ms CYCLE timer");
|
||||||
|
|
||||||
timer_init(&timer2, timer2_callback, 0, 5000, &timer2); // start timer right now
|
while (1) {
|
||||||
timer_start(&timer2);
|
MultiTimerYield();
|
||||||
|
|
||||||
while(1) {
|
|
||||||
/* Maybe some sleep time is needed to avoid running CPU all the time. */
|
|
||||||
timer_loop();
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue