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:
Fri Jun 24 13:51:42 2016 +0000
Revision:
80:38e274c4dafa
Parent:
73:bafd8d0f3daf
final

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 72:f8c4f731f0fe 30 printf("Clearing alarm: %s\r\n", this->getName().c_str());
sbouber1 59:614f713fb48b 31 timer.stop();
sbouber1 72:f8c4f731f0fe 32 timer.reset();
sbouber1 59:614f713fb48b 33 this->is_crit = false;
joran 67:851db0511c7c 34 sensor->setLed(false);
sbouber1 59:614f713fb48b 35 }
joran 27:4f73f754fdc9 36
joran 20:521f795ea9d7 37 }
joran 20:521f795ea9d7 38
sbouber1 59:614f713fb48b 39 void SensorAlarmController::raiseAlarm(bool isCrit) {
sbouber1 59:614f713fb48b 40
sbouber1 59:614f713fb48b 41 this->is_crit = isCrit;
sbouber1 59:614f713fb48b 42 int readtimer = timer.read();
sbouber1 59:614f713fb48b 43
sbouber1 59:614f713fb48b 44 if (isCrit)
sbouber1 72:f8c4f731f0fe 45 printf("Received a critical %s alarm %f timer is at %d\r\n", this->sensor->getName().c_str(), value, readtimer);
sbouber1 59:614f713fb48b 46 if (!isCrit)
sbouber1 72:f8c4f731f0fe 47 printf("Received a non-critical %s alarm %f timer is at %d\r\n", this->sensor->getName().c_str(), value, readtimer);
sbouber1 59:614f713fb48b 48
joran 20:521f795ea9d7 49 if (readtimer > 0 ) { //already running
sbouber1 59:614f713fb48b 50 if ((readtimer >= NUMBER_OF_SEC_BEFORE_CRITICAL_ALARM && is_crit) ||
sbouber1 59:614f713fb48b 51 (readtimer >= NUMBER_OF_SEC_BEFORE_UNDESIRED_ALARM && !is_crit)) {
joran 27:4f73f754fdc9 52
joran 27:4f73f754fdc9 53 this->error = true;
sbouber1 61:6b5c2ddcea0c 54 if (this->is_crit) {
sbouber1 64:735009c4c8aa 55 this->error_msg = this->error_msg_critical;
sbouber1 59:614f713fb48b 56 } else {
sbouber1 64:735009c4c8aa 57 this->error_msg = this->error_msg_undesired;
sbouber1 59:614f713fb48b 58 }
sbouber1 59:614f713fb48b 59
sbouber1 72:f8c4f731f0fe 60 printf("### %s alarm has been triggered after ### %d seconds\r\n", this->getName().c_str(), readtimer);
joran 67:851db0511c7c 61 sensor->setLed(true);
joran 20:521f795ea9d7 62 buzzOnce();
sbouber1 59:614f713fb48b 63 timer.stop();
sbouber1 59:614f713fb48b 64 timer.reset();
sbouber1 59:614f713fb48b 65 this->is_crit = false;
joran 20:521f795ea9d7 66 }
joran 20:521f795ea9d7 67 } else {
sbouber1 59:614f713fb48b 68 timer.start();
joran 27:4f73f754fdc9 69 }
joran 27:4f73f754fdc9 70 }
joran 27:4f73f754fdc9 71
sbouber1 58:b5f0c0f305ff 72 std::string SensorAlarmController::getName() {
sbouber1 58:b5f0c0f305ff 73 return "AlarmController[" + this->sensor->getName() + "]";
sbouber1 11:1a0a8fd74bc0 74 }
sbouber1 11:1a0a8fd74bc0 75
sbouber1 58:b5f0c0f305ff 76 bool SensorAlarmController::isError() {
sbouber1 11:1a0a8fd74bc0 77 return this->error;
sbouber1 11:1a0a8fd74bc0 78 }
sbouber1 11:1a0a8fd74bc0 79
sbouber1 64:735009c4c8aa 80 std::string SensorAlarmController::getErrorMessage() {
sbouber1 11:1a0a8fd74bc0 81 return this->error_msg;
sbouber1 11:1a0a8fd74bc0 82 }
sbouber1 11:1a0a8fd74bc0 83
sbouber1 64:735009c4c8aa 84 void SensorAlarmController::setCriticalErrorMsg(std::string msg) {
sbouber1 64:735009c4c8aa 85 this->error_msg_critical = msg;
sbouber1 64:735009c4c8aa 86 }
sbouber1 64:735009c4c8aa 87
sbouber1 64:735009c4c8aa 88 void SensorAlarmController::setUndesiredErrorMsg(std::string msg) {
sbouber1 64:735009c4c8aa 89 this->error_msg_undesired = msg;
sbouber1 64:735009c4c8aa 90 }
sbouber1 64:735009c4c8aa 91
sbouber1 73:bafd8d0f3daf 92 bool SensorAlarmController::isActive() {
sbouber1 72:f8c4f731f0fe 93 return this->timer.read() > 0;
sbouber1 72:f8c4f731f0fe 94 }
joran 20:521f795ea9d7 95
sbouber1 57:8dc3192ff150 96 void SensorAlarmController::buzzOnce() {
joran 20:521f795ea9d7 97
joran 20:521f795ea9d7 98 buzzer = 1;
joran 20:521f795ea9d7 99 wait(0.1);
joran 20:521f795ea9d7 100 buzzer = 0;
joran 20:521f795ea9d7 101
sbouber1 11:1a0a8fd74bc0 102 }