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

Dependents:   FinalProject

Committer:
groletter
Date:
Tue Sep 03 20:24:12 2013 +0000
Revision:
2:84432add9142
Parent:
1:2f5a62eb52ad
Added class documentation.  Not sure how to do this for pure C code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
groletter 0:3f846fc933a2 1 //
groletter 0:3f846fc933a2 2 // This file contains function related to the motion
groletter 0:3f846fc933a2 3 // control of the home monitoring system
groletter 0:3f846fc933a2 4 //
groletter 0:3f846fc933a2 5
groletter 0:3f846fc933a2 6 #include <vector>
groletter 0:3f846fc933a2 7 #include <string>
groletter 0:3f846fc933a2 8 #include <sstream>
groletter 0:3f846fc933a2 9
groletter 1:2f5a62eb52ad 10 // TODO - add timestamp to motion samples
groletter 0:3f846fc933a2 11 #include "Motion.h"
groletter 0:3f846fc933a2 12
groletter 0:3f846fc933a2 13 Motion::Motion() {
groletter 0:3f846fc933a2 14 // These are motion deltas based on playing with
groletter 1:2f5a62eb52ad 15 // mbed application board motion sensors.
groletter 1:2f5a62eb52ad 16 //
groletter 1:2f5a62eb52ad 17 // Originally 0.097 was sufficient, but I get false
groletter 1:2f5a62eb52ad 18 // motion of 0.14 sometimes. This makes it somewhat less
groletter 1:2f5a62eb52ad 19 // responsive to small motion, but better than too many
groletter 1:2f5a62eb52ad 20 // false alarms. I should look into motion sensors to
groletter 1:2f5a62eb52ad 21 // see what is up or if something has been fixed??
groletter 1:2f5a62eb52ad 22 min_motion.x = 0.15;
groletter 1:2f5a62eb52ad 23 min_motion.y = 0.15;
groletter 1:2f5a62eb52ad 24 min_motion.z = 0.15;
groletter 0:3f846fc933a2 25 max_samples_limit = 100; // Max motion buffer is 100 deep
groletter 0:3f846fc933a2 26 max_samples = 20;
groletter 0:3f846fc933a2 27 sample_ptr = 0;
groletter 0:3f846fc933a2 28 wrapped_once = false;
groletter 0:3f846fc933a2 29 // WARNING - width here is important - must be 7 characters!
groletter 0:3f846fc933a2 30 motion_samples.resize(max_samples,"0.00000");
groletter 0:3f846fc933a2 31 }
groletter 0:3f846fc933a2 32
groletter 0:3f846fc933a2 33 bool Motion::change_max_samples(int num_samples) {
groletter 0:3f846fc933a2 34 // WARNING - setting this dumps all saved data!
groletter 0:3f846fc933a2 35 if (num_samples > 0 && num_samples <= max_samples_limit) {
groletter 0:3f846fc933a2 36 max_samples = num_samples;
groletter 0:3f846fc933a2 37 // WARNING - width here is important - must be 7 characters!
groletter 0:3f846fc933a2 38 motion_samples.resize(max_samples, "0.00000");
groletter 0:3f846fc933a2 39 return true;
groletter 0:3f846fc933a2 40 }
groletter 0:3f846fc933a2 41 else {
groletter 0:3f846fc933a2 42 return false;
groletter 0:3f846fc933a2 43 }
groletter 0:3f846fc933a2 44 }
groletter 0:3f846fc933a2 45
groletter 0:3f846fc933a2 46 void Motion::add_sample(double sample) {
groletter 0:3f846fc933a2 47 std::string str_sample;
groletter 0:3f846fc933a2 48 convert_sample(sample, str_sample);
groletter 0:3f846fc933a2 49 if (sample_ptr == max_samples) {
groletter 0:3f846fc933a2 50 wrapped_once = true;
groletter 0:3f846fc933a2 51 sample_ptr = 0;
groletter 0:3f846fc933a2 52 motion_samples[sample_ptr] = str_sample;
groletter 0:3f846fc933a2 53 }
groletter 0:3f846fc933a2 54 else {
groletter 0:3f846fc933a2 55 motion_samples[sample_ptr++] = str_sample;
groletter 0:3f846fc933a2 56 }
groletter 0:3f846fc933a2 57 }
groletter 0:3f846fc933a2 58
groletter 0:3f846fc933a2 59 bool Motion::set_motion_thresh(motion_vec motion_thresh) {
groletter 0:3f846fc933a2 60 // FIXME - maybe add limit checking here. At least
groletter 0:3f846fc933a2 61 // limit to 7 character values?
groletter 0:3f846fc933a2 62 min_motion.x = motion_thresh.x;
groletter 0:3f846fc933a2 63 min_motion.y = motion_thresh.y;
groletter 0:3f846fc933a2 64 min_motion.z = motion_thresh.z;
groletter 0:3f846fc933a2 65 return true;
groletter 0:3f846fc933a2 66 }
groletter 0:3f846fc933a2 67
groletter 0:3f846fc933a2 68 motion_vec Motion::get_motion_thresh(void) {
groletter 0:3f846fc933a2 69 return min_motion;
groletter 0:3f846fc933a2 70 }
groletter 0:3f846fc933a2 71
groletter 0:3f846fc933a2 72 int Motion::get_max_samples() {
groletter 0:3f846fc933a2 73 return max_samples;
groletter 0:3f846fc933a2 74 }
groletter 0:3f846fc933a2 75
groletter 0:3f846fc933a2 76 const std::vector<std::string> &Motion::get_samples(void) {
groletter 0:3f846fc933a2 77 // FIXME - need to copy out samples in order of oldest
groletter 0:3f846fc933a2 78 // to newest or maybe not if change to contain timestamp
groletter 0:3f846fc933a2 79 // and let the host worry about adding it. If use multi-map
groletter 0:3f846fc933a2 80 // it would be sorted automatically. Maybe a better idea.
groletter 0:3f846fc933a2 81 return motion_samples;
groletter 0:3f846fc933a2 82 }
groletter 0:3f846fc933a2 83