This is a port of Henning Kralsen's UTFT library for Arduino/chipKIT to mbed, refactored to make full use of C inheritance and access control, in order to reduce work when implementing new drivers and at the same time make the code more readable and easier to maintain. As of now supported are SSD1289 (16-bit interface), HX8340-B (serial interface) and ST7735 (serial interface). Drivers for other controllers will be added as time and resources to acquire the displays to test the code permit.

Dependents:   UTFT_SSD1289

Fork of TFTLCD by Todor Todorov

Revision:
12:d0978272a340
Parent:
10:69571adcfad5
Child:
13:5ceeba86bbe4
--- a/lcd_base.cpp	Tue Dec 11 16:50:09 2012 +0000
+++ b/lcd_base.cpp	Tue Dec 11 18:11:14 2012 +0000
@@ -31,13 +31,13 @@
 }
 
 inline
-void LCD::SetForeground( unsigned short color )
+void LCD::SetForeground( unsigned int color )
 {
     _foreground = color;
 }
 
 inline
-void LCD::SetBackground( unsigned short color )
+void LCD::SetBackground( unsigned int color )
 {
     _background = color;
 }
@@ -54,20 +54,20 @@
 inline
 unsigned short LCD::GetWidth( void )
 {
-    if ( _orientation == LANDSCAPE ) return _disp_height;
+    if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_height;
     return _disp_width;
 }
 
 inline
 unsigned short LCD::GetHeight( void )
 {
-    if ( _orientation == LANDSCAPE ) return _disp_width;
+    if ( _orientation == LANDSCAPE || _orientation == LANDSCAPE_REV ) return _disp_width;
     return _disp_height;
 }
 
 void LCD::FillScreen( int color )
 {
-    unsigned short rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+    unsigned int rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
     Activate();
     ClearXY();
     for ( int i = 0; i < ( ( _disp_width ) * ( _disp_height ) ); i++ )
@@ -120,7 +120,7 @@
     }
     else
     {
-        unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+        unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
         Activate();
         if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
         {
@@ -206,21 +206,10 @@
     if ( x1 > x2 ) swap( ushort, x1, x2 );
     if ( y1 > y2 ) swap( ushort, y1, y2 );
 
-    if ( _orientation == PORTRAIT )
+    for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
     {
-        for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
-        {
-            DrawHLine( x1, y1 + i, x2 - x1, color );
-            DrawHLine( x1, y2 - i, x2 - x1, color );
-        }
-    }
-    else
-    {
-        for ( int i = 0; i < ( ( x2 - x1 ) / 2 ) + 1; i++ )
-        {
-            DrawVLine( x1 + i, y1, y2 - y1, color );
-            DrawVLine( x2 - i, y1, y2 - y1, color );
-        }
+        DrawHLine( x1, y1 + i, x2 - x1, color );
+        DrawHLine( x1, y2 - i, x2 - x1, color );
     }
 }
 
@@ -261,7 +250,7 @@
     int ddF_y = -2 * radius;
     int x1 = 0;
     int y1 = radius;
-    unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+    unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
 
     Activate();
     SetXY( x, y + radius, x, y + radius );
@@ -306,7 +295,7 @@
 
 void LCD::FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
 {
-    unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+    unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
     Activate();
     for ( int y1 = -radius; y1 <= radius; y1++ )
         for ( int x1 = -radius; x1 <= radius; x1++ )
@@ -324,20 +313,10 @@
 
     stl = strlen( str );
 
-    if ( _orientation == PORTRAIT )
-    {
-        if ( x == RIGHT )
-            x = _disp_width - ( stl * _font.width );
-        if ( x == CENTER )
-            x = ( _disp_width - ( stl * _font.width ) ) / 2;
-    }
-    else
-    {
-        if ( x == RIGHT )
-            x = _disp_height - ( stl * _font.width );
-        if ( x == CENTER )
-            x = ( _disp_height - ( stl * _font.width ) ) / 2;
-    }
+    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 )
@@ -346,31 +325,76 @@
             RotateChar( *str++, x, y, i, fgColor, bgColor, deg );
 }
 
