mcufriend 2.4 TFT LCD Shield Lib

Dependents:   Nucleo_LCD_mcufriend_test

Fork of 24_TFT_STMNUCLEO by Carlos Silva

mcufriend 2.4" TFT LCD Shield

front back

Import program

00001 #include "mbed.h"
00002 #include "ili9328.h"
00003 
00004 // prepare the data bus for writing commands and pixel data
00005 BusOut dataBus( D8, D9, D2, D3, D4, D5, D6, D7 ); // 8 pins
00006 // create the lcd instance
00007 ILI9328_LCD lcd( A3,  A4, A2,A1, &dataBus, NC, A0); // control pins and data bus
00008 //ILI9328_LCD(  CS,  RESET,  RS, WR, BusOut* DATA_PORT, PinName BL = NC,  RD );
00009    
00010 int main()
00011 {
00012     int ii,height,width;
00013     
00014     height = lcd.GetHeight();
00015     width =  lcd.GetWidth();
00016     // initialize display - place it in standard portrait mode and set background to black and
00017     //                      foreground to white color.
00018     lcd.Initialize();
00019 
00020     // print something on the screen
00021     lcd.Print( "Hello, World!", CENTER, 50); // align text to center horizontally and use starndard colors
00022  
00023     wait(2);
00024    
00025     lcd.ClearScreen();
00026  
00027     for(ii=0;ii<width;ii++)
00028     {
00029         lcd.DrawLine(0, 0, height, ii,COLOR_GREEN);
00030         ii = ii+10;    
00031     }
00032     wait(2);
00033  
00034     lcd.DrawCircle(height/4, width/4, 20, COLOR_GREEN);
00035     wait(2);
00036  
00037     lcd.FillCircle(height/2, width/2, 50, COLOR_GREEN);
00038     wait(2);
00039  
00040     lcd.FillTriangle(height/4, width/4,(height/4)+20, (width/4)+40,(height/4)-20, (width/4)+40, COLOR_RED);
00041  
00042     while ( 1 ) { }
00043 }

HW information about the mcufriend LCD Shield

Revision:
30:ede1a0a32e04
Parent:
22:4c169297f374
--- a/lcd_base.cpp	Mon Jun 30 17:29:42 2014 +0000
+++ b/lcd_base.cpp	Wed Nov 09 09:33:04 2016 +0000
@@ -21,13 +21,14 @@
  *********************************************************************/
 #include "lcd_base.h"
 #include "helpers.h"
-
+ 
 LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET, PinName BL, backlight_t blType, float defaultBacklight )
     : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS ), _lcd_pin_reset( RESET ), _bl_type( blType )
 {
     SetForeground();
     SetBackground();
-    _font = &TerminusFont;
+//    _font = &TerminusFont;
+    _font = &TerminusBigFont;
     if ( defaultBacklight < 0 ) _bl_pwm_default = 0;
     else if ( defaultBacklight > 1.0 ) _bl_pwm_default = 1.0;
     else _bl_pwm_default = defaultBacklight;
@@ -55,7 +56,7 @@
         _bl_pwm = 0;
     }
 }
-
+ 
 void LCD::Sleep( void )
 {
     if ( _lcd_pin_bl != 0 )
@@ -63,7 +64,7 @@
     else if ( _bl_pwm != 0 )
         *_bl_pwm = 0;
 }
-
+ 
 void LCD::WakeUp( void )
 {
     if ( _lcd_pin_bl != 0 )
@@ -71,52 +72,54 @@
     else if ( _bl_pwm != 0 )
         *_bl_pwm = _bl_pwm_current;
 }
-
+ 
 inline
 void LCD::SetForeground( unsigned int color )
 {
     _foreground = color;
 }
-
+ 
 inline
 void LCD::SetBackground( unsigned int color )
 {
     _background = color;
 }
-
+ 
 void LCD::SetFont( const font_t *font )
 {
     _font = font;
 }
