Orla Gilson / Mbed 2 deprecated WeatherLogger

Dependencies:   BMP180 N5110 mbed

main.cpp

Committer:
orlagilson
Date:
2015-04-30
Revision:
3:c9162dc9ba24
Parent:
2:6b564e388747
Child:
4:0302731434a5

File content as of revision 3:c9162dc9ba24:

#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
Serial serial(USBTX,USBRX);

AnalogIn BUT1(p15);
AnalogIn BUT2(p16);
AnalogIn POT(p20);

int nx=84;
int ny=48;
int i,j;
float temperature;
float pressure;

void clearCells();

Timeout callT;
Timeout callP;

Ticker timerT;
Ticker timerP;

int timerTFlag = 0;
int timerPFlag = 0;

void timerTExpired(){
    timerTFlag = 1;
}
void timerPExpired(){
    timerPFlag = 1;
}

void callTemp(){
    Measurement measurement;
    clearCells();
    lcd.printString("Menu",0,5);
    char bufferT[14];
    measurement = bmp180.readValues();
    int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
    if (length<=14){
        lcd.printString(bufferT,10,2);
    }
}

void callPress(){
    Measurement measurement;
    clearCells();
    lcd.printString("Menu",0,5);
    char bufferP[14];
    measurement = bmp180.readValues();
    int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
    if (length<=14){
        lcd.printString(bufferP,0,2);
    }
}

void readTemp(){
    callT.attach(&callTemp,0.1);
    while(1){
        if (timerTFlag){
            timerTFlag=0;
            Measurement measurement;
            clearCells();
            lcd.printString("Menu",0,5);
            char bufferT[14];
            measurement = bmp180.readValues();
            int length=sprintf(bufferT,"T = %.2f C",measurement.temperature);
            if (length<=14){
                lcd.printString(bufferT,10,2);
            }
        }
    }
}

void readPress(){
    callP.attach(&callPress,0.1);
    while(1){
        if (timerPFlag){
            timerPFlag=0;
            Measurement measurement;
            clearCells();
            lcd.printString("Menu",0,5);
            char bufferP[14];
            measurement = bmp180.readValues();
            int length=sprintf(bufferP,"P = %.2f mb",measurement.pressure);
            if (length<=14){
                lcd.printString(bufferP,0,2);
            }
        }
    }
}

void measurement(){
    Measurement measurement;
    measurement=bmp180.readValues();
    temperature=measurement.temperature;
    pressure=measurement.pressure;
}

void tempGraph(){
    int j=0; //start graph on left hand side of screen
    while(1){
        lcd.printString("Menu",0,5);
        if (timerTFlag){
            timerTFlag=0;
            clearCells();
            float tempArray[84]; //create array of temperature values
            measurement(); //read in the measured values of temperature
            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
            wait(0.1); //wait one second before plotting the next point
        }
    }
}

void pressGraph(){
    int j=0;
    while(1){
        lcd.printString("Menu",0,5);
        if (timerPFlag){
            timerPFlag=0;
            clearCells();
            float pressArray[84];
            measurement();
            pressArray[j]=(pressure/1100);
            j++;
            lcd.plotArray(pressArray);
        }     
    }
}

void menu()
{
    while(1) {
        wait (0.1);
        lcd.normalMode(); //normal LCD colour mode
        lcd.setBrightness(0.5); //LCD backlight set to 50% brightness
        if (POT>(2.0/3.0)) {
            clearCells();
            lcd.printString("Temperature",10,1);
            lcd.printString(">",80,2);
            lcd.printString("Graph",0,5);
            lcd.printString("Current",43,5);
            if (BUT1>0.9) { //left button takes the user to the graph option
                clearCells();
                tempGraph();
            }
            if (BUT2>0.9) { //right button takes the user to the current reading
                clearCells();
                readTemp();
            }
        }
        if ((POT>1.0/3.0)&&(POT<2.0/3.0)) {
            clearCells();
            lcd.printString("Pressure",20,1);
            lcd.printString("<",0,2);
            lcd.printString(">",80,2);
            lcd.printString("Graph",0,5);
            lcd.printString("Current",43,5);
            if (BUT1>0.9) {
                clearCells();
                pressGraph();
            }
            if (BUT2>0.9) {
                clearCells();
                readPress();
            }
        }
        if (POT<(1.0/3.0)) {
            clearCells();
            lcd.printString("Light",30,1);
            lcd.printString("<",0,2);
            lcd.printString("Graph",0,5);
            lcd.printString("Current",43,5);
            if (BUT1>0.9) {
                clearCells();
                //lightGraph();
            }
            if (BUT2>0.9) {
                clearCells(); 
                //readLight();
            }
        }
    }
}

void clearCells ()
{
    //loop through cells and clear
    for (int i=0; i<nx; i++) {
        for (int j=0; j<ny; j++) {
            lcd.clearPixel(i,j);
        }
    }
    lcd.refresh (); //must refresh to write buffer to display
}

int main(){
    lcd.init();
    bmp180.init();
    timerT.attach(&timerTExpired,60);
    timerP.attach(&timerPExpired,1800);
    menu();
}