-void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, const unsigned short* imgPixelData, unsigned char scale )
+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 )
     {
-        if ( _orientation == PORTRAIT )
+        SetXY( x, y, x + img->Width - 1, y + img->Height - 1 );
+            
+        if ( _colorDepth == RGB16 )
         {
-            SetXY( x, y, x + sx - 1, y + sy - 1 );
-            for ( tc = 0; tc < ( sx * sy ); tc++ )
-                SetPixelColor( imgPixelData[ tc ] );
+            if ( img->Format == RGB16 )
+            {
+                const unsigned short *pixel = (const unsigned short*) img->PixelData;
+                for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
+                    WriteData( *pixel++ );
+            }
+            else // img RGB18
+            {
+                unsigned char r, g, b;
+                const unsigned int *pixel = (const unsigned int*) img->PixelData;
+                for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
+                {
+                    r = ( *pixel >> 16 ) & 0xF8;
+                    g = ( *pixel >> 8 ) & 0xFC;
+                    b = ( *pixel++ & 0xF8 ) >> 3;
+                    WriteData( ( r << 8 ) | ( g >> 5 ) | ( g << 3 ) | b );
+                }
+            }
         }
-        else
+        else // color depth RGB18
         {
-            for ( ty = 0; ty < sy; ty++ )
+            unsigned short r, g, b, leftover = 0;
+            bool hasNext = false;
+            if ( img->Format == RGB16 )
             {
-                SetXY( x, y + ty, x + sx - 1, y + ty );
-                for ( tx = sx; tx >= 0; tx-- )
-                    SetPixelColor( imgPixelData[ ( ty * sx ) + tx ] );
+                const unsigned short *pixel = (const unsigned short*) img->PixelData;
+                for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
+                {
+                    r = ( *pixel & 0xF800 ) >> 8;
+                    g = ( *pixel & 0x07E0 ) >> 3;
+                    b = ( *pixel & 0x001F ) << 3;
+                    pixel++;
+                    if ( hasNext )
+                    {
+                        WriteData( ( leftover << 8 ) | r );
+                        WriteData( ( g << 8 ) | b );
+                        hasNext = false;
+                    }
+                    else
+                    {
+                        WriteData( ( r << 8 ) | g );
+                        leftover = b;
+                        hasNext = true;
+                    }
+                }
+            }
+            else // img RGB18
+            {
+                const unsigned int *pixel = (const unsigned int*) img->PixelData;
+                for ( tc = 0; tc < ( img->Width * img->Height ); tc++ )
+                {
+                    SetPixelColor( *pixel++ );
+                }
             }
         }
     }
     else
     {
+        /*
         if ( _orientation == PORTRAIT )
         {
             for ( ty = 0; ty < sy; ty++ )
@@ -379,7 +403,7 @@
                 for ( tsy = 0; tsy < scale; tsy++ )
                     for ( tx = 0; tx < sx; tx++ )
                         for ( tsx = 0; tsx < scale; tsx++ )
-                            SetPixelColor( imgPixelData[ ( ty * sx ) + tx ] );
+                            WriteData( imgPixelData[ ( ty * sx ) + tx ] );
             }
         }
         else
@@ -391,33 +415,34 @@
                     SetXY( x, y + ( ty * scale ) + tsy, x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + tsy );
                     for ( tx = sx; tx >= 0; tx-- )
                         for ( tsx = 0; tsx < scale; tsx++ )
-                            SetPixelColor( imgPixelData[ ( ty * sx ) + tx ] );
+                            WriteData( imgPixelData[ ( ty * sx ) + tx ] );
                 }
             }
         }
+        */
     }
     Deactivate();
 }
 
-void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, const unsigned short* imgPixelData, unsigned short deg, unsigned short rox, unsigned short roy )
+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, sx, sy, imgPixelData );
+        DrawBitmap( x, y, img );
     else
     {
         Activate();
-        for ( ty = 0; ty < sy; ty++ )
-            for ( tx = 0; tx < sx; tx++ )
+        for ( ty = 0; ty < img->Height; ty++ )
+            for ( tx = 0; tx < img->Width; tx++ )
             {
                 newx = x + rox + ( ( ( tx - rox ) * cos( radian ) ) - ( ( ty - roy ) * sin( radian ) ) );
                 newy = y + roy + ( ( ( ty - roy ) * cos( radian ) ) + ( ( tx - rox ) * sin( radian ) ) );
 
                 SetXY( newx, newy, newx, newy );
-                SetPixelColor( imgPixelData[ ( ty * sx ) + tx ] );
+                //WriteData( imgPixelData[ ( ty * sx ) + tx ] );
             }
         Deactivate();
     }
