summaryrefslogtreecommitdiff
path: root/src/sys/sys_timer.c
blob: b00fbc9cf2e339005930467110099a9d8d634c71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "sys.h"

TimerTask sTimerTasks[16];

TimerTask* Timer_AllocateTask(void) {
    s32 i;

    for (i = 0; i < ARRAY_COUNT(sTimerTasks); i++) {
        if (!sTimerTasks[i].active) {
            return &sTimerTasks[i];
        }
    }
    return NULL;
}

int32_t Timer_CreateTask(uint64_t time, TimerAction action, s32* address, s32 value) {
    TimerTask* task = Timer_AllocateTask();

    if (task == NULL) {
        return -1;
    }
    task->active = true;
    task->action = action;
    task->address = address;
    task->value = value;
    return osSetTimer(&task->timer, time, 0, &gTimerTaskMsgQueue, OS_MESG_PTR(task));
}

void Timer_Increment(s32* address, s32 value) {
    *address += value;
}

void Timer_SetValue(s32* address, s32 value) {
    *address = value;
}

void Timer_CompleteTask(TimerTask* task) {
    if (task->action != NULL) {
        task->action(task->address, task->value);
    }
    task->active = false;
}

void Timer_Wait(u64 time) {
    OSTimer timer;
    OSMesg dummy;

    osSetTimer(&timer, time, 0, &gTimerWaitMesgQueue, NULL);
    MQ_WAIT_FOR_MESG(&gTimerWaitMesgQueue, &dummy);
}