SOFT253 ASSIGNMENT

Dependencies:   LPS25H hts221

Fork of SOFT253_Assignment_V4 by lucas kovaci

Committer:
shikong
Date:
Mon May 15 05:35:35 2017 +0000
Revision:
51:dd88bae2a7e5
Parent:
50:6f0b82711082
Child:
52:6474f5b70859
SOFT253 ASSIGNMENT

Who changed what in which revision?

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