Time: 17:33 Date: 10/12/2017 Description: Task 1,7,8 Currently Functioning

Dependencies:   BME280 BMP280 TextLCD

Working Repository

LCD.cpp

Committer:
thomasmorris
Date:
2018-01-08
Revision:
47:6d128e500875
Parent:
20:cbb71f84cff9
Child:
48:244d6d81bb52

File content as of revision 47:6d128e500875:

#include "mbed.h"           //Include the mbed libraries
#include "LCD.hpp"          //Include the header file, this acts like a series of forward declarations
 
//Constructor
LCD::LCD(PinName E,PinName RS,PinName RW,PinName DB0,PinName DB1,PinName DB2,PinName DB3,PinName DB4,PinName DB5,PinName DB6,PinName DB7) : _E(E),_RS(RS),_RW(RW),_DB0(DB0),_DB1(DB1),_DB2(DB2),_DB3(DB3),_DB4(DB4),_DB5(DB5),_DB6(DB6),_DB7(DB7)
{}

LCD::~LCD(){}   //Destructor

void LCD::clock_in(){_E = 1; wait(0.001); _E = 0; wait(0.001); _E = 1;}

void LCD::Function_Set()     {_RS = 0; _RW = 0; _DB7 = 0; _DB6 = 0; _DB5 = 1; _DB4 = 1; _DB3 = 1; _DB2 = 1; _DB1 = 0; _DB0 = 0; clock_in();}    //DB4 sets Data Width. DB3 sets No. Lines. DB2 sets font.
void LCD::Display_Off()      {_RS = 0; _RW = 0; _DB7 = 0; _DB6 = 0; _DB5 = 0; _DB4 = 0; _DB3 = 1; _DB2 = 0; _DB1 = 0; _DB0 = 0; clock_in();}    //Turns the display off.
void LCD::Display_Clear()    {_RS = 0; _RW = 0; _DB7 = 0; _DB6 = 0; _DB5 = 0; _DB4 = 0; _DB3 = 0; _DB2 = 0; _DB1 = 0; _DB0 = 1; clock_in();}    //Clears display sets position to 0.
void LCD::Entry_Mode_Set()   {_RS = 0; _RW = 0; _DB7 = 0; _DB6 = 0; _DB5 = 0; _DB4 = 0; _DB3 = 0; _DB2 = 1; _DB1 = 1; _DB0 = 0; clock_in();}    //DB1 sets increment(1) vs decrement(0). DB0 sets display shift(1) vs cursor shift(0).
void LCD::Display_On()       {_RS = 0; _RW = 0; _DB7 = 0; _DB6 = 0; _DB5 = 0; _DB4 = 0; _DB3 = 1; _DB2 = 1; _DB1 = 1; _DB0 = 1; clock_in();}    //DB1 sets cursor on(1) vs off(0). DB0 sets blink(1) vs not(0).

void LCD::Initialise(){                         //Use previous functions to initialise LCD
        Function_Set();     wait(0.020);        //Function set followed by wait.
        Display_Off();      wait(0.020);        //Display off followed by wait.
        Display_Clear();    wait(0.020);        //Display clear followed by wait.
        Entry_Mode_Set();   wait(0.020);        //Entry Mode Set followed by wait.
        Display_On();       wait(0.020);        //Turn the display on.
        DDRAM_Address(0);   wait(0.020);        //Set the address to 0.
}

void LCD::DDRAM_Address(int Address){           //Sets the address to integer address
    _RS = 0;
    _RW = 0; 
    _DB7 = 1; 
    _DB6 = (Address & 0b1000000) >> 6; _DB5 = (Address & 0b0100000) >> 5; _DB4 = (Address & 0b0010000) >> 4;                                        //Use bit shifting to convert integer to binary.
    _DB3 = (Address & 0b0001000) >> 3; _DB2 = (Address & 0b0000100) >> 2; _DB1 = (Address & 0b0000010) >> 1; _DB0 = (Address & 0b0000001) >> 0;     //Use bit shifting to convert integer to binary.
    clock_in();     //Clock the data into the LCD.
    }

