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:
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?

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 0:3f846fc933a2 10 // FIXME - 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 0:3f846fc933a2 15 // mbed application board motion sensors
groletter 0:3f846fc933a2 16 min_motion.x = 0.097;
groletter 0:3f846fc933a2 17 min_motion.y = 0.097;
groletter 0:3f846fc933a2 18 min_motion.z = 0.097;
groletter 0:3f846fc933a2 19 max_samples_limit = 100; // Max motion buffer is 100 deep
groletter 0:3f846fc933a2 20 max_samples = 20;
groletter 0:3f846fc933a2 21 sample_ptr = 0;
groletter 0:3f846fc933a2 22 wrapped_once = false;
groletter 0:3f846fc933a2 23 // WARNING - width here is important - must be 7 characters!
groletter 0:3f846fc933a2 24 motion_samples.resize(max_samples,"0.00000");
groletter 0:3f846fc933a2 25 }
groletter 0:3f846fc933a2 26
groletter 0:3f846fc933a2 27 bool Motion::change_max_samples(int num_samples) {
groletter 0:3f846fc933a2 28 // WARNING - setting this dumps all saved data!
groletter 0:3f846fc933a2 29 if (num_samples > 0 && num_samples <= max_samples_limit) {
groletter 0:3f846fc933a2 30 max_samples = num_samples;
groletter 0:3f846fc933a2 31 // WARNING - width here is important - must be 7 characters!
groletter 0:3f846fc933a2 32 motion_samples.resize(max_samples, "0.00000");
groletter 0:3f846fc933a2 33 return true;
groletter 0:3f846fc933a2 34 }
groletter 0:3f846fc933a2 35 else {
groletter 0:3f846fc933a2 36 return false;
groletter 0:3f846fc933a2 37 }
groletter 0:3f846fc933a2 38 }
groletter 0:3f846fc933a2 39
groletter 0:3f846fc933a2 40 void Motion::add_sample(double sample) {
groletter 0:3f846fc933a2 41 std::string str_sample;
groletter 0:3f846fc933a2 42 convert_sample(sample, str_sample);
groletter 0:3f846fc933a2 43 if (sample_ptr == max_samples) {
groletter 0:3f846fc933a2 44 wrapped_once = true;
groletter 0:3f846fc933a2 45 sample_ptr = 0;
groletter 0:3f846fc933a2 46 motion_samples[sample_ptr] = str_sample;
groletter 0:3f846fc933a2 47 }
groletter 0:3f846fc933a2 48 else {
groletter 0:3f846fc933a2 49 motion_samples[sample_ptr++] = str_sample;
groletter 0:3f846fc933a2 50 }
groletter 0:3f846fc933a2 51 }
groletter 0:3f846fc933a2 52
groletter 0:3f846fc933a2 53 bool Motion::set_motion_thresh(motion_vec motion_thresh) {
groletter 0:3f846fc933a2 54 // FIXME - maybe add limit checking here. At least
groletter 0:3f846fc933a2 55 // limit to 7 character values?
groletter 0:3f846fc933a2 56 min_motion.x = motion_thresh.x;
groletter 0:3f846fc933a2 57 min_motion.y = motion_thresh.y;
groletter 0:3f846fc933a2 58 min_motion.z = motion_thresh.z;
groletter 0:3f846fc933a2 59 return true;
groletter 0:3f846fc933a2 60 }
groletter 0:3f846fc933a2 61
groletter 0:3f846fc933a2 62 motion_vec Motion::get_motion_thresh(void) {
groletter 0:3f846fc933a2 63 return min_motion;
groletter 0:3f846fc933a2 64 }
groletter 0:3f846fc933a2 65
groletter 0:3f846fc933a2 66 int Motion::get_max_samples() {
groletter 0:3f846fc933a2 67 return max_samples;
groletter 0:3f846fc933a2 68 }
groletter 0:3f846fc933a2 69
groletter 0:3f846fc933a2 70 const std::vector<std::string> &Motion::get_samples(void) {
groletter 0:3f846fc933a2 71 // FIXME - need to copy out samples in order of oldest
groletter 0:3f846fc933a2 72 // to newest or maybe not if change to contain timestamp
groletter 0:3f846fc933a2 73 // and let the host worry about adding it. If use multi-map
groletter 0:3f846fc933a2 74 // it would be sorted automatically. Maybe a better idea.
groletter 0:3f846fc933a2 75 return motion_samples;
groletter 0:3f846fc933a2 76 }
groletter 0:3f846fc933a2 77