Amir Asemanpayeh / Mbed 2 deprecated WeatherStation

Dependencies:   BMP180 N5110 PowerControl mbed

main.cpp

Committer:
amiraseman
Date:
2015-04-09
Revision:
2:1d7ef0f49b35
Parent:
1:e60b9f116d38
Child:
3:8a559b949470

File content as of revision 2:1d7ef0f49b35:

#include "mbed.h"
#include "N5110.h"
#include "BMP180.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"

BMP180 bmp180(p28,p27);   // SDA, SCL
N5110 lcd(p7,p8,p9,p10,p11,p13,p26);
BusOut leds(LED4,LED3,LED2,LED1);
Serial serial(USBTX,USBRX);
InterruptIn button1(p16);
InterruptIn button2(p17);
InterruptIn button3 (p15);
InterruptIn button4 (p18);


Ticker timer; //Create ticker object for temperature and pressure readings
Ticker timer1; //Create ticker object for real time clock


int setTimeFlag = 0; // flag for real time clock ISR
int timerFlag = 0; // Flag for tempesrature pressure reading ISR
int button1Flag = 0;
int button2Flag = 0 ;
int button3Flag = 0 ;
int button4Flag = 0 ;



int choice=1 ;
int length;


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)
// 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



void button1Pressed()  //ISR to subtract 1 from the value of choice when the first button is pressed
{
    choice --;
    if (choice == 0) {
        choice = 5 ;
    }

}



void button2Pressed() //ISR to add 1 to the value of the choice when the second button is pressed
{
    choice ++;
    if (choice == 6) {
        choice = 1 ;
    }

}



void button3Pressed()
{
    button3Flag=1;
}



void button4Pressed()
{
    button4Flag = 1 ;
}





void timer1Expired()
{
// set flag
    setTimeFlag = 1;
}




void setTime() // sets the time every second
{

    if (setTimeFlag) {
        setTimeFlag=0;
        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));
    }
}




void timerExpired() // ISR which sets the timer flag (flag used to display the buffers on LCD)
{
    timerFlag=1;
}



void readData() //Imports the data and saves them to the bufferss
{

    Measurement measurement;  // measurement structure declared in BMP180 class

    // read values (T in Celsius and P in mb) and print over serial port
    measurement = bmp180.readValues();
    // serial.printf("T = %.2f C P = %.2f mb\n",measurement.temperature,measurement.pressure);

    int temperature = measurement.temperature;
    length = sprintf(bufferT,"T = %0.1d C",temperature); // print formatted data to buffer
    // it is important the format specifier ensures the length will fit in the buffer

    float pressure = measurement.pressure;  // same idea with floats
    length = sprintf(bufferP,"P = %.2f mb",pressure);


}




void displayData() //Prints the buffers on the LCD
{
    if (length <= 14) { // if string will fit on display
        lcd.printString(bufferT,0,1);           // display on screen
        lcd.printString(bufferP,0,2);
        lcd.printString(bufferTime,0,3);
        lcd.printString(bufferDate,0,4);
    }
}




void updateData() //
{
    if (timerFlag) {
        timerFlag=0;
        readData();
        displayData();
    }
    Sleep();
}




void settingsMenu()
{


    choice = 1 ;
    while(1) {
        setTime();
        switch (choice) {
            case 1:

                lcd.clear();
                lcd.printString(bufferTime,48,0);
                lcd.printString("Time/Date",10,1);
                lcd.printString("Units",0,2);
                lcd.printString("Brightness",0,3);
                lcd.printString("Power Saver",0,4);
                lcd.printString("Log Interval",0,5);
                lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill

// rest of code here
                break;
            case 2:
                lcd.clear();
                lcd.printString(bufferTime,48,0);
                lcd.printString("Time/Date",0,1);
                lcd.printString("Units",10,2);
                lcd.printString("Brightness",0,3);
                lcd.printString("Power Saver",0,4);
                lcd.printString("Log Interval",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("Time/Date",0,1);
                lcd.printString("Units",0,2);
                lcd.printString("Brightness",10,3);
                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
                break;
            case 4:
                lcd.clear();
                lcd.printString(bufferTime,48,0);
                lcd.printString("Time/Date",0,1);
                lcd.printString("Units",0,2);
                lcd.printString("Brightness",0,3);
                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:

                lcd.clear();

                lcd.printString(bufferTime,48,0);
                lcd.printString("Time/Date",0,1);
                lcd.printString("Units",0,2);
                lcd.printString("Brightness",0,3);
                lcd.printString("Power Saver",0,4);
                lcd.printString("Log Interval",10,5);
                lcd.drawCircle(5,44,3,1);  // x,y,radius,black fill
                break;
            default:
                leds = 15;
                break;
        }
        wait (0.5);
        if (button3Flag){
            button3Flag=0;
            break;
            }
    }
}


void startMenu()  //The menu displayed at the beginning
{

    switch (choice) {
        case 1:

            lcd.clear();
            lcd.printString(bufferTime,48,0);
            lcd.printString("Live Data",20,1);
            lcd.printString("Saved Data",0,2);
            lcd.printString("Forecast",0,3);
            lcd.printString("Alarms",0,4);
            lcd.printString("Settings",0,5);
            lcd.drawCircle(5,12,3,1);  // x,y,radius,black fill

// 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:

            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;
                settingsMenu();
            }
            break;
        default:
            leds = 15;
            break;
    }
}





int main()
{

    // initiliase barometer
    bmp180.init();
    lcd.init();
    timer.attach(&timerExpired,1.0);
    timer1.attach(&timer1Expired,1.0);
    button1.rise(&button1Pressed); // call ISR on rising edge (button1pressed)
    button2.rise(&button2Pressed); // call ISR on rising edge (button2pressed)
    button3.rise(&button3Pressed);
    button4.rise(&button4Pressed);

    set_time(0); // initialise time to 1st January 1970///// This is where we should set our time

    while(1) {
        setTime();
        //updateData();
        startMenu();
        wait (0.5);
    }
}