Display driver for the Vives city game

Dependencies:   TextLCD

Display.cpp

Committer:
atomicLogic
Date:
2017-05-08
Revision:
4:f056a09af806
Parent:
2:dd81a3e3c360
Child:
5:06eccc902247

File content as of revision 4:f056a09af806:

#include "Display.h"
#include "TextLCD.h"
#include "CostumCharacters.h"
#include <string>

Display::Display() 
{
    topBarEnable = 0;
    
    backlight = new PwmOut(PTB19);
    backlight->period(0.001f);      //frequentie pwm
    backlight->write(0.75f);        //duty cycle
    
    emptyRow = "                    ";
    
    lcd = new TextLCD( PTC5, PTC7, PTC0, PTC9, PTC8, PTC1, TextLCD::LCD20x4 );
    costumCharacters = new CostumCharacters( lcd );
    
    costumCharacters->setBatteryIcon( 0 );
    costumCharacters->setGpsIcon( 1 );          //op 0 maar uitzonderlijk op 1 voor demo
    costumCharacters->setLoraIcon( 1 );         // zelfde als hierboven
    costumCharacters->setLoraStatusIcon( 0 );
    
    lcd->cls();
    lcd->locate(0,0);
    lcd->printf("\3\4    00:00:00    \2\1");
}

void Display::topBar(bool topBarEnable)
{
    this->topBarEnable = topBarEnable;
    if(topBarEnable)
    {
        Display::setTime( NULL, NULL, NULL );
    }
}

void Display::setTime(int hours, int minutes, int seconds)
{    
    if( hours != NULL && minutes != NULL )
    {
    this->hours = hours;
    this->minutes = minutes;
    this->seconds = seconds;
    }
    
    if(topBarEnable)
    {
    char buffer[10];
    std::string time = "";
    
    
    if(hours <= 9) { time.append( "0" ); }
    std::sprintf(buffer, "%d", this->hours);
    time.append( std::string(buffer) );

    time.append( ":" );
    
    if(minutes <= 9) { time.append( "0" ); }
    std::sprintf(buffer, "%d", this->minutes);
    time.append( std::string(buffer) );
    
    time.append( ":" );

    if(seconds <= 9) { time.append( "0" ); }
    std::sprintf(buffer, "%d", this->seconds);
    time.append( std::string(buffer) );
    
    lcd->locate(0,0);
    lcd->printf("\3\4");
    lcd->locate(17,0);
    lcd->printf("\2\1");
    lcd->locate(6,0);
    lcd->printf( time.c_str());
    }
}

void Display::setStatusBattery( float batteryLvl )
{
    if ( 1 >= batteryLvl && batteryLvl >= 0 )
    {
        int lvl;
        batteryLvl = batteryLvl * 5;         //get size of replace 5
        lvl = (int) batteryLvl;       
        costumCharacters->setBatteryIcon( lvl );
    }
}

void Display::setStatusLora( bool lora, int loraStatus )
{  
    costumCharacters->setLoraIcon( lora );
    costumCharacters->setLoraStatusIcon( loraStatus );
}

void Display::setStatusGps( bool gpsFix )
{
    costumCharacters->setGpsIcon( gpsFix );
}

void Display::setStartScreen( void )
{
    Display::topBar( 0 );
    for(int i = 13; i >= 0; i--)
    {
    lcd->locate( 0, 0 );
    lcd->printf( "                    " );
    lcd->locate( i, 0 );
    lcd->printf( "Welcome" );
    wait_ms( 20 );
    }
    for(int i = 14; i >= 0; i--)
    {
    lcd->locate( 0, 1 );
    lcd->printf( "                    " );
    lcd->locate( i, 1 );
    lcd->printf( "to the" );
    wait_ms( 20 );
    }
    for(int i = 15; i >= 0; i--)
    {
    lcd->locate( 0, 2 );
    lcd->printf( "                    " );
    lcd->locate( i, 2 );
    lcd->printf( "Vives" );
    wait_ms( 20 );
    }
    for(int i = 11; i >= 0; i--)
    {
    lcd->locate( 0, 3 );
    lcd->printf( "                    " );
    lcd->locate( i, 3 );
    lcd->printf( "City Game" );
    wait_ms( 20 );
    }
    lcd->locate( 19, 3 );
    lcd->putc( (char) 126 );
}

void Display::setMissionScreen( std::string description, int distance, int radius )
{
        
    Display::clearScreen();
    lcd->locate( 0, 1 );
    lcd->printf( description.c_str() );
    
    char buffer[10];
    std::string distanceString = "";
    distanceString.append( "Distance: " );
    std::sprintf(buffer, "%d", distance);
    distanceString.append( std::string(buffer) );
    distanceString.append( "m" );
    
    lcd->locate( 0, 3 );
    lcd->printf( distanceString.c_str() );
}

void Display::setEndScreen( void )
{
    Display::clearScreen();
    
    for(int i = 0; i < 7; i++)
    {
    lcd->locate( 2, 0 );
    lcd->printf( "Congratulations!" );
    wait_ms( 120 );
    Display::clearScreen();
    wait_ms( 100 );
    }
    
    lcd->locate( 2, 0 );
    lcd->printf( "Congratulations!" );
    lcd->locate( 3, 1 );
    lcd->printf( "You completed" );
    lcd->locate( 6, 2 );
    lcd->printf( "all the" );
    lcd->locate( 5, 3 );
    lcd->printf( "missions!" );
    
}

void Display::clearScreen( void )
{
    if( topBarEnable )
    {
        lcd->locate(0,1);
        lcd->printf( emptyRow.c_str() );
        lcd->locate(0,2);
        lcd->printf( emptyRow.c_str() );
        lcd->locate(0,3);
        lcd->printf( emptyRow.c_str());
    }
    else
    {
        lcd->cls();
    }
}

void Display::setMessageScreen( std::string message )
{
    Display::clearScreen();
    lcd->locate(0,0);
    lcd->printf( message.c_str() );
}