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:
Thu Apr 06 16:44:57 2017 +0000
Revision:
49:83bea7fb2728
Parent:
48:a8219954b3f2
Child:
50:c07e968b9582
Implmented 'logging on / off ' command.

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 34:09ed07f2acba 15
martinsimpson 32:260a288be58f 16 DigitalOut myled(LED1);
martinsimpson 32:260a288be58f 17 I2C i2c2(I2C_SDA, I2C_SCL);
martinsimpson 32:260a288be58f 18 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
FairyMental 34:09ed07f2acba 19 HTS221 measurer(I2C_SDA, I2C_SCL);
FairyMental 34:09ed07f2acba 20 DigitalIn onBoardSwitch(USER_BUTTON);
FairyMental 34:09ed07f2acba 21
FairyMental 34:09ed07f2acba 22 //Threads
FairyMental 37:00775e368a71 23 Thread *produceThread;
FairyMental 37:00775e368a71 24 Thread *measureThread;
FairyMental 39:618ad21e2b34 25 Thread *consumeThread;
FairyMental 38:e626a358e5e3 26
FairyMental 34:09ed07f2acba 27 int count= 0;
FairyMental 38:e626a358e5e3 28
FairyMental 34:09ed07f2acba 29 //Mail queue
FairyMental 34:09ed07f2acba 30 Mail<Measure, 16> mail_box;
FairyMental 34:09ed07f2acba 31
FairyMental 39:618ad21e2b34 32 LinkedList *listBuffer;
FairyMental 46:0de1f3c7d118 33 LocalDate *localDate;
FairyMental 49:83bea7fb2728 34 bool logging = true;
FairyMental 49:83bea7fb2728 35
FairyMental 46:0de1f3c7d118 36 void RealTimeDate()
FairyMental 46:0de1f3c7d118 37 {
FairyMental 46:0de1f3c7d118 38 localDate->TickSecond();
FairyMental 46:0de1f3c7d118 39
FairyMental 46:0de1f3c7d118 40 }
FairyMental 37:00775e368a71 41 void MeasureThread() {
FairyMental 35:484e384f9bf1 42
FairyMental 36:19d3f752f9c3 43 while(true)
FairyMental 36:19d3f752f9c3 44 {
FairyMental 36:19d3f752f9c3 45 Thread::signal_wait(SIGNAL_doMeasure);
FairyMental 36:19d3f752f9c3 46 //Read sample - make a copy
FairyMental 36:19d3f752f9c3 47 float temperature = 0 , humidity = 0,pressure = 0;
FairyMental 34:09ed07f2acba 48
FairyMental 34:09ed07f2acba 49
FairyMental 34:09ed07f2acba 50
FairyMental 36:19d3f752f9c3 51 //Allocate a block from the memory pool
FairyMental 36:19d3f752f9c3 52 Measure *measure = mail_box.alloc();
FairyMental 36:19d3f752f9c3 53 if (measure == NULL)
FairyMental 36:19d3f752f9c3 54 {
FairyMental 36:19d3f752f9c3 55 //Out of memory
FairyMental 36:19d3f752f9c3 56 printf("Out of memory\n\r");
FairyMental 36:19d3f752f9c3 57 return;
FairyMental 36:19d3f752f9c3 58 }
FairyMental 34:09ed07f2acba 59
FairyMental 36:19d3f752f9c3 60 //Fill in the data
FairyMental 36:19d3f752f9c3 61 measurer.ReadTempHumi(&temperature,&humidity);
FairyMental 36:19d3f752f9c3 62 barometer.get();
FairyMental 36:19d3f752f9c3 63 pressure = barometer.pressure();
FairyMental 36:19d3f752f9c3 64
FairyMental 47:468a89d62c23 65
FairyMental 36:19d3f752f9c3 66 measure->temperature = temperature;
FairyMental 36:19d3f752f9c3 67 measure->humidity = humidity;
FairyMental 36:19d3f752f9c3 68 measure->pressure = pressure;
FairyMental 47:468a89d62c23 69 measure->date = new LocalDate(localDate);
FairyMental 36:19d3f752f9c3 70
FairyMental 36:19d3f752f9c3 71 //Write to queue
FairyMental 36:19d3f752f9c3 72 osStatus stat = mail_box.put(measure); //Note we are sending the "pointer"
martinsimpson 32:260a288be58f 73
FairyMental 36:19d3f752f9c3 74 //Check if succesful
FairyMental 36:19d3f752f9c3 75 if (stat == osErrorResource) {
FairyMental 36:19d3f752f9c3 76 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
FairyMental 36:19d3f752f9c3 77 mail_box.free(measure);
FairyMental 36:19d3f752f9c3 78 return;
FairyMental 36:19d3f752f9c3 79 }
FairyMental 34:09ed07f2acba 80 }
FairyMental 34:09ed07f2acba 81 }
FairyMental 34:09ed07f2acba 82
FairyMental 34:09ed07f2acba 83 //Normal priority thread (consumer)
FairyMental 37:00775e368a71 84 void ProducerThread()
FairyMental 34:09ed07f2acba 85 {
FairyMental 34:09ed07f2acba 86 while (true) {
FairyMental 34:09ed07f2acba 87 //Block on the queue
FairyMental 34:09ed07f2acba 88 osEvent evt = mail_box.get();
FairyMental 34:09ed07f2acba 89
FairyMental 34:09ed07f2acba 90 //Check status
FairyMental 34:09ed07f2acba 91 if (evt.status == osEventMail) {
FairyMental 34:09ed07f2acba 92 Measure *measure = (Measure*)evt.value.p; //This is the pointer (address)
FairyMental 34:09ed07f2acba 93 //Make a copy
FairyMental 41:d222c043c96d 94 // printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure);
FairyMental 39:618ad21e2b34 95
FairyMental 47:468a89d62c23 96 Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure);
FairyMental 39:618ad21e2b34 97 listBuffer->addValueEnd(msr);
FairyMental 34:09ed07f2acba 98 //We are done with this, so give back the memory to the pool
FairyMental 34:09ed07f2acba 99 mail_box.free(measure);
FairyMental 34:09ed07f2acba 100
FairyMental 34:09ed07f2acba 101 //Echo to the terminal
FairyMental 34:09ed07f2acba 102
FairyMental 34:09ed07f2acba 103 } else {
FairyMental 34:09ed07f2acba 104 printf("ERROR: %x\n\r", evt.status);
FairyMental 34:09ed07f2acba 105 }
FairyMental 34:09ed07f2acba 106
FairyMental 34:09ed07f2acba 107 } //end while
FairyMental 34:09ed07f2acba 108 }
FairyMental 42:b1f29874ab70 109 int CompareCommands(char command[],char targetcommand[], int size)
FairyMental 42:b1f29874ab70 110 {
FairyMental 43:3983059e0d91 111 int i;
FairyMental 43:3983059e0d91 112 for(i = 0; i < size; i ++)
FairyMental 42:b1f29874ab70 113 {
FairyMental 42:b1f29874ab70 114 if(command[i] != targetcommand[i])
FairyMental 42:b1f29874ab70 115 return -1;
FairyMental 42:b1f29874ab70 116
FairyMental 42:b1f29874ab70 117 }
FairyMental 42:b1f29874ab70 118 return 1;
FairyMental 42:b1f29874ab70 119 }
FairyMental 39:618ad21e2b34 120 void ConsumeThread()
FairyMental 39:618ad21e2b34 121 {
FairyMental 41:d222c043c96d 122 char charCmd;
FairyMental 41:d222c043c96d 123 char command[40];
FairyMental 41:d222c043c96d 124 int crtChar = 0;
FairyMental 41:d222c043c96d 125 printf("\r\nAwaiting command:\r\n");
FairyMental 39:618ad21e2b34 126 while(1)
FairyMental 39:618ad21e2b34 127 {
FairyMental 41:d222c043c96d 128
FairyMental 41:d222c043c96d 129 charCmd = NULL;
FairyMental 41:d222c043c96d 130 charCmd = getchar();
FairyMental 41:d222c043c96d 131 if(charCmd != NULL)
FairyMental 41:d222c043c96d 132 {
FairyMental 44:b523c9a9dd97 133 if (charCmd == 127 && crtChar > 0 ) // If Backspace is pressed
FairyMental 43:3983059e0d91 134 {
FairyMental 43:3983059e0d91 135 printf("%c",charCmd);
FairyMental 43:3983059e0d91 136 command[--crtChar] = '\0';
FairyMental 43:3983059e0d91 137 }
FairyMental 44:b523c9a9dd97 138 else if(charCmd != 13 && charCmd != 127) // If NOT enter AND NOT Backspace is pressed
FairyMental 41:d222c043c96d 139 {
FairyMental 41:d222c043c96d 140 command[crtChar++] = charCmd;
FairyMental 41:d222c043c96d 141 printf("%c",charCmd);
FairyMental 41:d222c043c96d 142 }
FairyMental 44:b523c9a9dd97 143 else if(charCmd == 13) // If Enter is pressed
FairyMental 49:83bea7fb2728 144 {
FairyMental 43:3983059e0d91 145 // this thing that follows splits a string into multiple strings, just like String.Split(char[] delimiters)
FairyMental 43:3983059e0d91 146 // here we will check for commands and parameters :)
FairyMental 43:3983059e0d91 147 char *charPos;
FairyMental 43:3983059e0d91 148 charPos = strtok(command," -,");
FairyMental 43:3983059e0d91 149 if(CompareCommands(charPos, "list",4) == 1)
FairyMental 42:b1f29874ab70 150 {
FairyMental 43:3983059e0d91 151 charPos = strtok(NULL," -,");
FairyMental 43:3983059e0d91 152 if(CompareCommands(charPos, "all",3) == 1)
FairyMental 43:3983059e0d91 153 {
FairyMental 44:b523c9a9dd97 154 printf("\r\n Printing all measures performed so far: \r\n");
FairyMental 43:3983059e0d91 155 listBuffer->ListAll();
FairyMental 44:b523c9a9dd97 156 printf("\r\n D O N E ! \r\n");
FairyMental 43:3983059e0d91 157 }
FairyMental 43:3983059e0d91 158 else if(strtol(charPos,NULL,10) != 0)
FairyMental 43:3983059e0d91 159 {
FairyMental 43:3983059e0d91 160 listBuffer->ListX(atoi(charPos));
FairyMental 44:b523c9a9dd97 161 printf("\r\n D O N E ! \r\n");
FairyMental 43:3983059e0d91 162 }
FairyMental 42:b1f29874ab70 163 }
FairyMental 44:b523c9a9dd97 164 else if (CompareCommands(charPos,"delete",6) == 1)
FairyMental 44:b523c9a9dd97 165 {
FairyMental 44:b523c9a9dd97 166 charPos = strtok(NULL," -,");
FairyMental 44:b523c9a9dd97 167 if(CompareCommands(charPos,"all",3) == 1)
FairyMental 44:b523c9a9dd97 168 {
FairyMental 44:b523c9a9dd97 169 printf("\r\n Deleting all measures performed so far: \r\n");
FairyMental 44:b523c9a9dd97 170 listBuffer->DeleteAll();
FairyMental 44:b523c9a9dd97 171 printf("\r\n D O N E ! \r\n");
FairyMental 44:b523c9a9dd97 172 }
FairyMental 44:b523c9a9dd97 173 else if (strtol(charPos,NULL,10) != 0)
FairyMental 44:b523c9a9dd97 174 {
FairyMental 44:b523c9a9dd97 175 listBuffer->DeleteX(atoi(charPos));
FairyMental 48:a8219954b3f2 176 printf("\r\nD O N E ! \r\n");
FairyMental 44:b523c9a9dd97 177 }
FairyMental 44:b523c9a9dd97 178
FairyMental 44:b523c9a9dd97 179 }
FairyMental 45:9a33f2bc2b4e 180 else if (CompareCommands(charPos,"status",6) == 1)
FairyMental 45:9a33f2bc2b4e 181 {
FairyMental 46:0de1f3c7d118 182 char *ptr = localDate->ToString();
FairyMental 49:83bea7fb2728 183 if(logging == true)
FairyMental 49:83bea7fb2728 184 printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr);
FairyMental 49:83bea7fb2728 185 else
FairyMental 49:83bea7fb2728 186 printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: OFF \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr);
FairyMental 45:9a33f2bc2b4e 187 }
FairyMental 48:a8219954b3f2 188 else if (CompareCommands(charPos,"settime",7) == 1)
FairyMental 48:a8219954b3f2 189 {
FairyMental 48:a8219954b3f2 190 int h,m,s;
FairyMental 48:a8219954b3f2 191 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 192 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 193 {
FairyMental 48:a8219954b3f2 194 h = atoi(charPos);
FairyMental 48:a8219954b3f2 195 }
FairyMental 48:a8219954b3f2 196 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 197 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 198 {
FairyMental 48:a8219954b3f2 199 m = atoi(charPos);
FairyMental 48:a8219954b3f2 200 }
FairyMental 48:a8219954b3f2 201 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 202 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 203 {
FairyMental 48:a8219954b3f2 204 s = atoi(charPos);
FairyMental 48:a8219954b3f2 205 }
FairyMental 48:a8219954b3f2 206 if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60))
FairyMental 48:a8219954b3f2 207 {
FairyMental 48:a8219954b3f2 208 localDate->hour = h;
FairyMental 48:a8219954b3f2 209 localDate->min = m;
FairyMental 48:a8219954b3f2 210 localDate->sec = s;
FairyMental 48:a8219954b3f2 211 printf("\r\n D O N E ! \r\n");
FairyMental 48:a8219954b3f2 212 }
FairyMental 48:a8219954b3f2 213 else
FairyMental 48:a8219954b3f2 214 {
FairyMental 48:a8219954b3f2 215 printf("\r\nWrong format! \r\n");
FairyMental 48:a8219954b3f2 216 }
FairyMental 48:a8219954b3f2 217 }
FairyMental 48:a8219954b3f2 218 else if (CompareCommands(charPos,"setdate",7) == 1)
FairyMental 48:a8219954b3f2 219 {
FairyMental 48:a8219954b3f2 220 int d,m,y;
FairyMental 48:a8219954b3f2 221 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 222 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 223 {
FairyMental 48:a8219954b3f2 224 d = atoi(charPos);
FairyMental 48:a8219954b3f2 225 }
FairyMental 48:a8219954b3f2 226 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 227 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 228 {
FairyMental 48:a8219954b3f2 229 m = atoi(charPos);
FairyMental 48:a8219954b3f2 230 }
FairyMental 48:a8219954b3f2 231 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 232 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 233 {
FairyMental 48:a8219954b3f2 234 y = atoi(charPos);
FairyMental 48:a8219954b3f2 235 }
FairyMental 48:a8219954b3f2 236 if((d>=0 && d < 31) && (m>=0 && m<13))
FairyMental 48:a8219954b3f2 237 {
FairyMental 48:a8219954b3f2 238 localDate->day = d;
FairyMental 48:a8219954b3f2 239 localDate->month = m;
FairyMental 48:a8219954b3f2 240 localDate->year = y;
FairyMental 48:a8219954b3f2 241 printf("\r\n D O N E ! \r\n");
FairyMental 48:a8219954b3f2 242 }
FairyMental 48:a8219954b3f2 243 else
FairyMental 48:a8219954b3f2 244 {
FairyMental 48:a8219954b3f2 245 printf("\r\nWrong format! \r\n");
FairyMental 48:a8219954b3f2 246 }
FairyMental 48:a8219954b3f2 247 }
FairyMental 49:83bea7fb2728 248 else if(CompareCommands(charPos,"logging",7) == 1)
FairyMental 49:83bea7fb2728 249 {
FairyMental 49:83bea7fb2728 250 charPos = strtok(NULL," ,");
FairyMental 49:83bea7fb2728 251 if(CompareCommands(charPos,"on",2) == 1)
FairyMental 49:83bea7fb2728 252 {
FairyMental 49:83bea7fb2728 253 logging = true;
FairyMental 49:83bea7fb2728 254 printf("\r\n Logging turned ON!\r\n");
FairyMental 49:83bea7fb2728 255 }
FairyMental 49:83bea7fb2728 256 else if (CompareCommands(charPos,"off",3) == 1)
FairyMental 49:83bea7fb2728 257 {
FairyMental 49:83bea7fb2728 258 logging = false;
FairyMental 49:83bea7fb2728 259 printf("\r\n Logging turned OFF!\r\n");
FairyMental 49:83bea7fb2728 260 }
FairyMental 49:83bea7fb2728 261 }
FairyMental 48:a8219954b3f2 262
FairyMental 43:3983059e0d91 263 // printf("%s \r\n", charPos);
FairyMental 43:3983059e0d91 264 // charPos = strtok(NULL," -,");
FairyMental 43:3983059e0d91 265
FairyMental 42:b1f29874ab70 266
FairyMental 41:d222c043c96d 267 printf("Awaiting command: \r\n");
FairyMental 41:d222c043c96d 268 int i = 0;
FairyMental 41:d222c043c96d 269 for(i =0 ; i < crtChar; i++)
FairyMental 41:d222c043c96d 270 command[i] = ' ';
FairyMental 41:d222c043c96d 271 command[0] = 0;
FairyMental 41:d222c043c96d 272 crtChar = 0;
FairyMental 41:d222c043c96d 273 }
FairyMental 41:d222c043c96d 274 }
FairyMental 39:618ad21e2b34 275 }
FairyMental 39:618ad21e2b34 276 }
FairyMental 39:618ad21e2b34 277
FairyMental 42:b1f29874ab70 278
FairyMental 36:19d3f752f9c3 279 void SendSignalDoMeasure()
FairyMental 36:19d3f752f9c3 280 {
FairyMental 49:83bea7fb2728 281 if(logging == true)
FairyMental 49:83bea7fb2728 282 measureThread->signal_set(SIGNAL_doMeasure);
FairyMental 36:19d3f752f9c3 283 }
FairyMental 34:09ed07f2acba 284
FairyMental 34:09ed07f2acba 285 // Main thread
FairyMental 34:09ed07f2acba 286 int main() {
FairyMental 34:09ed07f2acba 287
FairyMental 39:618ad21e2b34 288 //Initialize stuff
FairyMental 34:09ed07f2acba 289 measurer.init();
FairyMental 34:09ed07f2acba 290 measurer.calib();
FairyMental 39:618ad21e2b34 291 listBuffer = new LinkedList();
FairyMental 46:0de1f3c7d118 292 localDate = new LocalDate();
FairyMental 34:09ed07f2acba 293 //Start message
FairyMental 37:00775e368a71 294 printf("Welcome\r\n");
FairyMental 34:09ed07f2acba 295
FairyMental 34:09ed07f2acba 296 //Hook up timer interrupt
FairyMental 36:19d3f752f9c3 297 Ticker timer;
FairyMental 44:b523c9a9dd97 298 timer.attach(&SendSignalDoMeasure, 2.0);
FairyMental 46:0de1f3c7d118 299 Ticker realTimeDate;
FairyMental 46:0de1f3c7d118 300 realTimeDate.attach(&RealTimeDate,1.0);
FairyMental 34:09ed07f2acba 301
FairyMental 34:09ed07f2acba 302 //Threads
FairyMental 37:00775e368a71 303 produceThread = new Thread();
FairyMental 37:00775e368a71 304 produceThread->start(ProducerThread);
FairyMental 37:00775e368a71 305 measureThread = new Thread();
FairyMental 37:00775e368a71 306 measureThread->start(MeasureThread);
FairyMental 39:618ad21e2b34 307 consumeThread = new Thread();
FairyMental 39:618ad21e2b34 308 consumeThread->start(ConsumeThread);
FairyMental 34:09ed07f2acba 309
FairyMental 34:09ed07f2acba 310 printf("Main Thread\n");
FairyMental 34:09ed07f2acba 311 while(1)
martinsimpson 32:260a288be58f 312 {
FairyMental 35:484e384f9bf1 313 Thread::wait(3000);
FairyMental 37:00775e368a71 314 // float temp,humi;
FairyMental 37:00775e368a71 315 // measurer.ReadTempHumi(&temp, &humi);
FairyMental 37:00775e368a71 316 // barometer.get();
FairyMental 36:19d3f752f9c3 317 // t2->signal_set(SIGNAL_doMeasure);
FairyMental 37:00775e368a71 318 // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure());
FairyMental 34:09ed07f2acba 319
martinsimpson 32:260a288be58f 320 }
Jonathan Austin 0:2757d7abb7d9 321 }
FairyMental 34:09ed07f2acba 322