You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
2.4 KiB
C
89 lines
2.4 KiB
C
#include "MultiTimer.h"
|
|
#include <stdio.h>
|
|
|
|
/* Timer handle list head. */
|
|
static MultiTimer* timerList = NULL;
|
|
|
|
/* Timer tick */
|
|
static PlatformTicksFunction_t platformTicksFunction = NULL;
|
|
|
|
int MultiTimerInstall(PlatformTicksFunction_t ticksFunc)
|
|
{
|
|
platformTicksFunction = ticksFunc;
|
|
return 0;
|
|
}
|
|
|
|
int MultiTimerStart(MultiTimer* timer, uint64_t timing, MultiTimerCallback_t callback, void* userData, uint8_t is_repeated)
|
|
{
|
|
if (!timer || !callback ) {
|
|
return -1;
|
|
}
|
|
MultiTimer** nextTimer = &timerList;
|
|
/* Remove the existing target timer. */
|
|
for (; *nextTimer; nextTimer = &(*nextTimer)->next) {
|
|
if (timer == *nextTimer) {
|
|
*nextTimer = timer->next; /* remove from list */
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* Init timer. */
|
|
timer->deadline = platformTicksFunction() + timing;
|
|
timer->timeout = timing;
|
|
timer->callback = callback;
|
|
timer->userData = userData;
|
|
timer->is_repeated = is_repeated;
|
|
|
|
/* Insert timer. */
|
|
for (nextTimer = &timerList;; nextTimer = &(*nextTimer)->next) {
|
|
if (!*nextTimer) {
|
|
timer->next = NULL;
|
|
*nextTimer = timer;
|
|
break;
|
|
}
|
|
if (timer->deadline < (*nextTimer)->deadline) {
|
|
timer->next = *nextTimer;
|
|
*nextTimer = timer;
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int MultiTimerStop(MultiTimer* timer)
|
|
{
|
|
MultiTimer** nextTimer = &timerList;
|
|
timer->is_repeated = 0;
|
|
/* Find and remove timer. */
|
|
for (; *nextTimer; nextTimer = &(*nextTimer)->next) {
|
|
MultiTimer* entry = *nextTimer;
|
|
if (entry == timer) {
|
|
*nextTimer = timer->next;
|
|
break;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int MultiTimerYield(void)
|
|
{
|
|
MultiTimer* entry = timerList;
|
|
for (; entry; entry = entry->next) {
|
|
/* Sorted list, just process with the front part. */
|
|
if (platformTicksFunction() < entry->deadline) {
|
|
return (int)(entry->deadline - platformTicksFunction());
|
|
}
|
|
/* remove expired timer from list */
|
|
timerList = entry->next;
|
|
|
|
/* call callback */
|
|
if (entry->callback) {
|
|
entry->callback(entry, entry->userData);
|
|
if(entry->is_repeated == 1) {
|
|
MultiTimerStart(entry, entry->timeout, entry->callback, entry->userData, 1);
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|