Michel Dequick / DisplayDriver

Dependencies:   TextLCD

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Display.cpp Source File

Display.cpp

00001 #include "Display.h"
00002 #include "TextLCD.h"
00003 #include "CostumCharacters.h"
00004 #include <string>
00005 #include <sstream>
00006 
00007 Display::Display() 
00008 {
00009     topBarEnable = 0;
00010     
00011     hours = 0;
00012     minutes = 0;
00013     seconds = 0;
00014     distance = 0;
00015     radius = 0;
00016     position = 0;
00017     description = "";
00018     
00019     status = 0;
00020     
00021     backlight = new PwmOut(PTB19);
00022     backlight->period(0.001f);      //frequentie pwm
00023     backlight->write(0.75f);        //duty cycle
00024     
00025     emptyRow = "                    ";
00026     
00027     lcd = new TextLCD( PTC5, PTC7, PTC0, PTC9, PTC8, PTC1, TextLCD::LCD20x4 );
00028     costumCharacters = new CostumCharacters( lcd );
00029     
00030     costumCharacters->setBatteryIcon( 0 );
00031     costumCharacters->setGpsIcon( 0 );
00032     costumCharacters->setLoraIcon( 1 );
00033     costumCharacters->setLoraStatusIcon( 0 );
00034     
00035     lcd->cls();
00036     lcd->locate(0,0);
00037 }
00038 
00039 void Display::topBar(bool topBarEnable)
00040 {
00041     if(topBarEnable)
00042     {
00043         Display::topBarRedraw();
00044     }
00045     
00046     if( this->topBarEnable == 1 && topBarEnable == 0 )
00047     {
00048         lcd->locate(0,0);
00049         lcd->printf( emptyRow.c_str() );
00050     }
00051     this->topBarEnable = topBarEnable;
00052 }
00053 
00054 void Display::setTime(int hours, int minutes, int seconds)
00055 {    
00056     this->hours = hours;
00057     this->minutes = minutes;
00058     this->seconds = seconds;
00059     Display::topBarRedraw();
00060 }
00061     
00062 void Display::topBarRedraw( void )
00063 { 
00064     if(topBarEnable)
00065     {
00066 
00067     char buffer[10];
00068     std::string time = "";
00069     
00070     if(this->hours <= 9) { time.append( "0" ); }
00071     std::sprintf(buffer, "%d", this->hours);
00072     time.append( std::string(buffer) );
00073 
00074     time.append( ":" );
00075     
00076     if(this->minutes <= 9) { time.append( "0" ); }
00077     std::sprintf(buffer, "%d", this->minutes);
00078     time.append( std::string(buffer) );
00079     
00080     time.append( ":" );
00081 
00082     if(this->seconds <= 9) { time.append( "0" ); }
00083     std::sprintf(buffer, "%d", this->seconds);
00084     time.append( std::string(buffer) );
00085     
00086     lcd->locate(0,0);
00087     lcd->printf( emptyRow.c_str() );
00088     lcd->locate(0,0);
00089     lcd->printf("\3\4");
00090     lcd->locate(18,0);
00091     lcd->printf("\2\1");
00092     lcd->locate(6,0);
00093     lcd->printf( time.c_str());
00094     }
00095 }
00096 
00097 void Display::setStatusBattery( float batteryLvl )
00098 {
00099     if ( 1 >= batteryLvl && batteryLvl >= 0 )
00100     {
00101         int lvl;
00102         batteryLvl = batteryLvl * 5;         //get size of replace 5
00103         lvl = (int) batteryLvl;       
00104         costumCharacters->setBatteryIcon( lvl );
00105     }
00106 }
00107 
00108 void Display::setStatusLora( int lora, int loraStatus )
00109 {  
00110     costumCharacters->setLoraIcon( lora );
00111     costumCharacters->setLoraStatusIcon( loraStatus );
00112 }
00113 
00114 void Display::setStatusGps( int gpsFix )
00115 {
00116     costumCharacters->setGpsIcon( gpsFix );
00117 }
00118 
00119 void Display::setStartScreen( void )
00120 {
00121     this->status = 0;
00122     
00123     Display::topBar( 0 );
00124     Display::clearScreen();
00125     
00126     for(int i = 13; i >= 0; i--)
00127     {
00128     lcd->locate( 0, 0 );
00129     lcd->printf( "                    " );
00130     lcd->locate( i, 0 );
00131     lcd->printf( "Welcome" );
00132     wait_ms( 20 );
00133     }
00134     for(int i = 14; i >= 0; i--)
00135     {
00136     lcd->locate( 0, 1 );
00137     lcd->printf( "                    " );
00138     lcd->locate( i, 1 );
00139     lcd->printf( "to the" );
00140     wait_ms( 20 );
00141     }
00142     for(int i = 15; i >= 0; i--)
00143     {
00144     lcd->locate( 0, 2 );
00145     lcd->printf( "                    " );
00146     lcd->locate( i, 2 );
00147     lcd->printf( "Vives" );
00148     wait_ms( 20 );
00149     }
00150     for(int i = 11; i >= 0; i--)
00151     {
00152     lcd->locate( 0, 3 );
00153     lcd->printf( "                    " );
00154     lcd->locate( i, 3 );
00155     lcd->printf( "City Game" );
00156     wait_ms( 20 );
00157     }
00158     lcd->locate( 19, 3 );
00159     lcd->putc( (char) 126 );
00160 }
00161 
00162 void Display::setMissionScreen( std::string description, int distance, int radius )
00163 {
00164     this->description = description;
00165     this->distance = distance;
00166     this->radius = radius;
00167     
00168     if( this->status == 3 )
00169     {
00170     
00171     
00172     Display::clearScreen();
00173     Display::topBar( 1 );
00174     
00175     lcd->locate( 0, 1 );
00176     lcd->printf( description.c_str() );
00177     
00178     char buffer[10];
00179     std::string distanceString = "";
00180     distanceString.append( "Dist:" );
00181     std::sprintf(buffer, "%d", distance);
00182     distanceString.append( std::string(buffer) );
00183     distanceString.append( "m" );
00184     
00185     lcd->locate( 0, 3 );
00186     lcd->printf( distanceString.c_str() );
00187     
00188     char buffer2[10];
00189     std::string radString = "";
00190     radString.append( "Rad:" );
00191     std::sprintf(buffer2, "%d", radius);
00192     radString.append( std::string(buffer2) );
00193     radString.append( "m" );
00194     
00195     lcd->locate( 12, 3 );
00196     lcd->printf( radString.c_str() );
00197     
00198     Display::topBarRedraw();
00199     }
00200 }
00201 
00202 void Display::setEndScreen( void )
00203 {
00204     Display::topBar( 0 );
00205     Display::clearScreen();
00206     
00207     for(int i = 0; i < 7; i++)
00208     {
00209     lcd->locate( 2, 0 );
00210     lcd->printf( "Congratulations!" );
00211     wait_ms( 120 );
00212     Display::clearScreen();
00213     wait_ms( 100 );
00214     }
00215     
00216     lcd->locate( 2, 0 );
00217     lcd->printf( "Congratulations!" );
00218     lcd->locate( 3, 1 );
00219     lcd->printf( "You completed" );
00220     lcd->locate( 6, 2 );
00221     lcd->printf( "all the" );
00222     lcd->locate( 5, 3 );
00223     lcd->printf( "missions!" );
00224     
00225 }
00226 
00227 void Display::clearScreen( void )
00228 {
00229     if( topBarEnable )
00230     {
00231         lcd->locate(0,1);
00232         lcd->printf( emptyRow.c_str() );
00233         lcd->locate(0,2);
00234         lcd->printf( emptyRow.c_str() );
00235         lcd->locate(0,3);
00236         lcd->printf( emptyRow.c_str());
00237     }
00238     else
00239     {
00240         lcd->cls();
00241     }
00242 }
00243 
00244 void Display::setMessageScreen( std::string message )
00245 {
00246     if( this->status == 3 )
00247     {
00248     Display::clearScreen();
00249     
00250     if(topBarEnable)
00251     {
00252         lcd->locate(0,1);
00253     }
00254     else
00255     {
00256         lcd->locate(0,0);
00257     }
00258     lcd->printf( message.c_str() );
00259     }
00260 }
00261 
00262 void Display::setMenuScreen( void )
00263 {
00264     position = ! position;
00265     
00266     Display::clearScreen();
00267     Display::topBar( 1 );
00268     
00269     
00270     if( position )
00271     {
00272         lcd->locate(0,2);
00273     }
00274     else
00275     {
00276         lcd->locate(0,3);
00277     }
00278     lcd->putc( (char) 126 );
00279     
00280     lcd->locate(0,1);
00281     lcd->printf( "Menu:" );
00282     lcd->locate(1,2);
00283     lcd->printf( "Start game" );
00284     lcd->locate(1,3);
00285     lcd->printf( "Status screen" );
00286    
00287 }
00288 
00289 void Display::eventHandler( char key )
00290 {
00291     switch( status )
00292     {
00293       case 0:           //Welcome screen
00294             {
00295                 Display::setMenuScreen();
00296                 this->status = 1;
00297             }
00298         break;
00299       case 1:           //Menu screen
00300             {
00301                 switch( key )
00302                 {
00303                     case '2':
00304                     {
00305                         Display::setMenuScreen();
00306                     }
00307                 break;
00308                     case '5':
00309                     {
00310                         if( position )
00311                         {
00312                             this->status = 3;
00313                             Display::setMissionScreen( this->description, this->distance, this->radius );
00314                         }
00315                         else
00316                         {
00317                             this->status = 2;
00318                             Display::setStatusScreen();
00319                         }
00320                         
00321                     }
00322                     break;
00323                     case '8':
00324                     {
00325                         Display::setMenuScreen();
00326                     }
00327                     break;
00328                 }
00329             }
00330         break;
00331       case 2:
00332             {
00333                 this->status = 1;
00334                 Display::setMenuScreen();
00335             }
00336         break;
00337       case 3:
00338             {
00339 
00340             }
00341         break;
00342     }
00343 }
00344 
00345 void Display::setStatusScreen( void )
00346 {
00347     Display::clearScreen();
00348     Display::topBar( 1 );
00349     
00350     lcd->locate(0,2);
00351     lcd->printf( "Battery" );
00352     lcd->locate(0,3);
00353     lcd->printf( "Voltage: " );
00354 }