-
Notifications
You must be signed in to change notification settings - Fork 31
/
Alarms.cpp
223 lines (191 loc) · 5.17 KB
/
Alarms.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
/**
* MIT Emergency Ventilator Controller
*
* MIT License:
*
* Copyright (c) 2020 MIT
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* Alarms.cpp
*/
#include "Alarms.h"
namespace alarms {
/// Tone ///
Tone::Tone(const Note notes[], const int& notes_length, const int* pin):
notes_(notes),
pin_(pin),
length_(notes_length),
tone_step_(length_) {}
void Tone::play() {
if (length_ == 0) {
return;
}
if (!playing_) { // Do once when tone starts
tone_timer_ = millis();
tone_step_ = 0;
playing_ = true;
}
tone_step_ %= length_; // Start again if tone finished
if (millis() > tone_timer_) {
tone(*pin_, notes_[tone_step_].note, notes_[tone_step_].duration);
tone_timer_ += notes_[tone_step_].duration + notes_[tone_step_].pause;
tone_step_ ++;
}
}
/// Beeper ///
void Beeper::begin() {
snooze_button_.begin();
pinMode(beeper_pin_, OUTPUT);
}
void Beeper::update(const AlarmLevel& alarm_level) {
if (snoozeButtonPressed()) {
toggleSnooze();
}
// check if snooze time is up
if (snoozed_ && millis() - snooze_time_ > kSnoozeTime) {
snoozed_ = false;
}
if (snoozed_) {
stop();
} else {
play(alarm_level);
}
}
bool Beeper::snoozeButtonPressed() const {
return snooze_button_.is_LOW();
}
void Beeper::toggleSnooze() {
if (snoozed_) {
snoozed_ = false;
} else {
snoozed_ = true;
snooze_time_ = millis();
}
}
void Beeper::play(const AlarmLevel& alarm_level) {
for (int i = 0; i < NUM_LEVELS; i++) {
if (i != alarm_level) {
tones_[i].stop();
}
}
tones_[alarm_level].play();
}
void Beeper::stop() {
for (int i = 0; i < NUM_LEVELS; i++) {
tones_[i].stop();
}
}
/// Alarm ///
Alarm::Alarm(const String& default_text, const int& min_bad_to_trigger,
const int& min_good_to_clear, const AlarmLevel& alarm_level):
text_(default_text),
min_bad_to_trigger_(min_bad_to_trigger),
min_good_to_clear_(min_good_to_clear),
alarm_level_(alarm_level) {}
void Alarm::reset() {
*this = Alarm::Alarm(text_, min_bad_to_trigger_, min_good_to_clear_, alarm_level_);
}
void Alarm::setCondition(const bool& bad, const unsigned long& seq) {
if (bad) {
consecutive_bad_ += (seq != last_bad_seq_);
last_bad_seq_ = seq;
if (!on_) {
on_ = consecutive_bad_ >= min_bad_to_trigger_;
}
consecutive_good_ = 0;
} else {
consecutive_good_ += (seq != last_good_seq_);
last_good_seq_ = seq;
if (on_) {
on_ = consecutive_good_ < min_good_to_clear_;
}
consecutive_bad_ = 0;
}
}
void Alarm::setText(const String& text) {
if (text.length() == display::kWidth) {
text_ = text;
}
else if (text.length() > display::kWidth) {
text_ = text.substring(0, display::kWidth);
}
else {
text_ = text;
while (text_.length() < display::kWidth) {
text_ += " ";
}
}
}
/// AlarmManager ///
void AlarmManager::begin() {
beeper_.begin();
pinMode(led_pin_, OUTPUT);
}
void AlarmManager::update() {
displ_->setAlarmText(getText());
AlarmLevel highest_level = getHighestLevel();
beeper_.update(highest_level);
if (highest_level > NO_ALARM) {
digitalWrite(led_pin_, led_pulse_.read() ? HIGH : LOW);
}
else {
digitalWrite(led_pin_, LOW);
}
}
void AlarmManager::allOff() {
for (int i = 0; i < NUM_ALARMS; i++) {
alarms_[i].reset();
}
}
int AlarmManager::numON() const {
int num = 0;
for (int i = 0; i < NUM_ALARMS; i++) {
num += (int)alarms_[i].isON();
}
return num;
}
String AlarmManager::getText() const {
const int num_on = numON();
String text = "";
if (num_on > 0) {
// determine which of the on alarms to display
const int index = millis() % (num_on * kDisplayTime) / kDisplayTime;
int count_on = 0;
int i;
for (i = 0; i < NUM_ALARMS; i++) {
if (alarms_[i].isON()) {
if (count_on++ == index) break;
}
}
text = alarms_[i].text();
}
return text;
}
AlarmLevel AlarmManager::getHighestLevel() const {
AlarmLevel alarm_level = NO_ALARM;
for (int i = 0; i < NUM_ALARMS; i++) {
if (alarms_[i].isON()) {
alarm_level = max(alarm_level, alarms_[i].alarmLevel());
}
}
return alarm_level;
}
} // namespace alarms