Orla Gilson / Mbed 2 deprecated WeatherLogger

Dependencies:   BMP180 N5110 mbed

Revision:
6:1b5603fd1a9c
Parent:
5:3246c4b8362c
--- a/main.cpp	Thu May 07 16:11:21 2015 +0000
+++ b/main.cpp	Mon May 11 14:25:05 2015 +0000
@@ -1,107 +1,120 @@
+/**
+@file main.cpp
+@brief code implementation
+*/
+
+#include "main.h"
 #include "mbed.h"
 #include "BMP180.h"
 #include "N5110.h"
-
-//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
-N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
-// Can also power (VCC) directly from VOUT (3.3 V) -
-// Can give better performance due to current limitation from GPIO pin
-
-BMP180 bmp180(p28,p27);   // SDA, SCL
-
-InterruptIn buttonleft(p15);
-AnalogIn BUT2(p16);
-AnalogIn POT(p20);
-DigitalOut led(p25);
+#include "PowerControl/PowerControl.h"
+#include "PowerControl/EthernetPowerControl.h"
 
-int nx=84;
-int ny=48;
-int i,j;
-float temperature;
-float pressure;
-
-int buttonFlag=0;
-
-void clearCells();
-void menu();
-
-Timeout callT;
-Timeout callP;
-
-Ticker timerT;
-Ticker timerP;
-Ticker timerE;
-
-int timerTFlag = 0;
-int timerPFlag = 0;
-int timerEFlag = 0;
-
+/** timer associated with temperature */
 void timerTExpired(){
     timerTFlag = 1;
 }
+/** timer associated with pressure */
 void timerPExpired(){
     timerPFlag = 1;
 }
+/** timer associated with error */
 void timerEExpired(){
     timerEFlag = 1;
 }
-
+/** timer associated with light */
+void timerLExpired(){
+    timerLFlag = 1;
+}
+/** function to set flag to 1 when button pressed */
 void buttonPressed(){
     buttonFlag=1;
 }
-
+/** function called when error occurs */
 void error(){
     timerE.attach(&timerEExpired,2.0);
     while(1){
         if (timerEFlag){
             timerEFlag=0;
+            buzzer.period(0.01);
+            buzzer=0.5;
             led=1;
             wait(1.0);
             led=0;
         }
     }
 }
-    
-
+/** function to write data to file */
+void writeDataToFile(int data) {
+    leds=3; //turn on LEDs for feedback
+    FILE *fp=fopen("/local/log.txt","a"); //open log.txt for appending
+    //if the file doesn't exist it is created, if it exists, data is appended to the end
+    fprintf(fp,"Counter=%i\n",data); //print string to file
+    fclose(fp); //close file
+    leds=0; //turn off LEDs to signify file access has finished
+}
+/** function to call the temperature */
 void callTemp(){
-    Measurement measurement;
+    measurement();
     clearCells();
     lcd.printString("Menu",0,5);
     char bufferT[14];
-    measurement = bmp180.readValues();
-    int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
+    int length=sprintf(bufferT,"T = %.2f C",temperature);
     if (length<=14){
         lcd.printString(bufferT,10,2);
     }
+    if (temperature>30){
+        error();
+    }
 }
-
+/** function to call pressure */
 void callPress(){
-    Measurement measurement;
+    measurement();
     clearCells();
     lcd.printString("Menu",0,5);
     char bufferP[14];
-    measurement = bmp180.readValues();
-    int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
+    int length=sprintf(bufferP,"P = %.2f mb",pressure);
     if (length<=14){
         lcd.printString(bufferP,0,2);
     }
+    if (pressure<337){
+        error();
+    }
 }
-
+/** function to call light */
+void callLight(){
+    float light = ((1-LDR)*100);
+    clearCells();
+    lcd.printString("Menu",0,5);
+    char bufferL[14];
+    int length=sprintf(bufferL,"L = %.2f %%",light);
+    if (length<=14){
+        lcd.printString(bufferL,10,2);
+    }
+    if (light>90){
+        error();
+    }
+}
+/** function to reads temperature */
 void readTemp(){
     callT.attach(&callTemp,0.1);
     timerT.attach(&timerTExpired,60);
     while(1){
         if (timerTFlag){
             timerTFlag=0;
-            Measurement measurement;
+            measurement();
             clearCells();
             lcd.printString("Menu",0,5);
             char bufferT[14];
-            measurement = bmp180.readValues();
-            int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
+            int length=sprintf(bufferT,"T = %.2f C",temperature);
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
             if (length<=14){
                 lcd.printString(bufferT,10,2);
             }
+            if (temperature>30){
+                error();
+            }
         }
         if (buttonFlag){
             buttonFlag=0;
@@ -109,22 +122,26 @@
         }
     }
 }
