Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
6:03f12eda070d
Parent:
5:6952998ab446
Child:
7:328dc986484f
diff -r 6952998ab446 -r 03f12eda070d main.cpp
--- a/main.cpp	Sun Apr 12 08:36:02 2015 +0000
+++ b/main.cpp	Sun Apr 12 12:17:31 2015 +0000
@@ -15,7 +15,7 @@
 
 
 Ticker timer; //Create ticker object for temperature and pressure readings
-
+Timeout powerSaverTimeout; // create a timeout object for the poweerSaver function
 
 int setTimeFlag = 0; // flag for real time clock ISR
 int timerFlag = 0; // Flag for tempesrature pressure reading ISR
@@ -24,6 +24,8 @@
 int button3Flag = 0 ;
 int button4Flag = 0 ;
 int unitFlag = 1 ;
+int powerSaverFlag = 0 ;
+int lcdPowerFlag = 1 ; // Indicates if the LCD is switched on or powered off
 
 
 
@@ -36,6 +38,10 @@
 int min=0;
 int sec=1;
 int UNIXdate;
+int powerSaverTime = 60;
+
+
+
 
 char rxString[16]; //buffer to store recieved string from serial
 char bufferTime[14]; // buffer to store time
@@ -49,13 +55,14 @@
 char Day[31];
 char Hour[24];
 char Min[60];
-
+char Time[2]; // string to store the powerSaverTime value to display on the LCD
 
 
 void button1Pressed()  //ISR to subtract 1 from the value of choice when the first button is pressed
 {
     choice --;
     leds = 1;
+
 }
 
 
@@ -64,6 +71,7 @@
 {
     choice ++;
     leds = 2 ;
+
 }
 
 
