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:
Wed May 10 10:02:22 2017 +0000
Revision:
80:959151952153
Parent:
79:4e6b53eb678b
Child:
81:996c0a3319b4
Fixed application crashing when sending 2 messages through to the logger at the same time. Other messages need to be changed to use the logger

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