-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsofttimer.c
executable file
·103 lines (88 loc) · 1.69 KB
/
softtimer.c
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <inttypes.h>
#include <stddef.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <util/atomic.h>
#include "softtimer.h"
#include "rtc.h"
#define MAXEVENTS 10
typedef struct cb_info
{
time_t trigger;
call_back cb;
} cb_data;
cb_data st_event_list_global[MAXEVENTS];
void soft_timer_init()
{
memset(st_event_list_global, 0, MAXEVENTS*sizeof(cb_data));
}
void update_timers(int32_t deltat)
{
uint8_t index;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
for (index = 0; index < MAXEVENTS; index++)
{
if (st_event_list_global[index].trigger)
{
st_event_list_global[index].trigger += deltat;
}
}
}
}
int8_t set_timer(time_t trigger, call_back cb)
{
uint8_t index;
cb_data data;
data.trigger = trigger;
data.cb = cb;
if (!trigger || !cb)
return -1;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
for (index = 0; index < MAXEVENTS; index++)
{
if (!st_event_list_global[index].trigger)
{
st_event_list_global[index] = data;
break;
}
}
}
return index;
}
void reset_timer(time_t trigger, call_back cb, uint8_t slot)
{
cb_data data;
data.trigger = trigger;
data.cb = cb;
if (!trigger || !cb || !(slot < MAXEVENTS))
return;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
st_event_list_global[slot] = data;
}
}
void exec_nonatomic(call_back cb)
{
sei();
(*cb)();
cli();
}
void soft_timer_tick()
{
uint8_t index;
time_t current_time = time();
ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
{
for (index = 0; index < MAXEVENTS; index++)
{
if (st_event_list_global[index].trigger != 0 && st_event_list_global[index].trigger <= current_time)
{
st_event_list_global[index].trigger = 0;
exec_nonatomic(st_event_list_global[index].cb);
}
}
}
}