SOFT253 ASSIGNMENT

Dependencies:   LPS25H hts221

Fork of SOFT253_Assignment_V4 by lucas kovaci

Committer:
shikong
Date:
Mon May 15 05:04:58 2017 +0000
Revision:
49:0dfef3825422
Parent:
48:f7487eb2c82a
Child:
50:6f0b82711082
New;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
akovaci 43:d7cb544ad771 1
Jonathan Austin 0:2757d7abb7d9 2 #include "mbed.h"
martinsimpson 32:260a288be58f 3 #include "rtos.h"
noutram 35:af125862c33e 4 #include "hts221.h"
martinsimpson 32:260a288be58f 5 #include "LPS25H.h"
akovaci 47:5f364311bc45 6 #include "time.h"
akovaci 47:5f364311bc45 7 #include "stdio.h"
akovaci 47:5f364311bc45 8 #include "stdlib.h"
akovaci 37:b62b3aa19645 9
akovaci 36:7426f37f0c96 10 Serial pc(USBTX, USBRX);
martinsimpson 32:260a288be58f 11
akovaci 42:ac413d9fb994 12
akovaci 36:7426f37f0c96 13 #define N 10
akovaci 36:7426f37f0c96 14 DigitalOut myled(D7);
akovaci 36:7426f37f0c96 15 Ticker t;
martinsimpson 32:260a288be58f 16 I2C i2c2(I2C_SDA, I2C_SCL);
noutram 35:af125862c33e 17
noutram 35:af125862c33e 18 float tempCelsius = 25.50;
noutram 35:af125862c33e 19 float humi = 55;
akovaci 38:8b6a6927b4f2 20 int humiMax = 100;
akovaci 38:8b6a6927b4f2 21 char cmd[20];
akovaci 38:8b6a6927b4f2 22 char cmd2[20];
akovaci 44:5bb7e0a2387a 23 char cmdSet[20];
akovaci 44:5bb7e0a2387a 24 char cmdSet2[20];
Mpoumtsakalaka 40:4ea91e49d60c 25 char input1[20];
akovaci 42:ac413d9fb994 26 char input2[20];
akovaci 44:5bb7e0a2387a 27 char inputSet1[20];
akovaci 44:5bb7e0a2387a 28 char inputSet2[20];
akovaci 47:5f364311bc45 29 char inputDel1[20];
akovaci 47:5f364311bc45 30 char inputDel2[20];
akovaci 47:5f364311bc45 31 char inputState1[20];
akovaci 47:5f364311bc45 32 char inputState2[20];
akovaci 47:5f364311bc45 33 char inputStateOFF1[20];
akovaci 47:5f364311bc45 34 char inputStateOFF2[20];
akovaci 36:7426f37f0c96 35 float tempArray[N];
akovaci 36:7426f37f0c96 36 float humArray[N];
akovaci 36:7426f37f0c96 37 float pressArray[N];
akovaci 44:5bb7e0a2387a 38 int sampleSpeed = 15;
noutram 35:af125862c33e 39 uint32_t seconds = 0, minutes=0, hours=0;
akovaci 37:b62b3aa19645 40
akovaci 43:d7cb544ad771 41 int counters = 0;
akovaci 44:5bb7e0a2387a 42 int res;
akovaci 44:5bb7e0a2387a 43 int res2;
akovaci 45:4ed55fd0f06f 44 int resSet1;
akovaci 45:4ed55fd0f06f 45 int resSet2;
akovaci 47:5f364311bc45 46 int resState1;
akovaci 47:5f364311bc45 47 int resState2;
martinsimpson 32:260a288be58f 48
noutram 35:af125862c33e 49 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
noutram 35:af125862c33e 50 HTS221 humidity(I2C_SDA, I2C_SCL);
akovaci 48:f7487eb2c82a 51
noutram 35:af125862c33e 52
akovaci 36:7426f37f0c96 53 void adcISR();
akovaci 37:b62b3aa19645 54 void thread1 (void const *args );
akovaci 37:b62b3aa19645 55 void threadcomun (void const *args);
akovaci 37:b62b3aa19645 56
akovaci 37:b62b3aa19645 57 Thread* t1;
akovaci 37:b62b3aa19645 58 Thread* t2;
akovaci 47:5f364311bc45 59
akovaci 37:b62b3aa19645 60
akovaci 36:7426f37f0c96 61
akovaci 36:7426f37f0c96 62
akovaci 36:7426f37f0c96 63 typedef struct {
akovaci 36:7426f37f0c96 64
akovaci 36:7426f37f0c96 65 float tempVal ;
akovaci 36:7426f37f0c96 66 float humVal;
akovaci 36:7426f37f0c96 67 float pressVal;
akovaci 43:d7cb544ad771 68 }message_t;
martinsimpson 32:260a288be58f 69
akovaci 44:5bb7e0a2387a 70 Mail<message_t, 16> mail_box;
akovaci 36:7426f37f0c96 71
akovaci 48:f7487eb2c82a 72
akovaci 36:7426f37f0c96 73 void adcISR()
akovaci 36:7426f37f0c96 74 {
shikong 49:0dfef3825422 75 message_t *message = mail_box.alloc();
akovaci 36:7426f37f0c96 76 message->tempVal = tempCelsius;
akovaci 36:7426f37f0c96 77 message->humVal = humi;
akovaci 36:7426f37f0c96 78 message->pressVal = barometer.pressure();
akovaci 36:7426f37f0c96 79 myled=1;
akovaci 43:d7cb544ad771 80 if (counters < 10)
akovaci 43:d7cb544ad771 81 {
akovaci 43:d7cb544ad771 82 counters= counters + 1;
akovaci 43:d7cb544ad771 83 }
akovaci 43:d7cb544ad771 84 else
akovaci 43:d7cb544ad771 85 {
akovaci 44:5bb7e0a2387a 86 counters =10;
akovaci 44:5bb7e0a2387a 87 }
akovaci 43:d7cb544ad771 88
akovaci 43:d7cb544ad771 89
akovaci 43:d7cb544ad771 90
akovaci 43:d7cb544ad771 91
akovaci 36:7426f37f0c96 92 mail_box.put(message);
akovaci 36:7426f37f0c96 93 //Thread::wait(1000);
akovaci 36:7426f37f0c96 94
akovaci 36:7426f37f0c96 95 }
akovaci 37:b62b3aa19645 96 void thread1 (void const *args )
akovaci 36:7426f37f0c96 97 {
akovaci 36:7426f37f0c96 98
akovaci 36:7426f37f0c96 99 pc.baud(115200);
akovaci 37:b62b3aa19645 100 //pc.printf("Temperature,Humidity,Pressure\n\n");
akovaci 36:7426f37f0c96 101 while(1)
akovaci 36:7426f37f0c96 102 {
akovaci 38:8b6a6927b4f2 103
akovaci 36:7426f37f0c96 104 osEvent evt = mail_box.get();
akovaci 36:7426f37f0c96 105 if (evt.status == osEventMail)
akovaci 36:7426f37f0c96 106 {
akovaci 36:7426f37f0c96 107 for (unsigned int n=(N-1); n>0; n--)
akovaci 36:7426f37f0c96 108 {
akovaci 36:7426f37f0c96 109 tempArray[n]= tempArray[n-1];
akovaci 36:7426f37f0c96 110 humArray[n]= humArray[n-1];
akovaci 36:7426f37f0c96 111 pressArray[n]= pressArray[n-1];
akovaci 36:7426f37f0c96 112 }
akovaci 36:7426f37f0c96 113 message_t *message = (message_t*)evt.value.p;
akovaci 36:7426f37f0c96 114 tempArray[0]= message->tempVal;
akovaci 36:7426f37f0c96 115 humArray[0] = message->humVal;
akovaci 36:7426f37f0c96 116 pressArray[0]= message->pressVal;
akovaci 37:b62b3aa19645 117
akovaci 36:7426f37f0c96 118
akovaci 36:7426f37f0c96 119 // pc.printf("%4.2f,%3.1f,%6.1f\n\r", tempArray[N/2], humArray[N/2], pressArray[N/2]);
akovaci 36:7426f37f0c96 120
akovaci 36:7426f37f0c96 121 mail_box.free(message);
akovaci 36:7426f37f0c96 122 }
akovaci 36:7426f37f0c96 123 }
akovaci 36:7426f37f0c96 124 }
akovaci 37:b62b3aa19645 125
akovaci 46:ca411482a095 126 void threadComun (void const *args)
shikong 49:0dfef3825422 127 {
shikong 49:0dfef3825422 128 pc.baud(115200);
shikong 49:0dfef3825422 129 pc.printf("Temperature,Humidity,Pressure\n");
shikong 49:0dfef3825422 130 while(1)
shikong 49:0dfef3825422 131 {
shikong 49:0dfef3825422 132 scanf("%s%s",cmd,cmd2);
shikong 49:0dfef3825422 133 strcpy (input1,cmd); //INPUT 1
shikong 49:0dfef3825422 134 pc.printf("you entered: %s\n\r",input1);
shikong 49:0dfef3825422 135 strcpy (input2,cmd2);
shikong 49:0dfef3825422 136 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 137 res2 = strncmp(input2,input2,20);
shikong 49:0dfef3825422 138 int val = atoi(input2); //Convert String to int
shikong 49:0dfef3825422 139 if(val <=10 && val >=1)
shikong 49:0dfef3825422 140 {
shikong 49:0dfef3825422 141 if(res==0 && res2==0)
shikong 49:0dfef3825422 142 {
shikong 49:0dfef3825422 143 for (unsigned int n=0; n<val; n++)
shikong 49:0dfef3825422 144 {
shikong 49:0dfef3825422 145 pc.printf("the element of %d is %4.2f ,%3.1f, %6.1f \n\r",n,tempArray[n],humArray[n],pressArray[n]);
shikong 49:0dfef3825422 146 //printf("the temperature is: %4.2fC\n\r The humidity is: %3.1f\n\r", tempCelsius, humi);
shikong 49:0dfef3825422 147 }
shikong 49:0dfef3825422 148 }else{
shikong 49:0dfef3825422 149 pc.printf("Invalid INPUT");
shikong 49:0dfef3825422 150 }
shikong 49:0dfef3825422 151 }else //1
shikong 49:0dfef3825422 152 {
shikong 49:0dfef3825422 153 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 154 res2 = strncmp(input2,"ALL",20);
shikong 49:0dfef3825422 155 if (res==0 && res2==0)
shikong 49:0dfef3825422 156 {
shikong 49:0dfef3825422 157 for (unsigned int n=0; n<N; n++)
shikong 49:0dfef3825422 158 {
shikong 49:0dfef3825422 159 pc.printf("the element of %d is %4.2f ,%3.1f, %6.1f \n\r",n,tempArray[n],humArray[n],pressArray[n]);
shikong 49:0dfef3825422 160 }
shikong 49:0dfef3825422 161
shikong 49:0dfef3825422 162 }
shikong 49:0dfef3825422 163 else //2
shikong 49:0dfef3825422 164 {
shikong 49:0dfef3825422 165 res = strncmp(input1,"DELETE",20);
shikong 49:0dfef3825422 166 res2 = strncmp(input2,"ALL",20);
shikong 49:0dfef3825422 167 if(res==0 && res2==0)
shikong 49:0dfef3825422 168 {
shikong 49:0dfef3825422 169 //Delete every element.
shikong 49:0dfef3825422 170 memset(tempArray, 0, sizeof tempArray);
shikong 49:0dfef3825422 171 memset(humArray, 0, sizeof humArray);
shikong 49:0dfef3825422 172 memset(pressArray, 0, sizeof pressArray);
shikong 49:0dfef3825422 173 pc.printf("%d Elements deleted\n",N);
shikong 49:0dfef3825422 174 counters=0;
shikong 49:0dfef3825422 175 }else{
shikong 49:0dfef3825422 176 pc.printf("Invalid INPUT");
shikong 49:0dfef3825422 177 }
shikong 49:0dfef3825422 178
shikong 49:0dfef3825422 179 }//1
akovaci 46:ca411482a095 180
shikong 49:0dfef3825422 181
shikong 49:0dfef3825422 182 } //1
shikong 49:0dfef3825422 183 strcpy (inputDel1,cmd);
shikong 49:0dfef3825422 184 strcpy (inputDel2,cmd2);
shikong 49:0dfef3825422 185 resSet1 = strncmp(input1,"DELETE",20);
shikong 49:0dfef3825422 186 resSet2 = strncmp(input2,inputDel2,20);
shikong 49:0dfef3825422 187 int valDel = atoi(inputDel2);
shikong 49:0dfef3825422 188 int startDel=(counters - valDel);
shikong 49:0dfef3825422 189 if (valDel <=10 && valDel >=1)
shikong 49:0dfef3825422 190 {
shikong 49:0dfef3825422 191 if(resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 192 {
shikong 49:0dfef3825422 193 for (unsigned int n=startDel; n<counters; n++)
shikong 49:0dfef3825422 194 {
shikong 49:0dfef3825422 195 tempArray[n]=0.00;
shikong 49:0dfef3825422 196 humArray[n]=0.00;
shikong 49:0dfef3825422 197 pressArray[n]=0.00;
shikong 49:0dfef3825422 198 }
shikong 49:0dfef3825422 199 pc.printf("Deleted %d records\n",valDel);
shikong 49:0dfef3825422 200 counters = counters - valDel;
shikong 49:0dfef3825422 201 }else{
shikong 49:0dfef3825422 202 pc.printf("Invalid INPUT");
shikong 49:0dfef3825422 203 }
shikong 49:0dfef3825422 204 }
shikong 49:0dfef3825422 205 strcpy (inputSet1,cmd); //INPUT 1
shikong 49:0dfef3825422 206 strcpy (inputSet2,cmd2);
shikong 49:0dfef3825422 207 resSet1 = strncmp(inputSet1,"SETT",20);
shikong 49:0dfef3825422 208 resSet2 = strncmp(inputSet2,inputSet2,20);
shikong 49:0dfef3825422 209 int inputSpeed = atoi(inputSet2);
shikong 49:0dfef3825422 210 if (inputSpeed <=60 && inputSpeed >=0.1)
shikong 49:0dfef3825422 211 {
shikong 49:0dfef3825422 212 if (resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 213 {
shikong 49:0dfef3825422 214 sampleSpeed = inputSpeed;
shikong 49:0dfef3825422 215 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 216 pc.printf("T UPDATED TO %d\n",sampleSpeed);
shikong 49:0dfef3825422 217
shikong 49:0dfef3825422 218
shikong 49:0dfef3825422 219 }else{
shikong 49:0dfef3825422 220 pc.printf("Invalid INPUT");
shikong 49:0dfef3825422 221 }
shikong 49:0dfef3825422 222 }
shikong 49:0dfef3825422 223 strcpy (inputState1,cmd); //INPUT 1
shikong 49:0dfef3825422 224 strcpy (inputState2,cmd2);
shikong 49:0dfef3825422 225 resSet1 = strncmp(inputState1,"STATE",20);
shikong 49:0dfef3825422 226 resSet2 = strncmp(inputState2,"ON",20);
shikong 49:0dfef3825422 227 if (resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 228 {
shikong 49:0dfef3825422 229 //t.detach();
shikong 49:0dfef3825422 230 // LPC_TIM3->TC = 0;
shikong 49:0dfef3825422 231 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 232 pc.printf("Sampling: ON\n");
akovaci 47:5f364311bc45 233
shikong 49:0dfef3825422 234 }
shikong 49:0dfef3825422 235 strcpy (inputStateOFF1,cmd); //INPUT 1
shikong 49:0dfef3825422 236 strcpy (inputStateOFF2,cmd2);
shikong 49:0dfef3825422 237 resState1 = strncmp(inputStateOFF1,"STATE",20);
shikong 49:0dfef3825422 238 resState2 = strncmp(inputStateOFF2,"OFF",20);
shikong 49:0dfef3825422 239
shikong 49:0dfef3825422 240 if (resState1==0 && resState2==0)
shikong 49:0dfef3825422 241 {
shikong 49:0dfef3825422 242 t.detach();
shikong 49:0dfef3825422 243 // LPC_TIM3->TC = 0;
shikong 49:0dfef3825422 244 // t.attach(&adcISR, 2.0);
shikong 49:0dfef3825422 245 pc.printf("Sampling: OFF\n");
shikong 49:0dfef3825422 246 }
shikong 49:0dfef3825422 247
shikong 49:0dfef3825422 248 }//while
shikong 49:0dfef3825422 249 }
akovaci 42:ac413d9fb994 250
akovaci 36:7426f37f0c96 251 char answer;
akovaci 46:ca411482a095 252
akovaci 47:5f364311bc45 253
akovaci 44:5bb7e0a2387a 254
akovaci 44:5bb7e0a2387a 255
akovaci 36:7426f37f0c96 256 int main(void)
akovaci 36:7426f37f0c96 257 {
akovaci 36:7426f37f0c96 258
akovaci 36:7426f37f0c96 259 puts("Loading... \n\n");
akovaci 44:5bb7e0a2387a 260
akovaci 37:b62b3aa19645 261 t1 = new Thread(thread1);
akovaci 46:ca411482a095 262 t2 = new Thread(threadComun);
akovaci 47:5f364311bc45 263
akovaci 44:5bb7e0a2387a 264 t.attach(&adcISR,sampleSpeed); // timer of measurements
akovaci 48:f7487eb2c82a 265
akovaci 48:f7487eb2c82a 266
akovaci 48:f7487eb2c82a 267
akovaci 48:f7487eb2c82a 268
akovaci 48:f7487eb2c82a 269
akovaci 48:f7487eb2c82a 270
akovaci 36:7426f37f0c96 271 while(1)
noutram 35:af125862c33e 272 {
akovaci 36:7426f37f0c96 273 humidity.init();
akovaci 36:7426f37f0c96 274 humidity.calib();
akovaci 36:7426f37f0c96 275 humidity.ReadTempHumi(&tempCelsius, &humi);
akovaci 36:7426f37f0c96 276 barometer.get();
akovaci 36:7426f37f0c96 277 barometer.pressure();
akovaci 36:7426f37f0c96 278 barometer.temperature();
akovaci 36:7426f37f0c96 279 sleep();
akovaci 36:7426f37f0c96 280 Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds!
akovaci 36:7426f37f0c96 281 myled = 0; // LED is OFF
akovaci 36:7426f37f0c96 282 Thread::wait(100); // 100 ms
FairyMental 34:62aae7d507e2 283 }
akovaci 36:7426f37f0c96 284
noutram 35:af125862c33e 285 }
noutram 35:af125862c33e 286
noutram 35:af125862c33e 287
noutram 35:af125862c33e 288
akovaci 36:7426f37f0c96 289
noutram 35:af125862c33e 290
akovaci 36:7426f37f0c96 291
noutram 35:af125862c33e 292
akovaci 36:7426f37f0c96 293
akovaci 36:7426f37f0c96 294
akovaci 36:7426f37f0c96 295
akovaci 36:7426f37f0c96 296