-
-inline
+ 
+//inline
+unsigned short LCD::GetHeight( void )
+{
+    if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_width;
+    return _disp_height;
+}
+ 
+ 
+//inline
 unsigned short LCD::GetWidth( void )
 {
     if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_height;
     return _disp_width;
 }
-
-inline
-unsigned short LCD::GetHeight( void )
-{
-    if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_width;
-    return _disp_height;
-}
-
-inline
+ 
+ 
+//inline
 uint8_t LCD::GetFontWidth( void )
 {
     if ( _font != 0 ) return _font->Width;
     return 0;
 }
-
+ 
 inline
 uint8_t LCD::GetFontHeight( void )
 {
     if ( _font != 0 ) return _font->Height;
     return 0;
 }
-
+ 
 void LCD::SetBacklightLevel( float level )
 {
     switch ( _bl_type )
@@ -136,7 +139,7 @@
             break;
     }
 }
-
+ 
 void LCD::FillScreen( int color )
 {
     unsigned int rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
@@ -146,13 +149,13 @@
         SetPixelColor( rgb );
     Deactivate();
 }
-
+ 
 inline
 void LCD::ClearScreen( void )
 {
     FillScreen( -1 );
 }
-
+ 
 void LCD::DrawPixel( unsigned short x, unsigned short y, int color )
 {
     Activate();
@@ -161,12 +164,12 @@
                     color == -2 ? _foreground : color );
     Deactivate();
 }
-
+ 
 void LCD::DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
 {
     
     double delta, tx, ty;
-
+ 
     if ( ( ( x2 - x1 ) < 0 ) )
     {
         swap( ushort, x1, x2 )
@@ -177,7 +180,7 @@
         swap( ushort, x1, x2 )
         swap( ushort, y1, y2 )
     }
-
+ 
     if ( y1 == y2 )
     {
         if ( x1 > x2 )
@@ -243,23 +246,84 @@
         Deactivate();
     }
 }
-
+void LCD::DrawTriangle( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, unsigned short x3, unsigned short y3,  int color )
+{
+//    if ( x1 > x2 ) swap( ushort, x1, x2 )
+//    if ( y1 > y2 ) swap( ushort, y1, y2 )
+ 
+    DrawLine( x1, y1, x2, y2, color );
+    DrawLine( x2, y2, x3, y3, color );
+    DrawLine( x3, y3, x1, y1, color );
+}
+ 
+void LCD::FillTriangle( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, unsigned short x3, unsigned short y3,  int color )
+{
+    int dx1, dx2, dx3;
+    int sx1, sx2, sy;
+    
+ 
+    if ( y1 > y2 ) { swap( ushort, y1, y2 ); swap( ushort, x1, x2 ); }
+    if ( y2 > y3 ) { swap( ushort, y3, y2 ); swap( ushort, x3, x2 ); }
+    if ( y1 > y2 ) { swap( ushort, y1, y2 ); swap( ushort, x1, x2 ); }
+ 
+    sx2= x1 * 1000; // Use fixed point math for x axis values
+    sx1 = sx2;
+    sy=y1;
+ 
+  // Calculate interpolation deltas
+    if (y2-y1 > 0) dx1=((x2-x1)*1000)/(y2-y1);
+      else dx1=0;
+    if (y3-y1 > 0) dx2=((x3-x1)*1000)/(y3-y1);
+      else dx2=0;
+    if (y3-y2 > 0) dx3=((x3-x2)*1000)/(y3-y2);
+      else dx3=0;
+ 
+  // Render scanlines (horizontal lines are the fastest rendering method)
+  if (dx1 > dx2)
+  {
+    for(; sy<=y2; sy++, sx1+=dx2, sx2+=dx1)
+    {
+      DrawHLine(sx1/1000, sy, (sx2-sx1)/1000, color);
+    }
+    sx2 = x2*1000;
+    sy = y2;
+    for(; sy<=y3; sy++, sx1+=dx2, sx2+=dx3)
+    {
+      DrawHLine(sx1/1000, sy, (sx2-sx1)/1000, color);
+    }
+  }
+  else
+  {
+    for(; sy<=y2; sy++, sx1+=dx1, sx2+=dx2)
+    {
+      DrawHLine(sx1/1000, sy, (sx2-sx1)/1000, color);
+    }
+    sx1 = x2*1000;
+    sy = y2;
+    for(; sy<=y3; sy++, sx1+=dx3, sx2+=dx2)
+    {
+      DrawHLine(sx1/1000, sy, (sx2-sx1)/1000, color);
+    }
+  }
+ 
+}
+ 
 void LCD::DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
 {
     if ( x1 > x2 ) swap( ushort, x1, x2 )
     if ( y1 > y2 ) swap( ushort, y1, y2 )
-
+ 
     DrawHLine( x1, y1, x2 - x1, color );
     DrawHLine( x1, y2, x2 - x1, color );
     DrawVLine( x1, y1, y2 - y1, color );
     DrawVLine( x2, y1, y2 - y1, color );
 }