@@ -97,7 +105,7 @@
 void setTime() // sets the time every second
 {
 //print time for debugging
-    serial.printf("set_time -%s ",rxString);
+    //serial.printf("set_time -%s ",rxString);
 //atoi() converts a string to an integer
     int time = atoi(rxString);
 //update the time
@@ -137,27 +145,27 @@
 
     int temperatureC = measurement.temperature;
     int temperatureF = (temperatureC*(9/5))+32;
-    
-    if (unitFlag==1 || unitFlag==2 ){
-    length = sprintf(bufferT,"T = %0.1d C",temperatureC); // print formatted data to buffer
-    // it is important the format specifier ensures the length will fit in the buffer
+
+    if (unitFlag==1 || unitFlag==2 ) {
+        length = sprintf(bufferT,"T = %0.1d C",temperatureC); // print formatted data to buffer
+        // it is important the format specifier ensures the length will fit in the buffer
     }
-    if (unitFlag==3 || unitFlag==4 ){
-    length = sprintf(bufferT,"T = %0.1d F",temperatureF); // print formatted data to buffer
-    // it is important the format specifier ensures the length will fit in the buffer
+    if (unitFlag==3 || unitFlag==4 ) {
+        length = sprintf(bufferT,"T = %0.1d F",temperatureF); // print formatted data to buffer
+        // it is important the format specifier ensures the length will fit in the buffer
     }
 
     float pressureMB = measurement.pressure;  // same idea with floats
     float pressureATM = pressureMB*0.0009869;
-    
-    
-    if (unitFlag==1 || unitFlag==3 ){
-    length = sprintf(bufferP,"P = %.2f mb",pressureMB); // print formatted data to buffer
-    // it is important the format specifier ensures the length will fit in the buffer
+
+
+    if (unitFlag==1 || unitFlag==3 ) {
+        length = sprintf(bufferP,"P = %.2f mb",pressureMB); // print formatted data to buffer
+        // it is important the format specifier ensures the length will fit in the buffer
     }
-    if (unitFlag==2 || unitFlag==4 ){
-    length = sprintf(bufferP,"P = %.2f atm",pressureATM); // print formatted data to buffer
-    // it is important the format specifier ensures the length will fit in the buffer
+    if (unitFlag==2 || unitFlag==4 ) {
+        length = sprintf(bufferP,"P = %.2f atm",pressureATM); // print formatted data to buffer
+        // it is important the format specifier ensures the length will fit in the buffer
     }
 
 
@@ -192,11 +200,147 @@
             button3Flag = 0;
             break;
         }
+
         Sleep();
     }
 }
 
 
+
+
+
+
+void savePower()   // turns off the LCD to save power
+{
+
+    lcd.turnOff () ; //turns off the lcd
+    lcdPowerFlag = 0 ;  // A flag is set to indicate the state of the LCD
+
+}
+
+
+
+
+
+
+void wakeUp ()  //Turns the LCD on after the mbed is interrupted
+{
+
+    if (button1Flag || button2Flag || button3Flag || button4Flag ) {
+
+
+        if (lcdPowerFlag == 0) { // if the LCD is powered off
+            lcdPowerFlag = 1 ;
+            lcd.init();  // turns the LCD on
+            powerSaverTimeout.attach(&savePower, powerSaverTime); // setup a timeiut to call the savePower function
+            button1Flag = 0 ;
+            button2Flag = 0 ;
+            button3Flag = 0 ;
+            button4Flag = 0 ;
+        }
+    }
+
+}
+
+
+
+
+
+
+
+
+void powerSaverCheck()   // checks if the powersaverFlag is set
+{
+
+    if (powerSaverFlag == 1 ) {
+
+        powerSaverTimeout.attach(&savePower, powerSaverTime); // setup a timeiut to call the savePower function
+
+    }
+
+    else {
+        powerSaverTimeout.detach();
+
+    }
+}
+
+
+
+void powerSaverSetting()
+{
+
+
+    lcd.clear();
+    choice = powerSaverFlag ;
+
+    while (1) {
+        wakeUp();
+        int time = sprintf (Time, "%d min", powerSaverTime/60);//convert integer to buffer str
+
+        if (choice < 1) {
+            choice = 2;
+        }
+        if (choice > 2) {
+            choice = 1;
+        }
+        if (powerSaverTime > 300){
+            powerSaverTime = 60 ;
+            }
+
+
+        switch (choice) {
+            case 1:
+
+                lcd.clear();
+                lcd.printString(bufferTime,48,0);
+                lcd.printString("Power Saver :",0,1);
+                lcd.printString("ON",0,2);
+                lcd.printString("Time Out :",0,3);
+                lcd.printString(Time,0,4);
+                lcd.printString("Back      Save",0,5);
+
+                if (button4Flag) {
+                    button4Flag=0;
+                    powerSaverFlag = 1 ;
+                    powerSaverTime += 60 ;
+
+                }
+
+
+                break;
+            case 2:
+                lcd.clear();
+                lcd.printString(bufferTime,48,0);
+                lcd.printString("Power Saver : ",0,1);
+                lcd.printString("OFF",0,2);
+                lcd.printString("Back      Save",0,5);
+                if (button4Flag) {
+                    button4Flag=0;
+                    powerSaverFlag = 0 ;
+
+                }
+
+            default:
+                break;
+
+        }
+
+        wait (0.1);
+        powerSaverCheck();
+        if (button3Flag) {
+            button3Flag=0;
+            break;
+        }
+    }
+}
+
+
+
+
+
+
+
+
 void calculateUNIXTime()
 {
     time_t rawtime;
@@ -226,9 +370,9 @@
 void unitsSetting()
 {
     choice = 1;
-
+    powerSaverCheck();
     while (1) {
-
+        wakeUp();
 
 
         if (choice < 1) {
@@ -309,6 +453,7 @@
 {
     lcd.clear();
     while(1) {
+        wakeUp();
         lcd.clear();
         lcd.refresh();
         if (choice < 1) {
@@ -338,7 +483,7 @@
 
     choice = 1 ;
     while (1) {
-
+        wakeUp();
         if (choice > 6) {
             choice = 1 ;
         }
@@ -485,9 +630,13 @@
 void settingsMenu()
 {
 
+    powerSaverCheck();
 
     choice = 1 ;
     while(1) {
+
+
+        wakeUp();
         if (choice > 5) {
             choice = 1 ;
         }
@@ -553,7 +702,10 @@
                 lcd.printString("Power Saver",10,4);
                 lcd.printString("Log Interval",0,5);
                 lcd.drawCircle(5,36,3,1);  // x,y,radius,black fill
-
+                if (button4Flag) {
+                    button4Flag = 0;
+                    powerSaverSetting();
+                }
                 break;
             case 5:
 
@@ -587,7 +739,7 @@
 
 void startMenu()   //The menu displayed at the beginning
 {
-
+    wakeUp();
     switch (choice) {
         case 1:
 
@@ -683,6 +835,9 @@
 
     set_time(UNIXdate); // initialise time from the calculated UNIX time entered by the user
 
+
+    powerSaverCheck();
+
     while(1) {
 
         if (choice > 5) {