@@ -435,24 +460,6 @@
     _lcd_pin_cs = HIGH;
 }
 
-/*
-void LCD::WriteCmd( unsigned short cmd )
-{
-    _lcd_pin_rs = LOW;
-    _lcd_port->write( cmd );
-    pulseLow( _lcd_pin_wr );
-}
-*/
-
-/*
-void LCD::WriteData( unsigned short data )
-{
-    _lcd_pin_rs = HIGH;
-    _lcd_port->write( data );
-    pulseLow( _lcd_pin_wr );
-}
-*/
-
 inline
 void LCD::WriteCmdData( unsigned short cmd, unsigned short data )
 {
@@ -460,38 +467,15 @@
     WriteData( data );
 }
 
-/*
-void LCD::SetXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
+inline
+void LCD::ClearXY( void )
 {
-    if ( _orientation == LANDSCAPE )
-    {
-        swap( uint16_t, x1, y1 )
-        swap( uint16_t, x2, y2 )
-        y1 = _disp_height - y1;
-        y2 = _disp_height - y2;
-        swap( uint16_t, y1, y2 )
-    }
-
-    WriteCmdData( 0x44, ( x2 << 8 ) + x1 );
-    WriteCmdData( 0x45, y1 );
-    WriteCmdData( 0x46, y2 );
-    WriteCmdData( 0x4e, x1 );
-    WriteCmdData( 0x4f, y1 );
-    WriteCmd( 0x22 );
-}
-*/
-
-void LCD::ClearXY()
-{
-    if ( _orientation == PORTRAIT )
-        SetXY( 0, 0, _disp_width - 1, _disp_height - 1 );
-    else
-        SetXY( 0, 0, _disp_height - 1, _disp_width - 1 );
+    SetXY( 0, 0, GetWidth() - 1, GetHeight() - 1 );
 }
 
 void LCD::DrawHLine( unsigned short x, unsigned short y, unsigned short len, int color )
 {
-    unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+    unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
     
     Activate();
     SetXY( x, y, x + len, y );
@@ -502,7 +486,7 @@
 
 void LCD::DrawVLine( unsigned short x, unsigned short y, unsigned short len, int color )
 {
-    unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
+    unsigned int usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned int ) color;
     
     Activate();
     SetXY( x, y, x, y + len );
@@ -516,49 +500,25 @@
     uint8_t i, ch;
     uint16_t j;
     uint16_t temp;
-    unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
-    unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
+    unsigned int usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned int ) fgColor;
+    unsigned int usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned int ) bgColor;
 
     Activate();
 
-    if ( _orientation == PORTRAIT )
+    SetXY( x, y, x + _font.width - 1, y + _font.height - 1 );
+
+    temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
+    for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j++ )
     {
-        SetXY( x, y, x + _font.width - 1, y + _font.height - 1 );
-
-        temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
-        for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j++ )
+        ch = _font.font[ temp ];
+        for ( i = 0; i < 8; i++ )
         {
-            ch = _font.font[ temp ];
-            for ( i = 0; i < 8; i++ )
-            {
-                if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
-                    SetPixelColor( usedColorFG );
-                else
-                    SetPixelColor( usedColorBG );
-            }
-            temp++;
+            if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
+                SetPixelColor( usedColorFG );
+            else
+                SetPixelColor( usedColorBG );
         }
-    }
-    else
-    {
-        temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
-
-        for ( j = 0; j < ( ( _font.width / 8 ) * _font.height ); j += ( _font.width / 8 ) )
-        {
-            SetXY( x, y + ( j / ( _font.width / 8 ) ), x + _font.width - 1, y + ( j / ( _font.width / 8 ) ) );
-            for ( int zz = ( _font.width / 8 ) - 1; zz >= 0; zz-- )
-            {
-                ch = _font.font[ temp + zz ];
-                for ( i = 0; i < 8; i++ )
-                {
-                    if ( ( ch & ( 1 << i ) ) != 0 )
-                        SetPixelColor( usedColorFG );
-                    else
-                        SetPixelColor( usedColorBG );
-                }
-            }
-            temp += ( _font.width / 8 );
-        }
+        temp++;
     }
     Deactivate();
 }
@@ -571,8 +531,8 @@
     double radian;
     radian = deg * 0.0175;
     
-    unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
-    unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
+    unsigned int usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned int ) fgColor;
+    unsigned int usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned int ) bgColor;
 
     Activate();