Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SOFT253_Assignment_V4 by
Diff: main.cpp
- Revision:
- 54:42895d5d43e5
- Parent:
- 53:14a17c6b0089
diff -r 14a17c6b0089 -r 42895d5d43e5 main.cpp
--- a/main.cpp Mon May 15 07:15:11 2017 +0000
+++ b/main.cpp Mon May 15 09:10:23 2017 +0000
@@ -83,21 +83,27 @@
message->humVal = humi;
message->pressVal = barometer.pressure();
//Assign values
- myled=1; //Set light on
+ //Set light on when timer is running every 15s(Default)
+ myled=1;
//Count how many records are
- if (counters < N) //If the count didnt reach maximum, keep count
+ if (counters < N) //If the count didnt reach maximum 120, keep count
{
counters= counters + 1;
- }else //else if count reaches max or more, set to maximum
+ }
+ else //else if count reaches more tha 120 , set to 120
{
counters = N;
- }
- mail_box.put(message);// Store data
+ }
+ // Store data of message to the mail Box
+ mail_box.put(message);
}
+/**
+thread for implementing the circular buffering (FIFO)
+**/
void thread1 (void const *args )
{
- pc.baud(115200); //Speed
+ pc.baud(115200); //Serial com Speed
while(1)
{
osEvent evt = mail_box.get();
@@ -119,52 +125,73 @@
}
}
+/**
+thread for the serial comunication with Putty
+
+**/
void threadComun (void const *args)
{
pc.baud(115200);
- pc.printf("Temperature,Humidity,Pressure\n");
+ pc.printf("Temperature,Humidity,Pressure\n");
+
while(1)
{
+ pc.printf("Temperature,Humidity,Pressure Samples\n");
scanf("%s%s",cmd,cmd2); //Read user input
- strcpy (input1,cmd); //INPUT 1
- pc.printf("you entered: %s\n\r",input1);
- strcpy (input2,cmd2); //Input 2
- //Compare values
- res = strncmp(input1,"READ",20);
- res2 = strncmp(input2,input2,20);
+ strcpy (input1,cmd); //Input 1(Before the space)
+
+ strcpy (input2,cmd2); //Input 2 (After the space)
+
+ res = strncmp(input1,"READ",20); //Compare the first input with the word "READ"
+ res2 = strncmp(input2,input2,20); //Compare the second input with the input (is always true)
int val = atoi(input2); //Convert String to int
- if(val <= N && val >=1) //Get the index
+ if(val <= N && val >=1) //check if the number of input is valid
{
if(res == 0 && res2 == 0)//If two values matches then execute
{
+
for (unsigned int n = 0; n < val; n++)
- { //Get values from index 0 to specific index
+ { //Get values from index 0 to val(user input) 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");
- //If the condition failed, Ask for re-input
}
- }else //1
+
+
+
+ }
+ if(val > N ) //if the user input is more than 120 the show 120 samples
+ {
+ if(res == 0 && res2 == 0)//If two values matches then execute
+ {
+ for (unsigned int n = 0; n < N; n++)
+ { //show sample from 1 to 120
+ pc.printf("Sample %d : %4.2f, %6.1f ,%3.1f\n\r",n,
+ tempArray[n],pressArray[n],humArray[n]);
+ }
+ }
+ }
+
+ else
{
- res = strncmp(input1,"READ",20);
- res2 = strncmp(input2,"ALL",20);
+ res = strncmp(input1,"READ",20); // Compare the first input with the word "READ"
+ res2 = strncmp(input2,"ALL",20); // Compare the second input with the word "ALL"
//Compare values
if (res == 0 && res2 == 0)
- {//If condition is true then print from index 0 to Max.
+ {//If condition is true then print from sample 1 to 120.
+
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
{
//Compare values
- res = strncmp(input1,"DELETE",20);
- res2 = strncmp(input2,"ALL",20);
- if(res==0 && res2==0)
+ res = strncmp(input1,"DELETE",20); // Compare the first input with the word "DELETE"
+ res2 = strncmp(input2,"ALL",20); // Compare the first input with the word "ALL"
+ if(res==0 && res2==0) //if match the execute
{
//Erase all elements, replace with 0
memset(tempArray, 0, sizeof tempArray);
@@ -172,25 +199,26 @@
memset(pressArray, 0, sizeof pressArray);
pc.printf("%d Elements deleted\n",N);
counters = 0; //Reset count
- }else{
- pc.printf("Invalid INPUT. \n");
}
-
- }//1
- } //1
+
+ }
+
+ }
+
+
- //Remove data from index 0 to specific index
- strcpy (inputDel1,cmd);
- strcpy (inputDel2,cmd2); //Get user input
- resSet1 = strncmp(input1,"DELETE",20);
- resSet2 = strncmp(input2,inputDel2,20);
- int valDel = atoi(inputDel2);
- int startDel=(counters - valDel);
- if (valDel <=10 && valDel >=1)
+
+ strcpy (inputDel1,cmd); // copy the input (cmd) to the variable inputDel1
+ strcpy (inputDel2,cmd2); // copy the input (cmd2) to the variable inputDel2
+ resSet1 = strncmp(input1,"DELETE",20); // Compare the first input with the word "DELETE"
+ resSet2 = strncmp(input2,inputDel2,20); // Compare the second input with its self (it is always same)
+ int valDel = atoi(inputDel2); //convert string to int
+ int startDel=(counters - valDel); // calculate the start index of deleting
+ if (valDel <=N && valDel >=1)
{
- if(resSet1==0 && resSet2==0)
+ if(resSet1==0 && resSet2==0) //if the input are same with resSet1,2 execute
{
- for (unsigned int n=startDel; n<counters; n++)
+ for (unsigned int n=startDel; n<counters; n++) //delete specific elements (set to "0")
{
tempArray[n]=0.00;
humArray[n]=0.00;
@@ -198,49 +226,48 @@
}
pc.printf("Deleted %d records\n",valDel);
counters = counters - valDel;
- }else{
- pc.printf("Invalid INPUT. \n");
}
}
- //Assign the input to other set
+
+ //Assign the input to other variables
strcpy (inputSet1,cmd);
strcpy (inputSet2,cmd2);
//Compare command
- resSet1 = strncmp(inputSet1,"SETT",20);
- resSet2 = strncmp(inputSet2,inputSet2,20);
+ resSet1 = strncmp(inputSet1,"SETT",20); // Compare the first input with the word "SETT"
+ resSet2 = strncmp(inputSet2,inputSet2,20); // Compare the second input with its self
int inputSpeed = atoi(inputSet2); // String to int
- if (inputSpeed <=60 && inputSpeed >=0.1)
+ if (inputSpeed <=60 && inputSpeed >=0.1) //if the input is valid execute
{
if (resSet1==0 && resSet2==0)
{
- //?
+ //update the timer speed
sampleSpeed = inputSpeed;
t.attach(&adcISR, sampleSpeed);
pc.printf("T UPDATED TO %d\n",sampleSpeed);
-
-
- }else{
- pc.printf("Invalid INPUT. \n");
}
- }
- strcpy (inputState1,cmd); //INPUT 1
+ }
+ //Assign the input to other variables
+ strcpy (inputState1,cmd);
strcpy (inputState2,cmd2);
- resSet1 = strncmp(inputState1,"STATE",20);
- resSet2 = strncmp(inputState2,"ON",20);
+ resSet1 = strncmp(inputState1,"STATE",20); // Compare the first input with the word "STATE"
+ resSet2 = strncmp(inputState2,"ON",20); // Compare the second input with the word "ON"
if (resSet1==0 && resSet2==0)
{
+ //set the timer ON
t.attach(&adcISR, sampleSpeed);
pc.printf("Sampling: ON\n");
}
- strcpy (inputStateOFF1,cmd); //INPUT 1
+ //Assign the input to other variables
+ strcpy (inputStateOFF1,cmd);
strcpy (inputStateOFF2,cmd2);
- resState1 = strncmp(inputStateOFF1,"STATE",20);
- resState2 = strncmp(inputStateOFF2,"OFF",20);
+ resState1 = strncmp(inputStateOFF1,"STATE",20); // Compare the first input with the word "STATE"
+ resState2 = strncmp(inputStateOFF2,"OFF",20); // Compare the second input with the word "OFF"
- if (resState1==0 && resState2==0)
- {
+ if (resState1==0 && resState2==0) //if are the same execute
+ {
+ //set the timer off
t.detach();
pc.printf("Sampling: OFF\n");
}
@@ -263,15 +290,14 @@
//Normal mode, which refers to normal power cosumption mode
while(1)
{
+ //read the sensor data
humidity.init();
humidity.calib();
humidity.ReadTempHumi(&tempCelsius, &humi);
- //Read humidity data
barometer.get();
barometer.pressure();
barometer.temperature();
- //Read temperature and pressure
- sleep();
+ sleep(); //if timer is off the sleep
Thread::wait(200); // 200 ms NB 'Thread::wait(int d);' !!! d is in milliseconds!
myled = 0; // LED is OFF
Thread::wait(100); // 100 ms
