
SOFT253 ASSIGNMENT
Fork of SOFT253_Assignment_V4 by
Revision 53:14a17c6b0089, committed 2017-05-15
- Comitter:
- shikong
- Date:
- Mon May 15 07:15:11 2017 +0000
- Parent:
- 52:6474f5b70859
- Child:
- 54:42895d5d43e5
- Commit message:
- louk;
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Mon May 15 05:43:02 2017 +0000 +++ b/main.cpp Mon May 15 07:15:11 2017 +0000 @@ -13,8 +13,7 @@ Serial pc(USBTX, USBRX); - -#define N 120 +#define N 120 //Set constant array size DigitalOut myled(D7); Ticker t; I2C i2c2(I2C_SDA, I2C_SCL); @@ -68,10 +67,14 @@ float tempVal ; float humVal; float pressVal; -}message_t; +}message_t; //Set the variable for array element -Mail<message_t, 16> mail_box; +Mail<message_t, 16> mail_box; //Array +/************************************************** +**The code above is use for declare the variables** +** and functions ** +**************************************************/ void adcISR() { @@ -79,24 +82,24 @@ message->tempVal = tempCelsius; message->humVal = humi; message->pressVal = barometer.pressure(); - myled=1; - if (counters < 10) + //Assign values + myled=1; //Set light on + //Count how many records are + if (counters < N) //If the count didnt reach maximum, keep count { counters= counters + 1; - }else + }else //else if count reaches max or more, set to maximum { - counters =10; + counters = N; } - mail_box.put(message); + mail_box.put(message);// Store data } void thread1 (void const *args ) -{ - - pc.baud(115200); +{ + pc.baud(115200); //Speed while(1) - { - + { osEvent evt = mail_box.get(); if (evt.status == osEventMail) { @@ -122,58 +125,63 @@ pc.printf("Temperature,Humidity,Pressure\n"); while(1) { - scanf("%s%s",cmd,cmd2); + scanf("%s%s",cmd,cmd2); //Read user input strcpy (input1,cmd); //INPUT 1 pc.printf("you entered: %s\n\r",input1); - strcpy (input2,cmd2); - res = strncmp(input1,"READ",20); + strcpy (input2,cmd2); //Input 2 + //Compare values + res = strncmp(input1,"READ",20); res2 = strncmp(input2,input2,20); int val = atoi(input2); //Convert String to int - if(val <=10 && val >=1) + if(val <= N && val >=1) //Get the index { - if(res==0 && res2==0) + if(res == 0 && res2 == 0)//If two values matches then execute { - for (unsigned int n=0; n<val; n++) - { - pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,tempArray[n],pressArray[n],humArray[n]); + for (unsigned int n = 0; n < val; n++) + { //Get values from index 0 to specific index + pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n, + tempArray[n],pressArray[n],humArray[n]); } }else{ - pc.printf("Invalid INPUT. \n"); + pc.printf("Invalid INPUT. \n"); + //If the condition failed, Ask for re-input } }else //1 { res = strncmp(input1,"READ",20); res2 = strncmp(input2,"ALL",20); - if (res==0 && res2==0) - { - for (unsigned int n=0; n<N; n++) - { - pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,tempArray[n],pressArray[n],humArray[n]); - } - + //Compare values + if (res == 0 && res2 == 0) + {//If condition is true then print from index 0 to Max. + for (unsigned int n = 0; n < N; n++) + { + pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n, + tempArray[n],pressArray[n],humArray[n]); + } } - else //2 + else //2 { + //Compare values res = strncmp(input1,"DELETE",20); res2 = strncmp(input2,"ALL",20); if(res==0 && res2==0) { - //Delete every element. + //Erase all elements, replace with 0 memset(tempArray, 0, sizeof tempArray); memset(humArray, 0, sizeof humArray); memset(pressArray, 0, sizeof pressArray); pc.printf("%d Elements deleted\n",N); - counters=0; + counters = 0; //Reset count }else{ pc.printf("Invalid INPUT. \n"); } - }//1 - - + }//1 } //1 + + //Remove data from index 0 to specific index strcpy (inputDel1,cmd); - strcpy (inputDel2,cmd2); + strcpy (inputDel2,cmd2); //Get user input resSet1 = strncmp(input1,"DELETE",20); resSet2 = strncmp(input2,inputDel2,20); int valDel = atoi(inputDel2); @@ -194,15 +202,19 @@ pc.printf("Invalid INPUT. \n"); } } - strcpy (inputSet1,cmd); //INPUT 1 + + //Assign the input to other set + strcpy (inputSet1,cmd); strcpy (inputSet2,cmd2); + //Compare command resSet1 = strncmp(inputSet1,"SETT",20); resSet2 = strncmp(inputSet2,inputSet2,20); - int inputSpeed = atoi(inputSet2); + int inputSpeed = atoi(inputSet2); // String to int if (inputSpeed <=60 && inputSpeed >=0.1) { if (resSet1==0 && resSet2==0) { + //? sampleSpeed = inputSpeed; t.attach(&adcISR, sampleSpeed); pc.printf("T UPDATED TO %d\n",sampleSpeed); @@ -217,9 +229,7 @@ resSet1 = strncmp(inputState1,"STATE",20); resSet2 = strncmp(inputState2,"ON",20); if (resSet1==0 && resSet2==0) - { - //t.detach(); - // LPC_TIM3->TC = 0; + { t.attach(&adcISR, sampleSpeed); pc.printf("Sampling: ON\n"); @@ -231,9 +241,7 @@ if (resState1==0 && resState2==0) { - t.detach(); - // LPC_TIM3->TC = 0; - // t.attach(&adcISR, 2.0); + t.detach(); pc.printf("Sampling: OFF\n"); } @@ -244,21 +252,25 @@ int main(void) { + //Pre load puts("Loading... \n\n"); - humidity.powerMode(HTS221::POWER_SHUTDOWN); + humidity.powerMode(HTS221::POWER_SHUTDOWN); //Low-power mode t1 = new Thread(thread1); t2 = new Thread(threadComun); t.attach(&adcISR,sampleSpeed); // timer of measurements humidity.powerMode(HTS221::POWER_NORMAL); + //Normal mode, which refers to normal power cosumption mode while(1) { humidity.init(); humidity.calib(); humidity.ReadTempHumi(&tempCelsius, &humi); + //Read humidity data barometer.get(); barometer.pressure(); barometer.temperature(); + //Read temperature and pressure sleep(); Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds! myled = 0; // LED is OFF