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 08:54:55 2013 +0000
Revision:
1:2f5a62eb52ad
Parent:
0:3f846fc933a2
Child:
2:84432add9142
A couple bugs found and fixed.  This should be final version for class.  Maybe I'll work on it on my own later.

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 - need error checking here on sample. Try
groletter 0:3f846fc933a2 122 // printing it to a string and make sure it will fit in
groletter 0:3f846fc933a2 123 // 7 characters or don't allow it to be set.
groletter 1:2f5a62eb52ad 124 // DEBUG printf("Sample = %s\n", char_sample);
groletter 0:3f846fc933a2 125 return atof(char_sample);
groletter 0:3f846fc933a2 126 }
groletter 0:3f846fc933a2 127
groletter 0:3f846fc933a2 128 msg_type parse_msg(char msg) {
groletter 0:3f846fc933a2 129 switch(msg) {
groletter 0:3f846fc933a2 130 case 'z':
groletter 0:3f846fc933a2 131 return SET_TEMP_MIN;
groletter 0:3f846fc933a2 132 break;
groletter 0:3f846fc933a2 133 case 'y':
groletter 0:3f846fc933a2 134 return SET_TEMP_MAX;
groletter 0:3f846fc933a2 135 break;
groletter 0:3f846fc933a2 136 case 'x':
groletter 0:3f846fc933a2 137 return SET_TEMP_NUM_SAMPLES;
groletter 0:3f846fc933a2 138 break;
groletter 0:3f846fc933a2 139 case 'w':
groletter 0:3f846fc933a2 140 return SET_MOTION_NUM_SAMPLES;
groletter 0:3f846fc933a2 141 break;
groletter 0:3f846fc933a2 142 case 'v':
groletter 0:3f846fc933a2 143 return SET_PERIOD;
groletter 0:3f846fc933a2 144 break;
groletter 0:3f846fc933a2 145 case 'u':
groletter 0:3f846fc933a2 146 return SET_MOTION_THRESH;
groletter 0:3f846fc933a2 147 break;
groletter 0:3f846fc933a2 148 case 't':
groletter 0:3f846fc933a2 149 return GET_TEMP_SAMPLES;
groletter 0:3f846fc933a2 150 break;
groletter 0:3f846fc933a2 151 case 's':
groletter 0:3f846fc933a2 152 return GET_MOTION_SAMPLES;
groletter 0:3f846fc933a2 153 break;
groletter 0:3f846fc933a2 154 case 'r':
groletter 0:3f846fc933a2 155 return GET_MIN_TEMP;
groletter 0:3f846fc933a2 156 break;
groletter 0:3f846fc933a2 157 case 'q':
groletter 0:3f846fc933a2 158 return GET_MAX_TEMP;
groletter 0:3f846fc933a2 159 break;
groletter 0:3f846fc933a2 160 case 'p':
groletter 0:3f846fc933a2 161 return GET_PERIOD;
groletter 0:3f846fc933a2 162 break;
groletter 0:3f846fc933a2 163 case 'o':
groletter 0:3f846fc933a2 164 return GET_TEMP_NUM_SAMPLES;
groletter 0:3f846fc933a2 165 break;
groletter 0:3f846fc933a2 166 case 'n':
groletter 0:3f846fc933a2 167 return GET_MOTION_NUM_SAMPLES;
groletter 0:3f846fc933a2 168 break;
groletter 0:3f846fc933a2 169 case 'j':
groletter 0:3f846fc933a2 170 return GET_MOTION_THRESH;
groletter 0:3f846fc933a2 171 break;
groletter 0:3f846fc933a2 172 default:
groletter 0:3f846fc933a2 173 printf("Received unknown command: %c\n", msg);
groletter 0:3f846fc933a2 174 return MON_ERROR;
groletter 0:3f846fc933a2 175 }
groletter 0:3f846fc933a2 176 }
groletter 0:3f846fc933a2 177
groletter 0:3f846fc933a2 178 void host_command_resp(msg_type msg, Temperature &temp, Motion &motion) {
groletter 0:3f846fc933a2 179 double sample, sample2, sample3;
groletter 0:3f846fc933a2 180 motion_vec motion_thresh;
groletter 0:3f846fc933a2 181 switch (msg) {
groletter 0:3f846fc933a2 182 case SET_TEMP_MIN:
groletter 0:3f846fc933a2 183 send_ack();
groletter 0:3f846fc933a2 184 sample = get_sample();
groletter 1:2f5a62eb52ad 185 // DEBUG printf(" sample = %f\n", sample);
groletter 0:3f846fc933a2 186 if(temp.set_min(sample)) {
groletter 0:3f846fc933a2 187 send_ack();
groletter 0:3f846fc933a2 188 }
groletter 0:3f846fc933a2 189 else {
groletter 0:3f846fc933a2 190 printf("Error receiving sample\n");
groletter 0:3f846fc933a2 191 send_err();
groletter 0:3f846fc933a2 192 }
groletter 0:3f846fc933a2 193 break;
groletter 0:3f846fc933a2 194 case SET_TEMP_MAX:
groletter 0:3f846fc933a2 195 send_ack();
groletter 0:3f846fc933a2 196 sample = get_sample();
groletter 0:3f846fc933a2 197 if(temp.set_max(sample)) {
groletter 0:3f846fc933a2 198 send_ack();
groletter 0:3f846fc933a2 199 }
groletter 0:3f846fc933a2 200 else {
groletter 0:3f846fc933a2 201 send_err();
groletter 0:3f846fc933a2 202 }
groletter 0:3f846fc933a2 203 break;
groletter 0:3f846fc933a2 204 case SET_TEMP_NUM_SAMPLES:
groletter 0:3f846fc933a2 205 send_ack();
groletter 0:3f846fc933a2 206 sample = get_sample();
groletter 0:3f846fc933a2 207 if (temp.change_max_samples((int)sample)) {
groletter 0:3f846fc933a2 208 send_ack();
groletter 0:3f846fc933a2 209 }
groletter 0:3f846fc933a2 210 else {
groletter 0:3f846fc933a2 211 send_err();
groletter 0:3f846fc933a2 212 }
groletter 0:3f846fc933a2 213 break;
groletter 0:3f846fc933a2 214 case SET_MOTION_NUM_SAMPLES:
groletter 0:3f846fc933a2 215 send_ack();
groletter 0:3f846fc933a2 216 sample = get_sample();
groletter 0:3f846fc933a2 217 if (motion.change_max_samples((int)sample)) {
groletter 0:3f846fc933a2 218 send_ack();
groletter 0:3f846fc933a2 219 }
groletter 0:3f846fc933a2 220 else {
groletter 0:3f846fc933a2 221 send_err();
groletter 0:3f846fc933a2 222 }
groletter 0:3f846fc933a2 223 break;
groletter 0:3f846fc933a2 224 case SET_PERIOD:
groletter 0:3f846fc933a2 225 send_ack();
groletter 0:3f846fc933a2 226 sample = get_sample();
groletter 0:3f846fc933a2 227 if (temp.set_period(sample)) {
groletter 0:3f846fc933a2 228 send_ack();
groletter 0:3f846fc933a2 229 }
groletter 0:3f846fc933a2 230 else {
groletter 0:3f846fc933a2 231 send_err();
groletter 0:3f846fc933a2 232 }
groletter 0:3f846fc933a2 233 break;
groletter 0:3f846fc933a2 234 case SET_MOTION_THRESH:
groletter 0:3f846fc933a2 235 send_ack();
groletter 0:3f846fc933a2 236 sample = get_sample();
groletter 0:3f846fc933a2 237 sample2 = get_sample();
groletter 0:3f846fc933a2 238 sample3 = get_sample();
groletter 0:3f846fc933a2 239 motion_thresh.x = sample;
groletter 0:3f846fc933a2 240 motion_thresh.y = sample2;
groletter 0:3f846fc933a2 241 motion_thresh.z = sample3;
groletter 0:3f846fc933a2 242 if (motion.set_motion_thresh(motion_thresh)) {
groletter 0:3f846fc933a2 243 send_ack();
groletter 0:3f846fc933a2 244 }
groletter 0:3f846fc933a2 245 else {
groletter 0:3f846fc933a2 246 send_err();
groletter 0:3f846fc933a2 247 }
groletter 0:3f846fc933a2 248 break;
groletter 0:3f846fc933a2 249 case GET_TEMP_SAMPLES:
groletter 0:3f846fc933a2 250 send_ack();
groletter 0:3f846fc933a2 251 send_sample(temp.get_max_samples());
groletter 0:3f846fc933a2 252 send_samples(temp.get_samples());
groletter 0:3f846fc933a2 253 break;
groletter 0:3f846fc933a2 254 case GET_MOTION_SAMPLES:
groletter 0:3f846fc933a2 255 send_ack();
groletter 0:3f846fc933a2 256 send_sample(motion.get_max_samples());
groletter 0:3f846fc933a2 257 send_samples(motion.get_samples());
groletter 0:3f846fc933a2 258 break;
groletter 0:3f846fc933a2 259 case GET_MIN_TEMP:
groletter 0:3f846fc933a2 260 send_ack();
groletter 0:3f846fc933a2 261 send_sample(temp.get_min());
groletter 0:3f846fc933a2 262 break;
groletter 0:3f846fc933a2 263 case GET_MAX_TEMP:
groletter 0:3f846fc933a2 264 send_ack();
groletter 0:3f846fc933a2 265 send_sample(temp.get_max());
groletter 0:3f846fc933a2 266 break;
groletter 0:3f846fc933a2 267 case GET_PERIOD:
groletter 0:3f846fc933a2 268 send_ack();
groletter 0:3f846fc933a2 269 send_sample(temp.get_period());
groletter 0:3f846fc933a2 270 break;
groletter 0:3f846fc933a2 271 case GET_TEMP_NUM_SAMPLES:
groletter 0:3f846fc933a2 272 send_ack();
groletter 0:3f846fc933a2 273 send_sample((double)temp.get_max_samples());
groletter 0:3f846fc933a2 274 break;
groletter 0:3f846fc933a2 275 case GET_MOTION_NUM_SAMPLES:
groletter 0:3f846fc933a2 276 send_ack();
groletter 0:3f846fc933a2 277 send_sample((double)motion.get_max_samples());
groletter 0:3f846fc933a2 278 break;
groletter 0:3f846fc933a2 279 case GET_MOTION_THRESH:
groletter 0:3f846fc933a2 280 send_ack();
groletter 0:3f846fc933a2 281 motion_thresh = motion.get_motion_thresh();
groletter 0:3f846fc933a2 282 send_sample(motion_thresh.x);
groletter 0:3f846fc933a2 283 send_sample(motion_thresh.y);
groletter 0:3f846fc933a2 284 send_sample(motion_thresh.z);
groletter 0:3f846fc933a2 285 break;
groletter 0:3f846fc933a2 286 default:
groletter 0:3f846fc933a2 287 printf("Received unkown message type\n");
groletter 0:3f846fc933a2 288 send_err();
groletter 0:3f846fc933a2 289 break;
groletter 0:3f846fc933a2 290 }
groletter 0:3f846fc933a2 291 }
groletter 0:3f846fc933a2 292