Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
6:0aca5c17c988
Parent:
5:6d85cafa1085
Child:
7:104ac8e707e6
--- a/main.cpp	Mon Apr 13 20:35:21 2015 +0000
+++ b/main.cpp	Sun Apr 19 00:32:26 2015 +0000
@@ -86,6 +86,11 @@
     {4,70.0}    //state 4, 70'C
 };
 
+char tempUnit [4] = {'C','F','K','R'}; //character that stores temperature unit type.
+
+char pressUnit[3] = {'M','P','A'}; //character that stores pressure units. M - millibars P - Pascals A - atmospheres
+
+char AltUnit[3] = {'m','f','y'}; //character that stores altitude units. m - metres f - feet y - yards
 
 /**
 @namespace bmp180
@@ -157,7 +162,7 @@
 @namespace displayTimer
 @brief Ticker object to display readings on screen with a specified interval. Can be varied with Interrupt Service Routines.
 */
-Ticker displayTimer;
+Ticker displayTimer; //ticker object to display readings
 
 /*
 @namespace leds
@@ -178,20 +183,25 @@
 @brief Object that belongs to the N5110 class. Set up pin outputs for the Nokia 5110 display. Defined in N5110.h.
 @see https://developer.mbed.org/users/eencae/code/N5110/
 */
-void printReadings();
-void clearCells();
+void printReadings();  // declare function to print readings here, which is then defined after the main() function.
+void clearCells();   //declare function to clear all cells should there be a need for it. Even though lcd.clear() also does the job this can be handy in case of failure.
 
-float temp = 0.0;
-float press = 0.0;
-float altitude = 0.0;
+float temp = 0.0;  //declare the temp variable for temperature universally so that it can be shared across functions to represent temperature.
+float press = 0.0; //do the same for pressure using press.
+float altitude = 0.0;  //and altitude using, well, altitude :P
 
-int i = 0;
-int j = 0;
+int i = 0;  //represents the column number (horizontal pixel number) of the display.
+int j = 0;  //represents the row number of the display.
 
 int dispSetting = 0; //set display setting to default.
 int recSetting = 3;  //set log setting to default.
 int tempSetting = 2; //set temperature threshold to default.
 
+int tempUnitSetting = 0; //set temperature unit setting to default.
+int pressUnitSetting = 0; //set pressure unit setting to default.
+int altUnitSetting = 0; //and do the same for altitude.
+int subMenuId = 0; //int used to store sub-menu number. For instance pressing the menu button once and then Button One gives Sub-Menu 1.
+
 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.*************
@@ -257,7 +267,11 @@
 
 void menuButtonPressed()
 {
-    menuButtonFlag = !menuButtonFlag; //if set to 1, set it back to 0. If not set, set the flag.
+    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 */
@@ -265,10 +279,10 @@
 
 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
+    if(menuButtonFlag > 0) { //if menu button has been pressed and main menu entered
         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
     }
 }
@@ -278,10 +292,10 @@
 
 void buttonTwoPressed()
 {
-    if(menuButtonFlag) {
-        menuButtonFlag = 0;
+    if(menuButtonFlag > 0) {
         buttonTwoAltFlag = !buttonTwoAltFlag;
-    } else {
+    } 
+    else {
         buttonTwoFlag = !buttonTwoFlag;
     }
 }
@@ -293,7 +307,8 @@
 {
     if(menuButtonFlag > 0) {
         buttonThreeAltFlag = !buttonThreeAltFlag;
-    } else
+    } 
+    else
         buttonThreeFlag = !buttonThreeFlag;
 }
 
@@ -328,7 +343,7 @@
 */
 void timerExpiLog()
 {
-    logTimerFlag = 0;
+    logTimerFlag = 1;
 }
 
 void displayInitSplash()  //display splash screen
@@ -340,6 +355,100 @@
     lcd.printString("Esendag",20,5);
 }
 
