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 #include "USBHomeMon.h"
groletter 0:3f846fc933a2 2 #include <stdio.h>
groletter 0:3f846fc933a2 3 #include <stdlib.h>
groletter 0:3f846fc933a2 4 #include <vector>
groletter 0:3f846fc933a2 5 #include "Temperature.h"
groletter 0:3f846fc933a2 6 #include "HomeMonUtils.h"
groletter 0:3f846fc933a2 7 #include "USBSerial.h"
groletter 0:3f846fc933a2 8 #include "Motion.h"
groletter 0:3f846fc933a2 9 #include <sstream>
groletter 0:3f846fc933a2 10 #include <string>
groletter 0:3f846fc933a2 11
groletter 0:3f846fc933a2 12 extern USBSerial serial;
groletter 0:3f846fc933a2 13
groletter 0:3f846fc933a2 14 int mon_get() {
groletter 0:3f846fc933a2 15 return serial._getc();
groletter 0:3f846fc933a2 16 }
groletter 0:3f846fc933a2 17
groletter 0:3f846fc933a2 18 int mon_send(char c) {
groletter 0:3f846fc933a2 19 return serial._putc(c);
groletter 0:3f846fc933a2 20 }
groletter 0:3f846fc933a2 21
groletter 0:3f846fc933a2 22 bool host_wait() {
groletter 0:3f846fc933a2 23 char resp;
groletter 0:3f846fc933a2 24 // FIXME - check buffer size before waiting so
groletter 0:3f846fc933a2 25 // we don't block?
groletter 0:3f846fc933a2 26 if ((resp = mon_get()) == 'k') {
groletter 0:3f846fc933a2 27 return true;
groletter 0:3f846fc933a2 28 }
groletter 0:3f846fc933a2 29 else {
groletter 0:3f846fc933a2 30 return false;
groletter 0:3f846fc933a2 31 }
groletter 0:3f846fc933a2 32 }
groletter 0:3f846fc933a2 33
groletter 0:3f846fc933a2 34 bool check_connection() {
groletter 0:3f846fc933a2 35 if (serial.available()) {
groletter 0:3f846fc933a2 36 if (mon_get() == 'c') {
groletter 0:3f846fc933a2 37 send_ack();
groletter 0:3f846fc933a2 38 return true;
groletter 0:3f846fc933a2 39 }
groletter 0:3f846fc933a2 40 else {
groletter 0:3f846fc933a2 41 return false;
groletter 0:3f846fc933a2 42 }
groletter 0:3f846fc933a2 43 }
groletter 0:3f846fc933a2 44 }
groletter 0:3f846fc933a2 45
groletter 0:3f846fc933a2 46
groletter 0:3f846fc933a2 47 bool send_alert(alert_type alert) {
groletter 0:3f846fc933a2 48 int response;
groletter 0:3f846fc933a2 49 mon_send('a'); // Send alert
groletter 0:3f846fc933a2 50 // Wait for response
groletter 0:3f846fc933a2 51 response = host_wait();
groletter 0:3f846fc933a2 52 if (!response) {
groletter 0:3f846fc933a2 53 return false;
groletter 0:3f846fc933a2 54 }
groletter 0:3f846fc933a2 55 switch (alert) {
groletter 0:3f846fc933a2 56 case MOTION:
groletter 0:3f846fc933a2 57 mon_send('m');
groletter 0:3f846fc933a2 58 break;
groletter 0:3f846fc933a2 59 case TEMP_LOW:
groletter 0:3f846fc933a2 60 mon_send('l');
groletter 0:3f846fc933a2 61 break;
groletter 0:3f846fc933a2 62 case TEMP_HI:
groletter 0:3f846fc933a2 63 mon_send('h');
groletter 0:3f846fc933a2 64 break;
groletter 0:3f846fc933a2 65 }
groletter 0:3f846fc933a2 66 response = host_wait();
groletter 0:3f846fc933a2 67 if (!response) {
groletter 0:3f846fc933a2 68 return false;
groletter 0:3f846fc933a2 69 }
groletter 0:3f846fc933a2 70
groletter 0:3f846fc933a2 71 return true;
groletter 0:3f846fc933a2 72 }
groletter 0:3f846fc933a2 73
groletter 0:3f846fc933a2 74 char rec_command() {
groletter 0:3f846fc933a2 75
groletter 0:3f846fc933a2 76 char host_msg;
groletter 0:3f846fc933a2 77
groletter 0:3f846fc933a2 78 host_msg = mon_get();
groletter 0:3f846fc933a2 79
groletter 0:3f846fc933a2 80 return host_msg;
groletter 0:3f846fc933a2 81 }
groletter 0:3f846fc933a2 82
groletter 0:3f846fc933a2 83 bool send_sample(double sample) {
groletter 0:3f846fc933a2 84 // Somewhat kludgy way of creating a fixed width field so
groletter 0:3f846fc933a2 85 // I don't have to worry about parsing. All samples are always
groletter 0:3f846fc933a2 86 // 7 characters wide and will be padded with spaces to ensure it.
groletter 0:3f846fc933a2 87
groletter 0:3f846fc933a2 88 std::string mystring;
groletter 0:3f846fc933a2 89 if (convert_sample(sample, mystring)) {
groletter 0:3f846fc933a2 90 for (size_t i=0; i < mystring.size(); ++i) {
groletter 0:3f846fc933a2 91 mon_send(mystring[i]);
groletter 0:3f846fc933a2 92 }
groletter 0:3f846fc933a2 93 return true;
groletter 0:3f846fc933a2 94 }
groletter 0:3f846fc933a2 95 else {
groletter 0:3f846fc933a2 96 printf("Error with %f\n", sample);
groletter 0:3f846fc933a2 97 send_err();
groletter 0:3f846fc933a2 98 return false;
groletter 0:3f846fc933a2 99 }
groletter 0:3f846fc933a2 100 }
groletter 0:3f846fc933a2 101
groletter 0:3f846fc933a2 102 bool send_samples(std::vector<std::string> samples) {
groletter 0:3f846fc933a2 103 for (size_t i=0; i < samples.size(); ++i) {
groletter 0:3f846fc933a2 104 std::string tempstr = samples[i];
groletter 0:3f846fc933a2 105 for (size_t j=0; j < tempstr.size(); ++j) {
groletter 0:3f846fc933a2 106 mon_send(tempstr[j]);
groletter 0:3f846fc933a2 107 }
groletter 0:3f846fc933a2 108 }
groletter 0:3f846fc933a2 109
groletter 0:3f846fc933a2 110 // FIXME - need to make sure samples were sent okay and return
groletter 0:3f846fc933a2 111 // proper value.
groletter 0:3f846fc933a2 112 return true;
groletter 0:3f846fc933a2 113 }
groletter 0:3f846fc933a2 114
groletter 0:3f846fc933a2 115 double get_sample(void) {
groletter 0:3f846fc933a2 116 char char_sample[SAMP_SIZE+1];
groletter 0:3f846fc933a2 117 char_sample[SAMP_SIZE] = '\0';
groletter 0:3f846fc933a2 118 for (int i=0; i<SAMP_SIZE; ++i) {
groletter 0:3f846fc933a2 119 char_sample[i] = mon_get();
groletter 0:3f846fc933a2 120 }
groletter 0:3f846fc933a2 121 // FIXME - remove
groletter 0:3f846fc933a2 122 // FIXME - need error checking here on sample. Try
groletter 0:3f846fc933a2 123 // printing it to a string and make sure it will fit in
groletter 0:3f846fc933a2 124 // 7 characters or don't allow it to be set.
groletter 0:3f846fc933a2 125 printf("Sample = %s\n", char_sample);
groletter 0:3f846fc933a2 126 return atof(char_sample);
groletter 0:3f846fc933a2 127 }
groletter 0:3f846fc933a2 128
groletter 0:3f846fc933a2 129 msg_type parse_msg(char msg) {
groletter 0:3f846fc933a2 130 switch(msg) {
groletter 0:3f846fc933a2 131 case 'z':
groletter 0:3f846fc933a2 132 return SET_TEMP_MIN;
groletter 0:3f846fc933a2 133 break;
groletter 0:3f846fc933a2 134 case 'y':
groletter 0:3f846fc933a2 135 return SET_TEMP_MAX;
groletter 0:3f846fc933a2 136 break;
groletter 0:3f846fc933a2 137 case 'x':
groletter 0:3f846fc933a2 138 return SET_TEMP_NUM_SAMPLES;
groletter 0:3f846fc933a2 139 break;
groletter 0:3f846fc933a2 140 case 'w':
groletter 0:3f846fc933a2 141 return SET_MOTION_NUM_SAMPLES;
groletter 0:3f846fc933a2 142 break;
groletter 0:3f846fc933a2 143 case 'v':
groletter 0:3f846fc933a2 144 return SET_PERIOD;
groletter 0:3f846fc933a2 145 break;
groletter 0:3f846fc933a2 146 case 'u':
groletter 0:3f846fc933a2 147 return SET_MOTION_THRESH;
groletter 0:3f846fc933a2 148 break;
groletter 0:3f846fc933a2 149 case 't':
groletter 0:3f846fc933a2 150 return GET_TEMP_SAMPLES;
groletter 0:3f846fc933a2 151 break;
groletter 0:3f846fc933a2 152 case 's':
groletter 0:3f846fc933a2 153 return GET_MOTION_SAMPLES;
groletter 0:3f846fc933a2 154 break;
groletter 0:3f846fc933a2 155 case 'r':
groletter 0:3f846fc933a2 156 return GET_MIN_TEMP;
groletter 0:3f846fc933a2 157 break;
groletter 0:3f846fc933a2 158 case 'q':
groletter 0:3f846fc933a2 159 return GET_MAX_TEMP;
groletter 0:3f846fc933a2 160 break;
groletter 0:3f846fc933a2 161 case 'p':
groletter 0:3f846fc933a2 162 return GET_PERIOD;
groletter 0:3f846fc933a2 163 break;
groletter 0:3f846fc933a2 164 case 'o':
groletter 0:3f846fc933a2 165 return GET_TEMP_NUM_SAMPLES;
groletter 0:3f846fc933a2 166 break;
groletter 0:3f846fc933a2 167 case 'n':
groletter 0:3f846fc933a2 168 return GET_MOTION_NUM_SAMPLES;
groletter 0:3f846fc933a2 169 break;
groletter 0:3f846fc933a2 170 case 'j':
groletter 0:3f846fc933a2 171 return GET_MOTION_THRESH;
groletter 0:3f846fc933a2 172 break;
groletter 0:3f846fc933a2 173 default:
groletter 0:3f846fc933a2 174 printf("Received unknown command: %c\n", msg);
groletter 0:3f846fc933a2 175 return MON_ERROR;
groletter 0:3f846fc933a2 176 }
groletter 0:3f846fc933a2 177 }
groletter 0:3f846fc933a2 178
groletter 0:3f846fc933a2 179 void host_command_resp(msg_type msg, Temperature &temp, Motion &motion) {
groletter 0:3f846fc933a2 180 double sample, sample2, sample3;
groletter 0:3f846fc933a2 181 motion_vec motion_thresh;
groletter 0:3f846fc933a2 182 switch (msg) {
groletter 0:3f846fc933a2 183 case SET_TEMP_MIN:
groletter 0:3f846fc933a2 184 send_ack();
groletter 0:3f846fc933a2 185 sample = get_sample();
groletter 0:3f846fc933a2 186 // FIXME - remove
groletter 0:3f846fc933a2 187 printf(" sample = %f\n", sample);
groletter 0:3f846fc933a2 188 if(temp.set_min(sample)) {
groletter 0:3f846fc933a2 189 send_ack();
groletter 0:3f846fc933a2 190 }
groletter 0:3f846fc933a2 191 else {
groletter 0:3f846fc933a2 192 printf("Error receiving sample\n");
groletter 0:3f846fc933a2 193 send_err();
groletter 0:3f846fc933a2 194 }
groletter 0:3f846fc933a2 195 break;
groletter 0:3f846fc933a2 196 case SET_TEMP_MAX:
groletter 0:3f846fc933a2 197 send_ack();
groletter 0:3f846fc933a2 198 sample = get_sample();
groletter 0:3f846fc933a2 199 if(temp.set_max(sample)) {
groletter 0:3f846fc933a2 200 send_ack();
groletter 0:3f846fc933a2 201 }
groletter 0:3f846fc933a2 202 else {
groletter 0:3f846fc933a2 203 send_err();
groletter 0:3f846fc933a2 204 }
groletter 0:3f846fc933a2 205 break;
groletter 0:3f846fc933a2 206 case SET_TEMP_NUM_SAMPLES:
groletter 0:3f846fc933a2 207 send_ack();
groletter 0:3f846fc933a2 208 sample = get_sample();
groletter 0:3f846fc933a2 209 if (temp.change_max_samples((int)sample)) {
groletter 0:3f846fc933a2 210 send_ack();
groletter 0:3f846fc933a2 211 }
groletter 0:3f846fc933a2 212 else {
groletter 0:3f846fc933a2 213 send_err();
groletter 0:3f846fc933a2 214 }
groletter 0:3f846fc933a2 215 break;
groletter 0:3f846fc933a2 216 case SET_MOTION_NUM_SAMPLES:
groletter 0:3f846fc933a2 217 send_ack();
groletter 0:3f846fc933a2 218 sample = get_sample();
groletter 0:3f846fc933a2 219 if (motion.change_max_samples((int)sample)) {
groletter 0:3f846fc933a2 220 send_ack();
groletter 0:3f846fc933a2 221 }
groletter 0:3f846fc933a2 222 else {
groletter 0:3f846fc933a2 223 send_err();
groletter 0:3f846fc933a2 224 }
groletter 0:3f846fc933a2 225 break;
groletter 0:3f846fc933a2 226 case SET_PERIOD:
groletter 0:3f846fc933a2 227 send_ack();
groletter 0:3f846fc933a2 228 sample = get_sample();
groletter 0:3f846fc933a2 229 if (temp.set_period(sample)) {
groletter 0:3f846fc933a2 230 send_ack();
groletter 0:3f846fc933a2 231 }
groletter 0:3f846fc933a2 232 else {
groletter 0:3f846fc933a2 233 send_err();
groletter 0:3f846fc933a2 234 }
groletter 0:3f846fc933a2 235 break;
groletter 0:3f846fc933a2 236 case SET_MOTION_THRESH:
groletter 0:3f846fc933a2 237 send_ack();
groletter 0:3f846fc933a2 238 sample = get_sample();
groletter 0:3f846fc933a2 239 sample2 = get_sample();
groletter 0:3f846fc933a2 240 sample3 = get_sample();
groletter 0:3f846fc933a2 241 motion_thresh.x = sample;
groletter 0:3f846fc933a2 242 motion_thresh.y = sample2;
groletter 0:3f846fc933a2 243 motion_thresh.z = sample3;
groletter 0:3f846fc933a2 244 if (motion.set_motion_thresh(motion_thresh)) {
groletter 0:3f846fc933a2 245 send_ack();
groletter 0:3f846fc933a2 246 }
groletter 0:3f846fc933a2 247 else {
groletter 0:3f846fc933a2 248 send_err();
groletter 0:3f846fc933a2 249 }
groletter 0:3f846fc933a2 250 break;
groletter 0:3f846fc933a2 251 case GET_TEMP_SAMPLES:
groletter 0:3f846fc933a2 252 send_ack();
groletter 0:3f846fc933a2 253 send_sample(temp.get_max_samples());
groletter 0:3f846fc933a2 254 send_samples(temp.get_samples());
groletter 0:3f846fc933a2 255 break;
groletter 0:3f846fc933a2 256 case GET_MOTION_SAMPLES:
groletter 0:3f846fc933a2 257 send_ack();
groletter 0:3f846fc933a2 258 send_sample(motion.get_max_samples());
groletter 0:3f846fc933a2 259 send_samples(motion.get_samples());
groletter 0:3f846fc933a2 260 break;
groletter 0:3f846fc933a2 261 case GET_MIN_TEMP:
groletter 0:3f846fc933a2 262 send_ack();
groletter 0:3f846fc933a2 263 send_sample(temp.get_min());
groletter 0:3f846fc933a2 264 break;
groletter 0:3f846fc933a2 265 case GET_MAX_TEMP:
groletter 0:3f846fc933a2 266 send_ack();
groletter 0:3f846fc933a2 267 send_sample(temp.get_max());
groletter 0:3f846fc933a2 268 break;
groletter 0:3f846fc933a2 269 case GET_PERIOD:
groletter 0:3f846fc933a2 270 send_ack();
groletter 0:3f846fc933a2 271 send_sample(temp.get_period());
groletter 0:3f846fc933a2 272 break;
groletter 0:3f846fc933a2 273 case GET_TEMP_NUM_SAMPLES:
groletter 0:3f846fc933a2 274 send_ack();
groletter 0:3f846fc933a2 275 send_sample((double)temp.get_max_samples());
groletter 0:3f846fc933a2 276 break;
groletter 0:3f846fc933a2 277 case GET_MOTION_NUM_SAMPLES:
groletter 0:3f846fc933a2 278 send_ack();
groletter 0:3f846fc933a2 279 send_sample((double)motion.get_max_samples());
groletter 0:3f846fc933a2 280 break;
groletter 0:3f846fc933a2 281 case GET_MOTION_THRESH:
groletter 0:3f846fc933a2 282 send_ack();
groletter 0:3f846fc933a2 283 motion_thresh = motion.get_motion_thresh();
groletter 0:3f846fc933a2 284 send_sample(motion_thresh.x);
groletter 0:3f846fc933a2 285 send_sample(motion_thresh.y);
groletter 0:3f846fc933a2 286 send_sample(motion_thresh.z);
groletter 0:3f846fc933a2 287 break;
groletter 0:3f846fc933a2 288 default:
groletter 0:3f846fc933a2 289 printf("Received unkown message type\n");
groletter 0:3f846fc933a2 290 send_err();
groletter 0:3f846fc933a2 291 break;
groletter 0:3f846fc933a2 292 }
groletter 0:3f846fc933a2 293 }
groletter 0:3f846fc933a2 294