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:12:12 2017 +0000
Revision:
48:a8219954b3f2
Parent:
47:468a89d62c23
Child:
49:83bea7fb2728
Implemented setdate/settime commands.

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 46:0de1f3c7d118 34 void RealTimeDate()
FairyMental 46:0de1f3c7d118 35 {
FairyMental 46:0de1f3c7d118 36 localDate->TickSecond();
FairyMental 46:0de1f3c7d118 37
FairyMental 46:0de1f3c7d118 38 }
FairyMental 37:00775e368a71 39 void MeasureThread() {
FairyMental 35:484e384f9bf1 40
FairyMental 36:19d3f752f9c3 41 while(true)
FairyMental 36:19d3f752f9c3 42 {
FairyMental 36:19d3f752f9c3 43 Thread::signal_wait(SIGNAL_doMeasure);
FairyMental 36:19d3f752f9c3 44 //Read sample - make a copy
FairyMental 36:19d3f752f9c3 45 float temperature = 0 , humidity = 0,pressure = 0;
FairyMental 34:09ed07f2acba 46
FairyMental 34:09ed07f2acba 47
FairyMental 34:09ed07f2acba 48
FairyMental 36:19d3f752f9c3 49 //Allocate a block from the memory pool
FairyMental 36:19d3f752f9c3 50 Measure *measure = mail_box.alloc();
FairyMental 36:19d3f752f9c3 51 if (measure == NULL)
FairyMental 36:19d3f752f9c3 52 {
FairyMental 36:19d3f752f9c3 53 //Out of memory
FairyMental 36:19d3f752f9c3 54 printf("Out of memory\n\r");
FairyMental 36:19d3f752f9c3 55 return;
FairyMental 36:19d3f752f9c3 56 }
FairyMental 34:09ed07f2acba 57
FairyMental 36:19d3f752f9c3 58 //Fill in the data
FairyMental 36:19d3f752f9c3 59 measurer.ReadTempHumi(&temperature,&humidity);
FairyMental 36:19d3f752f9c3 60 barometer.get();
FairyMental 36:19d3f752f9c3 61 pressure = barometer.pressure();
FairyMental 36:19d3f752f9c3 62
FairyMental 47:468a89d62c23 63
FairyMental 36:19d3f752f9c3 64 measure->temperature = temperature;
FairyMental 36:19d3f752f9c3 65 measure->humidity = humidity;
FairyMental 36:19d3f752f9c3 66 measure->pressure = pressure;
FairyMental 47:468a89d62c23 67 measure->date = new LocalDate(localDate);
FairyMental 36:19d3f752f9c3 68
FairyMental 36:19d3f752f9c3 69 //Write to queue
FairyMental 36:19d3f752f9c3 70 osStatus stat = mail_box.put(measure); //Note we are sending the "pointer"
martinsimpson 32:260a288be58f 71
FairyMental 36:19d3f752f9c3 72 //Check if succesful
FairyMental 36:19d3f752f9c3 73 if (stat == osErrorResource) {
FairyMental 36:19d3f752f9c3 74 printf("queue->put() Error code: %4Xh, Resource not available\r\n", stat);
FairyMental 36:19d3f752f9c3 75 mail_box.free(measure);
FairyMental 36:19d3f752f9c3 76 return;
FairyMental 36:19d3f752f9c3 77 }
FairyMental 34:09ed07f2acba 78 }
FairyMental 34:09ed07f2acba 79 }
FairyMental 34:09ed07f2acba 80
FairyMental 34:09ed07f2acba 81 //Normal priority thread (consumer)
FairyMental 37:00775e368a71 82 void ProducerThread()
FairyMental 34:09ed07f2acba 83 {
FairyMental 34:09ed07f2acba 84 while (true) {
FairyMental 34:09ed07f2acba 85 //Block on the queue
FairyMental 34:09ed07f2acba 86 osEvent evt = mail_box.get();
FairyMental 34:09ed07f2acba 87
FairyMental 34:09ed07f2acba 88 //Check status
FairyMental 34:09ed07f2acba 89 if (evt.status == osEventMail) {
FairyMental 34:09ed07f2acba 90 Measure *measure = (Measure*)evt.value.p; //This is the pointer (address)
FairyMental 34:09ed07f2acba 91 //Make a copy
FairyMental 41:d222c043c96d 92 // printf("Consumer: %fC | %f% % | %f \r\n", measure->temperature, measure->humidity,measure->pressure);
FairyMental 39:618ad21e2b34 93
FairyMental 47:468a89d62c23 94 Measure msr(measure->date,measure->temperature, measure->humidity,measure->pressure);
FairyMental 39:618ad21e2b34 95 listBuffer->addValueEnd(msr);
FairyMental 34:09ed07f2acba 96 //We are done with this, so give back the memory to the pool
FairyMental 34:09ed07f2acba 97 mail_box.free(measure);
FairyMental 34:09ed07f2acba 98
FairyMental 34:09ed07f2acba 99 //Echo to the terminal
FairyMental 34:09ed07f2acba 100
FairyMental 34:09ed07f2acba 101 } else {
FairyMental 34:09ed07f2acba 102 printf("ERROR: %x\n\r", evt.status);
FairyMental 34:09ed07f2acba 103 }
FairyMental 34:09ed07f2acba 104
FairyMental 34:09ed07f2acba 105 } //end while
FairyMental 34:09ed07f2acba 106 }
FairyMental 42:b1f29874ab70 107 int CompareCommands(char command[],char targetcommand[], int size)
FairyMental 42:b1f29874ab70 108 {
FairyMental 43:3983059e0d91 109 int i;
FairyMental 43:3983059e0d91 110 for(i = 0; i < size; i ++)
FairyMental 42:b1f29874ab70 111 {
FairyMental 42:b1f29874ab70 112 if(command[i] != targetcommand[i])
FairyMental 42:b1f29874ab70 113 return -1;
FairyMental 42:b1f29874ab70 114
FairyMental 42:b1f29874ab70 115 }
FairyMental 42:b1f29874ab70 116 return 1;
FairyMental 42:b1f29874ab70 117 }
FairyMental 39:618ad21e2b34 118 void ConsumeThread()
FairyMental 39:618ad21e2b34 119 {
FairyMental 41:d222c043c96d 120 char charCmd;
FairyMental 41:d222c043c96d 121 char command[40];
FairyMental 41:d222c043c96d 122 int crtChar = 0;
FairyMental 41:d222c043c96d 123 printf("\r\nAwaiting command:\r\n");
FairyMental 39:618ad21e2b34 124 while(1)
FairyMental 39:618ad21e2b34 125 {
FairyMental 41:d222c043c96d 126
FairyMental 41:d222c043c96d 127 charCmd = NULL;
FairyMental 41:d222c043c96d 128 charCmd = getchar();
FairyMental 41:d222c043c96d 129 if(charCmd != NULL)
FairyMental 41:d222c043c96d 130 {
FairyMental 44:b523c9a9dd97 131 if (charCmd == 127 && crtChar > 0 ) // If Backspace is pressed
FairyMental 43:3983059e0d91 132 {
FairyMental 43:3983059e0d91 133 printf("%c",charCmd);
FairyMental 43:3983059e0d91 134 command[--crtChar] = '\0';
FairyMental 43:3983059e0d91 135 }
FairyMental 44:b523c9a9dd97 136 else if(charCmd != 13 && charCmd != 127) // If NOT enter AND NOT Backspace is pressed
FairyMental 41:d222c043c96d 137 {
FairyMental 41:d222c043c96d 138 command[crtChar++] = charCmd;
FairyMental 41:d222c043c96d 139 printf("%c",charCmd);
FairyMental 41:d222c043c96d 140 }
FairyMental 44:b523c9a9dd97 141 else if(charCmd == 13) // If Enter is pressed
FairyMental 41:d222c043c96d 142 {
FairyMental 42:b1f29874ab70 143 printf("\r\n\r\nCommand entered: %s\r\n", command);
FairyMental 41:d222c043c96d 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 46:0de1f3c7d118 183 printf("\r\n STATUS: \r\n # of measures: %i \r\n SAMPLING: ON \r\n Current Date: %s \r\n", listBuffer->GetSize(),ptr);
FairyMental 45:9a33f2bc2b4e 184 }
FairyMental 48:a8219954b3f2 185 else if (CompareCommands(charPos,"settime",7) == 1)
FairyMental 48:a8219954b3f2 186 {
FairyMental 48:a8219954b3f2 187 int h,m,s;
FairyMental 48:a8219954b3f2 188 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 189 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 190 {
FairyMental 48:a8219954b3f2 191 h = atoi(charPos);
FairyMental 48:a8219954b3f2 192 }
FairyMental 48:a8219954b3f2 193 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 194 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 195 {
FairyMental 48:a8219954b3f2 196 m = atoi(charPos);
FairyMental 48:a8219954b3f2 197 }
FairyMental 48:a8219954b3f2 198 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 199 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 200 {
FairyMental 48:a8219954b3f2 201 s = atoi(charPos);
FairyMental 48:a8219954b3f2 202 }
FairyMental 48:a8219954b3f2 203 if((h>=0 && h < 24) && (m>=0 && m<60) && (s>=0 && s<60))
FairyMental 48:a8219954b3f2 204 {
FairyMental 48:a8219954b3f2 205 localDate->hour = h;
FairyMental 48:a8219954b3f2 206 localDate->min = m;
FairyMental 48:a8219954b3f2 207 localDate->sec = s;
FairyMental 48:a8219954b3f2 208 printf("\r\n D O N E ! \r\n");
FairyMental 48:a8219954b3f2 209 }
FairyMental 48:a8219954b3f2 210 else
FairyMental 48:a8219954b3f2 211 {
FairyMental 48:a8219954b3f2 212 printf("\r\nWrong format! \r\n");
FairyMental 48:a8219954b3f2 213 }
FairyMental 48:a8219954b3f2 214 }
FairyMental 48:a8219954b3f2 215 else if (CompareCommands(charPos,"setdate",7) == 1)
FairyMental 48:a8219954b3f2 216 {
FairyMental 48:a8219954b3f2 217 int d,m,y;
FairyMental 48:a8219954b3f2 218 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 219 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 220 {
FairyMental 48:a8219954b3f2 221 d = atoi(charPos);
FairyMental 48:a8219954b3f2 222 }
FairyMental 48:a8219954b3f2 223 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 224 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 225 {
FairyMental 48:a8219954b3f2 226 m = atoi(charPos);
FairyMental 48:a8219954b3f2 227 }
FairyMental 48:a8219954b3f2 228 charPos = strtok(NULL," ,");
FairyMental 48:a8219954b3f2 229 if(strtol(charPos,NULL,10) != 0)
FairyMental 48:a8219954b3f2 230 {
FairyMental 48:a8219954b3f2 231 y = atoi(charPos);
FairyMental 48:a8219954b3f2 232 }
FairyMental 48:a8219954b3f2 233 if((d>=0 && d < 31) && (m>=0 && m<13))
FairyMental 48:a8219954b3f2 234 {
FairyMental 48:a8219954b3f2 235 localDate->day = d;
FairyMental 48:a8219954b3f2 236 localDate->month = m;
FairyMental 48:a8219954b3f2 237 localDate->year = y;
FairyMental 48:a8219954b3f2 238 printf("\r\n D O N E ! \r\n");
FairyMental 48:a8219954b3f2 239 }
FairyMental 48:a8219954b3f2 240 else
FairyMental 48:a8219954b3f2 241 {
FairyMental 48:a8219954b3f2 242 printf("\r\nWrong format! \r\n");
FairyMental 48:a8219954b3f2 243 }
FairyMental 48:a8219954b3f2 244 }
FairyMental 48:a8219954b3f2 245
FairyMental 43:3983059e0d91 246 // printf("%s \r\n", charPos);
FairyMental 43:3983059e0d91 247 // charPos = strtok(NULL," -,");
FairyMental 43:3983059e0d91 248
FairyMental 42:b1f29874ab70 249
FairyMental 41:d222c043c96d 250 printf("Awaiting command: \r\n");
FairyMental 41:d222c043c96d 251 int i = 0;
FairyMental 41:d222c043c96d 252 for(i =0 ; i < crtChar; i++)
FairyMental 41:d222c043c96d 253 command[i] = ' ';
FairyMental 41:d222c043c96d 254 command[0] = 0;
FairyMental 41:d222c043c96d 255 crtChar = 0;
FairyMental 41:d222c043c96d 256 }
FairyMental 41:d222c043c96d 257 }
FairyMental 39:618ad21e2b34 258 }
FairyMental 39:618ad21e2b34 259 }
FairyMental 39:618ad21e2b34 260
FairyMental 42:b1f29874ab70 261
FairyMental 36:19d3f752f9c3 262 void SendSignalDoMeasure()
FairyMental 36:19d3f752f9c3 263 {
FairyMental 37:00775e368a71 264 measureThread->signal_set(SIGNAL_doMeasure);
FairyMental 36:19d3f752f9c3 265 }
FairyMental 34:09ed07f2acba 266
FairyMental 34:09ed07f2acba 267 // Main thread
FairyMental 34:09ed07f2acba 268 int main() {
FairyMental 34:09ed07f2acba 269
FairyMental 39:618ad21e2b34 270 //Initialize stuff
FairyMental 34:09ed07f2acba 271 measurer.init();
FairyMental 34:09ed07f2acba 272 measurer.calib();
FairyMental 39:618ad21e2b34 273 listBuffer = new LinkedList();
FairyMental 46:0de1f3c7d118 274 localDate = new LocalDate();
FairyMental 34:09ed07f2acba 275 //Start message
FairyMental 37:00775e368a71 276 printf("Welcome\r\n");
FairyMental 34:09ed07f2acba 277
FairyMental 34:09ed07f2acba 278 //Hook up timer interrupt
FairyMental 36:19d3f752f9c3 279 Ticker timer;
FairyMental 44:b523c9a9dd97 280 timer.attach(&SendSignalDoMeasure, 2.0);
FairyMental 46:0de1f3c7d118 281 Ticker realTimeDate;
FairyMental 46:0de1f3c7d118 282 realTimeDate.attach(&RealTimeDate,1.0);
FairyMental 34:09ed07f2acba 283
FairyMental 34:09ed07f2acba 284 //Threads
FairyMental 37:00775e368a71 285 produceThread = new Thread();
FairyMental 37:00775e368a71 286 produceThread->start(ProducerThread);
FairyMental 37:00775e368a71 287 measureThread = new Thread();
FairyMental 37:00775e368a71 288 measureThread->start(MeasureThread);
FairyMental 39:618ad21e2b34 289 consumeThread = new Thread();
FairyMental 39:618ad21e2b34 290 consumeThread->start(ConsumeThread);
FairyMental 34:09ed07f2acba 291
FairyMental 34:09ed07f2acba 292 printf("Main Thread\n");
FairyMental 34:09ed07f2acba 293 while(1)
martinsimpson 32:260a288be58f 294 {
FairyMental 35:484e384f9bf1 295 Thread::wait(3000);
FairyMental 37:00775e368a71 296 // float temp,humi;
FairyMental 37:00775e368a71 297 // measurer.ReadTempHumi(&temp, &humi);
FairyMental 37:00775e368a71 298 // barometer.get();
FairyMental 36:19d3f752f9c3 299 // t2->signal_set(SIGNAL_doMeasure);
FairyMental 37:00775e368a71 300 // printf("Main Thread Measures: %fC %f %f \r\n", temp, humi,barometer.pressure());
FairyMental 34:09ed07f2acba 301
martinsimpson 32:260a288be58f 302 }
Jonathan Austin 0:2757d7abb7d9 303 }
FairyMental 34:09ed07f2acba 304