-
Notifications
You must be signed in to change notification settings - Fork 71
/
AlarmClock.cpp
153 lines (137 loc) · 3.27 KB
/
AlarmClock.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//- -----------------------------------------------------------------------------------------------------------------------
// AskSin++
// 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//- -----------------------------------------------------------------------------------------------------------------------
#include "AlarmClock.h"
namespace as {
SysClock sysclock;
#ifdef ARDUINO_ARCH_EFM32
void callback(sl_sleeptimer_timer_handle_t *id , __attribute__((unused)) void *user) {
--sysclock;
//DPRINT(".");
sl_sleeptimer_restart_timer_ms( id, SysClock::instance().getTimeout(), callback , (void *)NULL, 0, 0);
}
#else
void callback(void) {
--sysclock;
//DPRINT(".");
}
#endif
#ifndef NORTC
RealTimeClock rtc;
#if defined(ARDUINO_ARCH_EFM32)
void rtccallback(__attribute__((unused)) sl_sleeptimer_timer_handle_t * id , __attribute__((unused)) void *user) {
#else
void rtccallback () {
#endif
rtc.overflow();
//rtc.debug();
--rtc;
//DPRINT('.');
}
#endif
#if ARDUINO_ARCH_AVR
ISR(TIMER1_OVF_vect) {
callback();
}
#ifndef NORTC
ISR(TIMER2_OVF_vect) {
rtccallback();
}
#endif
#endif
void AlarmClock::cancel(Alarm& item) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
item.active(false);
Alarm *tmp = (Alarm*) select();
Link *vor = this;
// search for the alarm to cancel
while (tmp != 0) {
if (tmp == &item) {
vor->unlink();
// raise next alarm about item ticks
tmp = (Alarm*) item.select();
if (tmp != 0) {
tmp->tick += item.tick;
}
return;
}
vor = tmp;
tmp = (Alarm*) tmp->select();
}
// cancel also in ready queue
ready.remove(item);
}
}
AlarmClock& AlarmClock::operator --() {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
Alarm* alarm = (Alarm*) select();
if (alarm != 0) {
--alarm->tick;
while ((alarm != 0) && (alarm->tick == 0)) {
unlink(); // remove expired alarm
// run in interrupt
if (alarm->async() == true) {
alarm->active(false);
alarm->trigger(*this);
}
// run in application
else {
ready.append(*alarm);
}
alarm = (Alarm*) select();
}
}
}
return *this;
}
void AlarmClock::add(Alarm& item) {
if (item.tick > 0) {
ATOMIC_BLOCK( ATOMIC_RESTORESTATE )
{
item.active(true);
Link* prev = this;
Alarm* temp = (Alarm*) select();
while ((temp != 0) && (temp->tick < item.tick)) {
item.tick -= temp->tick;
prev = temp;
temp = (Alarm*) temp->select();
}
item.select(temp);
prev->select(&item);
if (temp != 0) {
temp->tick -= item.tick;
}
}
} else {
ready.append(item);
}
}
uint32_t AlarmClock::get(const Alarm& item) const {
uint32_t aux = 0;
Alarm* tmp = (Alarm*) select();
while (tmp != 0) {
aux += tmp->tick;
if (tmp == &item) {
return aux;
}
tmp = (Alarm*) tmp->select();
}
return 0;
}
bool RTCAlarm::delayMillis () {
if( millis != 0 ) {
set(millis2ticks(millis));
sysclock.add(*this);
millis = 0;
return true;
}
return false;
}
SysClock& SysClock::instance() { return sysclock; }
#ifndef NORTC
RealTimeClock& RealTimeClock::instance() { return rtc; }
#endif
}