+void displayMenuOne(){
+    lcd.printString("Settings Menu",0,0);
+    lcd.printString("One",0,1);
+    lcd.printString("Use Buttons",0,2);
+    lcd.printString("To change",0,3);
+    lcd.printString("Settings",0,4);
+    lcd.refresh();
+}
+
+void displayMenuTwo(){
+    lcd.printString("Settings Menu",0,0);
+    lcd.printString("Two",0,1);
+    lcd.printString("Use menuButton",0,2);
+    lcd.printString("To Go Back To",0,3);
+    lcd.printString("Display menu",0,4);
+    lcd.refresh();
+}
+
+void displayTempUnit(){
+    lcd.printString("Use Button 2",0,0);
+    lcd.printString("To decrease",0,1);
+    lcd.printString("Temp. Setting;",0,2);
+    lcd.printString("Button 3",0,3);
+    lcd.printString("To increase.",0,4);
+    
+    char bufferSt[14];  //buffer to store string
+    sprintf(bufferSt,"current:%c",tempUnit[tempUnitSetting]); //write the typed string and the current unit setting onto the buffer
+    
+    lcd.printString(bufferSt,0,5); //print the buffer
+    lcd.refresh();  //needs to refresh to write the string buffers to the display
+}
+
+void displayPressUnit(){
+    lcd.printString("Use Button 1",0,0);
+    lcd.printString("To decrease",0,1);
+    lcd.printString("Press Setting;",0,2);
+    lcd.printString("Button 3",0,3);
+    lcd.printString("To increase.",0,4);
+    
+    char bufferSt[14];  //buffer to store string
+    sprintf(bufferSt,"current:%c",pressUnit[pressUnitSetting]);
+    
+    lcd.printString(bufferSt,0,5);
+    lcd.refresh();
+}
+
+void displayDispMenu(){
+    lcd.printString("Use Button 2",0,0);
+    lcd.printString("To decrease",0,1);
+    lcd.printString("Time Setting;",0,2);
+    lcd.printString("Button 3",0,3);
+    lcd.printString("To increase.",0,4);
+    
+    char bufferSt[14];  //buffer to store string
+    //just a universally applicable if-else if statement to create a string out of current time setting should more time settings be added...
+    if(dispInterval[dispSetting].time >= 1.0){ //if time setting is a second or more
+        sprintf(bufferSt,"set:%.1f s",dispInterval[dispSetting].time);
+    }
+    else if(dispInterval[dispSetting].time < 1.0){ //if time setting is less than a second
+        float tempTime = dispInterval[dispSetting].time * 1000.0; //convert time to milliseconds
+        sprintf(bufferSt,"set:%.1f ms",tempTime);
+    }    
+    lcd.printString(bufferSt,0,5);
+    lcd.refresh();
+}
+
+void displayThresholdTemp(){
+    lcd.printString("Use Button 1",0,0);
+    lcd.printString("To decrease",0,1);
+    lcd.printString("Temp Thresh;",0,2);
+    lcd.printString("Button 3",0,3);
+    lcd.printString("To increase.",0,4);
+    
+    char bufferSt[14];  //buffer to store string
+    sprintf(bufferSt,"current:%.1f",tempThres[tempSetting].thresTemp);
+    
+    lcd.printString(bufferSt,0,5);
+    lcd.refresh();
+}
+
+void displayAltitudeUnit(){
+    lcd.printString("Use Button 1",0,0);
+    lcd.printString("To decrease",0,1);
+    lcd.printString("Altitude Set;",0,2);
+    lcd.printString("Button 2",0,3);
+    lcd.printString("To increase.",0,4);
+    
+    char bufferSt[14];  //buffer to store string
+    sprintf(bufferSt,"current:%c",AltUnit[altUnitSetting]);
+    
+    lcd.printString(bufferSt,0,5);
+    lcd.refresh();
+}
+
 //1 bar is 100000 Pa. An atm is 101325 Pa. Therefore an mb is 100 Pa.
 
 int main()
@@ -347,9 +456,17 @@
 
     splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
 
-    displayTimer.attach(&timerExpiDisplay,1.0); //do the same for display dispInterval[dispSetting].time
+    displayTimer.attach(&timerExpiDisplay,dispInterval[dispSetting].time); //do the same for display dispInterval[dispSetting].time
+
+    //logTimer.attach(&timerExpiLog,recInterval[recSetting].time);
+
+    menuButton.rise(&menuButtonPressed); //event generated on rising edge (a positive spike in voltage), indicated by .rise
 
-    logTimer.attach(&timerExpiLog,recInterval[recSetting].time); //do the same for logging
+    buttonOne.rise(&buttonOnePressed);
+    
+    buttonTwo.rise(&buttonTwoPressed);
+    
+    buttonThree.rise(&buttonThreePressed);
 
     // first need to initialise display
     lcd.init();
@@ -372,11 +489,9 @@
             splashFlip.detach();    //detach Timeout object
             lcd.clear();
             clearCells();