void LCD::Write_String(string Word){            //Writes the string Word to the LCD starting at the current address
    int ASCII_Converted;                        //Temporary values
    for (int i = 0; i < Word.length(); i++)     //For loop running between 0 and length of string Word
    {
        ASCII_Converted = Word.at(i);           //Set ASCII_Converted to current character value
        _RS = 1; _RW = 0;                       //Set RS and RW
        _DB7 = (ASCII_Converted & 0b10000000) >> 7; _DB6 = (ASCII_Converted & 0b01000000) >> 6; _DB5 = (ASCII_Converted & 0b00100000) >> 5; _DB4 = (ASCII_Converted & 0b00010000) >> 4; //Use bit shifting to convert hex to binary.
        _DB3 = (ASCII_Converted & 0b00001000) >> 3; _DB2 = (ASCII_Converted & 0b00000100) >> 2; _DB1 = (ASCII_Converted & 0b00000010) >> 1; _DB0 = (ASCII_Converted & 0b00000001) >> 0; //Use bit shifting to convert hex to binary.
        clock_in(); //Clock the data into the LCD.
    }      
}




void LCD_Write_Year()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Year Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Years = 1900 + Time_Pointer->tm_year;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Year Time lock released\n");}
        stringstream ss;
        ss << Years;
        string Year_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Year");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Year_String);   
}
void LCD_Write_Month()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Month Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Months = 1 + Time_Pointer->tm_mon;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Month Time lock released\n");}
        stringstream ss;
        ss << Months;
        string Month_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Month");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Month_String);   
}
void LCD_Write_Day()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Day Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Days = Time_Pointer->tm_mday;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Day Time lock released\n");}
        stringstream ss;
        ss << Days;
        string Day_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Day");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Day_String);   
}
void LCD_Write_Hour()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Hour Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Hours = Time_Pointer->tm_hour;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Hour Time lock released\n");}
        stringstream ss;
        ss << Hours;
        string Hour_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Hour");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Hour_String);   
}
void LCD_Write_Minute()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Minute Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Minutes = Time_Pointer->tm_min;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Minute Time lock released\n");}
        stringstream ss;
        ss << Minutes;
        string Minute_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Minute");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Minute_String);   
}
void LCD_Write_Seconds()
{
        Time_Lock_Main.lock();//Appling lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Seconds Time lock taken\n");}
        time_t Time = time(NULL);
        tm* Time_Pointer = localtime(&Time);
        int Seconds = Time_Pointer->tm_sec;
        Time_Lock_Main.unlock();//Releasing lock for critial section
        if(Log_Value == 1){pc.printf("In LCD_Write_Seconds Time lock released\n");}
        stringstream ss;
        ss << Seconds;
        string Second_String = ss.str();
        LCD.DDRAM_Address(0x00);
        LCD.Write_String("Set Second");
        LCD.DDRAM_Address(0x40);
        LCD.Write_String(Second_String);   
}


