3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
aburch1
Date:
Thu May 11 15:53:15 2017 +0000
Revision:
82:668b51a39148
Parent:
81:996c0a3319b4
Child:
83:0d3572a8a851
Added formatting to messages, errors now terminate the program and wait for user input to restart

Who changed what in which revision?

UserRevisionLine numberNew contents of line
FairyMental 36:19d3f752f9c3 1
Jonathan Austin 0:2757d7abb7d9 2 #include "mbed.h"
martinsimpson 32:260a288be58f 3 #include "rtos.h"
aburch1 74:749727490f44 4 #include "hts221.h"
aburch1 74:749727490f44 5 #include "LPS25H.h"
aburch1 74:749727490f44 6
aburch1 81:996c0a3319b4 7 #include "MessageLogger.h"
aburch1 74:749727490f44 8 #include "CircularArray.h"
aburch1 74:749727490f44 9 #include "FakeSensor.h"
aburch1 74:749727490f44 10
FairyMental 43:3983059e0d91 11 #include <string.h>
FairyMental 34:09ed07f2acba 12 #include <stdio.h>
FairyMental 34:09ed07f2acba 13 #include <ctype.h>
FairyMental 41:d222c043c96d 14 #include <iostream>
aburch1 74:749727490f44 15 #include <sstream>
martinsimpson 32:260a288be58f 16
FairyMental 36:19d3f752f9c3 17 #define SIGNAL_doMeasure 1
aburch1 75:b44645bbf2d2 18 #define SIGNAL_printMessage 2
aburch1 81:996c0a3319b4 19 #define SIGNAL_terminate 3
FairyMental 36:19d3f752f9c3 20 #define SWITCH1_RELEASE 90
Netaphous 65:3723d2729b68 21 #define BUFFER_SIZE 120
FairyMental 34:09ed07f2acba 22
FairyMental 57:dfcdda1e42b6 23 //
FairyMental 57:dfcdda1e42b6 24 // MBED DECLARATIONS
FairyMental 57:dfcdda1e42b6 25 //
martinsimpson 32:260a288be58f 26 DigitalOut myled(LED1);
Netaphous 50:c07e968b9582 27 DigitalIn onBoardSwitch(USER_BUTTON);
martinsimpson 32:260a288be58f 28 I2C i2c2(I2C_SDA, I2C_SCL);
Netaphous 50:c07e968b9582 29
FairyMental 57:dfcdda1e42b6 30 //
FairyMental 57:dfcdda1e42b6 31 // SENSOR DECLARATIONS
Netaphous 50:c07e968b9582 32 // MAKE SURE ONE OF THESE IS COMMENTED OUT
Netaphous 50:c07e968b9582 33 // Real sensor
aburch1 75:b44645bbf2d2 34 // LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
aburch1 75:b44645bbf2d2 35 // HTS221 measurer(I2C_SDA, I2C_SCL);
Netaphous 50:c07e968b9582 36 // Fake sensor
aburch1 75:b44645bbf2d2 37 FakeBarometer barometer(1029.0, 1031.0);
aburch1 75:b44645bbf2d2 38 FakeMeasurer measurer(20.0, 25.0, 30.0, 50.0);
Netaphous 50:c07e968b9582 39
FairyMental 57:dfcdda1e42b6 40 //
FairyMental 57:dfcdda1e42b6 41 // THREADS DECLARATION
FairyMental 57:dfcdda1e42b6 42 //
aburch1 82:668b51a39148 43 osThreadId mainThreadId;
FairyMental 37:00775e368a71 44 Thread *produceThread;
FairyMental 37:00775e368a71 45 Thread *measureThread;
FairyMental 39:618ad21e2b34 46 Thread *consumeThread;
aburch1 74:749727490f44 47 Thread *loggingThread;
FairyMental 58:7fc6e3e4d746 48 Ticker timer;
FairyMental 58:7fc6e3e4d746 49 Ticker realTimeDate;
FairyMental 57:dfcdda1e42b6 50 //
FairyMental 57:dfcdda1e42b6 51 // GLOBAL VARIABLES
FairyMental 57:dfcdda1e42b6 52 //
FairyMental 34:09ed07f2acba 53 Mail<Measure, 16> mail_box;
FairyMental 46:0de1f3c7d118 54 LocalDate *localDate;
aburch1 74:749727490f44 55 //Mail<>
aburch1 82:668b51a39148 56 bool logging = false;
aburch1 82:668b51a39148 57 bool measuring = true;
aburch1 81:996c0a3319b4 58 double sampleRate = 15;
aburch1 81:996c0a3319b4 59 char temp[256];
FairyMental 49:83bea7fb2728 60
aburch1 74:749727490f44 61 // Logging objects
aburch1 75:b44645bbf2d2 62 MessageLogger logger;
aburch1 74:749727490f44 63
aburch1 81:996c0a3319b4 64 CircularArray buffer(BUFFER_SIZE, &logger);
FairyMental 57:dfcdda1e42b6 65 //
FairyMental 57:dfcdda1e42b6 66 // Called by a TICKER
FairyMental 57:dfcdda1e42b6 67 // Adds 1 second every second to the clock
FairyMental 46:0de1f3c7d118 68 void RealTimeDate()
FairyMental 46:0de1f3c7d118 69 {
FairyMental 46:0de1f3c7d118 70 localDate->TickSecond();
FairyMental 46:0de1f3c7d118 71 }
FairyMental 58:7fc6e3e4d746 72
FairyMental 58:7fc6e3e4d746 73 //
FairyMental 58:7fc6e3e4d746 74 // Ticker that signals the measureThread to do a measure
FairyMental 58:7fc6e3e4d746 75 //
FairyMental 58:7fc6e3e4d746 76 void SendSignalDoMeasure()
aburch1 82:668b51a39148 77 {
aburch1 82:668b51a39148 78 measureThread->signal_set(SIGNAL_doMeasure);
FairyMental 58:7fc6e3e4d746 79 }
FairyMental 58:7fc6e3e4d746 80
FairyMental 57:dfcdda1e42b6 81 //
FairyMental 57:dfcdda1e42b6 82 // SIGNALED BY Ticker at a frequency of <T> Hz
FairyMental 57:dfcdda1e42b6 83 // Reads values from sensor board, sends over through mail queue
aburch1 79:4e6b53eb678b 84 void MeasureThread()
aburch1 79:4e6b53eb678b 85 {
aburch1 82:668b51a39148 86 float temperature , humidity, pressure;
FairyMental 35:484e384f9bf1 87
FairyMental 36:19d3f752f9c3 88 while(true)
FairyMental 36:19d3f752f9c3 89 {
aburch1 79:4e6b53eb678b 90 temperature = 0;
aburch1 79:4e6b53eb678b 91 humidity = 0;
aburch1 79:4e6b53eb678b 92 pressure = 0;
FairyMental 57:dfcdda1e42b6 93 //Await signal from ticker
FairyMental 36:19d3f752f9c3 94 Thread::signal_wait(SIGNAL_doMeasure);
FairyMental 57:dfcdda1e42b6 95
FairyMental 36:19d3f752f9c3 96 Measure *measure = mail_box.alloc();
FairyMental 36:19d3f752f9c3 97 if (measure == NULL)
FairyMental 36:19d3f752f9c3 98 {
aburch1 82:668b51a39148 99 logger.SendError("Out of memory\r\n");
FairyMental 36:19d3f752f9c3 100 return;
FairyMental 36:19d3f752f9c3 101 }
FairyMental 34:09ed07f2acba 102
FairyMental 57:dfcdda1e42b6 103 //Read and fill in data
FairyMental 36:19d3f752f9c3 104 measurer.ReadTempHumi(&temperature,&humidity);
FairyMental 36:19d3f752f9c3 105 barometer.get();
FairyMental 36:19d3f752f9c3 106 pressure = barometer.pressure();
FairyMental 47:468a89d62c23 107
FairyMental 36:19d3f752f9c3 108 measure->temperature = temperature;
FairyMental 36:19d3f752f9c3 109 measure->humidity = humidity;
FairyMental 36:19d3f752f9c3 110 measure->pressure = pressure;
Netaphous 67:8d0e88172e2a 111 measure->date.setValues(localDate);
FairyMental 36:19d3f752f9c3 112
FairyMental 57:dfcdda1e42b6 113 osStatus stat = mail_box.put(measure);
martinsimpson 32:260a288be58f 114
FairyMental 36:19d3f752f9c3 115 //Check if succesful
FairyMental 36:19d3f752f9c3 116 if (stat == osErrorResource) {
aburch1 81:996c0a3319b4 117 snprintf(temp, 256, "queue->put() Error code: %4Xh, Resource not available\r\n", stat);
aburch1 81:996c0a3319b4 118 logger.SendError(temp);
FairyMental 36:19d3f752f9c3 119 mail_box.free(measure);
FairyMental 36:19d3f752f9c3 120 return;
FairyMental 36:19d3f752f9c3 121 }
aburch1 82:668b51a39148 122
aburch1 82:668b51a39148 123 if(logging)
aburch1 82:668b51a39148 124 {
aburch1 82:668b51a39148 125 logger.SendMessage("Measurement Taken:\r\n");
aburch1 82:668b51a39148 126 char *ptr = localDate->ToString();
aburch1 82:668b51a39148 127 snprintf(temp, 256, " %s T: %f | H: %f | P: %f |\n\r",ptr, temperature, humidity, pressure);
aburch1 82:668b51a39148 128 logger.SendMessage(temp);
aburch1 82:668b51a39148 129 }
FairyMental 34:09ed07f2acba 130 }
FairyMental 34:09ed07f2acba 131 }
FairyMental 34:09ed07f2acba 132
FairyMental 57:dfcdda1e42b6 133 //
FairyMental 57:dfcdda1e42b6 134 // Receives data through mail queue, then adds it to the global declared list
FairyMental 57:dfcdda1e42b6 135 // A.K.A. Producer Thread
FairyMental 37:00775e368a71 136 void ProducerThread()
FairyMental 34:09ed07f2acba 137 {
FairyMental 57:dfcdda1e42b6 138 while (true)
FairyMental 57:dfcdda1e42b6 139 {
FairyMental 34:09ed07f2acba 140 //Block on the queue
FairyMental 34:09ed07f2acba 141 osEvent evt = mail_box.get();
FairyMental 34:09ed07f2acba 142
FairyMental 34:09ed07f2acba 143 //Check status
FairyMental 34:09ed07f2acba 144 if (evt.status == osEventMail) {
FairyMental 39:618ad21e2b34 145
FairyMental 57:dfcdda1e42b6 146 Measure *measure = (Measure*)evt.value.p;
FairyMental 47:468a89d62c23 147 Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure);
Netaphous 65:3723d2729b68 148
Netaphous 65:3723d2729b68 149 // Changed to use circlar buffer rather than list buffer
Netaphous 65:3723d2729b68 150 buffer.pushValue(msr);
FairyMental 34:09ed07f2acba 151 mail_box.free(measure);
FairyMental 34:09ed07f2acba 152 } else {
aburch1 82:668b51a39148 153 snprintf(temp, 256, "ERROR: %x\r\n", evt.status);
aburch1 81:996c0a3319b4 154 logger.SendError(temp);
aburch1 81:996c0a3319b4 155 }
FairyMental 57:dfcdda1e42b6 156 }
FairyMental 34:09ed07f2acba 157 }
Netaphous 69:72f237750d85 158 int i;
FairyMental 57:dfcdda1e42b6 159 //
FairyMental 57:dfcdda1e42b6 160 // Compares two char arrays and returns result
FairyMental 57:dfcdda1e42b6 161 // Param1: First char Array / pointer
FairyMental 57:dfcdda1e42b6 162 // Param2: Second char Array / pointer
FairyMental 57:dfcdda1e42b6 163 // Param3. Size of the smallest char arrays (between param1 and param2)
FairyMental 57:dfcdda1e42b6 164 // Return: "-1" IF NOT EQUAL
FairyMental 57:dfcdda1e42b6 165 // "1 " IF EQUAL
aburch1 81:996c0a3319b4 166 bool CompareCommands(char command[],char targetcommand[], int size)
FairyMental 42:b1f29874ab70 167 {
FairyMental 43:3983059e0d91 168 for(i = 0; i < size; i ++)
FairyMental 42:b1f29874ab70 169 {
FairyMental 42:b1f29874ab70 170 if(command[i] != targetcommand[i])
aburch1 81:996c0a3319b4 171 {
aburch1 81:996c0a3319b4 172 return false;
aburch1 81:996c0a3319b4 173 }
FairyMental 42:b1f29874ab70 174 }
aburch1 81:996c0a3319b4 175 return true;
FairyMental 42:b1f29874ab70 176 }
aburch1 74:749727490f44 177
FairyMental 57:dfcdda1e42b6 178 //
FairyMental 57:dfcdda1e42b6 179 // Reads commands through PUTTY and 'consumes the data' accordingly
FairyMental 57:dfcdda1e42b6 180 // A.K.A. Consumer Thread
FairyMental 39:618ad21e2b34 181 void ConsumeThread()
FairyMental 39:618ad21e2b34 182 {
FairyMental 57:dfcdda1e42b6 183 //Last character pressed read (last key input)
FairyMental 41:d222c043c96d 184 char charCmd;
FairyMental 57:dfcdda1e42b6 185 //Char array that stores the command after user presses ENTER
FairyMental 41:d222c043c96d 186 char command[40];
FairyMental 57:dfcdda1e42b6 187 //Current Command Size
aburch1 79:4e6b53eb678b 188 int crtChar = 0;
aburch1 81:996c0a3319b4 189 logger.SendMessage("\r\nAwaiting Command: \r\n");
FairyMental 39:618ad21e2b34 190 while(1)
FairyMental 39:618ad21e2b34 191 {
FairyMental 41:d222c043c96d 192 charCmd = NULL;
FairyMental 41:d222c043c96d 193 charCmd = getchar();
aburch1 82:668b51a39148 194 if(logging)
aburch1 82:668b51a39148 195 {
aburch1 82:668b51a39148 196 logging = false;
aburch1 82:668b51a39148 197 logger.SendMessage("\r\033[1AKey Pressed. Debug logging disabled.\r\n");
aburch1 82:668b51a39148 198 charCmd = NULL;
aburch1 82:668b51a39148 199 }
aburch1 80:959151952153 200
FairyMental 41:d222c043c96d 201 if(charCmd != NULL)
FairyMental 41:d222c043c96d 202 {
FairyMental 57:dfcdda1e42b6 203 //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed.
FairyMental 57:dfcdda1e42b6 204 if (charCmd == 127 && crtChar > 0 )
FairyMental 43:3983059e0d91 205 {
aburch1 79:4e6b53eb678b 206 command[--crtChar] = '\0';
FairyMental 43:3983059e0d91 207 }
FairyMental 57:dfcdda1e42b6 208 //If NOT enter AND NOT Backspace is pressed, SAVE the char
FairyMental 57:dfcdda1e42b6 209 else if(charCmd != 13 && charCmd != 127)
FairyMental 41:d222c043c96d 210 {
FairyMental 41:d222c043c96d 211 command[crtChar++] = charCmd;
FairyMental 41:d222c043c96d 212 }
FairyMental 57:dfcdda1e42b6 213 //If ENTER is pressed, PROCESS it
FairyMental 44:b523c9a9dd97 214 else if(charCmd == 13) // If Enter is pressed
FairyMental 49:83bea7fb2728 215 {
FairyMental 57:dfcdda1e42b6 216 //Get first word of command:
FairyMental 43:3983059e0d91 217 char *charPos;
FairyMental 43:3983059e0d91 218 charPos = strtok(command," -,");
FairyMental 57:dfcdda1e42b6 219
FairyMental 57:dfcdda1e42b6 220 //Check if it's a "LIST" command
aburch1 81:996c0a3319b4 221 if(CompareCommands(charPos, "read",4))
FairyMental 42:b1f29874ab70 222 {
FairyMental 43:3983059e0d91 223 charPos = strtok(NULL," -,");
FairyMental 57:dfcdda1e42b6 224 //Check if it's a "LIST ALL" command
aburch1 81:996c0a3319b4 225 if(CompareCommands(charPos, "all",3))
aburch1 82:668b51a39148 226 {
Netaphous 65:3723d2729b68 227 // Changed to use circular buffer rather than list buffer
Netaphous 65:3723d2729b68 228 buffer.readAll();
aburch1 81:996c0a3319b4 229 logger.SendMessage("D O N E ! \r\n");
FairyMental 43:3983059e0d91 230 }
FairyMental 57:dfcdda1e42b6 231 //Check if it's a "LIST X" command
FairyMental 43:3983059e0d91 232 else if(strtol(charPos,NULL,10) != 0)
FairyMental 43:3983059e0d91 233 {
aburch1 81:996c0a3319b4 234 int num = atoi(charPos);
aburch1 80:959151952153 235
aburch1 79:4e6b53eb678b 236 logger.SendMessage(temp);
Netaphous 65:3723d2729b68 237
Netaphous 65:3723d2729b68 238 // Changed to use circular buffer rather than list buffer
Netaphous 69:72f237750d85 239 buffer.readX(num);
aburch1 81:996c0a3319b4 240 logger.SendMessage("D O N E ! \r\n");
FairyMental 60:db8c5b7fc548 241 }
FairyMental 60:db8c5b7fc548 242 else
FairyMental 60:db8c5b7fc548 243 {
aburch1 81:996c0a3319b4 244 logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number.\r\n");
FairyMental 43:3983059e0d91 245 }
FairyMental 42:b1f29874ab70 246 }
FairyMental 57:dfcdda1e42b6 247 //Check if it's a "DELETE" command
aburch1 81:996c0a3319b4 248 else if (CompareCommands(charPos,"delete",6))
FairyMental 44:b523c9a9dd97 249 {
FairyMental 44:b523c9a9dd97 250 charPos = strtok(NULL," -,");
FairyMental 57:dfcdda1e42b6 251 //Check if it's a "DELETE ALL" command
aburch1 81:996c0a3319b4 252 if(CompareCommands(charPos,"all",3))
FairyMental 44:b523c9a9dd97 253 {
aburch1 81:996c0a3319b4 254 logger.SendMessage("Deleting all measures performed so far: \r\n");
Netaphous 65:3723d2729b68 255
Netaphous 65:3723d2729b68 256 // Changed to use circular buffer rather than list buffer
Netaphous 65:3723d2729b68 257 buffer.deleteAll();
FairyMental 44:b523c9a9dd97 258 }
FairyMental 57:dfcdda1e42b6 259 //Check if it's a "DELETE X" command
FairyMental 44:b523c9a9dd97 260 else if (strtol(charPos,NULL,10) != 0)
FairyMental 44:b523c9a9dd97 261 {
Netaphous 65:3723d2729b68 262 // Changed to use circular buffer rather than list buffer
Netaphous 65:3723d2729b68 263 buffer.deleteX(atoi(charPos));
aburch1 81:996c0a3319b4 264 logger.SendMessage("Elements deleted!\r\n");
FairyMental 60:db8c5b7fc548 265 }
FairyMental 60:db8c5b7fc548 266 else
FairyMental 60:db8c5b7fc548 267 {
aburch1 81:996c0a3319b4 268 logger.SendMessage("Expected parameters: \"all\" | \"n\", where n is a number.");
aburch1 81:996c0a3319b4 269 }
FairyMental 44:b523c9a9dd97 270 }
FairyMental 60:db8c5b7fc548 271 //Check if it's a "STATUS" command
aburch1 81:996c0a3319b4 272 else if (CompareCommands(charPos,"status",6))
FairyMental 45:9a33f2bc2b4e 273 {
FairyMental 46:0de1f3c7d118 274 char *ptr = localDate->ToString();
Netaphous 65:3723d2729b68 275
Netaphous 65:3723d2729b68 276 // Changed to use circular buffer rather than list buffer
aburch1 82:668b51a39148 277 snprintf(temp, 256, "\nStatus: \r\n");
aburch1 82:668b51a39148 278 logger.SendMessage(temp);
aburch1 82:668b51a39148 279 snprintf(temp, 256, " # of measures: %i \r\n", buffer.getSize());
aburch1 82:668b51a39148 280 logger.SendMessage(temp);
aburch1 82:668b51a39148 281 snprintf(temp, 256, " Sampling: %s \r\n", measuring ? "On" : "Off");
aburch1 82:668b51a39148 282 logger.SendMessage(temp);
aburch1 82:668b51a39148 283 snprintf(temp, 256, " Logging: %s \r\n", logging ? "On" : "Off");
aburch1 82:668b51a39148 284 logger.SendMessage(temp);
aburch1 82:668b51a39148 285 snprintf(temp, 256, " Current Date: %s \r\n", ptr);
aburch1 82:668b51a39148 286 logger.SendMessage(temp);
aburch1 82:668b51a39148 287 snprintf(temp, 256, " Sample Rate(s): %2.2f \r\n", sampleRate);
aburch1 81:996c0a3319b4 288 logger.SendMessage(temp);
FairyMental 45:9a33f2bc2b4e 289 }
FairyMental 57:dfcdda1e42b6 290 //Check if it's a "SETTIME" command
aburch1 81:996c0a3319b4 291 else if (CompareCommands(charPos,"settime",7))
FairyMental 48:a8219954b3f2 292 {
FairyMental 48:a8219954b3f2 293 int h,m,s;
FairyMental 57:dfcdda1e42b6 294 //Fetch 1st Param
aburch1 81:996c0a3319b4 295 charPos = strtok(NULL," -,");
FairyMental 48:a8219954b3f2 296 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 297 {
FairyMental 48:a8219954b3f2 298 h = atoi(charPos);
FairyMental 48:a8219954b3f2 299 }
FairyMental 57:dfcdda1e42b6 300 //Fech 2nd Param
aburch1 81:996c0a3319b4 301 charPos = strtok(NULL," -,");
FairyMental 48:a8219954b3f2 302 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 303 {
FairyMental 48:a8219954b3f2 304 m = atoi(charPos);
FairyMental 48:a8219954b3f2 305 }
FairyMental 57:dfcdda1e42b6 306 //Fetch 3rd Param
aburch1 81:996c0a3319b4 307 charPos = strtok(NULL," -,");
FairyMental 48:a8219954b3f2 308 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 309 {
FairyMental 48:a8219954b3f2 310 s = atoi(charPos);
FairyMental 48:a8219954b3f2 311 }
FairyMental 57:dfcdda1e42b6 312 //Check if parameters are valid
FairyMental 48:a8219954b3f2 313 if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60))
FairyMental 48:a8219954b3f2 314 {
FairyMental 48:a8219954b3f2 315 localDate->hour = h;
FairyMental 48:a8219954b3f2 316 localDate->min = m;
FairyMental 48:a8219954b3f2 317 localDate->sec = s;
FairyMental 60:db8c5b7fc548 318 char *ptr = localDate->ToString();
aburch1 81:996c0a3319b4 319 snprintf(temp, 256, "Updated Date to: %s \r\n", ptr);
aburch1 81:996c0a3319b4 320 logger.SendMessage(temp);
FairyMental 48:a8219954b3f2 321 }
FairyMental 57:dfcdda1e42b6 322 //If not valid, prompt user
FairyMental 48:a8219954b3f2 323 else
FairyMental 48:a8219954b3f2 324 {
aburch1 81:996c0a3319b4 325 logger.SendMessage("\r\nWrong format! please use HH-MM-SS. \r\n");
FairyMental 48:a8219954b3f2 326 }
FairyMental 48:a8219954b3f2 327 }
FairyMental 57:dfcdda1e42b6 328 //Check if it's a "SETDATE" command
aburch1 81:996c0a3319b4 329 else if (CompareCommands(charPos,"setdate",7))
FairyMental 48:a8219954b3f2 330 {
FairyMental 48:a8219954b3f2 331 int d,m,y;
FairyMental 57:dfcdda1e42b6 332 //Fetch 1st Parameter
aburch1 81:996c0a3319b4 333 charPos = strtok(NULL," ,-");
FairyMental 48:a8219954b3f2 334 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 335 {
FairyMental 48:a8219954b3f2 336 d = atoi(charPos);
FairyMental 48:a8219954b3f2 337 }
FairyMental 57:dfcdda1e42b6 338 //Fetch 2nd Parameter
aburch1 81:996c0a3319b4 339 charPos = strtok(NULL," ,-");
FairyMental 48:a8219954b3f2 340 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 341 {
FairyMental 48:a8219954b3f2 342 m = atoi(charPos);
FairyMental 48:a8219954b3f2 343 }
FairyMental 57:dfcdda1e42b6 344 //Fetch 3rd Parameter
aburch1 81:996c0a3319b4 345 charPos = strtok(NULL," ,-");
FairyMental 48:a8219954b3f2 346 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 347 {
FairyMental 48:a8219954b3f2 348 y = atoi(charPos);
FairyMental 48:a8219954b3f2 349 }
FairyMental 57:dfcdda1e42b6 350 //Check if parameters are valid
FairyMental 48:a8219954b3f2 351 if((d>=0 && d < 31) && (m>=0 && m<13))
FairyMental 48:a8219954b3f2 352 {
FairyMental 48:a8219954b3f2 353 localDate->day = d;
FairyMental 48:a8219954b3f2 354 localDate->month = m;
FairyMental 48:a8219954b3f2 355 localDate->year = y;
FairyMental 60:db8c5b7fc548 356 char *ptr = localDate->ToString();
aburch1 81:996c0a3319b4 357 snprintf(temp, 256, "Updated Date to: %s \r\n", ptr);
aburch1 81:996c0a3319b4 358 logger.SendMessage(temp);
FairyMental 48:a8219954b3f2 359 }
FairyMental 57:dfcdda1e42b6 360 // Prompt user if they are not.
FairyMental 48:a8219954b3f2 361 else
FairyMental 48:a8219954b3f2 362 {
aburch1 81:996c0a3319b4 363 logger.SendMessage("Wrong format! please use DD-MM-YYYY. \r\n");
FairyMental 48:a8219954b3f2 364 }
FairyMental 48:a8219954b3f2 365 }
aburch1 82:668b51a39148 366 // Check if it's a "STATE" command
aburch1 81:996c0a3319b4 367 else if(CompareCommands(charPos,"state",5))
FairyMental 49:83bea7fb2728 368 {
FairyMental 49:83bea7fb2728 369 charPos = strtok(NULL," ,");
FairyMental 57:dfcdda1e42b6 370 //Check if it should be turned ON / OFF
aburch1 81:996c0a3319b4 371 if(CompareCommands(charPos,"on",2))
FairyMental 49:83bea7fb2728 372 {
aburch1 81:996c0a3319b4 373 logger.SendMessage("Sampling turned ON!\r\n");
aburch1 82:668b51a39148 374 timer.attach(&SendSignalDoMeasure, sampleRate);
aburch1 82:668b51a39148 375 SendSignalDoMeasure();
aburch1 82:668b51a39148 376 measuring = true;
FairyMental 49:83bea7fb2728 377 }
aburch1 81:996c0a3319b4 378 else if (CompareCommands(charPos,"off",3))
FairyMental 49:83bea7fb2728 379 {
aburch1 81:996c0a3319b4 380 logger.SendMessage("Sampling turned OFF!\r\n");
aburch1 82:668b51a39148 381 timer.detach();
aburch1 82:668b51a39148 382 measuring = false;
aburch1 82:668b51a39148 383 }
aburch1 82:668b51a39148 384 else
aburch1 82:668b51a39148 385 {
aburch1 82:668b51a39148 386 logger.SendMessage("Expected parameters: \"on\" | \"off\"\r\n");
aburch1 82:668b51a39148 387 }
aburch1 82:668b51a39148 388 }
aburch1 82:668b51a39148 389 // Check if it's a "LOGGING" command
aburch1 82:668b51a39148 390 else if(CompareCommands(charPos,"logging",7))
aburch1 82:668b51a39148 391 {
aburch1 82:668b51a39148 392 charPos = strtok(NULL," ,");
aburch1 82:668b51a39148 393 //Check if it should be turned ON / OFF
aburch1 82:668b51a39148 394 if(CompareCommands(charPos,"on",2))
aburch1 82:668b51a39148 395 {
aburch1 82:668b51a39148 396 logging = true;
aburch1 82:668b51a39148 397 logger.SendMessage("Debug logging turned ON!\r\n");
aburch1 82:668b51a39148 398 logger.SendMessage("Press any key to deactivate\r\n");
aburch1 82:668b51a39148 399 }
aburch1 82:668b51a39148 400 else if (CompareCommands(charPos,"off",3))
aburch1 82:668b51a39148 401 {
aburch1 82:668b51a39148 402 logger.SendMessage("Logging is already turned off.\r\n");
FairyMental 60:db8c5b7fc548 403 }
FairyMental 60:db8c5b7fc548 404 else
FairyMental 60:db8c5b7fc548 405 {
aburch1 81:996c0a3319b4 406 logger.SendMessage("Expected parameters: \"on\" | \"off\"\r\n");
FairyMental 49:83bea7fb2728 407 }
FairyMental 58:7fc6e3e4d746 408 }
FairyMental 68:d3765f93c16a 409 // Check if it's a "SETT" command
aburch1 81:996c0a3319b4 410 else if(CompareCommands(charPos,"sett",4))
FairyMental 58:7fc6e3e4d746 411 {
FairyMental 58:7fc6e3e4d746 412 charPos = strtok(NULL," ,");
aburch1 81:996c0a3319b4 413 double auxRate = atof(charPos);
FairyMental 68:d3765f93c16a 414 // Validate rate
aburch1 81:996c0a3319b4 415 if(auxRate >= 0.1 &&
aburch1 81:996c0a3319b4 416 auxRate <= 60)
FairyMental 58:7fc6e3e4d746 417 {
FairyMental 58:7fc6e3e4d746 418 sampleRate = auxRate;
FairyMental 58:7fc6e3e4d746 419 timer.detach();
FairyMental 58:7fc6e3e4d746 420 timer.attach(&SendSignalDoMeasure, sampleRate);
aburch1 81:996c0a3319b4 421 snprintf(temp, 256, "Successfully updated sample rate to: %2.2f .\r\n",sampleRate);
aburch1 81:996c0a3319b4 422 logger.SendMessage(temp);
FairyMental 58:7fc6e3e4d746 423 }
FairyMental 68:d3765f93c16a 424 // if rate is not valid, prompt:
FairyMental 58:7fc6e3e4d746 425 else
FairyMental 58:7fc6e3e4d746 426 {
aburch1 81:996c0a3319b4 427 logger.SendMessage("Sample rate must be between 0.1 and 60.\r\n");
FairyMental 58:7fc6e3e4d746 428 }
FairyMental 58:7fc6e3e4d746 429 }
FairyMental 68:d3765f93c16a 430 // Check if it's a "HELP" command
aburch1 81:996c0a3319b4 431 else if (CompareCommands(charPos,"help",4) || CompareCommands(charPos,"?",1))
aburch1 75:b44645bbf2d2 432 {
aburch1 82:668b51a39148 433 logger.SendMessage("\nAvailable Commands:\r\n");
aburch1 82:668b51a39148 434 logger.SendMessage(" read <all|n> - Read all or n first measures.\r\n");
aburch1 82:668b51a39148 435 logger.SendMessage(" delete <all|n> - Delete all or n first measures.\r\n");
aburch1 81:996c0a3319b4 436 logger.SendMessage(" setdate <DD> <MM> <YYYY> Set current date.\r\n");
aburch1 81:996c0a3319b4 437 logger.SendMessage(" settime <HH> <MM> <SS> Set current time.\r\n");
aburch1 81:996c0a3319b4 438 logger.SendMessage(" sett <T> Set sample rate (in seconds).\r\n");
aburch1 81:996c0a3319b4 439 logger.SendMessage(" status - Status report of device.\r\n");
aburch1 82:668b51a39148 440 logger.SendMessage(" state - <on|off> - Turn sampling on or off.\r\n");
aburch1 82:668b51a39148 441 logger.SendMessage(" logging <on|off> - Turn logging on or off.\r\n");
aburch1 82:668b51a39148 442 }
aburch1 82:668b51a39148 443 // Check if it's a "HELP" command
aburch1 82:668b51a39148 444 else if (CompareCommands(charPos,"reset",5))
aburch1 82:668b51a39148 445 {
aburch1 82:668b51a39148 446 logger.SendError("Program Terminating...\r\n");
aburch1 75:b44645bbf2d2 447 }
FairyMental 68:d3765f93c16a 448 // If command not recognized
FairyMental 60:db8c5b7fc548 449 else
FairyMental 60:db8c5b7fc548 450 {
aburch1 81:996c0a3319b4 451 logger.SendMessage("Command not recognized. Type \"help\" for more info.\r\n");
FairyMental 60:db8c5b7fc548 452 }
aburch1 82:668b51a39148 453 if(!logging)
aburch1 82:668b51a39148 454 {
aburch1 82:668b51a39148 455 logger.SendMessage("\r\nAwaiting Command:\r\n");
aburch1 82:668b51a39148 456 }
FairyMental 57:dfcdda1e42b6 457 //Clear command!
FairyMental 57:dfcdda1e42b6 458 //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason.
FairyMental 41:d222c043c96d 459 int i = 0;
FairyMental 41:d222c043c96d 460 for(i =0 ; i < crtChar; i++)
FairyMental 41:d222c043c96d 461 command[i] = ' ';
FairyMental 41:d222c043c96d 462 command[0] = 0;
FairyMental 41:d222c043c96d 463 crtChar = 0;
FairyMental 41:d222c043c96d 464 }
FairyMental 41:d222c043c96d 465 }
FairyMental 39:618ad21e2b34 466 }
FairyMental 39:618ad21e2b34 467 }
aburch1 73:cfad270d2f2c 468
aburch1 73:cfad270d2f2c 469 void LoggingThread()
aburch1 73:cfad270d2f2c 470 {
aburch1 75:b44645bbf2d2 471 //Thread::wait(300000);
aburch1 73:cfad270d2f2c 472 // ARRON: TODO
aburch1 73:cfad270d2f2c 473
aburch1 73:cfad270d2f2c 474 // - Printing messages
aburch1 73:cfad270d2f2c 475 // - Out of memory
aburch1 73:cfad270d2f2c 476 // - Current status
aburch1 73:cfad270d2f2c 477 // - Display time every X seconds/minutes
aburch1 73:cfad270d2f2c 478
aburch1 73:cfad270d2f2c 479 // Some queue system holding lines to print
aburch1 73:cfad270d2f2c 480 // If the queue has something to print, print it.
aburch1 75:b44645bbf2d2 481
aburch1 82:668b51a39148 482 while(true)
aburch1 82:668b51a39148 483 {
aburch1 82:668b51a39148 484 Thread::signal_wait(SIGNAL_printMessage);
aburch1 82:668b51a39148 485
aburch1 82:668b51a39148 486 while(true)
aburch1 82:668b51a39148 487 {
aburch1 75:b44645bbf2d2 488 if(logger.GetError())
aburch1 75:b44645bbf2d2 489 {
aburch1 82:668b51a39148 490 osSignalSet(mainThreadId, SIGNAL_terminate);
aburch1 75:b44645bbf2d2 491 }
aburch1 75:b44645bbf2d2 492 else if(!logger.GetMessage())
aburch1 75:b44645bbf2d2 493 {
aburch1 82:668b51a39148 494 break;
aburch1 82:668b51a39148 495 }
aburch1 82:668b51a39148 496 }
aburch1 82:668b51a39148 497 }
aburch1 73:cfad270d2f2c 498 }
FairyMental 34:09ed07f2acba 499
FairyMental 34:09ed07f2acba 500 // Main thread
FairyMental 34:09ed07f2acba 501 int main() {
aburch1 82:668b51a39148 502 mainThreadId = osThreadGetId();
FairyMental 57:dfcdda1e42b6 503 //Initialize all stuff you need here:
FairyMental 34:09ed07f2acba 504 measurer.init();
FairyMental 34:09ed07f2acba 505 measurer.calib();
Netaphous 54:53ee2d07d684 506
FairyMental 46:0de1f3c7d118 507 localDate = new LocalDate();
aburch1 81:996c0a3319b4 508 //Start message
FairyMental 34:09ed07f2acba 509
FairyMental 34:09ed07f2acba 510 //Hook up timer interrupt
FairyMental 58:7fc6e3e4d746 511 timer.attach(&SendSignalDoMeasure, sampleRate);
FairyMental 46:0de1f3c7d118 512 realTimeDate.attach(&RealTimeDate,1.0);
FairyMental 34:09ed07f2acba 513
FairyMental 57:dfcdda1e42b6 514 //Run Threads
aburch1 80:959151952153 515 loggingThread = new Thread();
aburch1 80:959151952153 516 loggingThread->start(LoggingThread);
aburch1 80:959151952153 517
aburch1 80:959151952153 518 logger.SetThread(loggingThread);
aburch1 80:959151952153 519
FairyMental 37:00775e368a71 520 produceThread = new Thread();
FairyMental 37:00775e368a71 521 produceThread->start(ProducerThread);
FairyMental 37:00775e368a71 522 measureThread = new Thread();
FairyMental 37:00775e368a71 523 measureThread->start(MeasureThread);
FairyMental 39:618ad21e2b34 524 consumeThread = new Thread();
FairyMental 39:618ad21e2b34 525 consumeThread->start(ConsumeThread);
aburch1 74:749727490f44 526
aburch1 82:668b51a39148 527 logger.SendMessage("\r\n--- W E L C O M E ---\r\n");
aburch1 82:668b51a39148 528
aburch1 82:668b51a39148 529 SendSignalDoMeasure();
FairyMental 34:09ed07f2acba 530
aburch1 75:b44645bbf2d2 531 while(true)
martinsimpson 32:260a288be58f 532 {
aburch1 82:668b51a39148 533 osSignalWait(SIGNAL_terminate, osWaitForever);
aburch1 82:668b51a39148 534
aburch1 82:668b51a39148 535 produceThread->terminate();
aburch1 82:668b51a39148 536 measureThread->terminate();
aburch1 82:668b51a39148 537 consumeThread->terminate();
aburch1 82:668b51a39148 538 loggingThread->terminate();
aburch1 82:668b51a39148 539
aburch1 82:668b51a39148 540 printf("Press any key to restart...");
aburch1 82:668b51a39148 541 char c = getchar();
aburch1 82:668b51a39148 542
aburch1 82:668b51a39148 543 NVIC_SystemReset();
FairyMental 68:d3765f93c16a 544 }
Netaphous 70:ee19a73ed215 545 }