3rd Repo, trying to figure this out.

Dependencies:   LPS25H hts221

Fork of SOFT253_Template_Weather_OS_54 by Stage-1 Students SoCEM

Committer:
Netaphous
Date:
Thu Apr 06 19:31:43 2017 +0000
Branch:
feature/listSizeLimit
Revision:
52:b95572c3d4c4
Parent:
50:c07e968b9582
Child:
53:abb161ed4c8c
Implemented a limit to the size of the list, needs testing

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