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