-
+ 
 void LCD::DrawRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
 {
     if ( x1 > x2 ) swap( ushort, x1, x2 )
     if ( y1 > y2 ) swap( ushort, y1, y2 )
-
+ 
     if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
     {
         DrawPixel( x1 + 1, y1 + 1, color );
@@ -272,24 +336,24 @@
         DrawVLine( x2, y1 + 2, y2 - y1 - 4, color );
     }
 }
-
+ 
 void LCD::FillRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
 {
     if ( x1 > x2 ) swap( ushort, x1, x2 );
     if ( y1 > y2 ) swap( ushort, y1, y2 );
-
+ 
     for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
     {
         DrawHLine( x1, y1 + i, x2 - x1, color );
         DrawHLine( x1, y2 - i, x2 - x1, color );
     }
 }
-
+ 
 void LCD::FillRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
 {
     if ( x1 > x2 ) swap( ushort, x1, x2 )
     if ( y1 > y2 ) swap( ushort, y1, y2 )
-
+ 
     if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 )
     {
         for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
@@ -300,12 +364,12 @@
                     DrawHLine( x1 + 2, y1 + i, x2 - x1 - 4, color );
                     DrawHLine( x1 + 2, y2 - i, x2 - x1 - 4, color );
                     break;
-
+ 
                 case 1:
                     DrawHLine( x1 + 1, y1 + i, x2 - x1 - 2, color );
                     DrawHLine( x1 + 1, y2 - i, x2 - x1 - 2, color );
                     break;
-
+ 
                 default:
                     DrawHLine( x1, y1 + i, x2 - x1, color );
                     DrawHLine( x1, y2 - i, x2 - x1, color );
@@ -314,7 +378,7 @@
         }
     }
 }
-
+ 
 void LCD::DrawCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
 {
     int f = 1 - radius;
@@ -323,7 +387,7 @@
     int x1 = 0;
     int y1 = radius;
     unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
-
+ 
     Activate();
     SetXY( x, y + radius, x, y + radius );
     SetPixelColor( usedColor );
@@ -333,7 +397,7 @@
     SetPixelColor( usedColor );
     SetXY( x - radius, y, x - radius, y );
     SetPixelColor( usedColor );
-
+ 
     while ( x1 < y1 )
     {
         if ( f >= 0 )
@@ -364,7 +428,7 @@
     }
     Deactivate();
 }
-
+ 
 void LCD::FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
 {
     unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
@@ -378,34 +442,34 @@
             }
     Deactivate();
 }
