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

Revision:
20:521f795ea9d7
Parent:
11:1a0a8fd74bc0
Child:
24:eeef4009640d
Child:
27:4f73f754fdc9
diff -r ee89eabe1fa2 -r 521f795ea9d7 AlarmController.cpp
--- a/AlarmController.cpp	Mon Jun 13 12:33:52 2016 +0000
+++ b/AlarmController.cpp	Mon Jun 13 12:51:19 2016 +0000
@@ -1,8 +1,104 @@
 #include "AlarmController.h"
 
+Timer temptimer;
+Timer salttimer;
+
+DigitalOut buzzer(p17);
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+DigitalOut led4(LED4);
+
+float temp_value_min_crit = 30.0f;
+float temp_value_min_undesired = 32.0f;
+float temp_value_max_crit = 40.0f;
+float temp_value_max_undesired = 38.0f;
+
+float salt_value_min_crit = 3.0f;
+float salt_value_min_undesired = 6.0f;
+float salt_value_max_crit = 12.0f;
+float salt_value_max_undesired = 9.0f;
+
+float readtemp;
+float readsalt;
+
 void AlarmController::update() {
+    readtemp = this->temp->getValue();
+    
+    if ((readtemp < temp_value_min_undesired) || (readtemp > temp_value_max_undesired))
+    {
+        if ((readtemp < temp_value_min_crit) || (readtemp > temp_value_max_crit))
+        {
+            this->raiseAlarmTemp(true);
+        } else {
+            this->raiseAlarmTemp(false);
+        }
+    } else {
+        //clear alarm
+        temptimer.stop();
+        this->tempiscrit = false;
+    }    
+    
     
     
+    readsalt = this->salt->getValue();
+    
+    if ((readsalt < salt_value_min_undesired) || (readsalt > salt_value_max_undesired))
+    {
+        if ((readsalt < salt_value_min_crit) || (readsalt > salt_value_max_crit))
+        {
+            this->raiseAlarmSalt(true);
+        } else {
+            this->raiseAlarmSalt(false);
+        }
+    } else {
+        //clear alarm
+        salttimer.stop();
+        this->saltiscrit = false;
+    }    
+    
+}
+
+void AlarmController::raiseAlarmTemp(bool isCrit)
+{ 
+    this->tempiscrit = isCrit;
+    int readtimer = temptimer.read();
+    if (isCrit) printf("Received a critical temperature alarm (%.1f), timer is at %d\r\n",readtemp,readtimer);
+    if (!isCrit) printf("Received a non-critical temperature alarm (%.1f), timer is at %d\r\n",readtemp,readtimer);
+    if (readtimer > 0 ) { //already running
+        if ((readtimer >= 30 && tempiscrit) || (readtimer >= 300 && !tempiscrit))
+        {
+            //DO STUFF!!
+            printf("### Temperature alarm has been triggered after %d ###\r\n",readtimer);
+            buzzOnce();
+            temptimer.stop();
+            temptimer.reset();
+            this->tempiscrit = false;
+        }
+    } else {
+        temptimer.start();    
+    }
+}
+
+void AlarmController::raiseAlarmSalt(bool isCrit)
+{
+    this->saltiscrit = isCrit;
+    int readtimer = salttimer.read();
+    if (isCrit) printf("Received a critical salt alarm (%.3f), timer is at %d\r\n",readsalt,readtimer);
+    if (!isCrit) printf("Received a non-critical salt alarm (%.3f), timer is at %d\r\n",readsalt,readtimer);
+    if (readtimer > 0 ) { //already running
+        if ((readtimer >= 30 && saltiscrit) || (readtimer >= 300 && !saltiscrit))
+        {
+            //DO STUFF!!
+            printf("### Salt alarm has been triggered after %d seconds ###\r\n",readtimer);
+            buzzOnce();
+            salttimer.stop();
+            salttimer.reset();
+            this->saltiscrit = false;
+        }
+    } else {
+        salttimer.start();    
+    }
 }
 
 std::string AlarmController::get_name() {
@@ -19,4 +115,20 @@
 
 void AlarmController::activate_buzzer() {
     printf("Should activate buzzer now\r\n");    
+}
+
+void AlarmController::buzzOnce() {
+    
+    buzzer = 1;
+    led1=1;
+    led2=1;
+    led3=1;
+    led4=1;
+    wait(0.1);
+    buzzer = 0;
+    led1=0;
+    led2=0;
+    led3=0;
+    led4=0;
+            
 }
\ No newline at end of file