Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

Revision:
1:454dddb8adc2
Parent:
0:b30f86a9a1c5
Child:
2:08f2469728d5
--- a/main.cpp	Wed Apr 08 15:27:46 2015 +0000
+++ b/main.cpp	Sat Apr 11 13:32:58 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) / 06 April 2015(last modified)
+@date 11 March 2015 (created) / 09 April 2015(last modified)
 */
 
 #include "mbed.h"
@@ -25,23 +25,109 @@
 #include "PowerControl/PowerControl.h"
 #include "PowerControl/EthernetPowerControl.h"
 
+#ifndef USR_POWERDOWN
 #define USR_POWERDOWN (0x104)    //defines USB interface powerdown.
+#endif
+
+/**
+@namespace bmp180
+@brief A special structed type of I2C object created for the BMP180.
+@brief For more info, see the BMP180 library by Craig Evans.
+@see http://developer.mbed.org/users/eencae/code/BMP180/
+*/
+BMP180 bmp180(p28,p27);
+//Pins are declared in the public domain and the sensor itself acts on private variables.
 
-BMP180 bmp180(p28,p27);  //a special structed type of I2C object created for the BMP180.
-//Pins are declared in the public domain and the sensor itself acts on private variables.
-//For more info, see the BMP180 library.
+/**
+@namespace buzzerPwm
+@brief PwmOut object to apply a PWM signal with a duty ratio of 50% to the buzzer as an improvised square wave.
+@brief Used for an audible feedback should the temperature reading exceed a certain value.
+*/
+PwmOut buzzerPwm(p24);
 
+/**
+@namespace redLED
+@brief PwmOut object to apply a PWM signal to the red visual feedback LED via pin 22.
+@brief Used for visual feedback to tell the user a certain temperature threshold has been reached.
+*/
+PwmOut redLED(p22);
+
+/**
+@namespace greenLED
+@brief PwmOut object to apply a PWM signal to the green visual feedback LED via pin 23.
+@brief Used to let the user know the device is operating normally.
+*/
+PwmOut greenLED(p23);
+
+/**
+@namespace serial
+@brief Serial object to print readings over a USB cable and display them on a terminal.
+*/
 Serial serial(USBTX,USBRX); //serial object to print readings for debugging WHILE the USB cable is connected.
 
+/**
+@namespace menuButton
+@brief Interrupt object to call ISR for the designated menu button when an input to p15 is applied.
+@namespace buttonOne
+@brief Interrupt object to call ISR for Button 1 when an input to p16 is applied.
+@namespace buttonTwo
+@brief Interrupt object to call ISR for Button 2 when an input to p17 is applied.
+@namespace buttonThree
+@brief Interrupt object to call ISR for Button 3 when an input to p18 is applied.
+*/
 
+InterruptIn menuButton(p15);  //Interrupt object for the designated menu button.
+InterruptIn buttonOne(p16);   //Interrupt objects for the other buttons.
+InterruptIn buttonTwo(p17);
+InterruptIn buttonThree(p18);
 
+/**
+@namespace potAin
+@brief Analogue input from potentiometer whose Vout is connected to pin 20.
+*/
+
+AnalogIn potAin(p20); //Potentiometer feedback pin to the mbed.
+
+/**
+@namespace logTimer
+@brief Ticker object to record readings with a specified interval. Can be varied with Interrupt Service Routines.
+*/
+
+Ticker logTimer;   //Ticker object to call ISR for a specified period of time.
+
+/*
+@namespace leds
+@brief GPIO output for status LEDs - used to display error message or as a flash memory overwrite operation feedback.
+*/
+
+BusOut leds(LED1,LED2,LED3,LED4);  //BusOut object for error feedback LEDs.
+//configure pins of the LCD display...
+
+/**
+@namespace splashFlip
+@brief Calls an ISR only once when attached. Used to delay splash screen for a few seconds before commencing with the program flow.
+*/
+Timeout splashFlip;
+
+/**
+@namespace lcd
+@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/
+*/
+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.*************
 
-char rxString[16]; // buffer to store received string
+char rxString[16]; // buffer to store received string. Each character is a byte long - hence the char pointer type.
+
+int setTimeFlag = 0; /*!< set time flag set in serial ISR */
 
