Saltware / Mbed 2 deprecated Water Play

Dependencies:   mbed DRV88255 TextLCD Ping mbed-rtos

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorAlarmController.cpp Source File

SensorAlarmController.cpp

00001 #include "SensorAlarmController.h"
00002 
00003 
00004 //outputpins for alarm
00005 static DigitalOut buzzer(p17);
00006 static DigitalOut led1(LED1);
00007 static DigitalOut led2(LED2);
00008 static DigitalOut led3(LED3);
00009 static DigitalOut led4(LED4);
00010 
00011 void SensorAlarmController::update() {
00012     
00013     #ifndef TEST_MODE
00014     if(this->num_iters < STARTUP_ITERATIONS) {
00015         cout << this->getName() << ": not running, startup phase";
00016         return;
00017     }
00018     #endif
00019     
00020     value = sensor->getValue();
00021     
00022     if ((value < min_undesired) || (value > max_undesired)) {
00023         if ((value < min_crit) || (value > max_crit)) {
00024             this->raiseAlarm(true);
00025         } else {
00026             this->raiseAlarm(false);
00027         }
00028     } else {
00029         //clear alarm
00030         printf("Clearing alarm: %s\r\n", this->getName().c_str());
00031         timer.stop();
00032         timer.reset();
00033         this->is_crit = false;
00034         sensor->setLed(false);
00035     }
00036     
00037 }
00038 
00039 void SensorAlarmController::raiseAlarm(bool isCrit) { 
00040 
00041     this->is_crit = isCrit;
00042     int readtimer = timer.read();
00043     
00044     if (isCrit)
00045         printf("Received a critical %s alarm %f timer is at %d\r\n", this->sensor->getName().c_str(), value, readtimer);
00046     if (!isCrit)
00047         printf("Received a non-critical %s alarm %f timer is at %d\r\n", this->sensor->getName().c_str(), value, readtimer);
00048     
00049     if (readtimer > 0 ) { //already running
00050         if ((readtimer >= NUMBER_OF_SEC_BEFORE_CRITICAL_ALARM && is_crit) ||
00051             (readtimer >= NUMBER_OF_SEC_BEFORE_UNDESIRED_ALARM && !is_crit)) {
00052             
00053             this->error = true;
00054             if (this->is_crit) {
00055                 this->error_msg = this->error_msg_critical;
00056             } else {
00057                 this->error_msg = this->error_msg_undesired;
00058             }
00059             
00060             printf("### %s alarm has been triggered after ### %d seconds\r\n", this->getName().c_str(), readtimer);
00061             sensor->setLed(true);
00062             buzzOnce();
00063             timer.stop();
00064             timer.reset();
00065             this->is_crit = false;
00066         }
00067     } else {
00068         timer.start();    
00069     }
00070 }
00071 
00072 std::string SensorAlarmController::getName() {
00073     return "AlarmController[" + this->sensor->getName() + "]";    
00074 }
00075 
00076 bool SensorAlarmController::isError() {
00077     return this->error;    
00078 }
00079 
00080 std::string SensorAlarmController::getErrorMessage() {
00081     return this->error_msg;
00082 }
00083 
00084 void SensorAlarmController::setCriticalErrorMsg(std::string msg) {
00085     this->error_msg_critical = msg;        
00086 }
00087         
00088 void SensorAlarmController::setUndesiredErrorMsg(std::string msg) {
00089     this->error_msg_undesired = msg;   
00090 }
00091 
00092 bool SensorAlarmController::isActive() {
00093     return this->timer.read() > 0;    
00094 }
00095 
00096 void SensorAlarmController::buzzOnce() {
00097     
00098     buzzer = 1;
00099     wait(0.1);
00100     buzzer = 0;
00101             
00102 }