-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalarmHandler.cpp
107 lines (89 loc) · 2.62 KB
/
alarmHandler.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
#include "alarmHandler.h"
AlarmHandler::AlarmHandler (StripHandler &strips, SoundHandler &buzzer) {
this->strips = &strips;
this->buzzer = &buzzer;
for (uint8_t i = 0; i < NR_ALARMS; i++) {
setAlarms[i] = false;
}
}
void AlarmHandler::setAlarm (const Alarm &newAlarm) {
//Serial.println("Set an alarm");
//Serial.print(newAlarm.getAlarmHour()); Serial.print(":"); Serial.println(newAlarm.getAlarmMinute());
uint8_t i;
for (i = 0; (i < NR_ALARMS); i++) {
if (!setAlarms[i]) {
break;
}
}
alarms[i] = newAlarm;
setAlarms[i] = true;
}
void AlarmHandler::deleteAlarm (const Alarm &alarm) {
//Serial.print("Delete an alarm");
//Serial.print(alarm.getAlarmHour()); Serial.print(":"); Serial.println(alarm.getAlarmMinute());
for (uint8_t i = 0; i < NR_ALARMS; i++) {
if (alarms[i] == alarm) {
setAlarms[i] = false;
return;
}
}
}
void AlarmHandler::deleteAllAlarms () {
//Serial.print("Delete all alarms");
for (uint8_t i = 0; i < NR_ALARMS; i++) {
setAlarms[i] = false;
}
}
void AlarmHandler::pollAlarms (const DateTime &timeNow) {
uint8_t nextDay = 0;
uint8_t doW = timeNow.dayOfTheWeek();
for (uint8_t i = 0; i < NR_ALARMS; i++) {
if (alarms[i].isActive()) {
nextDay = alarms[i].getNextDay (doW);
if (nextDay == doW) {
if (alarms[i].getAlarmHour() == timeNow.hour() && alarms[i].getAlarmMinute() == timeNow.minute() && timeNow.second() == 1) {
if (alarms[i] == snoozedAlarm) {
deleteAlarm (alarms[i]);
snoozedAlarm = Alarm();
}
//Serial.println("alarm start");
snoozeTime = alarms[i].getSnoozeTime();
activeSound = alarms[i].getSound();
isRinging = true;
strips->alarm (true);
}
}
}
}
}
void AlarmHandler::updateSound () {
unsigned long now = millis();
if (isRinging && now > nextUpdate) {
nextUpdate = now + buzzer->getDuration (activeSound);
buzzer->playAlarm (activeSound);
strips->setStripsOn (true, groupTop);
strips->setTimeOff (now + (buzzer->getDuration (activeSound) >> 1));
}
}
void AlarmHandler::snooze (const DateTime &timeNow) {
if (snoozeTime) {
//Serial.println("Alarm snoozes");
uint8_t h, m;
h = timeNow.hour();
m = timeNow.minute();
if (m >= (60 - snoozeTime)) {
h++;
}
m += snoozeTime;
m %= 60;
Alarm newSnoozedAlarm (B11111111, h, m, 1, 0, snoozeTime);
snoozedAlarm = newSnoozedAlarm;
setAlarm (newSnoozedAlarm);
}
dismiss();
}
void AlarmHandler::dismiss() {
//Serial.println("Alarm dismissed");
isRinging = false;
strips->alarm (false);
}