Program for the water play project for the course Software Testing Practical 2016 given at the VU University

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Committer:
sbouber1
Date:
Mon Jun 20 10:22:10 2016 +0000
Revision:
64:735009c4c8aa
Parent:
61:6b5c2ddcea0c
Child:
67:851db0511c7c
fixed alarm error msgs

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sbouber1 57:8dc3192ff150 1 #include "SensorAlarmController.h"
sbouber1 38:930469a33001 2
sbouber1 38:930469a33001 3
joran 27:4f73f754fdc9 4 //outputpins for alarm
sbouber1 57:8dc3192ff150 5 static DigitalOut buzzer(p17);
sbouber1 57:8dc3192ff150 6 static DigitalOut led1(LED1);
sbouber1 57:8dc3192ff150 7 static DigitalOut led2(LED2);
sbouber1 57:8dc3192ff150 8 static DigitalOut led3(LED3);
sbouber1 57:8dc3192ff150 9 static DigitalOut led4(LED4);
joran 20:521f795ea9d7 10
sbouber1 58:b5f0c0f305ff 11 void SensorAlarmController::update() {
sbouber1 38:930469a33001 12
sbouber1 57:8dc3192ff150 13 #ifndef TEST_MODE
sbouber1 38:930469a33001 14 if(this->num_iters < STARTUP_ITERATIONS) {
sbouber1 64:735009c4c8aa 15 cout << this->getName() << ": not running, startup phase";
sbouber1 38:930469a33001 16 return;
sbouber1 38:930469a33001 17 }
sbouber1 38:930469a33001 18 #endif
sbouber1 38:930469a33001 19
sbouber1 59:614f713fb48b 20 value = sensor->getValue();
joran 20:521f795ea9d7 21
sbouber1 59:614f713fb48b 22 if ((value < min_undesired) || (value > max_undesired)) {
sbouber1 59:614f713fb48b 23 if ((value < min_crit) || (value > max_crit)) {
sbouber1 59:614f713fb48b 24 this->raiseAlarm(true);
joran 20:521f795ea9d7 25 } else {
sbouber1 59:614f713fb48b 26 this->raiseAlarm(false);
joran 20:521f795ea9d7 27 }
joran 20:521f795ea9d7 28 } else {
joran 20:521f795ea9d7 29 //clear alarm
sbouber1 59:614f713fb48b 30 timer.stop();
sbouber1 59:614f713fb48b 31 this->is_crit = false;
sbouber1 59:614f713fb48b 32 }
joran 27:4f73f754fdc9 33
joran 20:521f795ea9d7 34 }
joran 20:521f795ea9d7 35
sbouber1 59:614f713fb48b 36 void SensorAlarmController::raiseAlarm(bool isCrit) {
sbouber1 59:614f713fb48b 37
sbouber1 59:614f713fb48b 38 this->is_crit = isCrit;
sbouber1 59:614f713fb48b 39 int readtimer = timer.read();
sbouber1 59:614f713fb48b 40
sbouber1 59:614f713fb48b 41 if (isCrit)
sbouber1 64:735009c4c8aa 42 cout << "Received a critical " << this->sensor->getName() << " alarm " << value << " timer is at " << readtimer << "\r\n";
sbouber1 59:614f713fb48b 43 if (!isCrit)
sbouber1 64:735009c4c8aa 44 cout << "Received a non-critical " << this->sensor->getName() << " alarm " << value << " timer is at " << readtimer << "\r\n";
sbouber1 59:614f713fb48b 45
joran 20:521f795ea9d7 46 if (readtimer > 0 ) { //already running
sbouber1 59:614f713fb48b 47 if ((readtimer >= NUMBER_OF_SEC_BEFORE_CRITICAL_ALARM && is_crit) ||
sbouber1 59:614f713fb48b 48 (readtimer >= NUMBER_OF_SEC_BEFORE_UNDESIRED_ALARM && !is_crit)) {
joran 27:4f73f754fdc9 49
joran 27:4f73f754fdc9 50 this->error = true;
sbouber1 61:6b5c2ddcea0c 51 if (this->is_crit) {
sbouber1 64:735009c4c8aa 52 this->error_msg = this->error_msg_critical;
sbouber1 59:614f713fb48b 53 } else {
sbouber1 64:735009c4c8aa 54 this->error_msg = this->error_msg_undesired;
sbouber1 59:614f713fb48b 55 }
sbouber1 59:614f713fb48b 56
sbouber1 64:735009c4c8aa 57 cout << "### " << this->getName() << " alarm has been triggered after " << " ###" << readtimer << "\r\n";
joran 20:521f795ea9d7 58 buzzOnce();
sbouber1 59:614f713fb48b 59 timer.stop();
sbouber1 59:614f713fb48b 60 timer.reset();
sbouber1 59:614f713fb48b 61 this->is_crit = false;
joran 20:521f795ea9d7 62 }
joran 20:521f795ea9d7 63 } else {
sbouber1 59:614f713fb48b 64 timer.start();
joran 27:4f73f754fdc9 65 }
joran 27:4f73f754fdc9 66 }
joran 27:4f73f754fdc9 67
sbouber1 58:b5f0c0f305ff 68 std::string SensorAlarmController::getName() {
sbouber1 58:b5f0c0f305ff 69 return "AlarmController[" + this->sensor->getName() + "]";
sbouber1 11:1a0a8fd74bc0 70 }
sbouber1 11:1a0a8fd74bc0 71
sbouber1 58:b5f0c0f305ff 72 bool SensorAlarmController::isError() {
sbouber1 11:1a0a8fd74bc0 73 return this->error;
sbouber1 11:1a0a8fd74bc0 74 }
sbouber1 11:1a0a8fd74bc0 75
sbouber1 64:735009c4c8aa 76 std::string SensorAlarmController::getErrorMessage() {
sbouber1 11:1a0a8fd74bc0 77 return this->error_msg;
sbouber1 11:1a0a8fd74bc0 78 }
sbouber1 11:1a0a8fd74bc0 79
sbouber1 64:735009c4c8aa 80 void SensorAlarmController::setCriticalErrorMsg(std::string msg) {
sbouber1 64:735009c4c8aa 81 this->error_msg_critical = msg;
sbouber1 64:735009c4c8aa 82 }
sbouber1 64:735009c4c8aa 83
sbouber1 64:735009c4c8aa 84 void SensorAlarmController::setUndesiredErrorMsg(std::string msg) {
sbouber1 64:735009c4c8aa 85 this->error_msg_undesired = msg;
sbouber1 64:735009c4c8aa 86 }
sbouber1 64:735009c4c8aa 87
joran 20:521f795ea9d7 88
sbouber1 57:8dc3192ff150 89 void SensorAlarmController::buzzOnce() {
joran 20:521f795ea9d7 90
joran 20:521f795ea9d7 91 buzzer = 1;
joran 20:521f795ea9d7 92 wait(0.1);
joran 20:521f795ea9d7 93 buzzer = 0;
joran 20:521f795ea9d7 94
sbouber1 11:1a0a8fd74bc0 95 }