Volkan Esendag / Mbed 2 deprecated mbed_PortableWeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

main.cpp

Committer:
Volcano_498
Date:
2015-04-11
Revision:
1:454dddb8adc2
Parent:
0:b30f86a9a1c5
Child:
2:08f2469728d5

File content as of revision 1:454dddb8adc2:

/**
@file main.cpp
@brief Code for a Portable Weather Station to be built on PCB.
@brief Design Specs: *A small, portable battery-powered data logger that records sensor data at regular intervals.
@brief -- Minimises mbed power consumption to the greatest extent possible.
@brief -- Displays the information to the user.
@brief -- Features a BMP180 pressure and temperature sensor. (TMP102 is unnecessary as BMP180 also takes temp. readings)
@brief -- Features a Nokia 5110 display to print the sensor readings over and display them to the user.     
@brief -- Sensor information is recorded on Flash memory on the mbed.
@brief -- Graph to display data to the user.
@brief -- mbed is powered by a Buck regulator (5V output) that is in turn supplied by a 9V PP3 battery which powers the PCB.
@brief -- Audible and visual alert when a threshold reading is obtained.
@brief -- Celsius-to-Fahrenheit and Celsius-to-Kelvin conversions of temperature readings and displaying them.
@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) / 09 April 2015(last modified)
*/

#include "mbed.h"
#include "N5110.h"
#include "BMP180.h"
//import the PowerControl/EthernetPowerControl header files from the Power Control library
//to enable the power down features for the microprocessor and the Ethernet feature.
#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.

/**
@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. Each character is a byte long - hence the char pointer type.

int setTimeFlag = 0; /*!< set time flag set in serial ISR */

/**
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
    serial.printf("set_time - %s",rxString);
// atoi() converts a string to an integer
    int time = atoi(rxString);
// update the time
    set_time(time);
}

void serialISR()
{
// when a serial interrupt occurs, read rx string into buffer. Holds 16 characters. gets implies fetching a string from serial port.
    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();
    
    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 = bmp180.readValues();
        
            Sleep();  //put the mbed to sleep once the program flow has been completed.
        
        } //close while
    } //close if statement
} //terminate main()