napoleon leoni / thermostat
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers thermostat.cpp Source File

thermostat.cpp

00001 #include "thermostat.h"
00002 //Thermostat class
00003 //Napoleon Leoni
00004 //Thermostat with Joystick control input
00005 //Class Thermostat: 
00006 //Has a control loop enable/disable(I will refer to this as Heater on/off), an over-ride enable/disable.
00007 //an object TemperatureRecording (keeps current temperature, max and min since last reset 
00008 //and circular buffer with recorded data)
00009 //Functions to check user input on joystick
00010 //sets interrupt on accelerometer (To be implemented)
00011 
00012 //regular constructor
00013 thermostat::thermostat(temperatureRecorder *ptrTrecord,joystickAppBoard *ptrinputJoystick,DigitalOut *ptrActuatorOutput){
00014     //initialize class pointers and default settings
00015     this->trecord=ptrTrecord;
00016     this->trecord->setTemperatureToFarenheit(); //by default our thermostat will be in Farenheit
00017     this->inputJoystick=ptrinputJoystick;
00018     this->actuatorOutput=ptrActuatorOutput;
00019     this->hysteresisGap=DEFAULTHYSTERESYS;
00020     this->temperatureGranularity=DEFAULTGRANULARITY, 
00021     this->defaultTemperature=DEFAULTTEMPSETTING;
00022     this->maxTemperature=DEFAULTMAXTEMPSETTING;
00023     this->minTemperature=DEFAULTMINTEMPSETTING;
00024     this->heaterEnabled=false;
00025     // set the time
00026     
00027     set_time(1256729737); //set default valid value to initialize
00028     timeInSeconds = time(&(this->timeInSeconds));//Update time to current time
00029 }
00030 
00031 void thermostat::setDay(int month,int day,int year){
00032     //Note to be implemented, Protection against out of range or invalid inputs
00033     this->timeKeeper.tm_year=year;
00034     this->timeKeeper.tm_year = this->timeKeeper.tm_year - 1900;
00035     this->timeKeeper.tm_mon=month-1;
00036     this->timeKeeper.tm_mday=day;
00037     set_time(mktime(&(this->timeKeeper)));
00038 }
00039 
00040 void thermostat::setTime(int hour,int min,int sec){
00041     //Note to be implemented, Protection against out of range or invalid inputs
00042     this->timeKeeper.tm_hour=hour;
00043     this->timeKeeper.tm_min=min;
00044     this->timeKeeper.tm_sec=sec;
00045     set_time(mktime(&(this->timeKeeper)));
00046 }
00047 
00048 //retutns the day and time as a string
00049 void thermostat::getDateandTime(char* buffer,int bufferSize){
00050     //TODO: check size of inout buffer and reject if smaller than minimum required
00051     timeInSeconds = time(&(this->timeInSeconds));//Update time to current time
00052     strftime(buffer, bufferSize, "%c\n", localtime(&(this->timeInSeconds)));
00053 }
00054 
00055 void thermostat::enableHeater(void){
00056     this->heaterEnabled=true;
00057 }
00058 
00059 void thermostat::disableHeater(void){
00060     this->heaterEnabled=false;
00061     this->heatingActionOn=false;        //turn off relay indicator as well as the thermostat is disabled
00062     this->actuatorOutput->write(0);     //turn off relay as well as the thermostat is disabled
00063 }
00064 
00065 void thermostat::checkJoystickInput(void){
00066     this->inputJoystick->readJoystick();
00067     if(this->inputJoystick->isJoystickUp()){
00068          if(this->defaultTemperature<this->maxTemperature){
00069             this->defaultTemperature+=this->temperatureGranularity;//increase temp. setting 
00070          } 
00071     }
00072     if(this->inputJoystick->isJoystickDown()){
00073         if(this->defaultTemperature>this->minTemperature){
00074             this->defaultTemperature-=this->temperatureGranularity;//decrease temp. setting   
00075         }
00076     }
00077     if(this->inputJoystick->isJoystickLeft()){
00078          this->disableHeater();//disable heater   
00079     }
00080     if(this->inputJoystick->isJoystickRight()){
00081          this->enableHeater();//enable heater   
00082     }
00083     
00084 }
00085 
00086 void thermostat::runThermostatControlCycle(void){
00087     //First read temperature values
00088     this->trecord->makeReading(); //Make temperature reading;
00089     if(this->heaterEnabled){ //the Thermostat heating loop is only run if it is enabled
00090         //next compare aginst target within +/- 1/2 Hysteresis Gap window    
00091         if(this->trecord->getTemperature() < (this->defaultTemperature - 0.5*hysteresisGap)){
00092             this->heatingActionOn=true;
00093             this->actuatorOutput->write(1); //turn on the relay driving digital output
00094         }
00095         
00096         if(this->trecord->getTemperature() > (this->defaultTemperature + 0.5*hysteresisGap)){
00097             this->heatingActionOn=false;        
00098             this->actuatorOutput->write(0); //turn off the relay driving digital output           
00099         }
00100     }
00101 }
00102 
00103 float thermostat::getTemperature(void){
00104     return this->trecord->getTemperature();
00105 }
00106 
00107 float thermostat::getMaxTemperature(){
00108     return this->trecord->getMaxTemperature();
00109 }
00110 
00111 float thermostat::getMinTemperature(){
00112     return this->trecord->getMinTemperature();
00113 }
00114 
00115 float thermostat::getTemperatureSetting(void){
00116     return this->defaultTemperature;    
00117 }
00118 
00119 bool thermostat::getThermostatStatus(void){
00120     if(this->heaterEnabled) return true;
00121     //defaults to false
00122     return false;   
00123 }
00124 
00125 void thermostat::ThermostatUnitTest(char *,int){
00126     int i;
00127     float testTemperatureInput[32]   =  { ;
00128     int expectedThermostatStatus[32] =  { ;//Expected thermostat control loop status 1 for ON,0 for Off
00129 
00130     for(i=0;i++;i<31){
00131         
00132     }
00133 }