Thermostat class implements basic functionality of a thermostat

Committer:
nleoni
Date:
Fri Feb 21 06:59:01 2014 +0000
Revision:
1:909cb1aef872
Parent:
0:cd3a00aa2dbc
1st checkin of Thermostat class: includes class tests

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nleoni 0:cd3a00aa2dbc 1 #include "thermostat.h"
nleoni 0:cd3a00aa2dbc 2 //Thermostat class
nleoni 0:cd3a00aa2dbc 3 //Napoleon Leoni
nleoni 0:cd3a00aa2dbc 4 //Thermostat with Joystick control input
nleoni 0:cd3a00aa2dbc 5 //Class Thermostat:
nleoni 0:cd3a00aa2dbc 6 //Has a control loop enable/disable(I will refer to this as Heater on/off), an over-ride enable/disable.
nleoni 0:cd3a00aa2dbc 7 //an object TemperatureRecording (keeps current temperature, max and min since last reset
nleoni 0:cd3a00aa2dbc 8 //and circular buffer with recorded data)
nleoni 0:cd3a00aa2dbc 9 //Functions to check user input on joystick
nleoni 0:cd3a00aa2dbc 10 //sets interrupt on accelerometer (To be implemented)
nleoni 0:cd3a00aa2dbc 11
nleoni 0:cd3a00aa2dbc 12 //regular constructor
nleoni 0:cd3a00aa2dbc 13 thermostat::thermostat(temperatureRecorder *ptrTrecord,joystickAppBoard *ptrinputJoystick,DigitalOut *ptrActuatorOutput){
nleoni 0:cd3a00aa2dbc 14 //initialize class pointers and default settings
nleoni 0:cd3a00aa2dbc 15 this->trecord=ptrTrecord;
nleoni 0:cd3a00aa2dbc 16 this->trecord->setTemperatureToFarenheit(); //by default our thermostat will be in Farenheit
nleoni 0:cd3a00aa2dbc 17 this->inputJoystick=ptrinputJoystick;
nleoni 0:cd3a00aa2dbc 18 this->actuatorOutput=ptrActuatorOutput;
nleoni 0:cd3a00aa2dbc 19 this->hysteresisGap=DEFAULTHYSTERESYS;
nleoni 0:cd3a00aa2dbc 20 this->temperatureGranularity=DEFAULTGRANULARITY,
nleoni 0:cd3a00aa2dbc 21 this->defaultTemperature=DEFAULTTEMPSETTING;
nleoni 0:cd3a00aa2dbc 22 this->maxTemperature=DEFAULTMAXTEMPSETTING;
nleoni 0:cd3a00aa2dbc 23 this->minTemperature=DEFAULTMINTEMPSETTING;
nleoni 0:cd3a00aa2dbc 24 this->heaterEnabled=false;
nleoni 0:cd3a00aa2dbc 25 // set the time
nleoni 0:cd3a00aa2dbc 26
nleoni 0:cd3a00aa2dbc 27 set_time(1256729737); //set default valid value to initialize
nleoni 0:cd3a00aa2dbc 28 timeInSeconds = time(&(this->timeInSeconds));//Update time to current time
nleoni 0:cd3a00aa2dbc 29 }
nleoni 0:cd3a00aa2dbc 30
nleoni 0:cd3a00aa2dbc 31 void thermostat::setDay(int month,int day,int year){
nleoni 0:cd3a00aa2dbc 32 //Note to be implemented, Protection against out of range or invalid inputs
nleoni 0:cd3a00aa2dbc 33 this->timeKeeper.tm_year=year;
nleoni 0:cd3a00aa2dbc 34 this->timeKeeper.tm_year = this->timeKeeper.tm_year - 1900;
nleoni 0:cd3a00aa2dbc 35 this->timeKeeper.tm_mon=month-1;
nleoni 0:cd3a00aa2dbc 36 this->timeKeeper.tm_mday=day;
nleoni 0:cd3a00aa2dbc 37 set_time(mktime(&(this->timeKeeper)));
nleoni 0:cd3a00aa2dbc 38 }
nleoni 0:cd3a00aa2dbc 39
nleoni 0:cd3a00aa2dbc 40 void thermostat::setTime(int hour,int min,int sec){
nleoni 0:cd3a00aa2dbc 41 //Note to be implemented, Protection against out of range or invalid inputs
nleoni 0:cd3a00aa2dbc 42 this->timeKeeper.tm_hour=hour;
nleoni 0:cd3a00aa2dbc 43 this->timeKeeper.tm_min=min;
nleoni 0:cd3a00aa2dbc 44 this->timeKeeper.tm_sec=sec;
nleoni 0:cd3a00aa2dbc 45 set_time(mktime(&(this->timeKeeper)));
nleoni 0:cd3a00aa2dbc 46 }
nleoni 0:cd3a00aa2dbc 47
nleoni 0:cd3a00aa2dbc 48 //retutns the day and time as a string
nleoni 0:cd3a00aa2dbc 49 void thermostat::getDateandTime(char* buffer,int bufferSize){
nleoni 0:cd3a00aa2dbc 50 //TODO: check size of inout buffer and reject if smaller than minimum required
nleoni 0:cd3a00aa2dbc 51 timeInSeconds = time(&(this->timeInSeconds));//Update time to current time
nleoni 0:cd3a00aa2dbc 52 strftime(buffer, bufferSize, "%c\n", localtime(&(this->timeInSeconds)));
nleoni 0:cd3a00aa2dbc 53 }
nleoni 0:cd3a00aa2dbc 54
nleoni 0:cd3a00aa2dbc 55 void thermostat::enableHeater(void){
nleoni 0:cd3a00aa2dbc 56 this->heaterEnabled=true;
nleoni 0:cd3a00aa2dbc 57 }
nleoni 0:cd3a00aa2dbc 58
nleoni 0:cd3a00aa2dbc 59 void thermostat::disableHeater(void){
nleoni 0:cd3a00aa2dbc 60 this->heaterEnabled=false;
nleoni 0:cd3a00aa2dbc 61 this->heatingActionOn=false; //turn off relay indicator as well as the thermostat is disabled
nleoni 0:cd3a00aa2dbc 62 this->actuatorOutput->write(0); //turn off relay as well as the thermostat is disabled
nleoni 0:cd3a00aa2dbc 63 }
nleoni 0:cd3a00aa2dbc 64
nleoni 0:cd3a00aa2dbc 65 void thermostat::checkJoystickInput(void){
nleoni 0:cd3a00aa2dbc 66 this->inputJoystick->readJoystick();
nleoni 0:cd3a00aa2dbc 67 if(this->inputJoystick->isJoystickUp()){
nleoni 0:cd3a00aa2dbc 68 if(this->defaultTemperature<this->maxTemperature){
nleoni 0:cd3a00aa2dbc 69 this->defaultTemperature+=this->temperatureGranularity;//increase temp. setting
nleoni 0:cd3a00aa2dbc 70 }
nleoni 0:cd3a00aa2dbc 71 }
nleoni 0:cd3a00aa2dbc 72 if(this->inputJoystick->isJoystickDown()){
nleoni 0:cd3a00aa2dbc 73 if(this->defaultTemperature>this->minTemperature){
nleoni 0:cd3a00aa2dbc 74 this->defaultTemperature-=this->temperatureGranularity;//decrease temp. setting
nleoni 0:cd3a00aa2dbc 75 }
nleoni 0:cd3a00aa2dbc 76 }
nleoni 0:cd3a00aa2dbc 77 if(this->inputJoystick->isJoystickLeft()){
nleoni 0:cd3a00aa2dbc 78 this->disableHeater();//disable heater
nleoni 0:cd3a00aa2dbc 79 }
nleoni 0:cd3a00aa2dbc 80 if(this->inputJoystick->isJoystickRight()){
nleoni 0:cd3a00aa2dbc 81 this->enableHeater();//enable heater
nleoni 0:cd3a00aa2dbc 82 }
nleoni 0:cd3a00aa2dbc 83
nleoni 0:cd3a00aa2dbc 84 }
nleoni 0:cd3a00aa2dbc 85
nleoni 0:cd3a00aa2dbc 86 void thermostat::runThermostatControlCycle(void){
nleoni 0:cd3a00aa2dbc 87 //First read temperature values
nleoni 0:cd3a00aa2dbc 88 this->trecord->makeReading(); //Make temperature reading;
nleoni 0:cd3a00aa2dbc 89 if(this->heaterEnabled){ //the Thermostat heating loop is only run if it is enabled
nleoni 0:cd3a00aa2dbc 90 //next compare aginst target within +/- 1/2 Hysteresis Gap window
nleoni 0:cd3a00aa2dbc 91 if(this->trecord->getTemperature() < (this->defaultTemperature - 0.5*hysteresisGap)){
nleoni 0:cd3a00aa2dbc 92 this->heatingActionOn=true;
nleoni 0:cd3a00aa2dbc 93 this->actuatorOutput->write(1); //turn on the relay driving digital output
nleoni 0:cd3a00aa2dbc 94 }
nleoni 0:cd3a00aa2dbc 95
nleoni 0:cd3a00aa2dbc 96 if(this->trecord->getTemperature() > (this->defaultTemperature + 0.5*hysteresisGap)){
nleoni 0:cd3a00aa2dbc 97 this->heatingActionOn=false;
nleoni 0:cd3a00aa2dbc 98 this->actuatorOutput->write(0); //turn off the relay driving digital output
nleoni 0:cd3a00aa2dbc 99 }
nleoni 0:cd3a00aa2dbc 100 }
nleoni 0:cd3a00aa2dbc 101 }
nleoni 0:cd3a00aa2dbc 102
nleoni 0:cd3a00aa2dbc 103 float thermostat::getTemperature(void){
nleoni 0:cd3a00aa2dbc 104 return this->trecord->getTemperature();
nleoni 0:cd3a00aa2dbc 105 }
nleoni 0:cd3a00aa2dbc 106
nleoni 0:cd3a00aa2dbc 107 float thermostat::getMaxTemperature(){
nleoni 0:cd3a00aa2dbc 108 return this->trecord->getMaxTemperature();
nleoni 0:cd3a00aa2dbc 109 }
nleoni 0:cd3a00aa2dbc 110
nleoni 0:cd3a00aa2dbc 111 float thermostat::getMinTemperature(){
nleoni 0:cd3a00aa2dbc 112 return this->trecord->getMinTemperature();
nleoni 0:cd3a00aa2dbc 113 }
nleoni 0:cd3a00aa2dbc 114
nleoni 0:cd3a00aa2dbc 115 float thermostat::getTemperatureSetting(void){
nleoni 0:cd3a00aa2dbc 116 return this->defaultTemperature;
nleoni 0:cd3a00aa2dbc 117 }
nleoni 0:cd3a00aa2dbc 118
nleoni 0:cd3a00aa2dbc 119 bool thermostat::getThermostatStatus(void){
nleoni 0:cd3a00aa2dbc 120 if(this->heaterEnabled) return true;
nleoni 0:cd3a00aa2dbc 121 //defaults to false
nleoni 0:cd3a00aa2dbc 122 return false;
nleoni 1:909cb1aef872 123 }
nleoni 1:909cb1aef872 124
nleoni 1:909cb1aef872 125 void thermostat::ThermostatUnitTest(char *,int){
nleoni 1:909cb1aef872 126 int i;
nleoni 1:909cb1aef872 127 float testTemperatureInput[32] = { ;
nleoni 1:909cb1aef872 128 int expectedThermostatStatus[32] = { ;//Expected thermostat control loop status 1 for ON,0 for Off
nleoni 1:909cb1aef872 129
nleoni 1:909cb1aef872 130 for(i=0;i++;i<31){
nleoni 1:909cb1aef872 131
nleoni 1:909cb1aef872 132 }
nleoni 0:cd3a00aa2dbc 133 }