Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
3:70e14f1577f7
Parent:
2:08f2469728d5
Child:
4:da904413485a
diff -r 08f2469728d5 -r 70e14f1577f7 main.cpp
--- a/main.cpp	Sat Apr 11 23:56:39 2015 +0000
+++ b/main.cpp	Mon Apr 13 14:40:32 2015 +0000
@@ -5,7 +5,7 @@
 @brief -- Minimises mbed power consumption to the greatest extent possible.
 @brief -- Displays the information to the user.
 @brief -- Features a BMP180 pressure and temperature sensor. (TMP102 is unnecessary as BMP180 also takes temp. readings)
-@brief -- Features a Nokia 5110 display to print the sensor readings over and display them to the user.     
+@brief -- Features a Nokia 5110 display to print the sensor readings over and display them to the user.
 @brief -- Sensor information is recorded on Flash memory on the mbed.
 @brief -- Graph to display data to the user.
 @brief -- mbed is powered by a Buck regulator (5V output) that is in turn supplied by a 9V PP3 battery which powers the PCB.
@@ -14,7 +14,7 @@
 @brief -- Adjusting the unit the pressure is displayed in: millibars(mb), Pascals(Pa) and Atmospheres (atm).
 @brief -- Displaying a brief splash screen to show changes in the unit display settings.
 @author Volkan Esendag (SID:200795870)
-@date 11 March 2015 (created) / 12 April 2015(last modified)
+@date 11 March 2015 (created) / 13 April 2015(last modified)
 */
 
 #include "mbed.h"
@@ -34,29 +34,58 @@
 #endif
 
 /**
-@namespace RecState
-@brief Custom struct for recording intervals. Fetch state number and select recording interval via the use of a button.
+Custom struct for logging intervals. Fetch state number and select recording interval via the use of a button.
 */
 
-struct RecState{
+struct RecState {
     int recordState;
     float time;
 };
-
+//once struct has been declared, define a pointer type with it.
 typedef const struct RecState RecS;
 
+//RecS object to determine states for logging intervals. Array size 10.
 RecS recInterval[10] = {
-    {0,2.0},    //state 0, 2 seconds
-    {1,5.0},    //state 1, 5 seconds 
-    {2,10.0},   //state 2, 10 seconds
-    {3,30.0},   //state 3, 30 seconds
-    {4,60.0},   //state 4, 1 minute
-    {5,120.0},  //state 5, 2 minutes
-    {6,300.0},  //state 6, 5 minutes
-    {7,600.0},  //state 7, 10 minutes
-    {8,1800.0}, //state 8, 30 minutes
-    {9,3600.0}  //state 9, 1 hour
-};    
+    {0,600.0},    //state 0, 10 minutes
+    {1,1200.0},   //state 1, 20 minutes
+    {2,1800.0},   //state 2, 30 minutes
+    {3,3600.0},   //state 3, 1 hour. Default State.
+    {4,7200.0},   //state 4, 2 hours
+    {5,10800.0},  //state 5, 3 hours
+    {6,14400.0},  //state 6, 4 hours
+    {7,21600.0},  //state 7, 6 hours
+    {8,28800.0}, //state 8, 8 hours
+    {9,43200.0}  //state 9, 12 hours
+};
+
+//Use the same struct again to create another object, this time for reading display intervals. Array size 6.
+RecS dispInterval [6] = {
+    {0,1.0},    //state 0, 1 second. Default state.
+    {1,2.0},   //state 1, 2 seconds
+    {2,3.0},   //state 2, 3 seconds
+    {3,4.0},   //state 3, 4 seconds
+    {4,5.0},   //state 4, 5 seconds
+    {5,0.5},  //state 5, 500 milliseconds
+};
+
+/**Struct to set the temperature threshold after which point the device gives off a warning.*/
+struct TempThreshold {
+    int tempState;
+    float thresTemp;
+};
+
+//define the TempTyp pointer type using the TempThreshold struct.
+typedef const struct TempThreshold TempTyp;
+
+//using this type create an object which contains an array of threshold states. Array size 5.
+TempTyp tempThres[5] = {
+    {0,30.0},   //state 0, 30'C
+    {1,40.0},   //state 1, 40'C
+    {2,50.0},   //state 2, 50'C. Default threshold.
+    {3,60.0},   //state 3, 60'C
+    {4,70.0}    //state 4, 70'C
+};
+
 
 /**
 @namespace bmp180
@@ -119,11 +148,17 @@
 
 /**
 @namespace logTimer
-@brief Ticker object to record readings with a specified interval. Can be varied with Interrupt Service Routines.
+@brief Ticker object to record/log readings with a specified interval. Can be varied with Interrupt Service Routines.
 */
 
 Ticker logTimer;   //Ticker object to call ISR for a specified period of time.
 