-int setTimeFlag = 0;
+/**
+Reads string input via serial interrupt and converts it into real time
+@param rxString - string received from serial
+@param time - integer that represents time, converted from input string (rxString).
 
+*/
 void setTime()
 {
 // print time for debugging
@@ -55,22 +141,125 @@
 void serialISR()
 {
 // when a serial interrupt occurs, read rx string into buffer. Holds 16 characters. gets implies fetching a string from serial port.
-    pc.gets(rxString,16);
+    serial.gets(rxString,16);
 // set flag
     setTimeFlag = 1;
 }
 
+/**
+Disables USB interface when the mbed's USB cable isn't attached.
+@param arg - argument function (pointer type int/uint32_t)
+@returns __semihost(USR_POWERDOWN, &arg)
+*/
+
+int semihost_powerdown(){
+    
+    uint32_t arg;  //variable for return function
+    return __semihost(USR_POWERDOWN, &arg);    //return semihost state...
+    
+} //...to power down the USB interface when the USB cable is detached.
+
+LocalFileSystem local("local"); // create local filesystem
+
+void writeDataToFile(float data, float dataTwo, char dataThree[30])
+{
+    time_t seconds = time(NULL); // get current time
+    strftime(dataThree, 30 , "%R %x", localtime(&seconds)); //convert it into a string, from an array size of 30.
+                
+    leds = 15; // turn on LEDs for feedback
+    FILE *fp = fopen("/local/tempLog.csv", "a"); // open 'log.txt' for appending. Instance of class FILE.
+// if the file doesn't exist it is created, if it exists, data is appended to the end
+    fprintf(fp,"%s, %.2f , %.2f \n",dataThree,dataTwo,data); // print string to file 
+    fclose(fp); // close file
+    leds = 0; // turn off LEDs to signify file access has finished
+}
+
+int menuButtonFlag = 0; /*!< menu button flag set in ISR */
+
+void menuButtonPressed(){
+    menuButtonFlag = !menuButtonFlag; //if set to 1, set it back to 0. If not set, set the flag.
+}
+
+int buttonOneFlag = 0; /*!< Button One flag set in ISR */
+int buttonOneAltFlag = 0;  /*!< Button One Alternate flag set in ISR */
+
+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
+        buttonOneAltFlag = !buttonOneAltFlag;  //set/reset-if-set alternate flag and proceed to next menu
+    }
+    else{
+        buttonOneFlag = !buttonOneFlag;  //set/reset-if-set flag if not navigated to a menu
+    }
+}
+
+int buttonTwoFlag = 0; /*!< Button Two flag set in ISR */
+int buttonTwoAltFlag = 0; /*!< Button Two Alternate flag set in ISR */
+
+void buttonTwoPressed(){
+    if(menuButtonFlag){
+        menuButtonFlag = 0;
+        buttonTwoAltFlag = !buttonTwoAltFlag;      
+    }
+    else{
+        buttonTwoFlag = !buttonTwoFlag;
+    }    
+}
+
+int buttonThreeFlag = 0; /*!< Button Three flag set in ISR */
+int buttonThreeAltFlag = 0; /*!< Button Three Alternate flag set in ISR */
+
+void buttonThreePressed(){
+    if(menuButtonFlag){
+        menuButtonFlag = 0;
+        buttonThreeAltFlag = !buttonThreeAltFlag;      
+    }
+    else
+        buttonThreeFlag = !buttonThreeFlag;
+}
+
+int splashFlag = 1;  /*!< Splash flag set to continue with program flow for the main function before proceeding to while(1) */
+
+void splashDelay(){
+    splashFlag = 0;
+}
+
+void displayInitSplash(){ //display splash screen
+    lcd.printString("Welcome to",15,1);
+    lcd.printString("Portable Weat.",3,2);
+    lcd.printString("Station",6,3);
+    lcd.printString("by Volkan",20,4);
+    lcd.printString("Esendag",20,5);
+}        
 
 int main() {
     
+    splashFlip.attach(&splashDelay,3.0); //attach timer and wait for ISR to be called
+    
+    // first need to initialise display
+    lcd.init();
+    
+    displayInitSplash();
+    
     //initialise barometer
     bmp180.init();
     
-    Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class    
-    while(1) {
+    PHY_PowerDown();    //powers down the Ethernet feature.
+
+    int result = semihost_powerdown();   //as a result, power down the USB connection
+    //unless the cable is connected.
+    
+    Measurement measurement; // object created for pressure & temperature using the structure declared in BMP180 class
+    
+    if(splashFlag == 0){ //if ISR has been called proceed with program flow
+        splashFlip.detach();    //detach Timeout object
+        while(1) {
         
-        //read values (T in degrees Celsius and P in mb).
-        measurement = bmp.readValues();
+            //read values (T in degrees Celsius and P in mb).
+            measurement = bmp180.readValues();
         
-    }
-}
+            Sleep();  //put the mbed to sleep once the program flow has been completed.
+        
+        } //close while
+    } //close if statement
+} //terminate main()