Library for my home monitoring classes and serial communication protocol. It monitors temperature and movement on the mbed application board.
Temperature/Temperature.cpp@0:3f846fc933a2, 2013-09-01 (annotated)
- Committer:
- groletter
- Date:
- Sun Sep 01 23:35:18 2013 +0000
- Revision:
- 0:3f846fc933a2
- Child:
- 1:2f5a62eb52ad
My library for temperature and motion monitoring and communication over a USB Serial device. Requires host-side code.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
groletter | 0:3f846fc933a2 | 1 | // |
groletter | 0:3f846fc933a2 | 2 | // This file contains function related to the temperature |
groletter | 0:3f846fc933a2 | 3 | // control of the home monitoring system |
groletter | 0:3f846fc933a2 | 4 | // |
groletter | 0:3f846fc933a2 | 5 | |
groletter | 0:3f846fc933a2 | 6 | #include "Temperature.h" |
groletter | 0:3f846fc933a2 | 7 | #include "HomeMonUtils.h" |
groletter | 0:3f846fc933a2 | 8 | #include <vector> |
groletter | 0:3f846fc933a2 | 9 | #include <string> |
groletter | 0:3f846fc933a2 | 10 | #include <sstream> |
groletter | 0:3f846fc933a2 | 11 | |
groletter | 0:3f846fc933a2 | 12 | // FIXME - add timestamp to temperature samples |
groletter | 0:3f846fc933a2 | 13 | |
groletter | 0:3f846fc933a2 | 14 | Temperature::Temperature() { |
groletter | 0:3f846fc933a2 | 15 | max_temp_limit = 150.0; // Maximum allowed value |
groletter | 0:3f846fc933a2 | 16 | min_temp_limit = -150.00; // Minimum allowed value |
groletter | 0:3f846fc933a2 | 17 | max_samples_limit = 100; // Maximum of 100 samples in buffer |
groletter | 0:3f846fc933a2 | 18 | period_limit = 0.1; // No faster than 1/10th of a second |
groletter | 0:3f846fc933a2 | 19 | min_temp = 80.0; // Farenheit |
groletter | 0:3f846fc933a2 | 20 | max_temp = 86.0; // Farenheit |
groletter | 0:3f846fc933a2 | 21 | temp_sample_period = 10.0; // seconds |
groletter | 0:3f846fc933a2 | 22 | max_samples = 20; |
groletter | 0:3f846fc933a2 | 23 | sample_ptr = 0; |
groletter | 0:3f846fc933a2 | 24 | wrapped_once = false; |
groletter | 0:3f846fc933a2 | 25 | // WARNING - width is important here, must be 7 characters! |
groletter | 0:3f846fc933a2 | 26 | temp_samples.resize(max_samples,"0.00000"); |
groletter | 0:3f846fc933a2 | 27 | } |
groletter | 0:3f846fc933a2 | 28 | |
groletter | 0:3f846fc933a2 | 29 | bool Temperature::change_max_samples(int num_samples) { |
groletter | 0:3f846fc933a2 | 30 | // WARNING - setting this dumps all saved data! |
groletter | 0:3f846fc933a2 | 31 | if (num_samples > 0 && num_samples <= max_samples_limit) { |
groletter | 0:3f846fc933a2 | 32 | max_samples = num_samples; |
groletter | 0:3f846fc933a2 | 33 | // WARNING - width is important here, must be 7 characters! |
groletter | 0:3f846fc933a2 | 34 | temp_samples.resize(max_samples, "0.00000"); |
groletter | 0:3f846fc933a2 | 35 | return true; |
groletter | 0:3f846fc933a2 | 36 | } |
groletter | 0:3f846fc933a2 | 37 | else { |
groletter | 0:3f846fc933a2 | 38 | return false; |
groletter | 0:3f846fc933a2 | 39 | } |
groletter | 0:3f846fc933a2 | 40 | } |
groletter | 0:3f846fc933a2 | 41 | |
groletter | 0:3f846fc933a2 | 42 | void Temperature::add_sample(double sample) { |
groletter | 0:3f846fc933a2 | 43 | |
groletter | 0:3f846fc933a2 | 44 | std::string str_sample; |
groletter | 0:3f846fc933a2 | 45 | convert_sample(sample, str_sample); |
groletter | 0:3f846fc933a2 | 46 | if (sample_ptr == max_samples) { |
groletter | 0:3f846fc933a2 | 47 | wrapped_once = true; |
groletter | 0:3f846fc933a2 | 48 | sample_ptr = 0; |
groletter | 0:3f846fc933a2 | 49 | temp_samples[sample_ptr] = str_sample; |
groletter | 0:3f846fc933a2 | 50 | } |
groletter | 0:3f846fc933a2 | 51 | else { |
groletter | 0:3f846fc933a2 | 52 | temp_samples[sample_ptr++] = str_sample; |
groletter | 0:3f846fc933a2 | 53 | } |
groletter | 0:3f846fc933a2 | 54 | } |
groletter | 0:3f846fc933a2 | 55 | |
groletter | 0:3f846fc933a2 | 56 | bool Temperature::set_period(double period) { |
groletter | 0:3f846fc933a2 | 57 | // Only 7 digits max allowed in a number so make |
groletter | 0:3f846fc933a2 | 58 | // sure period fits in that limit. String sent to |
groletter | 0:3f846fc933a2 | 59 | // host will be rounded to 7 digits before transmission |
groletter | 0:3f846fc933a2 | 60 | if (period > period_limit && period < 999999.99) { |
groletter | 0:3f846fc933a2 | 61 | temp_sample_period = period; |
groletter | 0:3f846fc933a2 | 62 | return true; |
groletter | 0:3f846fc933a2 | 63 | } |
groletter | 0:3f846fc933a2 | 64 | else { |
groletter | 0:3f846fc933a2 | 65 | return false; |
groletter | 0:3f846fc933a2 | 66 | } |
groletter | 0:3f846fc933a2 | 67 | } |
groletter | 0:3f846fc933a2 | 68 | |
groletter | 0:3f846fc933a2 | 69 | bool Temperature::set_min(double min) { |
groletter | 0:3f846fc933a2 | 70 | if (min > max_temp_limit || min < min_temp_limit || min > max_temp_limit) { |
groletter | 0:3f846fc933a2 | 71 | return false; |
groletter | 0:3f846fc933a2 | 72 | } |
groletter | 0:3f846fc933a2 | 73 | else { |
groletter | 0:3f846fc933a2 | 74 | min_temp = min; |
groletter | 0:3f846fc933a2 | 75 | return true; |
groletter | 0:3f846fc933a2 | 76 | } |
groletter | 0:3f846fc933a2 | 77 | } |
groletter | 0:3f846fc933a2 | 78 | |
groletter | 0:3f846fc933a2 | 79 | bool Temperature::set_max(double max) { |
groletter | 0:3f846fc933a2 | 80 | if (max > max_temp_limit || max < min_temp_limit || max < min_temp) { |
groletter | 0:3f846fc933a2 | 81 | return false; |
groletter | 0:3f846fc933a2 | 82 | } |
groletter | 0:3f846fc933a2 | 83 | else { |
groletter | 0:3f846fc933a2 | 84 | max_temp = max; |
groletter | 0:3f846fc933a2 | 85 | return true; |
groletter | 0:3f846fc933a2 | 86 | } |
groletter | 0:3f846fc933a2 | 87 | } |
groletter | 0:3f846fc933a2 | 88 | |
groletter | 0:3f846fc933a2 | 89 | double Temperature::get_min(void) { |
groletter | 0:3f846fc933a2 | 90 | return min_temp; |
groletter | 0:3f846fc933a2 | 91 | } |
groletter | 0:3f846fc933a2 | 92 | |
groletter | 0:3f846fc933a2 | 93 | double Temperature::get_max(void) { |
groletter | 0:3f846fc933a2 | 94 | return max_temp; |
groletter | 0:3f846fc933a2 | 95 | } |
groletter | 0:3f846fc933a2 | 96 | |
groletter | 0:3f846fc933a2 | 97 | double Temperature:: get_period() { |
groletter | 0:3f846fc933a2 | 98 | return temp_sample_period; |
groletter | 0:3f846fc933a2 | 99 | } |
groletter | 0:3f846fc933a2 | 100 | |
groletter | 0:3f846fc933a2 | 101 | int Temperature::get_max_samples() { |
groletter | 0:3f846fc933a2 | 102 | return max_samples; |
groletter | 0:3f846fc933a2 | 103 | } |
groletter | 0:3f846fc933a2 | 104 | |
groletter | 0:3f846fc933a2 | 105 | const std::vector<std::string> &Temperature::get_samples(void) { |
groletter | 0:3f846fc933a2 | 106 | // FIXME - need to copy out samples in order of oldest |
groletter | 0:3f846fc933a2 | 107 | // to newest or maybe not if change to contain timestamp |
groletter | 0:3f846fc933a2 | 108 | // and let the host worry about adding it. If use multi-map |
groletter | 0:3f846fc933a2 | 109 | // it would be sorted automatically. Maybe a better idea. |
groletter | 0:3f846fc933a2 | 110 | return temp_samples; |
groletter | 0:3f846fc933a2 | 111 | } |
groletter | 0:3f846fc933a2 | 112 |