Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
4:783276521952
Parent:
3:8a559b949470
Child:
5:6952998ab446
--- a/main.cpp	Thu Apr 09 22:09:09 2015 +0000
+++ b/main.cpp	Fri Apr 10 03:09:38 2015 +0000
@@ -28,25 +28,33 @@
 
 int choice=1 ;
 int length;
+int year=2015;
+int month=1;
+int day=1;
+int hour=12;
+int min=0;
+int sec=1;
+int UNIXdate;
 
 char rxString[16]; //buffer to store recieved string from serial
 char bufferTime[14]; // buffer to store time
 char bufferDate[14]; //buffer to store date
-char bufferT[14];
-char bufferP[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
+char bufferT[14]; //buffer to store temperature
+char bufferP[14];  //buffer to store pressure, 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
-
+char Year[4];
+char Month[12];
+char Day[31];
+char Hour[24];
+char Min[60];
 
 
 
 void button1Pressed()  //ISR to subtract 1 from the value of choice when the first button is pressed
 {
     choice --;
-    if (choice == 0) {
-        choice = 5 ;
-    }
-
+    leds = 1;
 }
 
 
@@ -54,10 +62,7 @@
 void button2Pressed() //ISR to add 1 to the value of the choice when the second button is pressed
 {
     choice ++;
-    if (choice == 6) {
-        choice = 1 ;
-    }
-
+    leds = 2 ;
 }
 
 
@@ -80,33 +85,33 @@
 
 void serialISR()
 {
-    //reads rx string into buffer when a serial interrupt occurs 
+    //reads rx string into buffer when a serial interrupt occurs
     serial.gets(rxString,16);
-    //set flag 
+    //set flag
     setTimeFlag=1;
 }
-    
-    
+
+
 
 void setTime() // sets the time every second
 {
-//print time for debugging 
-serial.printf("set_time -%s ",rxString);
+//print time for debugging
+    serial.printf("set_time -%s ",rxString);
 //atoi() converts a string to an integer
-int time = atoi(rxString);
-//update the time 
-set_time(time);
+    int time = atoi(rxString);
+//update the time
+    set_time(time);
 }
 
 
 
 void updateTime()
 {
-            time_t seconds = time(NULL); // get current time
+    time_t seconds = time(NULL); // get current time
 // format time into a string (time and date)
-        strftime(bufferTime, 14 , "%I:%M", localtime(&seconds));
-        strftime(bufferDate, 14 , "%d/%m/%y", localtime(&seconds));
-    
+    strftime(bufferTime, 14 , "%I:%M", localtime(&seconds));
+    strftime(bufferDate, 14 , "%d/%m/%y", localtime(&seconds));
+
     if (setTimeFlag) {
         setTimeFlag=0;
     }
@@ -144,6 +149,7 @@
 
 void displayData() //Prints the buffers on the LCD
 {
+    lcd.clear();
     if (length <= 14) { // if string will fit on display
         lcd.printString(bufferT,0,1);           // display on screen
         lcd.printString(bufferP,0,2);
@@ -157,88 +163,221 @@
 
 void updateData() //
 {
-    if (timerFlag) {
-        timerFlag=0;
-        readData();
-        displayData();
+    while (1) {
+        if (timerFlag) {
+            timerFlag=0;
+            readData();
+            displayData();
+        }
+        if (button3Flag) {
+            button3Flag = 0;
+            break;
+        }
+        Sleep();
     }
-    Sleep();
+}
+
+
+void calculateUNIXTime()
+{
+    time_t rawtime;
+    struct tm * timeinfo;
+
+    /* get current timeinfo: */
+    time ( &rawtime ); //or: rawtime = time(0);
+    /* convert to struct: */
+    timeinfo = localtime ( &rawtime );
+
+    /* now modify the timeinfo to the given date: */
+    timeinfo->tm_year   = year - 1900;
+    timeinfo->tm_mon    = month - 1;    //months since January - [0,11]
+    timeinfo->tm_mday   = day;          //day of the month - [1,31]
+    timeinfo->tm_hour   = hour;         //hours since midnight - [0,23]
+    timeinfo->tm_min    = min;          //minutes after the hour - [0,59]
+    timeinfo->tm_sec    = sec;          //seconds after the minute - [0,59]
+
+    /* call mktime: create unix time stamp from timeinfo struct */
+    UNIXdate = mktime ( timeinfo );
+
+}
+
+void brightnessSetting ()
+{
+    lcd.clear();
+    while(1) {
+     lcd.clear();
+     lcd.refresh();   
+        if (choice << 1) {
+        choice = 6 ;
+    }
+        if (choice >> 6)
+        {
+            choice = 1;
+    }
+        
+        float brightness=choice/6;
+        lcd.setBrightness(brightness);
+        
+        
+        int w = choice*10 ;
+        for (int x = 0; x < WIDTH ; x+=10) {
+            // x0,y0,x1,y1,type 0-white,1-black,2-dotted
+            lcd.drawLine(10,24,w,24,1);
+        }
+        if (button3Flag) {
+            button3Flag=0;
+            break;
+        }
+    }
 }
 
 
 
-void timeDate()
+void timeDateSetting()
 {
 
+    choice = 1 ;
+    while (1) {
+
+        if (choice > 6) {
+            choice = 1 ;
+        }
+        if (choice < 1) {
+            choice = 6 ;
+        }
+
 
-choice = 1 ;
-while (1){    
+//convert integers to strings to display on the LCD
+        int y = sprintf (Year, "Year : %d", year);//convert integer to buffer str
+        int m = sprintf (Month, "Month : %d", month);//convert integer to buffer str
+        int d = sprintf (Day, "Day : %d", day);//convert integer to buffer str
+        int h = sprintf (Hour, "Hour : %d", hour);//convert integer to buffer str
+        int mi = sprintf (Min, "Min : %d", min);//convert integer to buffer str
 
-    switch (choice) {
-        case 1:
+        wait(1);
+
+        switch (choice) {
+            case 1:
 
-            lcd.clear();
-            lcd.printString("Minute :",20,0);
-            lcd.printString("Hour :",0,1);
-            lcd.printString("Day :",0,2);
-            lcd.printString("Month :",0,3);
-            lcd.printString("Year :",0,4);
-            lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill
+                lcd.clear();
+                lcd.printString(Hour,10,0);
+                lcd.printString(Min,0,1);
+                lcd.printString(Day,0,2);
+                lcd.printString(Month,0,3);
+                lcd.printString(Year,0,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(5,4,3,1);  // x,y,radius,black fill
+                if (button3Flag) {
+                    button3Flag= 0;
+                    hour--;
+                }
+                if (button4Flag) {
+                    button4Flag= 0;
+                    hour++;
+                }
 
-// rest of code here
-            break;
-        case 2:
-            lcd.clear();
-            lcd.printString(bufferTime,48,0);
-            lcd.printString("Live Data",0,1);
-            lcd.printString("Saved Data",20,2);
-            lcd.printString("Forecast",0,3);
-            lcd.printString("Alarms",0,4);
-            lcd.printString("Settings",0,5);
-            lcd.drawCircle(5,20,3,1);  // x,y,radius,black fill
-// rest of code here
-            break;
-        case 3:
-            lcd.clear();
-            lcd.printString(bufferTime,48,0);
-            lcd.printString("Live Data",0,1);
-            lcd.printString("Saved Data",0,2);
-            lcd.printString("Forecast",20,3);
-            lcd.printString("Alarms",0,4);
-            lcd.printString("Settings",0,5);
-            lcd.drawCircle(5,28,3,1);  // x,y,radius,black fill
-// rest of code here
-            break;
-        case 4:
-            lcd.clear();
-            lcd.printString(bufferTime,48,0);
-            lcd.printString("Live Data",0,1);
-            lcd.printString("Saved Data",0,2);
-            lcd.printString("Forecast",0,3);
-            lcd.printString("Alarms",20,4);
-            lcd.printString("Settings",0,5);
-            lcd.drawCircle(5,36,3,1);  // x,y,radius,black fill
-            break;
-        case 5:
+                break;
+            case 2:
+                lcd.clear();
+                lcd.printString(Hour,0,0);
+                lcd.printString(Min,10,1);
+                lcd.printString(Day,0,2);
+                lcd.printString(Month,0,3);
+                lcd.printString(Year,0,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill
+                if (button3Flag) {
+                    button3Flag= 0;
+                    min--;
+                }
+                if (button4Flag) {
+                    button4Flag= 0;
+                    min++;
+                }
+                break;
+            case 3:
+                lcd.clear();
+                lcd.printString(Hour,0,0);
+                lcd.printString(Min,0,1);
+                lcd.printString(Day,10,2);
+                lcd.printString(Month,0,3);
+                lcd.printString(Year,0,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(5,20,3,1);  // x,y,radius,black fill
+                if (button3Flag) {
+                    button3Flag= 0;
+                    day--;
+                }
+                if (button4Flag) {
+                    button4Flag= 0;
+                    day++;
+                }
+                break;
+            case 4:
+                lcd.clear();
+                lcd.printString(Hour,0,0);
+                lcd.printString(Min,0,1);
+                lcd.printString(Day,0,2);
+                lcd.printString(Month,10,3);
+                lcd.printString(Year,0,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(5,28,3,1);  // x,y,radius,black fill
+                if (button3Flag) {
+                    button3Flag= 0;
+                    month--;
+                }
+                if (button4Flag) {
+                    button4Flag= 0;
+                    month++;
+                }
+                break;
+            case 5:
 
-            lcd.clear();
-            lcd.printString(bufferTime,48,0);
-            lcd.printString("Live Data",0,1);
-            lcd.printString("Saved Data",0,2);
-            lcd.printString("Forecast",0,3);
-            lcd.printString("Alarms",0,4);
-            lcd.printString("Settings",20,5);
-            lcd.drawCircle(5,44,3,1);  // x,y,radius,black fill
-            if (button4Flag) {
-                button4Flag=0;
-            }
-            break;
-        default:
-            leds = 15;
-            break;
+                lcd.clear();
+                lcd.printString(Hour,0,0);
+                lcd.printString(Min,0,1);
+                lcd.printString(Day,0,2);
+                lcd.printString(Month,0,3);
+                lcd.printString(Year,10,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(5,36,3,1);  // x,y,radius,black fill
+                if (button3Flag) {
+                    button3Flag= 0;
+                    year--;
+                }
+                if (button4Flag) {
+                    button4Flag= 0;
+                    year++;
+                }
+                break;
+            case 6:
+
+                lcd.clear();
+                lcd.printString(Hour,0,0);
+                lcd.printString(Min,0,1);
+                lcd.printString(Day,0,2);
+                lcd.printString(Month,0,3);
+                lcd.printString(Year,0,4);
+                lcd.printString("Back      Save",0,5);
+                lcd.drawCircle(40,44,3,1);  // x,y,radius,black fill
+                if (button4Flag) {
+                    button4Flag=0;
+                    calculateUNIXTime();
+                    set_time(UNIXdate); // initialise time from the calculated UNIX time entered by the user
+                    serial.printf ("Until the given date, since 1970/01/01 %i seconds have passed.\n", UNIXdate);
+                }
+                if (button3Flag) {
+                    button3Flag=0;
+                    goto exit_loop;
+                }
+                break;
+            default:
+                leds = 15;
+                break;
+        }
     }
-
-    }
+exit_loop:
+    ;
 }
 
 
@@ -250,6 +389,12 @@
 
     choice = 1 ;
     while(1) {
+        if (choice > 5) {
+            choice = 1 ;
+        }
+        if (choice < 1) {
+            choice = 5 ;
+        }
         updateTime();
         switch (choice) {
             case 1:
@@ -263,7 +408,12 @@
                 lcd.printString("Log Interval",0,5);
                 lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill
 
-// rest of code here
+                if (button4Flag) {
+                    button4Flag=0;
+                    timeDateSetting();
+                }
+
+
                 break;
             case 2:
                 lcd.clear();
@@ -285,7 +435,10 @@
                 lcd.printString("Power Saver",0,4);
                 lcd.printString("Log Interval",0,5);
                 lcd.drawCircle(5,28,3,1);  // x,y,radius,black fill
-// rest of code here
+                if (button4Flag) {
+                    button4Flag = 0;
+                    brightnessSetting();
+                }
                 break;
             case 4:
                 lcd.clear();
@@ -296,6 +449,7 @@
                 lcd.printString("Power Saver",10,4);
                 lcd.printString("Log Interval",0,5);
                 lcd.drawCircle(5,36,3,1);  // x,y,radius,black fill
+
                 break;
             case 5:
 
@@ -309,15 +463,19 @@
                 lcd.printString("Log Interval",10,5);
                 lcd.drawCircle(5,44,3,1);  // x,y,radius,black fill
                 break;
+            case 6:
+
+                lcd.clear();
+                choice = 1;
+                break;
             default:
                 leds = 15;
                 break;
         }
-        wait (0.5);
-        if (button3Flag){
+        if (button3Flag) {
             button3Flag=0;
             break;
-            }
+        }
     }
 }
 
@@ -336,6 +494,10 @@
             lcd.printString("Alarms",0,4);
             lcd.printString("Settings",0,5);
             lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill
+            if (button4Flag) {
+                button4Flag=0;
+                updateData();
+            }
 
 // rest of code here
             break;
@@ -386,6 +548,11 @@
                 settingsMenu();
             }
             break;
+        case 6:
+
+            lcd.clear();
+            choice = 1;
+            break;
         default:
             leds = 15;
             break;
@@ -409,13 +576,19 @@
     button3.rise(&button3Pressed);
     button4.rise(&button4Pressed);
 
-    set_time(0); // initialise time to 1st January 1970///// This is where we should set our time
+    set_time(UNIXdate); // initialise time from the calculated UNIX time entered by the user
 
     while(1) {
+
+        if (choice > 5) {
+            choice = 1 ;
+        }
+        if (choice < 1) {
+            choice = 5 ;
+        }
         updateTime();
-        //updateData();
         startMenu();
-        wait (0.5);
+        wait (0.1);
     }
 }