Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
7:104ac8e707e6
Parent:
6:0aca5c17c988
Child:
8:29ac7d274ae0
--- a/main.cpp	Sun Apr 19 00:32:26 2015 +0000
+++ b/main.cpp	Sun Apr 19 17:53:39 2015 +0000
@@ -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) / 13 April 2015(last modified)
+@date 11 March 2015 (created) / 19 April 2015(last modified)
 */
 
 #include "mbed.h"
@@ -265,15 +265,6 @@
 
 int menuButtonFlag = 0; /*!< menu button flag set in ISR */
 
-void menuButtonPressed()
-{
-    menuButtonFlag++; //increment the flag to access different menu states.
-
-    if(menuButtonFlag > 2) { //if menu button has been clicked three times
-        menuButtonFlag = 0; //go back to the measurements menu
-    }
-}
-
 int buttonOneFlag = 0; /*!< Button One flag set in ISR */
 int buttonOneAltFlag = 0;  /*!< Button One Alternate flag set in ISR */
 
@@ -312,6 +303,17 @@
         buttonThreeFlag = !buttonThreeFlag;
 }
 
+void menuButtonPressed()
+{
+    if(buttonOneAltFlag == 0 && buttonTwoAltFlag == 0 && buttonThreeAltFlag == 0){ //if no flag is set and therefore no menu accessed
+        menuButtonFlag++; //increment the flag to access different menu states.
+
+        if(menuButtonFlag > 2) { //if menu button has been clicked three times
+            menuButtonFlag = 0; //go back to the measurements menu
+        }
+    }
+}
+
 int splashFlag = 1;  /*!< Splash flag set to continue with program flow for the main function before proceeding with program flow */
 
 /**
@@ -499,7 +501,6 @@
                 /*formula for calculating altitude from sea level by using atmospheric pressure. Unit in metres.
                 Use pow(double a,double b) for indices, not the ^ sign. Just a reminder! Also check out this site:
                 http://www.mide.com/products/slamstick/air-pressure-altitude-calculator.php
-                Also to bear in mind is that a metre is 3.2808399 feet; or 1.0936133 yards. Three feet equals a yard.
                 */
                 altitude = 44330.0*(1.0-(pow((press/PNought),(1.0/5.255))));
                 dispTimerFlag = 0;
@@ -512,9 +513,14 @@
                 
                 lcd.clear();  //clear the lcd display
                 displayMenuOne();  //display the menu strings function
+                subMenuId = 0; //initially set subMenuId to zero in case it is not
                 while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is;
-                    lcd.clear(); //clear lcd
+                    if(subMenuId == 0){  //if initially the UI wasn't in a sub-menu
+                        lcd.clear(); //clear lcd
+                    }
                     displayTempUnit();  //display unit change menu
+                    lcd.refresh();
+                    //this helps avoid vertical swipe blurs and re-overwrites on the display while in sub-menu.
                     subMenuId = 1; //set sub-menu number to avoid confusions for the processor as it might change other settings!
                         
                     if(buttonTwoAltFlag && subMenuId == 1){  //if added to the above conditions button 2 is pressed
@@ -531,10 +537,16 @@
                     }
                 } //close button one alt
                 buttonOneAltFlag = 0;
+                lcd.clear();
+                displayMenuOne();  //display the menu strings function
+                subMenuId = 0; //reset sub-menu ID when loop has finished executing so it can be applied to other sub-menus.
                 
                 while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
-                    lcd.clear(); //clear lcd
+                    if(subMenuId == 0){
+                        lcd.clear(); //clear lcd
+                    }
                     displayPressUnit();  //display unit change menu
+                    lcd.refresh();    
                     subMenuId = 2;
                     
                     if(buttonOneAltFlag && subMenuId == 2){  //if added to the above conditions button 1 is pressed
@@ -551,17 +563,24 @@
                     }
                 } //close button two alt
                 buttonTwoAltFlag = 0;
+                lcd.clear();
+                displayMenuOne();  //display the menu strings function
+                subMenuId = 0;
             } //close menu button flag
             
-            
-            else if(menuButtonFlag == 2){  //if menu button has been pressed twice
+            subMenuId = 0; //reset sub-menu ID after going through first menu in case it is not already
+            if(menuButtonFlag == 2){  //if menu button has been pressed twice
                 
                 lcd.clear();  //clear the lcd display
                 displayMenuTwo();  //display the menu strings function
                 while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is;
-                    lcd.clear(); //clear lcd
+                    if(subMenuId == 0){
+                        lcd.clear(); //clear lcd
+                    }
                     displayDispMenu();  //display unit change menu
+                    lcd.refresh();
                     subMenuId = 4;
+                    lcd.refresh();
                     if(buttonTwoAltFlag && subMenuId == 4){  //if added to the above conditions button 2 is pressed
                         dispSetting--;  //decrease setting
                         buttonTwoAltFlag = 0;  //reset flag to avoid repeated unit changes on loop
@@ -576,10 +595,16 @@
                     }
                 } //close button one alt
                 buttonOneAltFlag = 0;
+                lcd.clear();  //clear the lcd display
+                displayMenuTwo();  //display the menu strings function
+                subMenuId = 0;
                 
                 while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
-                    lcd.clear(); //clear lcd
+                    if(subMenuId == 0){
+                        lcd.clear(); //clear lcd
+                    }
                     displayThresholdTemp();  //display unit change menu
+                    lcd.refresh();
                     subMenuId = 5;
                     if(buttonOneAltFlag && subMenuId == 5){  //if added to the above conditions button 1 is pressed
                         tempSetting--;  //decrease setting
@@ -595,10 +620,16 @@
                     }
                 } //close button two alt
                 buttonTwoAltFlag = 0;
