3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
FairyMental
Date:
Fri Apr 07 11:48:05 2017 +0000
Revision:
60:db8c5b7fc548
Parent:
59:a69cd12dafca
Child:
65:3723d2729b68
Added some more error handling for commands. Implmented "help command". Changed "list" to "Read" command. Commands now return errors if wrong parameters are introduced. Format / layout modifciations.

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"
FairyMental 43:3983059e0d91 4 #include <string.h>
FairyMental 34:09ed07f2acba 5 #include <stdio.h>
FairyMental 34:09ed07f2acba 6 #include <ctype.h>
FairyMental 36:19d3f752f9c3 7 #include "hts221.h"
martinsimpson 32:260a288be58f 8 #include "LPS25H.h"
FairyMental 40:ba083993b481 9 #include "LinkedList.h"
FairyMental 41:d222c043c96d 10 #include <iostream>
martinsimpson 32:260a288be58f 11
FairyMental 36:19d3f752f9c3 12 #define SIGNAL_doMeasure 1
FairyMental 36:19d3f752f9c3 13 #define SWITCH1_RELEASE 90
FairyMental 34:09ed07f2acba 14
FairyMental 57:dfcdda1e42b6 15 //
FairyMental 57:dfcdda1e42b6 16 // MBED DECLARATIONS
FairyMental 57:dfcdda1e42b6 17 //
martinsimpson 32:260a288be58f 18 DigitalOut myled(LED1);
Netaphous 50:c07e968b9582 19 DigitalIn onBoardSwitch(USER_BUTTON);
martinsimpson 32:260a288be58f 20 I2C i2c2(I2C_SDA, I2C_SCL);
Netaphous 50:c07e968b9582 21
FairyMental 57:dfcdda1e42b6 22 //
FairyMental 57:dfcdda1e42b6 23 // SENSOR DECLARATIONS
Netaphous 50:c07e968b9582 24 // MAKE SURE ONE OF THESE IS COMMENTED OUT
Netaphous 50:c07e968b9582 25 // Real sensor
Netaphous 56:4dd780d8fb47 26 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
Netaphous 56:4dd780d8fb47 27 HTS221 measurer(I2C_SDA, I2C_SCL);
Netaphous 50:c07e968b9582 28 // Fake sensor
Netaphous 56:4dd780d8fb47 29 //FakeBarometer barometer(1029.0, 1031.0);
Netaphous 56:4dd780d8fb47 30 //FakeMeasurer measurer(20.0, 25.0, 30.0, 50.0);
Netaphous 50:c07e968b9582 31
FairyMental 57:dfcdda1e42b6 32 //
FairyMental 57:dfcdda1e42b6 33 // THREADS DECLARATION
FairyMental 57:dfcdda1e42b6 34 //
FairyMental 37:00775e368a71 35 Thread *produceThread;
FairyMental 37:00775e368a71 36 Thread *measureThread;
FairyMental 39:618ad21e2b34 37 Thread *consumeThread;
FairyMental 58:7fc6e3e4d746 38 Ticker timer;
FairyMental 58:7fc6e3e4d746 39 Ticker realTimeDate;
FairyMental 57:dfcdda1e42b6 40 //
FairyMental 57:dfcdda1e42b6 41 // GLOBAL VARIABLES
FairyMental 57:dfcdda1e42b6 42 //
FairyMental 34:09ed07f2acba 43 Mail<Measure, 16> mail_box;
FairyMental 39:618ad21e2b34 44 LinkedList *listBuffer;
FairyMental 46:0de1f3c7d118 45 LocalDate *localDate;
FairyMental 49:83bea7fb2728 46 bool logging = true;
FairyMental 58:7fc6e3e4d746 47 float sampleRate = 1;
FairyMental 49:83bea7fb2728 48
FairyMental 57:dfcdda1e42b6 49 //
FairyMental 57:dfcdda1e42b6 50 // Called by a TICKER
FairyMental 57:dfcdda1e42b6 51 // Adds 1 second every second to the clock
FairyMental 46:0de1f3c7d118 52 void RealTimeDate()
FairyMental 46:0de1f3c7d118 53 {
FairyMental 46:0de1f3c7d118 54 localDate->TickSecond();
FairyMental 46:0de1f3c7d118 55 }
FairyMental 58:7fc6e3e4d746 56
FairyMental 58:7fc6e3e4d746 57 //
FairyMental 58:7fc6e3e4d746 58 // Ticker that signals the measureThread to do a measure
FairyMental 58:7fc6e3e4d746 59 //
FairyMental 58:7fc6e3e4d746 60 void SendSignalDoMeasure()
FairyMental 58:7fc6e3e4d746 61 {
FairyMental 58:7fc6e3e4d746 62 if(logging == true)
FairyMental 58:7fc6e3e4d746 63 measureThread->signal_set(SIGNAL_doMeasure);
FairyMental 58:7fc6e3e4d746 64 }
FairyMental 58:7fc6e3e4d746 65
FairyMental 57:dfcdda1e42b6 66 //
FairyMental 57:dfcdda1e42b6 67 // SIGNALED BY Ticker at a frequency of <T> Hz
FairyMental 57:dfcdda1e42b6 68 // Reads values from sensor board, sends over through mail queue
FairyMental 37:00775e368a71 69 void MeasureThread() {
FairyMental 35:484e384f9bf1 70
FairyMental 36:19d3f752f9c3 71 while(true)
FairyMental 36:19d3f752f9c3 72 {
FairyMental 57:dfcdda1e42b6 73 //Await signal from ticker
FairyMental 36:19d3f752f9c3 74 Thread::signal_wait(SIGNAL_doMeasure);
FairyMental 57:dfcdda1e42b6 75
FairyMental 36:19d3f752f9c3 76 float temperature = 0 , humidity = 0,pressure = 0;
FairyMental 57:dfcdda1e42b6 77
FairyMental 36:19d3f752f9c3 78 Measure *measure = mail_box.alloc();
FairyMental 36:19d3f752f9c3 79 if (measure == NULL)
FairyMental 36:19d3f752f9c3 80 {
FairyMental 36:19d3f752f9c3 81 printf("Out of memory\n\r");
FairyMental 36:19d3f752f9c3 82 return;
FairyMental 36:19d3f752f9c3 83 }
FairyMental 34:09ed07f2acba 84
FairyMental 57:dfcdda1e42b6 85 //Read and fill in data
FairyMental 36:19d3f752f9c3 86 measurer.ReadTempHumi(&temperature,&humidity);
FairyMental 36:19d3f752f9c3 87 barometer.get();
FairyMental 36:19d3f752f9c3 88 pressure = barometer.pressure();
FairyMental 47:468a89d62c23 89
FairyMental 36:19d3f752f9c3 90 measure->temperature = temperature;
FairyMental 36:19d3f752f9c3 91 measure->humidity = humidity;
FairyMental 36:19d3f752f9c3 92 measure->pressure = pressure;
FairyMental 47:468a89d62c23 93 measure->date = new LocalDate(localDate);
FairyMental 36:19d3f752f9c3 94
FairyMental 57:dfcdda1e42b6 95 osStatus stat = mail_box.put(measure);
martinsimpson 32:260a288be58f 96
FairyMental 36:19d3f752f9c3 97 //Check if succesful
FairyMental 36:19d3f752f9c3 98 if (stat == osErrorResource) {
FairyMental 36:19d3f752f9c3 99 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
FairyMental 36:19d3f752f9c3 100 mail_box.free(measure);
FairyMental 36:19d3f752f9c3 101 return;
FairyMental 36:19d3f752f9c3 102 }
FairyMental 34:09ed07f2acba 103 }
FairyMental 34:09ed07f2acba 104 }
FairyMental 34:09ed07f2acba 105
FairyMental 57:dfcdda1e42b6 106 //
FairyMental 57:dfcdda1e42b6 107 // Receives data through mail queue, then adds it to the global declared list
FairyMental 57:dfcdda1e42b6 108 // A.K.A. Producer Thread
FairyMental 37:00775e368a71 109 void ProducerThread()
FairyMental 34:09ed07f2acba 110 {
FairyMental 57:dfcdda1e42b6 111 while (true)
FairyMental 57:dfcdda1e42b6 112 {
FairyMental 34:09ed07f2acba 113 //Block on the queue
FairyMental 34:09ed07f2acba 114 osEvent evt = mail_box.get();
FairyMental 34:09ed07f2acba 115
FairyMental 34:09ed07f2acba 116 //Check status
FairyMental 34:09ed07f2acba 117 if (evt.status == osEventMail) {
FairyMental 39:618ad21e2b34 118
FairyMental 57:dfcdda1e42b6 119 Measure *measure = (Measure*)evt.value.p;
FairyMental 47:468a89d62c23 120 Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure);
FairyMental 39:618ad21e2b34 121 listBuffer->addValueEnd(msr);
FairyMental 34:09ed07f2acba 122 mail_box.free(measure);
FairyMental 34:09ed07f2acba 123 } else {
FairyMental 34:09ed07f2acba 124 printf("ERROR: %x\n\r", evt.status);
FairyMental 34:09ed07f2acba 125 }
FairyMental 34:09ed07f2acba 126
FairyMental 57:dfcdda1e42b6 127 }
FairyMental 34:09ed07f2acba 128 }
FairyMental 57:dfcdda1e42b6 129 //
FairyMental 57:dfcdda1e42b6 130 // Compares two char arrays and returns result
FairyMental 57:dfcdda1e42b6 131 // Param1: First char Array / pointer
FairyMental 57:dfcdda1e42b6 132 // Param2: Second char Array / pointer
FairyMental 57:dfcdda1e42b6 133 // Param3. Size of the smallest char arrays (between param1 and param2)
FairyMental 57:dfcdda1e42b6 134 // Return: "-1" IF NOT EQUAL
FairyMental 57:dfcdda1e42b6 135 // "1 " IF EQUAL
FairyMental 42:b1f29874ab70 136 int CompareCommands(char command[],char targetcommand[], int size)
FairyMental 42:b1f29874ab70 137 {
FairyMental 43:3983059e0d91 138 int i;
FairyMental 43:3983059e0d91 139 for(i = 0; i < size; i ++)
FairyMental 42:b1f29874ab70 140 {
FairyMental 42:b1f29874ab70 141 if(command[i] != targetcommand[i])
FairyMental 42:b1f29874ab70 142 return -1;
FairyMental 42:b1f29874ab70 143 }
FairyMental 42:b1f29874ab70 144 return 1;
FairyMental 42:b1f29874ab70 145 }
FairyMental 57:dfcdda1e42b6 146 //
FairyMental 57:dfcdda1e42b6 147 // Reads commands through PUTTY and 'consumes the data' accordingly
FairyMental 57:dfcdda1e42b6 148 // A.K.A. Consumer Thread
FairyMental 39:618ad21e2b34 149 void ConsumeThread()
FairyMental 39:618ad21e2b34 150 {
FairyMental 57:dfcdda1e42b6 151 //Last character pressed read (last key input)
FairyMental 41:d222c043c96d 152 char charCmd;
FairyMental 57:dfcdda1e42b6 153 //Char array that stores the command after user presses ENTER
FairyMental 41:d222c043c96d 154 char command[40];
FairyMental 57:dfcdda1e42b6 155 //Current Command Size
FairyMental 41:d222c043c96d 156 int crtChar = 0;
FairyMental 41:d222c043c96d 157 printf("\r\nAwaiting command:\r\n");
FairyMental 39:618ad21e2b34 158 while(1)
FairyMental 39:618ad21e2b34 159 {
FairyMental 41:d222c043c96d 160 charCmd = NULL;
FairyMental 41:d222c043c96d 161 charCmd = getchar();
FairyMental 41:d222c043c96d 162 if(charCmd != NULL)
FairyMental 41:d222c043c96d 163 {
FairyMental 57:dfcdda1e42b6 164 //If BACKSPACE is pressed, Print "DEL" so it deletes last character typed.
FairyMental 57:dfcdda1e42b6 165 if (charCmd == 127 && crtChar > 0 )
FairyMental 43:3983059e0d91 166 {
FairyMental 43:3983059e0d91 167 printf("%c",charCmd);
FairyMental 43:3983059e0d91 168 command[--crtChar] = '\0';
FairyMental 43:3983059e0d91 169 }
FairyMental 57:dfcdda1e42b6 170 //If NOT enter AND NOT Backspace is pressed, SAVE the char
FairyMental 57:dfcdda1e42b6 171 else if(charCmd != 13 && charCmd != 127)
FairyMental 41:d222c043c96d 172 {
FairyMental 41:d222c043c96d 173 command[crtChar++] = charCmd;
FairyMental 41:d222c043c96d 174 printf("%c",charCmd);
FairyMental 41:d222c043c96d 175 }
FairyMental 57:dfcdda1e42b6 176 //If ENTER is pressed, PROCESS it
FairyMental 44:b523c9a9dd97 177 else if(charCmd == 13) // If Enter is pressed
FairyMental 49:83bea7fb2728 178 {
FairyMental 57:dfcdda1e42b6 179 //Get first word of command:
FairyMental 43:3983059e0d91 180 char *charPos;
FairyMental 43:3983059e0d91 181 charPos = strtok(command," -,");
FairyMental 57:dfcdda1e42b6 182
FairyMental 57:dfcdda1e42b6 183 //Check if it's a "LIST" command
FairyMental 60:db8c5b7fc548 184 if(CompareCommands(charPos, "read",4) == 1)
FairyMental 42:b1f29874ab70 185 {
FairyMental 43:3983059e0d91 186 charPos = strtok(NULL," -,");
FairyMental 57:dfcdda1e42b6 187 //Check if it's a "LIST ALL" command
FairyMental 43:3983059e0d91 188 if(CompareCommands(charPos, "all",3) == 1)
FairyMental 43:3983059e0d91 189 {
FairyMental 44:b523c9a9dd97 190 printf("\r\n Printing all measures performed so far: \r\n");
FairyMental 43:3983059e0d91 191 listBuffer->ListAll();
FairyMental 60:db8c5b7fc548 192 printf("\r\nD O N E ! \r\n");
FairyMental 43:3983059e0d91 193 }
FairyMental 57:dfcdda1e42b6 194 //Check if it's a "LIST X" command
FairyMental 43:3983059e0d91 195 else if(strtol(charPos,NULL,10) != 0)
FairyMental 43:3983059e0d91 196 {
FairyMental 60:db8c5b7fc548 197 printf("\r\n Printing %i measures: \r\n",atoi(charPos));
FairyMental 43:3983059e0d91 198 listBuffer->ListX(atoi(charPos));
FairyMental 60:db8c5b7fc548 199 printf("\r\nD O N E ! \r\n");
FairyMental 60:db8c5b7fc548 200 }
FairyMental 60:db8c5b7fc548 201 else
FairyMental 60:db8c5b7fc548 202 {
FairyMental 60:db8c5b7fc548 203 printf("Expected parameters: \"all\" | \"n\", where n is a number.");
FairyMental 43:3983059e0d91 204 }
FairyMental 42:b1f29874ab70 205 }
FairyMental 57:dfcdda1e42b6 206 //Check if it's a "DELETE" command
FairyMental 44:b523c9a9dd97 207 else if (CompareCommands(charPos,"delete",6) == 1)
FairyMental 44:b523c9a9dd97 208 {
FairyMental 44:b523c9a9dd97 209 charPos = strtok(NULL," -,");
FairyMental 57:dfcdda1e42b6 210 //Check if it's a "DELETE ALL" command
FairyMental 44:b523c9a9dd97 211 if(CompareCommands(charPos,"all",3) == 1)
FairyMental 44:b523c9a9dd97 212 {
FairyMental 44:b523c9a9dd97 213 printf("\r\n Deleting all measures performed so far: \r\n");
FairyMental 44:b523c9a9dd97 214 listBuffer->DeleteAll();
FairyMental 60:db8c5b7fc548 215 printf("\r\nElements deleted!\r\n");
FairyMental 44:b523c9a9dd97 216 }
FairyMental 57:dfcdda1e42b6 217 //Check if it's a "DELETE X" command
FairyMental 44:b523c9a9dd97 218 else if (strtol(charPos,NULL,10) != 0)
FairyMental 44:b523c9a9dd97 219 {
FairyMental 44:b523c9a9dd97 220 listBuffer->DeleteX(atoi(charPos));
FairyMental 60:db8c5b7fc548 221 printf("\r\nElements deleted!\r\n");
FairyMental 60:db8c5b7fc548 222 }
FairyMental 60:db8c5b7fc548 223 else
FairyMental 60:db8c5b7fc548 224 {
FairyMental 60:db8c5b7fc548 225 printf("Expected parameters: \"all\" | \"n\", where n is a number.");
FairyMental 44:b523c9a9dd97 226 }
FairyMental 44:b523c9a9dd97 227
FairyMental 44:b523c9a9dd97 228 }
FairyMental 60:db8c5b7fc548 229 //Check if it's a "STATUS" command
FairyMental 60:db8c5b7fc548 230 else if (CompareCommands(charPos,"status",6) == 1)
FairyMental 45:9a33f2bc2b4e 231 {
FairyMental 46:0de1f3c7d118 232 char *ptr = localDate->ToString();
FairyMental 49:83bea7fb2728 233 if(logging == true)
FairyMental 58:7fc6e3e4d746 234 printf("\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", listBuffer->GetSize(),ptr,sampleRate);
FairyMental 49:83bea7fb2728 235 else
FairyMental 58:7fc6e3e4d746 236 printf("\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", listBuffer->GetSize(),ptr,sampleRate);
FairyMental 45:9a33f2bc2b4e 237 }
FairyMental 57:dfcdda1e42b6 238 //Check if it's a "SETTIME" command
FairyMental 48:a8219954b3f2 239 else if (CompareCommands(charPos,"settime",7) == 1)
FairyMental 48:a8219954b3f2 240 {
FairyMental 48:a8219954b3f2 241 int h,m,s;
FairyMental 57:dfcdda1e42b6 242 //Fetch 1st Param
FairyMental 48:a8219954b3f2 243 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 244 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 245 {
FairyMental 48:a8219954b3f2 246 h = atoi(charPos);
FairyMental 48:a8219954b3f2 247 }
FairyMental 57:dfcdda1e42b6 248 //Fech 2nd Param
FairyMental 48:a8219954b3f2 249 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 250 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 251 {
FairyMental 48:a8219954b3f2 252 m = atoi(charPos);
FairyMental 48:a8219954b3f2 253 }
FairyMental 57:dfcdda1e42b6 254 //Fetch 3rd Param
FairyMental 48:a8219954b3f2 255 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 256 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 257 {
FairyMental 48:a8219954b3f2 258 s = atoi(charPos);
FairyMental 48:a8219954b3f2 259 }
FairyMental 57:dfcdda1e42b6 260 //Check if parameters are valid
FairyMental 48:a8219954b3f2 261 if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60))
FairyMental 48:a8219954b3f2 262 {
FairyMental 48:a8219954b3f2 263 localDate->hour = h;
FairyMental 48:a8219954b3f2 264 localDate->min = m;
FairyMental 48:a8219954b3f2 265 localDate->sec = s;
FairyMental 60:db8c5b7fc548 266 char *ptr = localDate->ToString();
FairyMental 60:db8c5b7fc548 267 printf("\r\nUpdated Date to: %s \r\n", ptr);
FairyMental 48:a8219954b3f2 268 }
FairyMental 57:dfcdda1e42b6 269 //If not valid, prompt user
FairyMental 48:a8219954b3f2 270 else
FairyMental 48:a8219954b3f2 271 {
FairyMental 60:db8c5b7fc548 272 printf("\r\nWrong format! please use HH-MM-SS separated by spaces. \r\n");
FairyMental 48:a8219954b3f2 273 }
FairyMental 48:a8219954b3f2 274 }
FairyMental 57:dfcdda1e42b6 275 //Check if it's a "SETDATE" command
FairyMental 48:a8219954b3f2 276 else if (CompareCommands(charPos,"setdate",7) == 1)
FairyMental 48:a8219954b3f2 277 {
FairyMental 48:a8219954b3f2 278 int d,m,y;
FairyMental 57:dfcdda1e42b6 279 //Fetch 1st Parameter
FairyMental 48:a8219954b3f2 280 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 281 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 282 {
FairyMental 48:a8219954b3f2 283 d = atoi(charPos);
FairyMental 48:a8219954b3f2 284 }
FairyMental 57:dfcdda1e42b6 285 //Fetch 2nd Parameter
FairyMental 48:a8219954b3f2 286 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 287 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 288 {
FairyMental 48:a8219954b3f2 289 m = atoi(charPos);
FairyMental 48:a8219954b3f2 290 }
FairyMental 57:dfcdda1e42b6 291 //Fetch 3rd Parameter
FairyMental 48:a8219954b3f2 292 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 293 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 294 {
FairyMental 48:a8219954b3f2 295 y = atoi(charPos);
FairyMental 48:a8219954b3f2 296 }
FairyMental 57:dfcdda1e42b6 297 //Check if parameters are valid
FairyMental 48:a8219954b3f2 298 if((d>=0 && d < 31) && (m>=0 && m<13))
FairyMental 48:a8219954b3f2 299 {
FairyMental 48:a8219954b3f2 300 localDate->day = d;
FairyMental 48:a8219954b3f2 301 localDate->month = m;
FairyMental 48:a8219954b3f2 302 localDate->year = y;
FairyMental 60:db8c5b7fc548 303 char *ptr = localDate->ToString();
FairyMental 60:db8c5b7fc548 304 printf("\r\nUpdated Date to: %s \r\n", ptr);
FairyMental 48:a8219954b3f2 305 }
FairyMental 57:dfcdda1e42b6 306 // Prompt user if they are not.
FairyMental 48:a8219954b3f2 307 else
FairyMental 48:a8219954b3f2 308 {
FairyMental 60:db8c5b7fc548 309 printf("\r\nWrong format! please use DD-MM-YYYY separated by spaces. \r\n");
FairyMental 48:a8219954b3f2 310 }
FairyMental 48:a8219954b3f2 311 }
FairyMental 57:dfcdda1e42b6 312 // Check if it's a "LOGGING" command
FairyMental 60:db8c5b7fc548 313 else if(CompareCommands(charPos,"state",5) == 1)
FairyMental 49:83bea7fb2728 314 {
FairyMental 49:83bea7fb2728 315 charPos = strtok(NULL," ,");
FairyMental 57:dfcdda1e42b6 316 //Check if it should be turned ON / OFF
FairyMental 49:83bea7fb2728 317 if(CompareCommands(charPos,"on",2) == 1)
FairyMental 49:83bea7fb2728 318 {
FairyMental 49:83bea7fb2728 319 logging = true;
FairyMental 60:db8c5b7fc548 320 printf("\r\nSampling turned ON!\r\n");
FairyMental 49:83bea7fb2728 321 }
FairyMental 49:83bea7fb2728 322 else if (CompareCommands(charPos,"off",3) == 1)
FairyMental 49:83bea7fb2728 323 {
FairyMental 49:83bea7fb2728 324 logging = false;
FairyMental 60:db8c5b7fc548 325 printf("\r\Sampling turned OFF!\r\n");
FairyMental 60:db8c5b7fc548 326 }
FairyMental 60:db8c5b7fc548 327 else
FairyMental 60:db8c5b7fc548 328 {
FairyMental 60:db8c5b7fc548 329 printf("Expected parameters: \"on\" | \"off\"");
FairyMental 49:83bea7fb2728 330 }
FairyMental 58:7fc6e3e4d746 331 }
FairyMental 58:7fc6e3e4d746 332 else if(CompareCommands(charPos,"sett",4) == 1)
FairyMental 58:7fc6e3e4d746 333 {
FairyMental 58:7fc6e3e4d746 334 charPos = strtok(NULL," ,");
FairyMental 58:7fc6e3e4d746 335 float auxRate = atof(charPos);
FairyMental 58:7fc6e3e4d746 336 if(auxRate != 0 && auxRate >0.09 && auxRate <= 60 )
FairyMental 58:7fc6e3e4d746 337 {
FairyMental 58:7fc6e3e4d746 338 sampleRate = auxRate;
FairyMental 58:7fc6e3e4d746 339 timer.detach();
FairyMental 58:7fc6e3e4d746 340 timer.attach(&SendSignalDoMeasure, sampleRate);
FairyMental 58:7fc6e3e4d746 341 printf("\r\nSuccessfully updated sample rate to: %2.2f .\r\n",sampleRate);
FairyMental 58:7fc6e3e4d746 342 }
FairyMental 58:7fc6e3e4d746 343 else
FairyMental 58:7fc6e3e4d746 344 {
FairyMental 60:db8c5b7fc548 345 printf("\r\n Sample rate must be between greater than 0.1 or less equal than 60. \r\n");
FairyMental 58:7fc6e3e4d746 346 }
FairyMental 58:7fc6e3e4d746 347 }
FairyMental 60:db8c5b7fc548 348 else if (CompareCommands(charPos,"help",4) == 1 || CompareCommands(charPos,"?",1) == 1)
FairyMental 59:a69cd12dafca 349 {
FairyMental 59:a69cd12dafca 350 printf("\r\nAvailable Commands:\r\n");
FairyMental 59:a69cd12dafca 351 printf(" read <ALL|N> - Read ALL or N first measures.\r\n");
FairyMental 59:a69cd12dafca 352 printf(" delete <ALL|N> - Delete ALL or N first measures.\r\n");
FairyMental 59:a69cd12dafca 353 printf(" setdate <DD> <MM> <YYYY> Set current date.\r\n");
FairyMental 59:a69cd12dafca 354 printf(" settime <HH> <MM> <SS> Set current time.\r\n");
FairyMental 59:a69cd12dafca 355 printf(" sett <T> Set sample rate (in seconds).\r\n");
FairyMental 60:db8c5b7fc548 356 printf(" status - Status report of device.\r\n");
FairyMental 60:db8c5b7fc548 357 printf(" state - <ON|OFF> - Turn sampling on or OFF.\r\n");
FairyMental 59:a69cd12dafca 358 printf(" logging <ON|OFF> - Turn logging on or OFF.\r\n");
FairyMental 59:a69cd12dafca 359 }
FairyMental 60:db8c5b7fc548 360 else
FairyMental 60:db8c5b7fc548 361 {
FairyMental 60:db8c5b7fc548 362 printf("\r\n Command not recognized. Type \"help\" for more info.\r\n");
FairyMental 60:db8c5b7fc548 363 }
FairyMental 58:7fc6e3e4d746 364 printf("\r\nAwaiting command: \r\n");
FairyMental 57:dfcdda1e42b6 365 //Clear command!
FairyMental 57:dfcdda1e42b6 366 //* NOTE * Setting first char in array to '\0' WILL NOT RESET IT...for some reason.
FairyMental 41:d222c043c96d 367 int i = 0;
FairyMental 41:d222c043c96d 368 for(i =0 ; i < crtChar; i++)
FairyMental 41:d222c043c96d 369 command[i] = ' ';
FairyMental 41:d222c043c96d 370 command[0] = 0;
FairyMental 41:d222c043c96d 371 crtChar = 0;
FairyMental 41:d222c043c96d 372 }
FairyMental 41:d222c043c96d 373 }
FairyMental 39:618ad21e2b34 374 }
FairyMental 39:618ad21e2b34 375 }
FairyMental 34:09ed07f2acba 376
FairyMental 34:09ed07f2acba 377 // Main thread
FairyMental 34:09ed07f2acba 378 int main() {
FairyMental 34:09ed07f2acba 379
FairyMental 57:dfcdda1e42b6 380 //Initialize all stuff you need here:
FairyMental 34:09ed07f2acba 381 measurer.init();
FairyMental 34:09ed07f2acba 382 measurer.calib();
Netaphous 54:53ee2d07d684 383
Netaphous 54:53ee2d07d684 384 // Creates a list with a max size of 120
Netaphous 53:abb161ed4c8c 385 listBuffer = new LinkedList(120);
FairyMental 46:0de1f3c7d118 386 localDate = new LocalDate();
FairyMental 34:09ed07f2acba 387 //Start message
FairyMental 37:00775e368a71 388 printf("Welcome\r\n");
FairyMental 34:09ed07f2acba 389
FairyMental 34:09ed07f2acba 390 //Hook up timer interrupt
FairyMental 58:7fc6e3e4d746 391
FairyMental 58:7fc6e3e4d746 392 timer.attach(&SendSignalDoMeasure, sampleRate);
FairyMental 46:0de1f3c7d118 393 realTimeDate.attach(&RealTimeDate,1.0);
FairyMental 34:09ed07f2acba 394
FairyMental 57:dfcdda1e42b6 395 //Run Threads
FairyMental 37:00775e368a71 396 produceThread = new Thread();
FairyMental 37:00775e368a71 397 produceThread->start(ProducerThread);
FairyMental 37:00775e368a71 398 measureThread = new Thread();
FairyMental 37:00775e368a71 399 measureThread->start(MeasureThread);
FairyMental 39:618ad21e2b34 400 consumeThread = new Thread();
FairyMental 39:618ad21e2b34 401 consumeThread->start(ConsumeThread);
FairyMental 34:09ed07f2acba 402
FairyMental 34:09ed07f2acba 403 printf("Main Thread\n");
FairyMental 34:09ed07f2acba 404 while(1)
martinsimpson 32:260a288be58f 405 {
FairyMental 35:484e384f9bf1 406 Thread::wait(3000);
FairyMental 37:00775e368a71 407 // float temp,humi;
FairyMental 37:00775e368a71 408 // measurer.ReadTempHumi(&temp, &humi);
FairyMental 37:00775e368a71 409 // barometer.get();
FairyMental 36:19d3f752f9c3 410 // t2->signal_set(SIGNAL_doMeasure);
FairyMental 37:00775e368a71 411 // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure());
FairyMental 34:09ed07f2acba 412
martinsimpson 32:260a288be58f 413 }
Jonathan Austin 0:2757d7abb7d9 414 }
FairyMental 34:09ed07f2acba 415