//TemperatureRecorder class //Written by Napoleon Leoni, February 2014 //This class takes a pointer to an LM75B sensor on the MBED //application board and reads it temperature value, keeps track //of the max and min values (since a last reset of the values) and also //keeps a circular buffer of recorded temperature values
temperatureRecorder.cpp@1:8ec88bb8425f, 2014-02-21 (annotated)
- Committer:
- nleoni
- Date:
- Fri Feb 21 06:57:23 2014 +0000
- Revision:
- 1:8ec88bb8425f
- Parent:
- 0:aca019487653
Updated testing of class
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nleoni | 0:aca019487653 | 1 | //TemperatureRecorder class |
nleoni | 0:aca019487653 | 2 | //Written by Napoleon Leoni, February 2014 |
nleoni | 0:aca019487653 | 3 | //This class takes a pointer to an LM75B sensor |
nleoni | 0:aca019487653 | 4 | //and reads it temperature value, keeps track of the max and |
nleoni | 0:aca019487653 | 5 | //min values (since a last reset of the values) and also keeps |
nleoni | 0:aca019487653 | 6 | //a circular buffer of recorded temperature values |
nleoni | 0:aca019487653 | 7 | |
nleoni | 0:aca019487653 | 8 | //***************************** TESTING ********************************************************// |
nleoni | 0:aca019487653 | 9 | // TEST TEST DESCRIPTION STATUS // |
nleoni | 0:aca019487653 | 10 | // 1 Time display is inceremented in seconds and date shown PASS // |
nleoni | 0:aca019487653 | 11 | // corresponds to date set in code // |
nleoni | 0:aca019487653 | 12 | // 2 Max temperature displayed is highest reached PASS // |
nleoni | 0:aca019487653 | 13 | // during a heating event (finger touch) // |
nleoni | 0:aca019487653 | 14 | // 3 Min Temperature displayed is lowest reached PASS // |
nleoni | 0:aca019487653 | 15 | // during a cooling event // |
nleoni | 0:aca019487653 | 16 | // 4 Changing from degrees C to degress F and back retains PASS // |
nleoni | 0:aca019487653 | 17 | // Max and min values and displays proper conversion // |
nleoni | 0:aca019487653 | 18 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 19 | |
nleoni | 0:aca019487653 | 20 | |
nleoni | 0:aca019487653 | 21 | #include "temperatureRecorder.h" |
nleoni | 0:aca019487653 | 22 | |
nleoni | 0:aca019487653 | 23 | //temperature Recorder class definition |
nleoni | 0:aca019487653 | 24 | |
nleoni | 0:aca019487653 | 25 | //default constructor, sets default buffer size to max |
nleoni | 0:aca019487653 | 26 | temperatureRecorder::temperatureRecorder(){ |
nleoni | 0:aca019487653 | 27 | //set size of circular buffer for temperature history |
nleoni | 0:aca019487653 | 28 | this->recordingBufferSize=maxRecordingBufferSize; |
nleoni | 0:aca019487653 | 29 | this->sensorValid=false; |
nleoni | 0:aca019487653 | 30 | this->resetTemperatureFlag=true; |
nleoni | 0:aca019487653 | 31 | } |
nleoni | 0:aca019487653 | 32 | |
nleoni | 0:aca019487653 | 33 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 34 | // temperatureRecorder::temperatureRecorder(int bufferSize,LM75B *ptrTsensor) // |
nleoni | 0:aca019487653 | 35 | // Constructor with buffer size specified and ptr to temperature sensor object // |
nleoni | 0:aca019487653 | 36 | // Inputs: bufferSize integer, buffer size for circular buffer use to record temperatures // |
nleoni | 0:aca019487653 | 37 | // limited to max value set in MAXRECORDINGBUFFER, internally enforced // |
nleoni | 0:aca019487653 | 38 | // no error or warning displayed // |
nleoni | 0:aca019487653 | 39 | // *ptrTsensor pointer to LM75B sensor object, if sensor is valid temperature // |
nleoni | 0:aca019487653 | 40 | // readings can be made, otherwise the object is still created but // |
nleoni | 0:aca019487653 | 41 | // temperatures are returned as zero. // |
nleoni | 0:aca019487653 | 42 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 43 | temperatureRecorder::temperatureRecorder(int bufferSize,LM75B *ptrTsensor){ |
nleoni | 0:aca019487653 | 44 | this->sensorValid=false; |
nleoni | 0:aca019487653 | 45 | this->resetTemperatureFlag=true; |
nleoni | 1:8ec88bb8425f | 46 | this->externalSensor=false; |
nleoni | 0:aca019487653 | 47 | if(bufferSize <= this->maxRecordingBufferSize){ |
nleoni | 0:aca019487653 | 48 | this->recordingBufferSize=bufferSize; |
nleoni | 0:aca019487653 | 49 | } else { |
nleoni | 0:aca019487653 | 50 | this->recordingBufferSize=maxRecordingBufferSize; |
nleoni | 0:aca019487653 | 51 | //TODO return error message, but to where? |
nleoni | 0:aca019487653 | 52 | } |
nleoni | 0:aca019487653 | 53 | this->tsensor=ptrTsensor; |
nleoni | 0:aca019487653 | 54 | //Check if sensor is valid |
nleoni | 0:aca019487653 | 55 | if (this->tsensor->open()) { |
nleoni | 0:aca019487653 | 56 | this->sensorValid=true; |
nleoni | 0:aca019487653 | 57 | this->makeReading(); |
nleoni | 0:aca019487653 | 58 | } else { |
nleoni | 0:aca019487653 | 59 | //TODO: how to return error condition here |
nleoni | 0:aca019487653 | 60 | } |
nleoni | 0:aca019487653 | 61 | } |
nleoni | 0:aca019487653 | 62 | |
nleoni | 0:aca019487653 | 63 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 64 | // void temperatureRecorder::makeReading(void){ // |
nleoni | 0:aca019487653 | 65 | // Makes a temperature reading from the LM75B, stores the reading in the appropiate units // |
nleoni | 0:aca019487653 | 66 | // in the object's member this->currentTemperature and updates the maxTemperature and // |
nleoni | 0:aca019487653 | 67 | // minTemperature // |
nleoni | 0:aca019487653 | 68 | // Inputs: void // |
nleoni | 0:aca019487653 | 69 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 70 | void temperatureRecorder::makeReading(void){ |
nleoni | 0:aca019487653 | 71 | if(this->sensorValid){ |
nleoni | 0:aca019487653 | 72 | this->currentTemperature=this->tsensor->temp(); |
nleoni | 0:aca019487653 | 73 | //check if Trecorder is set in farehnheit if so make the conversion |
nleoni | 0:aca019487653 | 74 | if(this->temperatureInFarenheit) this->currentTemperature=convertToFarenheit(this->currentTemperature); |
nleoni | 0:aca019487653 | 75 | //TODO store temperature into buffer |
nleoni | 1:8ec88bb8425f | 76 | } |
nleoni | 0:aca019487653 | 77 | if(this->resetTemperatureFlag){ |
nleoni | 0:aca019487653 | 78 | //reset max an min values |
nleoni | 0:aca019487653 | 79 | this->maxTemperature=this->currentTemperature; |
nleoni | 0:aca019487653 | 80 | this->minTemperature=this->currentTemperature; |
nleoni | 0:aca019487653 | 81 | this->resetTemperatureFlag=false; |
nleoni | 0:aca019487653 | 82 | } else { |
nleoni | 0:aca019487653 | 83 | //compare current temperature to stored esxtremes,replace if neccesary |
nleoni | 0:aca019487653 | 84 | if((this->currentTemperature) > (this->maxTemperature)) this->maxTemperature=this->currentTemperature; |
nleoni | 0:aca019487653 | 85 | if((this->currentTemperature) < (this->minTemperature)) this->minTemperature=this->currentTemperature; |
nleoni | 0:aca019487653 | 86 | } |
nleoni | 0:aca019487653 | 87 | }//end of temperatureRecorder::makeReading() |
nleoni | 1:8ec88bb8425f | 88 | |
nleoni | 1:8ec88bb8425f | 89 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 90 | // void temperatureRecorder::makeReading(float temperatureInC) // |
nleoni | 1:8ec88bb8425f | 91 | // Makes a temperature reading from an external sensor, stores the reading in the appropiate // |
nleoni | 1:8ec88bb8425f | 92 | // units in the object's member this->currentTemperature and updates the maxTemperature and // |
nleoni | 1:8ec88bb8425f | 93 | // minTemperature // |
nleoni | 1:8ec88bb8425f | 94 | // Inputs: float temperatureInC, provide an external reading as opposed to the internally used // |
nleoni | 1:8ec88bb8425f | 95 | // LM75B // |
nleoni | 1:8ec88bb8425f | 96 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 97 | void temperatureRecorder::makeReading(float temperatureInC){ |
nleoni | 1:8ec88bb8425f | 98 | this->currentTemperature=temperatureInC; |
nleoni | 1:8ec88bb8425f | 99 | //check if Trecorder is set in farehnheit if so make the conversion |
nleoni | 1:8ec88bb8425f | 100 | if(this->temperatureInFarenheit) this->currentTemperature=convertToFarenheit(this->currentTemperature); |
nleoni | 1:8ec88bb8425f | 101 | //TODO store temperature into buffer |
nleoni | 1:8ec88bb8425f | 102 | if(this->resetTemperatureFlag){ |
nleoni | 1:8ec88bb8425f | 103 | //reset max an min values |
nleoni | 1:8ec88bb8425f | 104 | this->maxTemperature=this->currentTemperature; |
nleoni | 1:8ec88bb8425f | 105 | this->minTemperature=this->currentTemperature; |
nleoni | 1:8ec88bb8425f | 106 | this->resetTemperatureFlag=false; |
nleoni | 1:8ec88bb8425f | 107 | } else { |
nleoni | 1:8ec88bb8425f | 108 | //compare current temperature to stored esxtremes,replace if neccesary |
nleoni | 1:8ec88bb8425f | 109 | if((this->currentTemperature) > (this->maxTemperature)) this->maxTemperature=this->currentTemperature; |
nleoni | 1:8ec88bb8425f | 110 | if((this->currentTemperature) < (this->minTemperature)) this->minTemperature=this->currentTemperature; |
nleoni | 1:8ec88bb8425f | 111 | } |
nleoni | 1:8ec88bb8425f | 112 | }//end of temperatureRecorder::makeReading() |
nleoni | 0:aca019487653 | 113 | |
nleoni | 0:aca019487653 | 114 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 115 | // void temperatureRecorder::getTemperature(void){ // |
nleoni | 0:aca019487653 | 116 | // returns this->currentTemperature (last reading made) in the set units // |
nleoni | 0:aca019487653 | 117 | // if the sensor is not valid returns 0 // |
nleoni | 0:aca019487653 | 118 | // Inputs: void // |
nleoni | 0:aca019487653 | 119 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 120 | float temperatureRecorder::getTemperature(void){ |
nleoni | 0:aca019487653 | 121 | if(this->sensorValid){ |
nleoni | 0:aca019487653 | 122 | return this->currentTemperature; |
nleoni | 0:aca019487653 | 123 | } else { |
nleoni | 0:aca019487653 | 124 | return 0; |
nleoni | 0:aca019487653 | 125 | } |
nleoni | 0:aca019487653 | 126 | } |
nleoni | 0:aca019487653 | 127 | |
nleoni | 0:aca019487653 | 128 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 129 | // void temperatureRecorder::getMaxTemperature(void){ // |
nleoni | 0:aca019487653 | 130 | // returns this->maxTemperature (last reading made) in the set units // |
nleoni | 0:aca019487653 | 131 | // if the sensor is not valid returns 0 // |
nleoni | 0:aca019487653 | 132 | // Inputs: void // |
nleoni | 0:aca019487653 | 133 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 134 | float temperatureRecorder::getMaxTemperature(void){ |
nleoni | 0:aca019487653 | 135 | if(this->sensorValid){ |
nleoni | 0:aca019487653 | 136 | return this->maxTemperature; |
nleoni | 0:aca019487653 | 137 | } else { |
nleoni | 0:aca019487653 | 138 | return 0; |
nleoni | 0:aca019487653 | 139 | } |
nleoni | 0:aca019487653 | 140 | } |
nleoni | 0:aca019487653 | 141 | |
nleoni | 0:aca019487653 | 142 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 143 | // void temperatureRecorder::getMinTemperature(void){ // |
nleoni | 0:aca019487653 | 144 | // returns this->minTemperature (last reading made) in the set units // |
nleoni | 0:aca019487653 | 145 | // if the sensor is not valid returns 0 // |
nleoni | 0:aca019487653 | 146 | // Inputs: void // |
nleoni | 0:aca019487653 | 147 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 148 | float temperatureRecorder::getMinTemperature(void){ |
nleoni | 0:aca019487653 | 149 | if(this->sensorValid){ |
nleoni | 0:aca019487653 | 150 | return this->minTemperature; |
nleoni | 0:aca019487653 | 151 | } else { |
nleoni | 0:aca019487653 | 152 | return 0; |
nleoni | 0:aca019487653 | 153 | } |
nleoni | 0:aca019487653 | 154 | } |
nleoni | 0:aca019487653 | 155 | |
nleoni | 0:aca019487653 | 156 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 157 | // void temperatureRecorder::resetTemperatureExtremes(void){ // |
nleoni | 0:aca019487653 | 158 | // sets an object internal flag that will trigger a reset of the Max and Min Temepratures // |
nleoni | 0:aca019487653 | 159 | // recorded when the next reading is made from temperature sensor // |
nleoni | 0:aca019487653 | 160 | // Inputs: void // |
nleoni | 0:aca019487653 | 161 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 162 | void temperatureRecorder::resetTemperatureExtremes(void){ |
nleoni | 0:aca019487653 | 163 | this->resetTemperatureFlag=true; |
nleoni | 0:aca019487653 | 164 | } |
nleoni | 0:aca019487653 | 165 | |
nleoni | 0:aca019487653 | 166 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 167 | // void temperatureRecorder::setTemperatureToCelsius(void){ // |
nleoni | 0:aca019487653 | 168 | // sets all recorded values to celsius and converts all future readings as well // |
nleoni | 0:aca019487653 | 169 | // Inputs: void // |
nleoni | 0:aca019487653 | 170 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 171 | void temperatureRecorder::setTemperatureToCelsius(void){ |
nleoni | 0:aca019487653 | 172 | if(this->temperatureInFarenheit){ |
nleoni | 0:aca019487653 | 173 | this->temperatureInFarenheit=false; |
nleoni | 0:aca019487653 | 174 | this->currentTemperature=this->convertToCelsius(this->currentTemperature); |
nleoni | 0:aca019487653 | 175 | this->maxTemperature=this->convertToCelsius(this->maxTemperature); |
nleoni | 0:aca019487653 | 176 | this->minTemperature=this->convertToCelsius(this->minTemperature); |
nleoni | 0:aca019487653 | 177 | } |
nleoni | 0:aca019487653 | 178 | } |
nleoni | 0:aca019487653 | 179 | |
nleoni | 0:aca019487653 | 180 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 181 | // void temperatureRecorder::setTemperatureToFarenheit(void){ // |
nleoni | 0:aca019487653 | 182 | // sets all recorded values to farenheit and converts all future readings as well // |
nleoni | 0:aca019487653 | 183 | // Inputs: void // |
nleoni | 0:aca019487653 | 184 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 185 | void temperatureRecorder::setTemperatureToFarenheit(void){ |
nleoni | 0:aca019487653 | 186 | if(!this->temperatureInFarenheit){ |
nleoni | 0:aca019487653 | 187 | this->temperatureInFarenheit=true; |
nleoni | 0:aca019487653 | 188 | this->currentTemperature=this->convertToFarenheit(this->currentTemperature); |
nleoni | 0:aca019487653 | 189 | this->maxTemperature=this->convertToFarenheit(this->maxTemperature); |
nleoni | 0:aca019487653 | 190 | this->minTemperature=this->convertToFarenheit(this->minTemperature); |
nleoni | 0:aca019487653 | 191 | } |
nleoni | 0:aca019487653 | 192 | } |
nleoni | 0:aca019487653 | 193 | |
nleoni | 0:aca019487653 | 194 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 195 | // void temperatureRecorder::convertToCelsius(void){ // |
nleoni | 0:aca019487653 | 196 | // returns a float resulting from converting farenheit to celcius // |
nleoni | 0:aca019487653 | 197 | // Inputs: float Tfarenheit....Input Temperature in farenheit to be converted to Celsius // // |
nleoni | 0:aca019487653 | 198 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 199 | float temperatureRecorder::convertToCelsius(float Tfarenheit){ |
nleoni | 0:aca019487653 | 200 | float Tc; |
nleoni | 0:aca019487653 | 201 | Tc=(Tfarenheit-32.0)*5.0/9.0; |
nleoni | 0:aca019487653 | 202 | return Tc; |
nleoni | 0:aca019487653 | 203 | } |
nleoni | 0:aca019487653 | 204 | |
nleoni | 0:aca019487653 | 205 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 206 | // void temperatureRecorder::convertToFarenheit(void){ // |
nleoni | 0:aca019487653 | 207 | // returns a float resulting from converting celcius to farenheit // |
nleoni | 0:aca019487653 | 208 | // Inputs: float Tcelsius....Input Temperature in Celsius to be converted to farenheit // // |
nleoni | 0:aca019487653 | 209 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 210 | float temperatureRecorder::convertToFarenheit(float Tcelsius){ |
nleoni | 0:aca019487653 | 211 | float Tf; |
nleoni | 0:aca019487653 | 212 | Tf=(Tcelsius*9.0/5.0)+32.0; |
nleoni | 0:aca019487653 | 213 | return Tf; |
nleoni | 0:aca019487653 | 214 | } |
nleoni | 0:aca019487653 | 215 | |
nleoni | 0:aca019487653 | 216 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 217 | // void temperatureRecorder::isTemperatureInFarenheit(void){ // |
nleoni | 0:aca019487653 | 218 | // returns a BOOL value which is true if the current units are farenheit otherwise false // |
nleoni | 0:aca019487653 | 219 | // Inputs: void // |
nleoni | 0:aca019487653 | 220 | //**********************************************************************************************// |
nleoni | 0:aca019487653 | 221 | bool temperatureRecorder::isTemperatureInFarenheit(void){ |
nleoni | 0:aca019487653 | 222 | return this->temperatureInFarenheit; |
nleoni | 0:aca019487653 | 223 | } |
nleoni | 1:8ec88bb8425f | 224 | |
nleoni | 1:8ec88bb8425f | 225 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 226 | // void temperatureRecorder::enableExternalSensor(void){ // |
nleoni | 1:8ec88bb8425f | 227 | // enables the external sensor mode which allows for manual setting of the temperature // |
nleoni | 1:8ec88bb8425f | 228 | // Inputs: void // |
nleoni | 1:8ec88bb8425f | 229 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 230 | void temperatureRecorder::enableExternalSensor(void){ |
nleoni | 1:8ec88bb8425f | 231 | this->externalSensor=true; |
nleoni | 1:8ec88bb8425f | 232 | } |
nleoni | 1:8ec88bb8425f | 233 | |
nleoni | 1:8ec88bb8425f | 234 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 235 | // void temperatureRecorder::enableExternalSensor(void){ // |
nleoni | 1:8ec88bb8425f | 236 | // disables the external sensor mode which allows for manual setting of the temperature // |
nleoni | 1:8ec88bb8425f | 237 | // Inputs: void // |
nleoni | 1:8ec88bb8425f | 238 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 239 | void temperatureRecorder::disableExternalSensor(void){ |
nleoni | 1:8ec88bb8425f | 240 | this->externalSensor=false; |
nleoni | 1:8ec88bb8425f | 241 | } |
nleoni | 1:8ec88bb8425f | 242 | |
nleoni | 1:8ec88bb8425f | 243 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 244 | // void temperatureRecorder::inputExternalSensorTemperatureInC(float inputTemperatureInC)){ // |
nleoni | 1:8ec88bb8425f | 245 | // sets the external sensor temperature which is what is recorded and processed when then // |
nleoni | 1:8ec88bb8425f | 246 | // the externalSensor flag is set to true. this enables feeding a synthetic temperature // |
nleoni | 1:8ec88bb8425f | 247 | // waveform for testing purposes |
nleoni | 1:8ec88bb8425f | 248 | // Inputs: void // |
nleoni | 1:8ec88bb8425f | 249 | //**********************************************************************************************// |
nleoni | 1:8ec88bb8425f | 250 | void temperatureRecorder::inputExternalSensorTemperatureInC(float inputTemperatureInC){ |
nleoni | 1:8ec88bb8425f | 251 | this->externalSensorTemperatureInC=inputTemperatureInC; |
nleoni | 1:8ec88bb8425f | 252 | } |
nleoni | 1:8ec88bb8425f | 253 |