+                lcd.clear();  //clear the lcd display
+                displayMenuTwo();  //display the menu strings function
+                subMenuId = 0;
                 
                 while(buttonThreeAltFlag){ //if Button Three flag is set AND the menu button is;
-                    lcd.clear(); //clear lcd
+                    if(subMenuId == 0){
+                        lcd.clear(); //clear lcd
+                    }
                     displayAltitudeUnit();  //display unit change menu
+                    lcd.refresh();
                     subMenuId = 6;
                     if(buttonOneAltFlag && subMenuId == 6){  //if added to the above conditions button 1 is pressed
                         altUnitSetting--;  //decrease the unit setting
@@ -614,6 +645,9 @@
                     }
                 } //close button three alt
                 buttonThreeAltFlag = 0;
+                lcd.clear();  //clear the lcd display
+                displayMenuTwo();  //display the menu strings function
+                subMenuId = 0;
             } //close menu button flag
             
 
@@ -638,26 +672,90 @@
     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,"T = %.2f 'C",temp); // print formatted data to buffer
+    
+    if(tempUnitSetting == 0){ //if Celsius has been selected as the temperature unit
+        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.
+        }
+    } //close unit setting 0
+    else if(tempUnitSetting == 1){ //Fahrenheit
+        float tempTemp = (temp*1.8) + 32;
+        int length = sprintf(buffer,"T = %.2f F",tempTemp); // 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.
+        }
+    } //close unit setting 1
+    else if(tempUnitSetting == 2){ //Kelvin
+        float tempTemp = temp + 273.15;
+        int length = sprintf(buffer,"T = %.1f K",tempTemp); // 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.
+        }
+    } //close unit setting 2
+    
+    else if(tempUnitSetting == 3){ //Rankine
+        float tempTemp = (temp + 273.15)*1.8;
+        int length = sprintf(buffer,"T = %.1f 'R",tempTemp); // 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); //use single letters to represent parameters or string may not fit in the banks!
-    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 (length <= 14) { // if string will fit on display
+            lcd.printString(buffer,0,1);  // display on screen. Column 0, row 1.
+        }
+    } //close unit setting 3
+    
+    if(pressUnitSetting == 0){ //if pressure is to be displayed in mb
+        int length = sprintf(buffer,"P = %.2f mb",press); //use single letters to represent parameters or string may not fit in the banks!
+        if (length <= 14) {
+            lcd.printString(buffer,0,2); // Column 0, row 2.
+        }
+    } //close unit setting 0
+    
+    else if(pressUnitSetting == 1){ //Pa
+        float tempPress = press*100; //convert from mb to Pa
+        int length = sprintf(buffer,"P = %.0f Pa",tempPress); //use single letters to represent parameters or string may not fit in the banks!
+        if (length <= 14) {
+            lcd.printString(buffer,0,2); // Column 0, row 2.
+        }
+    } //close unit setting 1
+    else if(pressUnitSetting == 2){ //atm
+        float tempPress = press/1013.25; //an atm is 1013.25 mb; therefore dividing press by that gives pressure in atm.
+        int length = sprintf(buffer,"P = %.1f atm",tempPress); //use single letters to represent parameters or string may not fit in the banks!
+        if (length <= 14) {
+            lcd.printString(buffer,0,2); // Column 0, row 2.
+        }
+    } //close unit setting 2
+    
+    //Now for the altitude display settings. Bear in mind that a metre is 3.2808399 feet; or 1.0936133 yards. Three feet equals a yard.
+    if(altUnitSetting == 0){ //if metres have been selected
+        int length = sprintf(buffer,"A = %.1f m",altitude);
+        if (length <= 14) {
+            lcd.printString(buffer,0,3);  //Column 0, row 3.
+        }
+    } // close unit setting 0
+    else if(altUnitSetting == 1){ //feet
+        float tempAlt = altitude*3.2808399; //convert to feet
+        int length = sprintf(buffer,"A = %.1f ft",tempAlt);
+        if (length <= 14) {
+            lcd.printString(buffer,0,3);  //Column 0, row 3.
+        }
+    } //close unit setting 1
+    else if(altUnitSetting == 2){ //yards
+        float tempAlt = altitude*1.0936133; //convert to yards
+        int length = sprintf(buffer,"A = %.1f ft",tempAlt);
+        if (length <= 14) {
+            lcd.printString(buffer,0,3);  //Column 0, row 3.
+        }
+    } //close unit setting 2
+    
+    
     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--) {
+    } 
+    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.