SOFT253 ASSIGNMENT

Dependencies:   LPS25H hts221

Fork of SOFT253_Assignment_V4 by lucas kovaci

Committer:
shikong
Date:
Mon May 15 05:23:13 2017 +0000
Revision:
50:6f0b82711082
Parent:
49:0dfef3825422
Child:
51:dd88bae2a7e5
LOUKA

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
shikong 50:6f0b82711082 13 #define N 120
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
shikong 50:6f0b82711082 63 typedef struct {
shikong 50:6f0b82711082 64 float tempVal ;
shikong 50:6f0b82711082 65 float humVal;
shikong 50:6f0b82711082 66 float pressVal;
shikong 50:6f0b82711082 67 }message_t;
martinsimpson 32:260a288be58f 68
akovaci 44:5bb7e0a2387a 69 Mail<message_t, 16> mail_box;
akovaci 36:7426f37f0c96 70
akovaci 48:f7487eb2c82a 71
akovaci 36:7426f37f0c96 72 void adcISR()
akovaci 36:7426f37f0c96 73 {
shikong 50:6f0b82711082 74 message_t *message = mail_box.alloc();
shikong 50:6f0b82711082 75 message->tempVal = tempCelsius;
shikong 50:6f0b82711082 76 message->humVal = humi;
shikong 50:6f0b82711082 77 message->pressVal = barometer.pressure();
shikong 50:6f0b82711082 78 myled=1;
shikong 50:6f0b82711082 79 if (counters < 10)
shikong 50:6f0b82711082 80 {
shikong 50:6f0b82711082 81 counters= counters + 1;
shikong 50:6f0b82711082 82 }else
shikong 50:6f0b82711082 83 {
shikong 50:6f0b82711082 84 counters =10;
shikong 50:6f0b82711082 85 }
shikong 50:6f0b82711082 86 mail_box.put(message);
akovaci 36:7426f37f0c96 87 }
shikong 50:6f0b82711082 88
akovaci 37:b62b3aa19645 89 void thread1 (void const *args )
akovaci 36:7426f37f0c96 90 {
akovaci 36:7426f37f0c96 91
shikong 50:6f0b82711082 92 pc.baud(115200);
shikong 50:6f0b82711082 93 while(1)
shikong 50:6f0b82711082 94 {
akovaci 38:8b6a6927b4f2 95
shikong 50:6f0b82711082 96 osEvent evt = mail_box.get();
akovaci 36:7426f37f0c96 97 if (evt.status == osEventMail)
akovaci 36:7426f37f0c96 98 {
akovaci 36:7426f37f0c96 99 for (unsigned int n=(N-1); n>0; n--)
shikong 50:6f0b82711082 100 {
shikong 50:6f0b82711082 101 tempArray[n]= tempArray[n-1];
shikong 50:6f0b82711082 102 humArray[n]= humArray[n-1];
shikong 50:6f0b82711082 103 pressArray[n]= pressArray[n-1];
shikong 50:6f0b82711082 104 }
shikong 50:6f0b82711082 105 message_t *message = (message_t*)evt.value.p;
shikong 50:6f0b82711082 106 tempArray[0]= message->tempVal;
shikong 50:6f0b82711082 107 humArray[0] = message->humVal;
shikong 50:6f0b82711082 108 pressArray[0]= message->pressVal;
akovaci 36:7426f37f0c96 109
akovaci 36:7426f37f0c96 110 mail_box.free(message);
akovaci 36:7426f37f0c96 111 }
akovaci 36:7426f37f0c96 112 }
akovaci 36:7426f37f0c96 113 }
akovaci 37:b62b3aa19645 114
akovaci 46:ca411482a095 115 void threadComun (void const *args)
shikong 49:0dfef3825422 116 {
shikong 49:0dfef3825422 117 pc.baud(115200);
shikong 49:0dfef3825422 118 pc.printf("Temperature,Humidity,Pressure\n");
shikong 49:0dfef3825422 119 while(1)
shikong 49:0dfef3825422 120 {
shikong 49:0dfef3825422 121 scanf("%s%s",cmd,cmd2);
shikong 49:0dfef3825422 122 strcpy (input1,cmd); //INPUT 1
shikong 49:0dfef3825422 123 pc.printf("you entered: %s\n\r",input1);
shikong 49:0dfef3825422 124 strcpy (input2,cmd2);
shikong 49:0dfef3825422 125 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 126 res2 = strncmp(input2,input2,20);
shikong 49:0dfef3825422 127 int val = atoi(input2); //Convert String to int
shikong 49:0dfef3825422 128 if(val <=10 && val >=1)
shikong 49:0dfef3825422 129 {
shikong 49:0dfef3825422 130 if(res==0 && res2==0)
shikong 49:0dfef3825422 131 {
shikong 49:0dfef3825422 132 for (unsigned int n=0; n<val; n++)
shikong 49:0dfef3825422 133 {
shikong 50:6f0b82711082 134 pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,tempArray[n],pressArray[n],humArray[n]);
shikong 49:0dfef3825422 135 }
shikong 49:0dfef3825422 136 }else{
shikong 50:6f0b82711082 137 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 138 }
shikong 49:0dfef3825422 139 }else //1
shikong 49:0dfef3825422 140 {
shikong 49:0dfef3825422 141 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 142 res2 = strncmp(input2,"ALL",20);
shikong 49:0dfef3825422 143 if (res==0 && res2==0)
shikong 49:0dfef3825422 144 {
shikong 49:0dfef3825422 145 for (unsigned int n=0; n<N; n++)
shikong 49:0dfef3825422 146 {
shikong 50:6f0b82711082 147 pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,tempArray[n],pressArray[n],humArray[n]);
shikong 49:0dfef3825422 148 }
shikong 49:0dfef3825422 149
shikong 49:0dfef3825422 150 }
shikong 49:0dfef3825422 151 else //2
shikong 49:0dfef3825422 152 {
shikong 49:0dfef3825422 153 res = strncmp(input1,"DELETE",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 //Delete every element.
shikong 49:0dfef3825422 158 memset(tempArray, 0, sizeof tempArray);
shikong 49:0dfef3825422 159 memset(humArray, 0, sizeof humArray);
shikong 49:0dfef3825422 160 memset(pressArray, 0, sizeof pressArray);
shikong 49:0dfef3825422 161 pc.printf("%d Elements deleted\n",N);
shikong 49:0dfef3825422 162 counters=0;
shikong 49:0dfef3825422 163 }else{
shikong 50:6f0b82711082 164 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 165 }
shikong 49:0dfef3825422 166
shikong 49:0dfef3825422 167 }//1
akovaci 46:ca411482a095 168
shikong 49:0dfef3825422 169
shikong 49:0dfef3825422 170 } //1
shikong 49:0dfef3825422 171 strcpy (inputDel1,cmd);
shikong 49:0dfef3825422 172 strcpy (inputDel2,cmd2);
shikong 49:0dfef3825422 173 resSet1 = strncmp(input1,"DELETE",20);
shikong 49:0dfef3825422 174 resSet2 = strncmp(input2,inputDel2,20);
shikong 49:0dfef3825422 175 int valDel = atoi(inputDel2);
shikong 49:0dfef3825422 176 int startDel=(counters - valDel);
shikong 49:0dfef3825422 177 if (valDel <=10 && valDel >=1)
shikong 49:0dfef3825422 178 {
shikong 49:0dfef3825422 179 if(resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 180 {
shikong 49:0dfef3825422 181 for (unsigned int n=startDel; n<counters; n++)
shikong 49:0dfef3825422 182 {
shikong 49:0dfef3825422 183 tempArray[n]=0.00;
shikong 49:0dfef3825422 184 humArray[n]=0.00;
shikong 49:0dfef3825422 185 pressArray[n]=0.00;
shikong 49:0dfef3825422 186 }
shikong 49:0dfef3825422 187 pc.printf("Deleted %d records\n",valDel);
shikong 49:0dfef3825422 188 counters = counters - valDel;
shikong 49:0dfef3825422 189 }else{
shikong 50:6f0b82711082 190 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 191 }
shikong 49:0dfef3825422 192 }
shikong 49:0dfef3825422 193 strcpy (inputSet1,cmd); //INPUT 1
shikong 49:0dfef3825422 194 strcpy (inputSet2,cmd2);
shikong 49:0dfef3825422 195 resSet1 = strncmp(inputSet1,"SETT",20);
shikong 49:0dfef3825422 196 resSet2 = strncmp(inputSet2,inputSet2,20);
shikong 49:0dfef3825422 197 int inputSpeed = atoi(inputSet2);
shikong 49:0dfef3825422 198 if (inputSpeed <=60 && inputSpeed >=0.1)
shikong 49:0dfef3825422 199 {
shikong 49:0dfef3825422 200 if (resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 201 {
shikong 49:0dfef3825422 202 sampleSpeed = inputSpeed;
shikong 49:0dfef3825422 203 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 204 pc.printf("T UPDATED TO %d\n",sampleSpeed);
shikong 49:0dfef3825422 205
shikong 49:0dfef3825422 206
shikong 49:0dfef3825422 207 }else{
shikong 50:6f0b82711082 208 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 209 }
shikong 49:0dfef3825422 210 }
shikong 49:0dfef3825422 211 strcpy (inputState1,cmd); //INPUT 1
shikong 49:0dfef3825422 212 strcpy (inputState2,cmd2);
shikong 49:0dfef3825422 213 resSet1 = strncmp(inputState1,"STATE",20);
shikong 49:0dfef3825422 214 resSet2 = strncmp(inputState2,"ON",20);
shikong 49:0dfef3825422 215 if (resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 216 {
shikong 49:0dfef3825422 217 //t.detach();
shikong 49:0dfef3825422 218 // LPC_TIM3->TC = 0;
shikong 49:0dfef3825422 219 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 220 pc.printf("Sampling: ON\n");
akovaci 47:5f364311bc45 221
shikong 49:0dfef3825422 222 }
shikong 49:0dfef3825422 223 strcpy (inputStateOFF1,cmd); //INPUT 1
shikong 49:0dfef3825422 224 strcpy (inputStateOFF2,cmd2);
shikong 49:0dfef3825422 225 resState1 = strncmp(inputStateOFF1,"STATE",20);
shikong 49:0dfef3825422 226 resState2 = strncmp(inputStateOFF2,"OFF",20);
shikong 49:0dfef3825422 227
shikong 49:0dfef3825422 228 if (resState1==0 && resState2==0)
shikong 49:0dfef3825422 229 {
shikong 49:0dfef3825422 230 t.detach();
shikong 49:0dfef3825422 231 // LPC_TIM3->TC = 0;
shikong 49:0dfef3825422 232 // t.attach(&adcISR, 2.0);
shikong 49:0dfef3825422 233 pc.printf("Sampling: OFF\n");
shikong 49:0dfef3825422 234 }
shikong 49:0dfef3825422 235
shikong 49:0dfef3825422 236 }//while
shikong 49:0dfef3825422 237 }
akovaci 42:ac413d9fb994 238
akovaci 36:7426f37f0c96 239 char answer;
akovaci 46:ca411482a095 240
akovaci 36:7426f37f0c96 241 int main(void)
shikong 50:6f0b82711082 242 {
akovaci 36:7426f37f0c96 243 puts("Loading... \n\n");
shikong 50:6f0b82711082 244 t1 = new Thread(thread1);
shikong 50:6f0b82711082 245 t2 = new Thread(threadComun);
shikong 50:6f0b82711082 246 t.attach(&adcISR,sampleSpeed); // timer of measurements
akovaci 44:5bb7e0a2387a 247
akovaci 36:7426f37f0c96 248 while(1)
noutram 35:af125862c33e 249 {
akovaci 36:7426f37f0c96 250 humidity.init();
akovaci 36:7426f37f0c96 251 humidity.calib();
akovaci 36:7426f37f0c96 252 humidity.ReadTempHumi(&tempCelsius, &humi);
akovaci 36:7426f37f0c96 253 barometer.get();
akovaci 36:7426f37f0c96 254 barometer.pressure();
akovaci 36:7426f37f0c96 255 barometer.temperature();
akovaci 36:7426f37f0c96 256 sleep();
akovaci 36:7426f37f0c96 257 Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds!
akovaci 36:7426f37f0c96 258 myled = 0; // LED is OFF
akovaci 36:7426f37f0c96 259 Thread::wait(100); // 100 ms
FairyMental 34:62aae7d507e2 260 }
noutram 35:af125862c33e 261
shikong 50:6f0b82711082 262 }