void LCD_Print_Output()
{      

        if(mode == 0)//Default mode
        { 
            if(Log_Value == 1){pc.printf("In mode 0 \n");}
            
            Thread::wait(Default_Mode_Toggle_Time);//Wait for this amount of time
            if(Log_Value == 1){pc.printf("Writing Data to LCD\n");}
            sprintf (LCD_buffer, "%1.1f  %1.0f  %1.2f",Data_Active.get_temperature(),Data_Active.get_pressure(),Data_Active.get_light());//Used for converting to a sting  
             
            LCD.DDRAM_Address(0x00);
            LCD.Write_String("Temp  Pres  Lite");
            LCD.DDRAM_Address(0x40);
            LCD.Write_String(LCD_buffer);//Print out current data values to the LCD

            Thread::wait(Default_Mode_Toggle_Time); //Wait for this amount of time   
            if(Log_Value == 1){pc.printf("Writing Time and Date to LCD\n");}
            Time_Lock_Main.lock();//lock Time_lock for critial section
            time_t Time = Data_Active.get_time();
            tm* Time_Pointer = localtime(&Time);
            LCD.Display_Clear();
            sprintf (LCD_buffer, "%02d:%02d    %02d,%d",Time_Pointer->tm_hour,Time_Pointer->tm_min,(Time_Pointer->tm_mon+1),(Time_Pointer->tm_year+1900));//Used for converting to a sting  
            Time_Lock_Main.unlock();//unlock Time_lock for critial section
            
            LCD.DDRAM_Address(0x00);
            LCD.Write_String("Current Time:");
            LCD.DDRAM_Address(0x40);
            LCD.Write_String(LCD_buffer);//Print the current time to the LCD
            
            if(Log_Value == 1){pc.printf("Checking Switches for next mode\n");}
            if(SW1.read() & SW2.read() == 1)
            {
                mode = 1;   
            }
        }
        else if(mode == 1)//Choose either date setting or time setting
        {
            if(Log_Value == 1){pc.printf("In Mode 1\n");}
            LCD.Display_Clear();
            while(1)
            {
                LCD.DDRAM_Address(0x00);
                LCD.Write_String("Date        Time");
                LCD.DDRAM_Address(0x40);
                LCD.Write_String("<              >");
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 to go to Date setting\n");}
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    mode = 2;//Date Setting
                    break;   
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to go to Time setting\n");}
                if(SW2.read() == 1 & SW1.read() == 0)
                {
                    mode = 5;//Time Setting
                    break;
                }
            }
        } 
        else if(mode == 2)//Set the Year
        {
            if(Log_Value == 1){pc.printf("In Mode 2\n");}
            LCD.Display_Clear();
            while(1)
            {          
                LCD_Write_Year();
                Thread::wait(1000);
                 if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go to Month setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 3;
                    break;   
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to add Year\n");}
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Year();
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Year\n");}
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Year();   
                }
                LCD_Write_Year();
            }
        } 
        else if(mode == 3)//Set the Month
        {
            if(Log_Value == 1){pc.printf("In Mode 3\n");}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Month();
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go to Day setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 4;
                    break;   
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to add Month\n");}
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Month();
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Month\n");}
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Month();   
                }                

            }
        }
        else if(mode == 4)//Set the Day
        {
            if(Log_Value == 1){pc.printf("In Mode 4\n");}
            LCD.Display_Clear();
            while(1)
            {      
                LCD_Write_Day();
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Default setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 0;   
                    break;
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to add Day\n");}
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Day();
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Day\n");}
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Day();   
                }                
            }
        } 
        else if(mode == 5)//Set the Hour
        {
            if(Log_Value == 1){pc.printf("In Mode 5\n");}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Hour();
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Minute setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 6;  
                    break; 
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to add Hour\n");}                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Hour();
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Hour\n");} 
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Hour();   
                }
            }
        } 
        else if(mode == 6)//Set the Minute
        {
            if(Log_Value == 1){pc.printf("In Mode 6\n");}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Minute();
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Seconds setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 7;  
                    break; 
                }                
                if(Log_Value == 1){pc.printf("Checking SW1 to add Minute\n");}
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Minute();
                }
                if(Log_Value == 1){pc.printf("Checking SW2 to subtract Minute\n");}
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Minute();   
                }                
            }
        }  
        else if(mode == 7)//Set the Seconds
        {
            if(Log_Value == 1){pc.printf("In Mode 7\n");}
            LCD.Display_Clear();
            while(1)
            {
                LCD_Write_Seconds();
                Thread::wait(1000);
                if(Log_Value == 1){pc.printf("Checking SW1 and SW2 to go Default setting\n");}
                if(SW1.read() & SW2.read() == 1)
                {
                    mode = 0;  
                    break; 
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to add Second\n");}                
                if(SW1.read() == 1 & SW2.read() == 0)
                {
                    Add_Second();
                }
                if(Log_Value == 1){pc.printf("Checking SW1 to subtract Second\n");}   
                else if(SW2.read() == 1 & SW1.read() == 0)
                {
                    Subtract_Second();   
                }                
            }
        }
        else
        {
            if(Log_Value == 1){pc.printf("Mode Error occured mode now set to 0\n");}   
            mode = 0;
        }
}