-
+ 
 void LCD::Print( const char *str, unsigned short x, unsigned short y, int fgColor, int bgColor, unsigned short deg )
 {
     int stl, i;
-
+ 
     stl = strlen( str );
-
+ 
     if ( x == RIGHT )
         x = GetWidth() - ( stl * _font->Width );
     if ( x == CENTER )
         x = ( GetWidth() - ( stl * _font->Width ) ) / 2;
-
+ 
     for ( i = 0; i < stl; i++ )
         if ( deg == 0 )
             PrintChar( *str++, x + ( i * ( _font->Width ) ), y, fgColor, bgColor );
         else
             RotateChar( *str++, x, y, i, fgColor, bgColor, deg );
 }
-
+ 
 void LCD::DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned char scale )
 {
     int tx, ty, tc, tsx, tsy;
-
+ 
     Activate();
     if ( scale == 1 )
     {
         SetXY( x, y, x + img->Width - 1, y + img->Height - 1 );
-
+ 
         if ( img->Format == RGB16 )
         {
             const unsigned short *pixel = ( const unsigned short* ) img->PixelData;
@@ -458,13 +522,13 @@
     }
     Deactivate();
 }
-
+ 
 void LCD::DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned short deg, unsigned short rox, unsigned short roy )
 {
     int tx, ty, newx, newy;
     double radian;
     radian = deg * 0.0175;
-
+ 
     if ( deg == 0 )
         DrawBitmap( x, y, img );
     else
@@ -502,32 +566,32 @@
         Deactivate();
     }
 }
-
+ 
 inline
 void LCD::Activate( void )
 {
     _lcd_pin_cs = LOW;
 }
-
+ 
 inline
 void LCD::Deactivate( void )
 {
     _lcd_pin_cs = HIGH;
 }
-
+ 
 inline
 void LCD::WriteCmdData( unsigned short cmd, unsigned short data )
 {
     WriteCmd( cmd );
     WriteData( data );
 }
-
+ 
 inline
 void LCD::ClearXY( void )
 {
     SetXY( 0, 0, GetWidth() - 1, GetHeight() - 1 );
 }
-
+ 
 void LCD::DrawHLine( unsigned short x, unsigned short y, unsigned short len, int color )
 {
     unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
@@ -538,7 +602,7 @@
         SetPixelColor( usedColor );
     Deactivate();
 }
-
+ 
 void LCD::DrawVLine( unsigned short x, unsigned short y, unsigned short len, int color )
 {
     unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
@@ -549,7 +613,7 @@
         SetPixelColor( usedColor );
     Deactivate();
 }
-
+ 
 void LCD::PrintChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
 {
     uint8_t i, ch;
@@ -562,7 +626,7 @@
     if ( position == -1 ) position = 0; // will print space character
     
     Activate();
-
+ 
     SetXY( x, y, x + _font->Width - 1, y + _font->Height - 1 );
     
     for ( j = 0; j < totalCharBytes; j++ )
@@ -577,7 +641,7 @@
     }
     Deactivate();
 }
-
+ 
 void LCD::RotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor, int bgColor, unsigned short deg )
 {
     uint8_t i, j, ch;
@@ -587,7 +651,7 @@
     
     unsigned int usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned int ) fgColor;
     unsigned int usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned int ) bgColor;
-
+ 
     int16_t position = _font->Position[ c - _font->Offset ];
     if ( position == -1 ) position = 0; // will print space character
     
@@ -602,15 +666,15 @@
             {
                 newx = x + ( ( ( i + ( zz * 8 ) + ( pos * _font->Width ) ) * cos( radian ) ) - ( ( j ) * sin( radian ) ) );
                 newy = y + ( ( ( j ) * cos( radian ) ) + ( ( i + ( zz * 8 ) + ( pos * _font->Width ) ) * sin( radian ) ) );
-
+ 
                 SetXY( newx, newy, newx + 1, newy + 1 );
-
+ 
                 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 ) SetPixelColor( usedColorFG );
                 else SetPixelColor( usedColorBG );
             }
         }
         position += ( _font->Width / 8 );
     }
-
+ 
     Deactivate();
-}
+}
\ No newline at end of file