-
+/** function to read pressure */
 void readPress(){
     callP.attach(&callPress,0.1);
     timerP.attach(&timerPExpired,1800);
     while(1){
         if (timerPFlag){
             timerPFlag=0;
-            Measurement measurement;
+            measurement();
             clearCells();
             lcd.printString("Menu",0,5);
             char bufferP[14];
-            measurement = bmp180.readValues();
-            int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
+            int length=sprintf(bufferP,"P = %.2f mb",pressure);
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
             if (length<=14){
                 lcd.printString(bufferP,0,2);
             }
+            if (pressure<337){
+                error();
+            }
         }
         if (buttonFlag){
             buttonFlag=0;
@@ -132,14 +149,41 @@
         }
     }
 }
-
+/** function to read light */
+void readLight(){
+    callL.attach(&callLight,0.1);
+    timerL.attach(&timerLExpired,60);
+    while(1){
+        if (timerLFlag){
+            timerLFlag=0;
+            clearCells();
+            float light = ((1-LDR)*100);
+            lcd.printString("Menu",0,5);
+            char bufferL[14];
+            int length=sprintf(bufferL,"L = %.2f %%",light);
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
+            if (length<=14){
+                lcd.printString(bufferL,10,2);
+            }
+            if (light>90){
+                error();
+            }
+        }
+        if (buttonFlag){
+            buttonFlag=0;
+            menu();
+        }
+    }
+}    
+/** function to take BMP180 meaasurement */
 void measurement(){
     Measurement measurement;
     measurement=bmp180.readValues();
     temperature=measurement.temperature;
     pressure=measurement.pressure;
 }
-
+/** function to plot temperature graph */
 void tempGraph(){
     timerT.attach(&timerTExpired,2);
     int j=0; //start graph on left hand side of screen
@@ -153,7 +197,15 @@
             tempArray[j]=(temperature/47);
             j++; //add one to j so that the next point plotted moves across the screen by one pixel
             lcd.plotArray(tempArray); //plot the array
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
             wait(0.1); //wait one second before plotting the next point
+            if (j>83){
+                j=0;
+            }
+            if (temperature>30){
+                error();
+            }
         }
         if (buttonFlag){
             buttonFlag=0;
@@ -161,7 +213,7 @@
         }
     }
 }
-
+/** function to plot pressure graph */
 void pressGraph(){
     timerP.attach(&timerPExpired,2);
     int j=0;
@@ -175,7 +227,15 @@
             pressArray[j]=(pressure/1100);
             j++;
             lcd.plotArray(pressArray);
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
             wait(0.1);
+            if (j>83){
+                j=0;
+            }
+            if (pressure<337){
+                error();
+            }
         }
         if (buttonFlag){
             buttonFlag=0;
@@ -183,7 +243,37 @@
         }     
     }
 }
-
+/** function to plot light graph */
+void lightGraph(){
+    timerL.attach(&timerLExpired,2);
+    int j=0;
+    while(1){
+        lcd.printString("Menu",0,5);
+        if (timerLFlag){
+            timerLFlag=0;
+            clearCells();
+            float lightArray[84];
+            float light = ((1-LDR)*100);
+            lightArray[j]=((light/100)+0.2);
+            j++;
+            lcd.plotArray(lightArray);
+            writeDataToFile(counter); //write current value to disk
+            counter++; //increment counter
+            wait(0.1);
+            if (j>83){
+                j=0;
+            }
+            if (light>90){
+                error();
+            }
+        }
+        if (buttonFlag){
+            buttonFlag=0;
+            menu();
+        }
+    }
+}
+/** function to display menu options */
 void menu(){
     buttonleft.rise(&buttonPressed);
     while(1) {
@@ -232,16 +322,16 @@
             if (buttonFlag) {
                 buttonFlag=0;
                 clearCells();
-                //lightGraph();
+                lightGraph();
             }
             if (BUT2>0.9) {
                 clearCells(); 
-                //readLight();
+                readLight();
             }
         }
     }
 }
-
+/** function to clear cells */
 void clearCells ()
 {
     //loop through cells and clear
@@ -252,9 +342,12 @@
     }
     lcd.refresh (); //must refresh to write buffer to display
 }
-
+ /** main function */
 int main(){
-    lcd.init();
-    bmp180.init();
-    menu();
+    while(1){
+        PHY_PowerDown(); //powers down the ethernet link
+        lcd.init();
+        bmp180.init();
+        menu();
+    }
 }
\ No newline at end of file