-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathalarm_controller.cpp
381 lines (328 loc) · 12.8 KB
/
alarm_controller.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/******************************************************************************
* @author Makers For Life
* @copyright Copyright (c) 2020 Makers For Life
* @file alarm_controller.cpp
* @brief Core logic to manage alarm features
*****************************************************************************/
#pragma once
// INCLUDES ===================================================================
// Externals
// Internals
#include "../includes/alarm_controller.h"
#include "../includes/buzzer.h"
#include "../includes/cycle.h"
#include "../includes/screen.h"
#include "../includes/telemetry.h"
// INITIALISATION =============================================================
AlarmController alarmController;
// FUNCTIONS ==================================================================
AlarmController::AlarmController()
: m_highestPriority(AlarmPriority::ALARM_NONE),
m_snoozeTime(0u),
m_alarms({
/**
* RCM-SW-2
* The device shall embed a high priority alarm 11 when the pressure is below < 2cmH2O
* from the 3th cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_2, 3u),
/* RCM-SW-1
* The device shall embed a high priority alarm 12 when the plateau pressure is not
* reached (absolute difference > 20% in absolute value) from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_1, 3u),
/**
* RCM-SW-12
* The device shall monitor the battery voltage and trig a high priority alarm 13 when
* voltage is < 22,6V (20% SoC).
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_12, 1u),
/**
* RCM-SW-3
* The device shall embed a high priority alarm 14 when the PEEP target is not reached
* (absolute difference > 2cmH2O) from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_3, 3u),
/**
* RCM-SW-4
* The device shall embed a high priority alarm 40 when Inspiratory minute Volume is too
* low from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_4, 3u),
/**
* RCM-SW-5
* The device shall embed a high priority alarm 41 when Inspiratory minute Volume is too
* high from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_5, 3u),
/**
* RCM-SW-6
* The device shall embed a high priority alarm 42 when Expiratory minute Volume is too
* low from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_6, 3u),
/**
* RCM-SW-7
* The device shall embed a high priority alarm 43 when Expiratory minute Volume is too
* high from the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_7, 3u),
/**
* RCM-SW-8
* The device shall embed a high priority alarm 44 when Respiratory rate is too low from
* the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_8, 3u),
/**
* RCM-SW-9
* The device shall embed a high priority alarm 45 when Respiratory rate is too high from
* the 3th respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_9, 3u),
/**
* RCM-SW-10
* The device shall embed a high priority alarm 46 when Leak is too high.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_10, 3u),
/**
* RCM-SW-18
* The device shall embed a high priority alarm 17 when the peak pressure is > 80cmH2O.
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_18, 1u),
/**
* RCM-SW-11
* The device shall monitor the battery voltage and trigger a medium priority alarm 21
* when voltage is < 23,2V (40% SoC).
*/
Alarm(AlarmPriority::ALARM_MEDIUM, RCM_SW_11, 1u),
/**
* RCM-SW-14
* The device shall embed a medium priority alarm 22 when the plateau pressure is not
* reached (absolute difference > 20% in absolute value) until the 2nd respiratory
* cycle.
*/
Alarm(AlarmPriority::ALARM_MEDIUM, RCM_SW_14, 2u),
/**
* RCM-SW-15
* The device shall embed a medium priority alarm 23 when the PEEP target is not reached
* (absolute difference > 2cmH2O) until the 2nd respiratory cycle.
*/
Alarm(AlarmPriority::ALARM_MEDIUM, RCM_SW_15, 2u),
/**
* RCM-SW-19
* The device shall embed a medium priority alarm 24 when the pressure is < 2cmH2O at the
* second cycle (patient disconnection).
*/
Alarm(AlarmPriority::ALARM_MEDIUM, RCM_SW_19, 2u),
/**
* RCM-SW-16
* The device shall embed an information (audible) signal 31 when the mains are
* disconnected to alert the user (vOut < 26,5V).
*/
Alarm(AlarmPriority::ALARM_LOW, RCM_SW_16, 1u),
/**
* RCM-SW-20
* The device shall embed a medium priority alarm 47 when Tidal Volume is too low
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_20, 3u),
/**
* RCM-SW-21
* The device shall embed a medium priority alarm 48 when Tidal Volume is too high
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_21, 3u),
/**
* RCM-SW-22
* The device shall embed a medium priority alarm 48 when peak pressure is too high
*/
Alarm(AlarmPriority::ALARM_HIGH, RCM_SW_22, 1u),
}),
m_tick(0u),
m_unsnooze(true),
m_pressure(0u),
m_phase(CyclePhases::INHALATION),
// cppcheck-suppress misra-c2012-5.2 ; false positive
m_cycle_number(0u) {
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
m_snoozedAlarms[i] = false;
}
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
m_triggeredAlarms[i] = 0u;
}
}
void AlarmController::snooze() {
if (m_unsnooze) {
m_unsnooze = false;
digitalWrite(PIN_LED_GREEN, LED_GREEN_ACTIVE);
m_snoozeTime = millis();
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
Alarm* current = &m_alarms[i];
if (current->isTriggered()) {
m_snoozedAlarms[i] = true;
} else {
m_snoozedAlarms[i] = false;
}
}
Buzzer_Mute();
}
sendControlAck(9, !m_unsnooze);
}
void AlarmController::detectedAlarm(uint8_t p_alarmCode,
uint32_t p_cycleNumber,
uint32_t p_expected,
uint32_t p_measured) {
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
Alarm* current = &m_alarms[i];
bool wasTriggered = current->isTriggered();
if (current->isEnabled() && (current->getCode() == p_alarmCode)) {
current->detected(p_cycleNumber);
if (current->isTriggered()) {
for (uint8_t j = 0; j < ALARMS_SIZE; j++) {
if (m_triggeredAlarms[j] == p_alarmCode) {
break;
}
if (m_triggeredAlarms[j] == 0u) {
m_triggeredAlarms[j] = p_alarmCode;
break;
}
}
if (!wasTriggered) {
sendAlarmTrap(m_tick, m_pressure, m_phase, m_cycle_number, current->getCode(),
current->getPriority(), true, p_expected, p_measured,
current->getCyclesSinceTrigger());
}
}
break;
}
}
}
bool compare(uint8_t a, uint8_t b) { return a > b; }
void AlarmController::notDetectedAlarm(uint8_t p_alarmCode) {
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
Alarm* current = &m_alarms[i];
bool wasTriggered = current->isTriggered();
if (current->getCode() == p_alarmCode) {
current->notDetected();
if (!current->isTriggered()) {
for (uint8_t j = 0; j < ALARMS_SIZE; j++) {
if (m_triggeredAlarms[j] == p_alarmCode) {
m_triggeredAlarms[j] = 0u;
std::sort(m_triggeredAlarms, &m_triggeredAlarms[ALARMS_SIZE], compare);
break;
}
}
if (wasTriggered) {
sendAlarmTrap(m_tick, m_pressure, m_phase, m_cycle_number, current->getCode(),
current->getPriority(), false, 0u, 0u,
current->getCyclesSinceTrigger());
}
}
break;
}
}
}
void AlarmController::runAlarmEffects(uint32_t p_tick) {
AlarmPriority highestPriority = AlarmPriority::ALARM_NONE;
uint8_t triggeredAlarmCodes[ALARMS_SIZE];
uint8_t numberOfTriggeredAlarms = 0;
bool justUnsnoozed = false;
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
Alarm* current = &m_alarms[i];
if (current->isTriggered()) {
if (numberOfTriggeredAlarms == 0u) {
highestPriority = current->getPriority();
}
triggeredAlarmCodes[numberOfTriggeredAlarms] = current->getCode();
numberOfTriggeredAlarms++;
if (!m_unsnooze && !m_snoozedAlarms[i]) {
unsnooze();
justUnsnoozed = true;
}
} else {
m_snoozedAlarms[i] = false;
}
}
uint32_t millisSinceSnooze = millis() - m_snoozeTime;
if (!m_unsnooze && (m_snoozeTime > 0u) && (millisSinceSnooze >= 120000u)) {
unsnooze();
}
if ((p_tick % (LCD_UPDATE_PERIOD_US / MAIN_CONTROLLER_COMPUTE_PERIOD_MICROSECONDS)) == 0u) {
displayAlarmInformation(triggeredAlarmCodes, numberOfTriggeredAlarms);
}
if (highestPriority == AlarmPriority::ALARM_HIGH) {
if ((m_highestPriority != highestPriority) || justUnsnoozed) {
if (m_unsnooze) {
Buzzer_High_Prio_Start();
}
}
if ((p_tick % 100u) == 50u) {
digitalWrite(PIN_LED_RED, LED_RED_ACTIVE);
} else if ((p_tick % 100u) == 0u) {
digitalWrite(PIN_LED_RED, LED_RED_INACTIVE);
} else {
}
digitalWrite(PIN_LED_YELLOW, LED_YELLOW_INACTIVE);
} else if (highestPriority == AlarmPriority::ALARM_MEDIUM) {
if ((m_highestPriority != highestPriority) || justUnsnoozed) {
if (m_unsnooze) {
Buzzer_Medium_Prio_Start();
}
}
digitalWrite(PIN_LED_RED, LED_RED_INACTIVE);
if ((p_tick % 100u) == 50u) {
digitalWrite(PIN_LED_YELLOW, LED_YELLOW_ACTIVE);
} else if ((p_tick % 100u) == 0u) {
digitalWrite(PIN_LED_YELLOW, LED_YELLOW_INACTIVE);
} else {
}
} else if (highestPriority == AlarmPriority::ALARM_LOW) {
if ((m_highestPriority != highestPriority) || justUnsnoozed) {
if (m_unsnooze) {
Buzzer_Low_Prio_Start();
}
}
digitalWrite(PIN_LED_RED, LED_RED_INACTIVE);
digitalWrite(PIN_LED_YELLOW, LED_YELLOW_ACTIVE);
} else {
Buzzer_Stop();
digitalWrite(PIN_LED_RED, LED_RED_INACTIVE);
digitalWrite(PIN_LED_YELLOW, LED_YELLOW_INACTIVE);
}
m_highestPriority = highestPriority;
}
// cppcheck-suppress unusedFunction
void AlarmController::unsnooze() {
digitalWrite(PIN_LED_GREEN, LED_GREEN_INACTIVE);
m_snoozeTime = 0u;
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
m_snoozedAlarms[i] = false;
}
m_unsnooze = true;
sendControlAck(9, !m_unsnooze);
}
// cppcheck-suppress unusedFunction
void AlarmController::updateCoreData(uint32_t p_tick,
uint16_t p_pressure,
CyclePhases p_phase,
uint32_t p_cycle_number) {
m_tick = p_tick;
m_pressure = p_pressure;
m_phase = p_phase;
m_cycle_number = p_cycle_number;
}
void AlarmController::updateEnabledAlarms(Alarms enabledAlarms) {
// Disable every alarms
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
m_alarms[i].disable();
}
// Enable provided alarms
for (uint8_t i = 0; i < ALARMS_SIZE; i++) {
if (enabledAlarms.alarms[i] != 0u) {
for (uint8_t j = 0; j < ALARMS_SIZE; j++) {
if (m_alarms[j].getCode() == enabledAlarms.alarms[i]) {
m_alarms[j].enable();
break;
}
}
}
}
}