-            lcd.refresh();
+            lcd.refresh(); //need to refresh to write buffer on lcd
 
             if(dispTimerFlag) {
-                dispTimerFlag = 0;
-
                 //read values (T in degrees Celsius and P in mb).
                 measurement = bmp180.readValues();
                 temp = measurement.temperature;
@@ -387,10 +502,121 @@
                 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;
+                
                 printReadings();
                 lcd.refresh();
+            }
+            
+            if(menuButtonFlag == 1){  //if menu button has been pressed once
+                
+                lcd.clear();  //clear the lcd display
+                displayMenuOne();  //display the menu strings function
+                while(buttonOneAltFlag){ //if Button One is pressed AND the menu button is;
+                    lcd.clear(); //clear lcd
+                    displayTempUnit();  //display unit change 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
+                        tempUnitSetting--;  //decrease the unit setting
+                        buttonTwoAltFlag = 0;  //reset flag to avoid repeated unit changes on loop
+                        if(tempUnitSetting < 0)  //if it goes below 0
+                            tempUnitSetting = 3; //go back to the highest value
+                    }
+                    if(buttonThreeAltFlag && subMenuId == 1){  //if otherwise button 3 has been pressed
+                        tempUnitSetting++;  //increase temp setting
+                        buttonThreeAltFlag = 0;
+                        if(tempUnitSetting > 3)  //if the upper limit has been exceeded (3)
+                            tempUnitSetting = 0;  //reset it to zero
+                    }
+                } //close button one alt
+                buttonOneAltFlag = 0;
+                
+                while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
+                    lcd.clear(); //clear lcd
+                    displayPressUnit();  //display unit change menu
+                    subMenuId = 2;
+                    
+                    if(buttonOneAltFlag && subMenuId == 2){  //if added to the above conditions button 1 is pressed
+                        pressUnitSetting--;  //decrease the unit setting
+                        buttonOneAltFlag = 0;  //reset flag to avoid repeated unit changes on loop
+                        if(pressUnitSetting < 0)  //if it goes below 0
+                            pressUnitSetting = 2; //go back to the highest value
+                    }
+                    if(buttonThreeAltFlag && subMenuId == 2){  //if otherwise button 3 has been pressed
+                        pressUnitSetting++;  //increase pressure setting
+                        buttonThreeAltFlag = 0;
+                        if(pressUnitSetting > 2)  //if the upper limit has been exceeded (2)
+                            pressUnitSetting = 0;  //reset it to zero
+                    }
+                } //close button two alt
+                buttonTwoAltFlag = 0;
+            } //close menu button flag
+            
+            
+            else 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
+                    displayDispMenu();  //display unit change menu
+                    subMenuId = 4;
+                    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
+                        if(dispSetting < 0)  //if it goes below 0
+                            dispSetting = 5; //go back to the highest value
+                    }
+                    if(buttonThreeAltFlag && subMenuId == 4){  //if otherwise button 3 has been pressed
+                        dispSetting++;  //increase setting
+                        buttonThreeAltFlag = 0;
+                        if(dispSetting > 5)  //if the upper limit has been exceeded (3)
+                            dispSetting = 0;  //reset it to zero
+                    }
+                } //close button one alt
+                buttonOneAltFlag = 0;
+                
+                while(buttonTwoAltFlag){ //if Button Two flag is set AND the menu button is;
+                    lcd.clear(); //clear lcd
+                    displayThresholdTemp();  //display unit change menu
+                    subMenuId = 5;
+                    if(buttonOneAltFlag && subMenuId == 5){  //if added to the above conditions button 1 is pressed
+                        tempSetting--;  //decrease setting
+                        buttonOneAltFlag = 0;  //reset flag to avoid repeated unit changes on loop
+                        if(tempSetting < 0)  //if it goes below 0
+                            tempSetting = 4; //go back to the highest value
+                    }
+                    if(buttonThreeAltFlag && subMenuId == 5){  //if otherwise button 3 has been pressed
+                        tempSetting++;  //increase setting
+                        buttonThreeAltFlag = 0;
+                        if(tempSetting > 4)  //if the upper limit has been exceeded (2)
+                            tempSetting = 0;  //reset it to zero
+                    }
+                } //close button two alt
+                buttonTwoAltFlag = 0;
+                
+                while(buttonThreeAltFlag){ //if Button Three flag is set AND the menu button is;
+                    lcd.clear(); //clear lcd
+                    displayAltitudeUnit();  //display unit change menu
+                    subMenuId = 6;
+                    if(buttonOneAltFlag && subMenuId == 6){  //if added to the above conditions button 1 is pressed
+                        altUnitSetting--;  //decrease the unit setting
+                        buttonOneAltFlag = 0;  //reset flag to avoid repeated unit changes on loop
+                        if(altUnitSetting < 0)  //if it goes below 0
+                            altUnitSetting = 2; //go back to the highest value
+                    }
+                    if(buttonTwoAltFlag && subMenuId == 6){  //if otherwise button 2 has been pressed
+                        altUnitSetting++;  //increase pressure setting
+                        buttonTwoAltFlag = 0;
+                        if(altUnitSetting > 2)  //if the upper limit has been exceeded (2)
+                            altUnitSetting = 0;  //reset it to zero
+                    }
+                } //close button three alt
+                buttonThreeAltFlag = 0;
+            } //close menu button flag
+            
 
-            }
 
             Sleep();  //put the mbed to sleep once the program flow has been completed.