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

SensorAlarmController.cpp

Committer:
sbouber1
Date:
2016-06-19
Revision:
61:6b5c2ddcea0c
Parent:
60:17e0a8e2be4a
Child:
64:735009c4c8aa

File content as of revision 61:6b5c2ddcea0c:

#include "SensorAlarmController.h"


//outputpins for alarm
static DigitalOut buzzer(p17);
static DigitalOut led1(LED1);
static DigitalOut led2(LED2);
static DigitalOut led3(LED3);
static DigitalOut led4(LED4);

void SensorAlarmController::update() {
    
    #ifndef TEST_MODE
    if(this->num_iters < STARTUP_ITERATIONS) {
        printf("SensorAlarmController: not running, startup phase");
        return;
    }
    #endif
    
    value = sensor->getValue();
    
    if ((value < min_undesired) || (value > max_undesired)) {
        if ((value < min_crit) || (value > max_crit)) {
            this->raiseAlarm(true);
        } else {
            this->raiseAlarm(false);
        }
    } else {
        //clear alarm
        timer.stop();
        this->is_crit = false;
    }
    
}

void SensorAlarmController::raiseAlarm(bool isCrit) { 

    this->is_crit = isCrit;
    int readtimer = timer.read();
    
    if (isCrit)
        printf("Received a critical temperature alarm (%.1f), timer is at %d\r\n", value, readtimer);
    if (!isCrit)
        printf("Received a non-critical temperature alarm (%.1f), timer is at %d\r\n", value, readtimer);
    
    if (readtimer > 0 ) { //already running
        if ((readtimer >= NUMBER_OF_SEC_BEFORE_CRITICAL_ALARM && is_crit) ||
            (readtimer >= NUMBER_OF_SEC_BEFORE_UNDESIRED_ALARM && !is_crit)) {
            
            this->error = true;
            if (this->is_crit) {
                this->error_msg = "Crit temp!";
            } else {
                this->error_msg = "Undes temp!";
            }
            
            printf("### Temperature alarm has been triggered after %d ###\r\n", readtimer);
            buzzOnce();
            timer.stop();
            timer.reset();
            this->is_crit = false;
        }
    } else {
        timer.start();    
    }
}

std::string SensorAlarmController::getName() {
    return "AlarmController[" + this->sensor->getName() + "]";    
}

bool SensorAlarmController::isError() {
    return this->error;    
}

char *SensorAlarmController::getErrorMessage() {
    return this->error_msg;
}


void SensorAlarmController::buzzOnce() {
    
    buzzer = 1;
    led1=1;
    led2=1;
    led3=1;
    led4=1;
    wait(0.1);
    buzzer = 0;
    led1=0;
    led2=0;
    led3=0;
    led4=0;
            
}