+/**
+@namespace displayTimer
+@brief Ticker object to display readings on screen with a specified interval. Can be varied with Interrupt Service Routines.
+*/
+Ticker displayTimer;
+
 /*
 @namespace leds
 @brief GPIO output for status LEDs - used to display error message or as a flash memory overwrite operation feedback.
@@ -144,11 +179,15 @@
 @see https://developer.mbed.org/users/eencae/code/N5110/
 */
 void printReadings();
+void clearCells();
 
 float temp = 0.0;
 float press = 0.0;
 float altitude = 0.0;
 
+int i = 0;
+int j = 0;
+
 N5110 lcd(p7,p8,p9,p10,p11,p13,p21);    //VCC,SCE,RST,DC,MOSI,SCLK,BACKLIGHT
 
 ///////////The following pieces of code are to configure real-time clock for the data logger.*************
@@ -187,11 +226,12 @@
 @returns __semihost(USR_POWERDOWN, &arg)
 */
 
-int semihost_powerdown(){
-    
+int semihost_powerdown()
+{
+
     uint32_t arg;  //variable for return function
     return __semihost(USR_POWERDOWN, &arg);    //return semihost state...
-    
+
 } //...to power down the USB interface when the USB cable is detached.
 
 LocalFileSystem local("local"); // create local filesystem
@@ -200,30 +240,31 @@
 {
     time_t seconds = time(NULL); // get current time
     strftime(dataThree, 30 , "%R %x", localtime(&seconds)); //convert it into a string, from an array size of 30.
-                
+
     leds = 15; // turn on LEDs for feedback
     FILE *fp = fopen("/local/tempLog.csv", "a"); // open 'log.txt' for appending. Instance of class FILE.
 // if the file doesn't exist it is created, if it exists, data is appended to the end
-    fprintf(fp,"%s, %.2f , %.2f \n",dataThree,dataTwo,data); // print string to file 
+    fprintf(fp,"%s, %.2f , %.2f \n",dataThree,dataTwo,data); // print string to file
     fclose(fp); // close file
     leds = 0; // turn off LEDs to signify file access has finished
 }
 
 int menuButtonFlag = 0; /*!< menu button flag set in ISR */
 
-void menuButtonPressed(){
+void menuButtonPressed()
+{
     menuButtonFlag = !menuButtonFlag; //if set to 1, set it back to 0. If not set, set the flag.
 }
 
 int buttonOneFlag = 0; /*!< Button One flag set in ISR */
 int buttonOneAltFlag = 0;  /*!< Button One Alternate flag set in ISR */
 
-void buttonOnePressed(){
-    if(menuButtonFlag){  //if menu button has been pressed and main menu entered
+void buttonOnePressed()
+{
+    if(menuButtonFlag) { //if menu button has been pressed and main menu entered
         menuButtonFlag = 0;  //set menu button flag to 0 so that one can proceed to the temperature setting menu and go back by pressing again
         buttonOneAltFlag = !buttonOneAltFlag;  //set/reset-if-set alternate flag and proceed to next menu
-    }
-    else{
+    } else {
         buttonOneFlag = !buttonOneFlag;  //set/reset-if-set flag if not navigated to a menu
     }
 }
@@ -231,38 +272,40 @@
 int buttonTwoFlag = 0; /*!< Button Two flag set in ISR */
 int buttonTwoAltFlag = 0; /*!< Button Two Alternate flag set in ISR */
 
-void buttonTwoPressed(){
-    if(menuButtonFlag){
+void buttonTwoPressed()
+{
+    if(menuButtonFlag) {
         menuButtonFlag = 0;
-        buttonTwoAltFlag = !buttonTwoAltFlag;      
+        buttonTwoAltFlag = !buttonTwoAltFlag;
+    } else {
+        buttonTwoFlag = !buttonTwoFlag;
     }
-    else{
-        buttonTwoFlag = !buttonTwoFlag;
-    }    
 }
 
 int buttonThreeFlag = 0; /*!< Button Three flag set in ISR */
 int buttonThreeAltFlag = 0; /*!< Button Three Alternate flag set in ISR */
 
-void buttonThreePressed(){
-    if(menuButtonFlag){
+void buttonThreePressed()
+{
+    if(menuButtonFlag) {
         menuButtonFlag = 0;
-        buttonThreeAltFlag = !buttonThreeAltFlag;      
-    }
-    else
+        buttonThreeAltFlag = !buttonThreeAltFlag;
+    } else
         buttonThreeFlag = !buttonThreeFlag;
 }
 
 int splashFlag = 1;  /*!< Splash flag set to continue with program flow for the main function before proceeding to while(1) */
 
-void splashDelay(){
+void splashDelay()
+{
     splashFlag = 0;
 }
 
-void displayInitSplash(){ //display splash screen
+void displayInitSplash()  //display splash screen
+{
     lcd.printString("Welcome to",15,1);
     lcd.printString("Portable Weat.",3,2);
-    lcd.printString("Station",6,3);
+    lcd.printString("Station",20,3);
     lcd.printString("by Volkan",20,4);
     lcd.printString("Esendag",20,5);
 }
@@ -277,31 +320,35 @@
 @param altitude - altitude calculated from the pressure reading (m).
 */
 
-      
+
 
-int main() {
-    
+int main()
+{
+
     splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
-    
+
     // first need to initialise display
     lcd.init();
-    
+
     displayInitSplash();
-    
+
     //initialise barometer
     bmp180.init();
-    
+
     PHY_PowerDown();    //powers down the Ethernet feature.
 
     int result = semihost_powerdown();   //as a result, power down the USB connection
     //unless the cable is connected.
-    
+
     Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class
-    
-    if(splashFlag == 0){ //if ISR has been called proceed with program flow
-        splashFlip.detach();    //detach Timeout object
-        while(1) {
-        
+
+    while(1) {
+
+        if(splashFlag == 0) { //if ISR has been called proceed with program flow
+            splashFlip.detach();    //detach Timeout object
+            clearCells();
+            lcd.refresh();
+
             //read values (T in degrees Celsius and P in mb).
             measurement = bmp180.readValues();
             temp = measurement.temperature;
@@ -310,40 +357,55 @@
             //use pow(double a,double b) for indices, not the ^ sign. Just a reminder!
             altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
             printReadings();
-        
-            Sleep();  //put the mbed to sleep once the program flow has been completed.
-        
-        } //close while
-    } //close if statement
+            lcd.refresh();
+
+            //Sleep();  //put the mbed to sleep once the program flow has been completed.
+            
+            wait(1.0);
+
+        } //close if
+    } //close while
 } //terminate main()
 
-void printReadings(){
+void printReadings()
+{
     char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
-        // so can display a string of a maximum 14 characters in length
-        // or create formatted strings - ensure they aren't more than 14 characters long
-    
-    int length = sprintf(buffer,"Temp. = %.2f 'C",temp); // print formatted data to buffer
-        // it is important the format specifier ensures the length will fit in the buffer
-        if (length <= 14){  // if string will fit on display
-            lcd.printString(buffer,0,1);  // display on screen. Column 0, row 1.
-        }               
-    
-    length = sprintf(buffer,"Press. = %.2f mb",press);
-        if (length <= 14){
-            lcd.printString(buffer,0,2); // Column 0, row 2.
+    // so can display a string of a maximum 14 characters in length
+    // or create formatted strings - ensure they aren't more than 14 characters long
+
+    int length = sprintf(buffer,"T = %.2f 'C",temp); // print formatted data to buffer
+    // it is important the format specifier ensures the length will fit in the buffer
+    if (length <= 14) { // if string will fit on display
+        lcd.printString(buffer,0,1);  // display on screen. Column 0, row 1.
+    }
+
+    length = sprintf(buffer,"P = %.2f mb",press);
+    if (length <= 14) {
+        lcd.printString(buffer,0,2); // Column 0, row 2.
+    }
+
+    length = sprintf(buffer,"A = %.1f m",altitude);
+    if (length <= 14) {
+        lcd.printString(buffer,0,3);  //Column 0, row 3.
+    }
+    if (press++) {
+        lcd.printString("Hotter Weather",2,4); //column 2, row 4.
+        lcd.printString("On the Way!",2,5);   //column 2 , row 5.
+    }
+    else if (press--) {
+        lcd.printString("The temperature",2,4); //column 2, row 4.
+        lcd.printString("is predicted",2,5);   //column 2 , row 5.
+        lcd.printString("to fall!",2,6); //column 2, row 4.
+    }
+}
+
+void clearCells()
+{
+    //loop through cells, and clear them
+    for (int i = 0; i < 83 ; i++) {
+        for (int j = 0; j < 47; j++) {
+            lcd.clearPixel(i,j);         //function to "kill" all cells after looping through.
         }
-     
-    length = sprintf(buffer,"Altit. = %.1f m",altitude);
-        if (length <= 14){
-            lcd.printString(buffer,0,3);  //Column 0, row 3.
-        }
-        if (press++){
-            lcd.printString("Hotter Weather",2,4); //column 2, row 4.
-            lcd.printString("On the Way!",2,5);   //column 2 , row 5.
-        }
-        else if (press--){
-            lcd.printString("The temperature",2,4); //column 2, row 4.
-            lcd.printString("is predicted",2,5);   //column 2 , row 5.
-            lcd.printString("to fall!",2,6); //column 2, row 4.
-        }    
+    }
+    lcd.refresh(); //must refresh to write buffer to display
 }