Library for my home monitoring classes and serial communication protocol. It monitors temperature and movement on the mbed application board.

Dependents:   FinalProject

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Temperature.cpp Source File

Temperature.cpp

00001 //
00002 // This file contains function related to the temperature 
00003 // control of the home monitoring system
00004 //
00005 
00006 #include "Temperature.h"
00007 #include "HomeMonUtils.h"
00008 #include <vector>
00009 #include <string>
00010 #include <sstream>
00011 
00012 // TODO - add timestamp to temperature samples
00013   
00014 Temperature::Temperature() {
00015     max_temp_limit = 150.0;  // Maximum allowed value
00016     min_temp_limit = -150.00; // Minimum allowed value
00017     max_samples_limit = 100; // Maximum of 100 samples in buffer
00018     period_limit = 0.1;  // No faster than 1/10th of a second
00019     min_temp = 80.0; // Farenheit
00020     max_temp = 86.0; // Farenheit
00021     temp_sample_period = 10.0;  // seconds
00022     max_samples = 20;
00023     sample_ptr = 0;
00024     wrapped_once = false;
00025     // WARNING - width is important here, must be 7 characters!
00026     temp_samples.resize(max_samples,"0.00000");
00027 }
00028 
00029 bool Temperature::change_max_samples(int num_samples) {
00030     // WARNING - setting this dumps all saved data!
00031     if (num_samples > 0  && num_samples <= max_samples_limit) {
00032         max_samples = num_samples;
00033         // WARNING - width is important here, must be 7 characters!
00034         temp_samples.resize(max_samples, "0.00000");
00035         return true;
00036     }
00037     else {
00038         return false;
00039     }
00040 }
00041 
00042 void Temperature::add_sample(double sample) {
00043     
00044     std::string str_sample;
00045     convert_sample(sample, str_sample);
00046     if (sample_ptr == max_samples) {
00047         wrapped_once = true;
00048         sample_ptr = 0;
00049         temp_samples[sample_ptr] = str_sample;
00050     }
00051     else {
00052         temp_samples[sample_ptr++] = str_sample;
00053     }
00054 }
00055 
00056 bool Temperature::set_period(double period) {
00057     // Only 7 digits max allowed in a number so make
00058     // sure period fits in that limit.  String sent to
00059     // host will be rounded to 7 digits before transmission
00060     if (period > period_limit && period < 999999.99) {
00061         temp_sample_period = period;
00062         return true;
00063     }
00064     else {
00065         return false;
00066     }
00067 }
00068 
00069 bool Temperature::set_min(double min) {
00070     if (min > max_temp_limit || min < min_temp_limit || min > max_temp) {
00071         return false;
00072     }
00073     else {
00074         min_temp = min;
00075         return true;
00076     }
00077 }
00078 
00079 bool Temperature::set_max(double max) {
00080     if (max > max_temp_limit || max < min_temp_limit || max < min_temp) {
00081         return false;
00082     }
00083     else {
00084         max_temp = max;
00085         return true;
00086     }
00087 }
00088 
00089 double Temperature::get_min(void) {
00090     return min_temp;
00091 }
00092 
00093 double Temperature::get_max(void) {
00094     return max_temp;
00095 }
00096 
00097 double Temperature:: get_period() {
00098     return temp_sample_period;
00099 }
00100 
00101 int Temperature::get_max_samples() {
00102     return max_samples;
00103 }
00104 
00105 const std::vector<std::string> &Temperature::get_samples(void) {
00106     // FIXME - need to copy out samples in order of oldest
00107     // to newest or maybe not if change to contain timestamp
00108     // and let the host worry about adding it.  If use multi-map
00109     // it would be sorted automatically.  Maybe a better idea.
00110     return temp_samples;
00111 }
00112