SOFT253 ASSIGNMENT

Dependencies:   LPS25H hts221

Fork of SOFT253_Assignment_V4 by lucas kovaci

Committer:
shikong
Date:
Mon May 15 07:15:11 2017 +0000
Revision:
53:14a17c6b0089
Parent:
52:6474f5b70859
Child:
54:42895d5d43e5
louk;

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
shikong 53:14a17c6b0089 16 #define N 120 //Set constant array size
akovaci 36:7426f37f0c96 17 DigitalOut myled(D7);
akovaci 36:7426f37f0c96 18 Ticker t;
martinsimpson 32:260a288be58f 19 I2C i2c2(I2C_SDA, I2C_SCL);
noutram 35:af125862c33e 20
noutram 35:af125862c33e 21 float tempCelsius = 25.50;
noutram 35:af125862c33e 22 float humi = 55;
akovaci 38:8b6a6927b4f2 23 int humiMax = 100;
akovaci 38:8b6a6927b4f2 24 char cmd[20];
akovaci 38:8b6a6927b4f2 25 char cmd2[20];
akovaci 44:5bb7e0a2387a 26 char cmdSet[20];
akovaci 44:5bb7e0a2387a 27 char cmdSet2[20];
Mpoumtsakalaka 40:4ea91e49d60c 28 char input1[20];
akovaci 42:ac413d9fb994 29 char input2[20];
akovaci 44:5bb7e0a2387a 30 char inputSet1[20];
akovaci 44:5bb7e0a2387a 31 char inputSet2[20];
akovaci 47:5f364311bc45 32 char inputDel1[20];
akovaci 47:5f364311bc45 33 char inputDel2[20];
akovaci 47:5f364311bc45 34 char inputState1[20];
akovaci 47:5f364311bc45 35 char inputState2[20];
akovaci 47:5f364311bc45 36 char inputStateOFF1[20];
akovaci 47:5f364311bc45 37 char inputStateOFF2[20];
akovaci 36:7426f37f0c96 38 float tempArray[N];
akovaci 36:7426f37f0c96 39 float humArray[N];
akovaci 36:7426f37f0c96 40 float pressArray[N];
akovaci 44:5bb7e0a2387a 41 int sampleSpeed = 15;
noutram 35:af125862c33e 42 uint32_t seconds = 0, minutes=0, hours=0;
akovaci 37:b62b3aa19645 43
akovaci 43:d7cb544ad771 44 int counters = 0;
akovaci 44:5bb7e0a2387a 45 int res;
akovaci 44:5bb7e0a2387a 46 int res2;
akovaci 45:4ed55fd0f06f 47 int resSet1;
akovaci 45:4ed55fd0f06f 48 int resSet2;
akovaci 47:5f364311bc45 49 int resState1;
akovaci 47:5f364311bc45 50 int resState2;
martinsimpson 32:260a288be58f 51
noutram 35:af125862c33e 52 LPS25H barometer(i2c2, LPS25H_V_CHIP_ADDR);
noutram 35:af125862c33e 53 HTS221 humidity(I2C_SDA, I2C_SCL);
akovaci 48:f7487eb2c82a 54
noutram 35:af125862c33e 55
akovaci 36:7426f37f0c96 56 void adcISR();
akovaci 37:b62b3aa19645 57 void thread1 (void const *args );
akovaci 37:b62b3aa19645 58 void threadcomun (void const *args);
akovaci 37:b62b3aa19645 59
akovaci 37:b62b3aa19645 60 Thread* t1;
akovaci 37:b62b3aa19645 61 Thread* t2;
akovaci 47:5f364311bc45 62
akovaci 37:b62b3aa19645 63
akovaci 36:7426f37f0c96 64
akovaci 36:7426f37f0c96 65
shikong 50:6f0b82711082 66 typedef struct {
shikong 50:6f0b82711082 67 float tempVal ;
shikong 50:6f0b82711082 68 float humVal;
shikong 50:6f0b82711082 69 float pressVal;
shikong 53:14a17c6b0089 70 }message_t; //Set the variable for array element
martinsimpson 32:260a288be58f 71
shikong 53:14a17c6b0089 72 Mail<message_t, 16> mail_box; //Array
akovaci 36:7426f37f0c96 73
shikong 53:14a17c6b0089 74 /**************************************************
shikong 53:14a17c6b0089 75 **The code above is use for declare the variables**
shikong 53:14a17c6b0089 76 ** and functions **
shikong 53:14a17c6b0089 77 **************************************************/
akovaci 48:f7487eb2c82a 78
akovaci 36:7426f37f0c96 79 void adcISR()
akovaci 36:7426f37f0c96 80 {
shikong 50:6f0b82711082 81 message_t *message = mail_box.alloc();
shikong 50:6f0b82711082 82 message->tempVal = tempCelsius;
shikong 50:6f0b82711082 83 message->humVal = humi;
shikong 50:6f0b82711082 84 message->pressVal = barometer.pressure();
shikong 53:14a17c6b0089 85 //Assign values
shikong 53:14a17c6b0089 86 myled=1; //Set light on
shikong 53:14a17c6b0089 87 //Count how many records are
shikong 53:14a17c6b0089 88 if (counters < N) //If the count didnt reach maximum, keep count
shikong 50:6f0b82711082 89 {
shikong 50:6f0b82711082 90 counters= counters + 1;
shikong 53:14a17c6b0089 91 }else //else if count reaches max or more, set to maximum
shikong 50:6f0b82711082 92 {
shikong 53:14a17c6b0089 93 counters = N;
shikong 50:6f0b82711082 94 }
shikong 53:14a17c6b0089 95 mail_box.put(message);// Store data
akovaci 36:7426f37f0c96 96 }
shikong 50:6f0b82711082 97
akovaci 37:b62b3aa19645 98 void thread1 (void const *args )
shikong 53:14a17c6b0089 99 {
shikong 53:14a17c6b0089 100 pc.baud(115200); //Speed
shikong 50:6f0b82711082 101 while(1)
shikong 53:14a17c6b0089 102 {
shikong 50:6f0b82711082 103 osEvent evt = mail_box.get();
akovaci 36:7426f37f0c96 104 if (evt.status == osEventMail)
akovaci 36:7426f37f0c96 105 {
akovaci 36:7426f37f0c96 106 for (unsigned int n=(N-1); n>0; n--)
shikong 50:6f0b82711082 107 {
shikong 50:6f0b82711082 108 tempArray[n]= tempArray[n-1];
shikong 50:6f0b82711082 109 humArray[n]= humArray[n-1];
shikong 50:6f0b82711082 110 pressArray[n]= pressArray[n-1];
shikong 50:6f0b82711082 111 }
shikong 50:6f0b82711082 112 message_t *message = (message_t*)evt.value.p;
shikong 50:6f0b82711082 113 tempArray[0]= message->tempVal;
shikong 50:6f0b82711082 114 humArray[0] = message->humVal;
shikong 50:6f0b82711082 115 pressArray[0]= message->pressVal;
akovaci 36:7426f37f0c96 116
akovaci 36:7426f37f0c96 117 mail_box.free(message);
akovaci 36:7426f37f0c96 118 }
akovaci 36:7426f37f0c96 119 }
akovaci 36:7426f37f0c96 120 }
akovaci 37:b62b3aa19645 121
akovaci 46:ca411482a095 122 void threadComun (void const *args)
shikong 49:0dfef3825422 123 {
shikong 49:0dfef3825422 124 pc.baud(115200);
shikong 49:0dfef3825422 125 pc.printf("Temperature,Humidity,Pressure\n");
shikong 49:0dfef3825422 126 while(1)
shikong 49:0dfef3825422 127 {
shikong 53:14a17c6b0089 128 scanf("%s%s",cmd,cmd2); //Read user input
shikong 49:0dfef3825422 129 strcpy (input1,cmd); //INPUT 1
shikong 49:0dfef3825422 130 pc.printf("you entered: %s\n\r",input1);
shikong 53:14a17c6b0089 131 strcpy (input2,cmd2); //Input 2
shikong 53:14a17c6b0089 132 //Compare values
shikong 53:14a17c6b0089 133 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 134 res2 = strncmp(input2,input2,20);
shikong 49:0dfef3825422 135 int val = atoi(input2); //Convert String to int
shikong 53:14a17c6b0089 136 if(val <= N && val >=1) //Get the index
shikong 49:0dfef3825422 137 {
shikong 53:14a17c6b0089 138 if(res == 0 && res2 == 0)//If two values matches then execute
shikong 49:0dfef3825422 139 {
shikong 53:14a17c6b0089 140 for (unsigned int n = 0; n < val; n++)
shikong 53:14a17c6b0089 141 { //Get values from index 0 to specific index
shikong 53:14a17c6b0089 142 pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,
shikong 53:14a17c6b0089 143 tempArray[n],pressArray[n],humArray[n]);
shikong 49:0dfef3825422 144 }
shikong 49:0dfef3825422 145 }else{
shikong 53:14a17c6b0089 146 pc.printf("Invalid INPUT. \n");
shikong 53:14a17c6b0089 147 //If the condition failed, Ask for re-input
shikong 49:0dfef3825422 148 }
shikong 49:0dfef3825422 149 }else //1
shikong 49:0dfef3825422 150 {
shikong 49:0dfef3825422 151 res = strncmp(input1,"READ",20);
shikong 49:0dfef3825422 152 res2 = strncmp(input2,"ALL",20);
shikong 53:14a17c6b0089 153 //Compare values
shikong 53:14a17c6b0089 154 if (res == 0 && res2 == 0)
shikong 53:14a17c6b0089 155 {//If condition is true then print from index 0 to Max.
shikong 53:14a17c6b0089 156 for (unsigned int n = 0; n < N; n++)
shikong 53:14a17c6b0089 157 {
shikong 53:14a17c6b0089 158 pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,
shikong 53:14a17c6b0089 159 tempArray[n],pressArray[n],humArray[n]);
shikong 53:14a17c6b0089 160 }
shikong 49:0dfef3825422 161 }
shikong 53:14a17c6b0089 162 else //2
shikong 49:0dfef3825422 163 {
shikong 53:14a17c6b0089 164 //Compare values
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 53:14a17c6b0089 169 //Erase all elements, replace with 0
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 53:14a17c6b0089 174 counters = 0; //Reset count
shikong 49:0dfef3825422 175 }else{
shikong 50:6f0b82711082 176 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 177 }
shikong 49:0dfef3825422 178
shikong 53:14a17c6b0089 179 }//1
shikong 49:0dfef3825422 180 } //1
shikong 53:14a17c6b0089 181
shikong 53:14a17c6b0089 182 //Remove data from index 0 to specific index
shikong 49:0dfef3825422 183 strcpy (inputDel1,cmd);
shikong 53:14a17c6b0089 184 strcpy (inputDel2,cmd2); //Get user input
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 50:6f0b82711082 202 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 203 }
shikong 49:0dfef3825422 204 }
shikong 53:14a17c6b0089 205
shikong 53:14a17c6b0089 206 //Assign the input to other set
shikong 53:14a17c6b0089 207 strcpy (inputSet1,cmd);
shikong 49:0dfef3825422 208 strcpy (inputSet2,cmd2);
shikong 53:14a17c6b0089 209 //Compare command
shikong 49:0dfef3825422 210 resSet1 = strncmp(inputSet1,"SETT",20);
shikong 49:0dfef3825422 211 resSet2 = strncmp(inputSet2,inputSet2,20);
shikong 53:14a17c6b0089 212 int inputSpeed = atoi(inputSet2); // String to int
shikong 49:0dfef3825422 213 if (inputSpeed <=60 && inputSpeed >=0.1)
shikong 49:0dfef3825422 214 {
shikong 49:0dfef3825422 215 if (resSet1==0 && resSet2==0)
shikong 49:0dfef3825422 216 {
shikong 53:14a17c6b0089 217 //?
shikong 49:0dfef3825422 218 sampleSpeed = inputSpeed;
shikong 49:0dfef3825422 219 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 220 pc.printf("T UPDATED TO %d\n",sampleSpeed);
shikong 49:0dfef3825422 221
shikong 49:0dfef3825422 222
shikong 49:0dfef3825422 223 }else{
shikong 50:6f0b82711082 224 pc.printf("Invalid INPUT. \n");
shikong 49:0dfef3825422 225 }
shikong 49:0dfef3825422 226 }
shikong 49:0dfef3825422 227 strcpy (inputState1,cmd); //INPUT 1
shikong 49:0dfef3825422 228 strcpy (inputState2,cmd2);
shikong 49:0dfef3825422 229 resSet1 = strncmp(inputState1,"STATE",20);
shikong 49:0dfef3825422 230 resSet2 = strncmp(inputState2,"ON",20);
shikong 49:0dfef3825422 231 if (resSet1==0 && resSet2==0)
shikong 53:14a17c6b0089 232 {
shikong 49:0dfef3825422 233 t.attach(&adcISR, sampleSpeed);
shikong 49:0dfef3825422 234 pc.printf("Sampling: ON\n");
akovaci 47:5f364311bc45 235
shikong 49:0dfef3825422 236 }
shikong 49:0dfef3825422 237 strcpy (inputStateOFF1,cmd); //INPUT 1
shikong 49:0dfef3825422 238 strcpy (inputStateOFF2,cmd2);
shikong 49:0dfef3825422 239 resState1 = strncmp(inputStateOFF1,"STATE",20);
shikong 49:0dfef3825422 240 resState2 = strncmp(inputStateOFF2,"OFF",20);
shikong 49:0dfef3825422 241
shikong 49:0dfef3825422 242 if (resState1==0 && resState2==0)
shikong 49:0dfef3825422 243 {
shikong 53:14a17c6b0089 244 t.detach();
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 36:7426f37f0c96 253 int main(void)
shikong 50:6f0b82711082 254 {
shikong 53:14a17c6b0089 255 //Pre load
akovaci 36:7426f37f0c96 256 puts("Loading... \n\n");
shikong 53:14a17c6b0089 257 humidity.powerMode(HTS221::POWER_SHUTDOWN); //Low-power mode
shikong 50:6f0b82711082 258 t1 = new Thread(thread1);
shikong 50:6f0b82711082 259 t2 = new Thread(threadComun);
shikong 50:6f0b82711082 260 t.attach(&adcISR,sampleSpeed); // timer of measurements
akovaci 44:5bb7e0a2387a 261
shikong 52:6474f5b70859 262 humidity.powerMode(HTS221::POWER_NORMAL);
shikong 53:14a17c6b0089 263 //Normal mode, which refers to normal power cosumption mode
akovaci 36:7426f37f0c96 264 while(1)
noutram 35:af125862c33e 265 {
akovaci 36:7426f37f0c96 266 humidity.init();
akovaci 36:7426f37f0c96 267 humidity.calib();
akovaci 36:7426f37f0c96 268 humidity.ReadTempHumi(&tempCelsius, &humi);
shikong 53:14a17c6b0089 269 //Read humidity data
akovaci 36:7426f37f0c96 270 barometer.get();
akovaci 36:7426f37f0c96 271 barometer.pressure();
akovaci 36:7426f37f0c96 272 barometer.temperature();
shikong 53:14a17c6b0089 273 //Read temperature and pressure
akovaci 36:7426f37f0c96 274 sleep();
akovaci 36:7426f37f0c96 275 Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds!
akovaci 36:7426f37f0c96 276 myled = 0; // LED is OFF
akovaci 36:7426f37f0c96 277 Thread::wait(100); // 100 ms
FairyMental 34:62aae7d507e2 278 }
noutram 35:af125862c33e 279
shikong 50:6f0b82711082 280 }