forked from jhagberg/ESP8266TimeAlarms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESP8266TimeAlarms.cpp
295 lines (258 loc) · 8.92 KB
/
ESP8266TimeAlarms.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
TimeAlarms.cpp - Arduino Time alarms for use with Time library
Copyright (c) 2008-2011 Michael Margolis.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
*/
/*
2 July 2011 - replaced alarm types implied from alarm value with enums to make trigger logic more robust
- this fixes bug in repeating weekly alarms - thanks to Vincent Valdy and draythomp for testing
*/
#include "ESP8266TimeAlarms.h"
#define IS_ONESHOT true // constants used in arguments to create method
#define IS_REPEAT false
//**************************************************************
//* Alarm Class Constructor
AlarmClass::AlarmClass()
{
Mode.isEnabled = Mode.isOneShot = 0;
Mode.alarmType = dtNotAllocated;
value = nextTrigger = 0;
onTickHandler = NULL; // prevent a callback until this pointer is explicitly set
}
//**************************************************************
//* Private Methods
void AlarmClass::updateNextTrigger()
{
if (Mode.isEnabled) {
time_t now = time(nullptr);
if (dtIsAlarm(Mode.alarmType) && nextTrigger <= now) {
// update alarm if next trigger is not yet in the future
if (Mode.alarmType == dtExplicitAlarm) {
// is the value a specific date and time in the future
nextTrigger = value; // yes, trigger on this value
} else if (Mode.alarmType == dtDailyAlarm) {
//if this is a daily alarm
if (value + previousMidnight(now) <= now) {
// if time has passed then set for tomorrow
nextTrigger = value + nextMidnight(now);
} else {
// set the date to today and add the time given in value
nextTrigger = value + previousMidnight(now);
}
} else if (Mode.alarmType == dtWeeklyAlarm) {
// if this is a weekly alarm
if ((value + previousSunday(now)) <= now) {
// if day has passed then set for the next week.
nextTrigger = value + nextSunday(now);
} else {
// set the date to this week today and add the time given in value
nextTrigger = value + previousSunday(now);
}
} else {
// its not a recognized alarm type - this should not happen
Mode.isEnabled = false; // Disable the alarm
}
}
if (Mode.alarmType == dtTimer) {
// its a timer
nextTrigger = now + value; // add the value to previous time (this ensures delay always at least Value seconds)
}
}
}
//**************************************************************
//* Time Alarms Public Methods
TimeAlarmsClass::TimeAlarmsClass()
{
isServicing = false;
for(uint8_t id = 0; id < dtNBR_ALARMS; id++) {
free(id); // ensure all Alarms are cleared and available for allocation
}
}
void TimeAlarmsClass::enable(AlarmID_t ID)
{
if (isAllocated(ID)) {
if (( !(dtUseAbsoluteValue(Alarm[ID].Mode.alarmType) && (Alarm[ID].value == 0)) ) && (Alarm[ID].onTickHandler != NULL)) {
// only enable if value is non zero and a tick handler has been set
// (is not NULL, value is non zero ONLY for dtTimer & dtExplicitAlarm
// (the rest can have 0 to account for midnight))
Alarm[ID].Mode.isEnabled = true;
Alarm[ID].updateNextTrigger(); // trigger is updated whenever this is called, even if already enabled
} else {
Alarm[ID].Mode.isEnabled = false;
}
}
}
void TimeAlarmsClass::disable(AlarmID_t ID)
{
if (isAllocated(ID)) {
Alarm[ID].Mode.isEnabled = false;
}
}
// write the given value to the given alarm
void TimeAlarmsClass::write(AlarmID_t ID, time_t value)
{
if (isAllocated(ID)) {
Alarm[ID].value = value; //note: we don't check value as we do it in enable()
Alarm[ID].nextTrigger = 0; // clear out previous trigger time (see issue #12)
enable(ID); // update trigger time
}
}
// return the value for the given alarm ID
time_t TimeAlarmsClass::read(AlarmID_t ID)
{
if (isAllocated(ID)) {
return Alarm[ID].value ;
} else {
return dtINVALID_TIME;
}
}
// return the alarm type for the given alarm ID
dtAlarmPeriod_t TimeAlarmsClass::readType(AlarmID_t ID)
{
if (isAllocated(ID)) {
return (dtAlarmPeriod_t)Alarm[ID].Mode.alarmType ;
} else {
return dtNotAllocated;
}
}
void TimeAlarmsClass::free(AlarmID_t ID)
{
if (isAllocated(ID)) {
Alarm[ID].Mode.isEnabled = false;
Alarm[ID].Mode.alarmType = dtNotAllocated;
Alarm[ID].onTickHandler = NULL;
Alarm[ID].value = 0;
Alarm[ID].nextTrigger = 0;
}
}
// returns the number of allocated timers
uint8_t TimeAlarmsClass::count()
{
uint8_t c = 0;
for(uint8_t id = 0; id < dtNBR_ALARMS; id++) {
if (isAllocated(id)) c++;
}
return c;
}
// returns true only if id is allocated and the type is a time based alarm, returns false if not allocated or if its a timer
bool TimeAlarmsClass::isAlarm(AlarmID_t ID)
{
return( isAllocated(ID) && dtIsAlarm(Alarm[ID].Mode.alarmType) );
}
// returns true if this id is allocated
bool TimeAlarmsClass::isAllocated(AlarmID_t ID)
{
return (ID < dtNBR_ALARMS && Alarm[ID].Mode.alarmType != dtNotAllocated);
}
// returns the currently triggered alarm id
// returns dtINVALID_ALARM_ID if not invoked from within an alarm handler
AlarmID_t TimeAlarmsClass::getTriggeredAlarmId()
{
if (isServicing) {
return servicedAlarmId; // new private data member used instead of local loop variable i in serviceAlarms();
} else {
return dtINVALID_ALARM_ID; // valid ids only available when servicing a callback
}
}
// following functions are not Alarm ID specific.
void TimeAlarmsClass::delay(unsigned long ms)
{
unsigned long start = millis();
while (millis() - start <= ms) {
serviceAlarms();
}
}
void TimeAlarmsClass::waitForDigits( uint8_t Digits, dtUnits_t Units)
{
while (Digits != getDigitsNow(Units)) {
serviceAlarms();
}
}
void TimeAlarmsClass::waitForRollover( dtUnits_t Units)
{
// if its just rolled over than wait for another rollover
while (getDigitsNow(Units) == 0) {
serviceAlarms();
}
waitForDigits(0, Units);
}
uint8_t TimeAlarmsClass::getDigitsNow( dtUnits_t Units)
{
time_t now = time(nullptr);
struct tm *NOW = localtime(&now);
if (Units == dtSecond) return NOW->tm_sec;
if (Units == dtMinute) return NOW->tm_min;
if (Units == dtHour) return NOW->tm_hour;
if (Units == dtDay) return NOW->tm_wday;
return 255; // This should never happen
}
//returns isServicing
bool TimeAlarmsClass::getIsServicing()
{
return isServicing;
}
//***********************************************************
//* Private Methods
void TimeAlarmsClass::serviceAlarms()
{
if (!isServicing) {
time_t now = time(nullptr);
isServicing = true;
for (servicedAlarmId = 0; servicedAlarmId < dtNBR_ALARMS; servicedAlarmId++) {
if (Alarm[servicedAlarmId].Mode.isEnabled && (now >= Alarm[servicedAlarmId].nextTrigger)) {
OnTick_t TickHandler = Alarm[servicedAlarmId].onTickHandler;
if (Alarm[servicedAlarmId].Mode.isOneShot) {
free(servicedAlarmId); // free the ID if mode is OnShot
} else {
Alarm[servicedAlarmId].updateNextTrigger();
}
if (TickHandler != NULL) {
TickHandler(); // call the handler
}
}
}
isServicing = false;
}
}
// returns the absolute time of the next scheduled alarm, or 0 if none
time_t TimeAlarmsClass::getNextTrigger()
{
time_t nextTrigger = (time_t)0xffffffff; // the max time value
for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
if (isAllocated(id)) {
if (Alarm[id].nextTrigger < nextTrigger) {
nextTrigger = Alarm[id].nextTrigger;
}
}
}
return nextTrigger == (time_t)0xffffffff ? 0 : nextTrigger;
}
// attempt to create an alarm and return true if successful
AlarmID_t TimeAlarmsClass::create(time_t value, OnTick_t onTickHandler, uint8_t isOneShot, dtAlarmPeriod_t alarmType)
{
time_t now = time(nullptr);
if ( ! ( (dtIsAlarm(alarmType) && now < SECS_PER_YEAR) || (dtUseAbsoluteValue(alarmType) && (value == 0)) ) ) {
// only create alarm ids if the time is at least Jan 1 1971
for (uint8_t id = 0; id < dtNBR_ALARMS; id++) {
if (Alarm[id].Mode.alarmType == dtNotAllocated) {
// here if there is an Alarm id that is not allocated
Alarm[id].onTickHandler = onTickHandler;
Alarm[id].Mode.isOneShot = isOneShot;
Alarm[id].Mode.alarmType = alarmType;
Alarm[id].value = value;
enable(id);
return id; // alarm created ok
}
}
}
return dtINVALID_ALARM_ID; // no IDs available or time is invalid
}
// make one instance for the user to use
TimeAlarmsClass Alarm = TimeAlarmsClass() ;