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:
2:81ed304b7e9b
Parent:
1:14bef43daf6f
Child:
3:64a5b67d5b51
--- a/lcd_base.cpp	Sat Dec 01 23:49:10 2012 +0000
+++ b/lcd_base.cpp	Sun Dec 02 00:12:43 2012 +0000
@@ -68,10 +68,10 @@
 {
     unsigned short rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
     _lcd_pin_cs = LOW;
-    clearXY();
+    ClearXY();
     _lcd_pin_rs = HIGH;
     for ( int i = 0; i < ( ( _disp_width ) * ( _disp_height ) ); i++ )
-        writeData( rgb );
+        WriteData( rgb );
     _lcd_pin_cs = HIGH;
 }
 
@@ -84,11 +84,11 @@
 void LCD::DrawPixel( unsigned short x, unsigned short y, int color )
 {
     _lcd_pin_cs = LOW;
-    setXY( x, y, x, y );
-    writeData( color == -1 ? _background :
+    SetXY( x, y, x, y );
+    WriteData( color == -1 ? _background :
                     color == -2 ? _foreground : color );
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
 void LCD::DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
@@ -111,13 +111,13 @@
     {
         if ( x1 > x2 )
             swap( ushort, x1, x2 )
-        drawHLine( x1, y1, x2 - x1, color );
+        DrawHLine( x1, y1, x2 - x1, color );
     }
     else if ( x1 == x2 )
     {
         if ( y1 > y2 )
             swap( ushort, y1, y2 )
-        drawVLine( x1, y1, y2 - y1, color );
+        DrawVLine( x1, y1, y2 - y1, color );
     }
     else if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
     {
@@ -129,8 +129,8 @@
         {
             for ( int i = x1; i >= x2; i-- )
             {
-                setXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
-                writeData( usedColor );
+                SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
+                WriteData( usedColor );
                 ty = ty - delta;
             }
         }
@@ -138,8 +138,8 @@
         {
             for ( int i = x1; i <= x2; i++ )
             {
-                setXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
-                writeData( usedColor );
+                SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
+                WriteData( usedColor );
                 ty = ty + delta;
             }
         }
@@ -155,8 +155,8 @@
         {
             for ( int i = y2 + 1; i > y1; i-- )
             {
-                setXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
-                writeData( usedColor );
+                SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
+                WriteData( usedColor );
                 tx = tx + delta;
             }
         }
@@ -164,15 +164,15 @@
         {
             for ( int i = y1; i < y2 + 1; i++ )
             {
-                setXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
-                writeData( usedColor );
+                SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
+                WriteData( usedColor );
                 tx = tx + delta;
             }
         }
         _lcd_pin_cs = HIGH;
     }
 
-    clearXY();
+    ClearXY();
 }
 
 void LCD::DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
@@ -180,10 +180,10 @@
     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 );
+    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 )
@@ -197,10 +197,10 @@
         DrawPixel( x2 - 1, y1 + 1, color );
         DrawPixel( x1 + 1, y2 - 1, color );
         DrawPixel( x2 - 1, y2 - 1, color );
-        drawHLine( x1 + 2, y1, x2 - x1 - 4, color );
-        drawHLine( x1 + 2, y2, x2 - x1 - 4, color );
-        drawVLine( x1, y1 + 2, y2 - y1 - 4, color );
-        drawVLine( x2, y1 + 2, y2 - y1 - 4, color );
+        DrawHLine( x1 + 2, y1, x2 - x1 - 4, color );
+        DrawHLine( x1 + 2, y2, x2 - x1 - 4, color );
+        DrawVLine( x1, y1 + 2, y2 - y1 - 4, color );
+        DrawVLine( x2, y1 + 2, y2 - y1 - 4, color );
     }
 }
 
@@ -213,16 +213,16 @@
     {
         for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ )
         {
-            drawHLine( x1, y1 + i, x2 - x1, color );
-            drawHLine( x1, y2 - i, x2 - x1, color );
+            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 );
+            DrawVLine( x1 + i, y1, y2 - y1, color );
+            DrawVLine( x2 - i, y1, y2 - y1, color );
         }
     }
 }
@@ -239,18 +239,18 @@
             switch ( i )
             {
                 case 0:
-                    drawHLine( x1 + 2, y1 + i, x2 - x1 - 4, color );
-                    drawHLine( x1 + 2, y2 - i, x2 - x1 - 4, color );
+                    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 );
+                    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 );
+                    DrawHLine( x1, y1 + i, x2 - x1, color );
+                    DrawHLine( x1, y2 - i, x2 - x1, color );
                     break;
             }
         }
@@ -267,14 +267,14 @@
     unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
 
     _lcd_pin_cs = LOW;
-    setXY( x, y + radius, x, y + radius );
-    writeData( usedColor );
-    setXY( x, y - radius, x, y - radius );
-    writeData( usedColor );
-    setXY( x + radius, y, x + radius, y );
-    writeData( usedColor );
-    setXY( x - radius, y, x - radius, y );
-    writeData( usedColor );
+    SetXY( x, y + radius, x, y + radius );
+    WriteData( usedColor );
+    SetXY( x, y - radius, x, y - radius );
+    WriteData( usedColor );
+    SetXY( x + radius, y, x + radius, y );
+    WriteData( usedColor );
+    SetXY( x - radius, y, x - radius, y );
+    WriteData( usedColor );
 
     while ( x1 < y1 )
     {
@@ -287,25 +287,25 @@
         x1++;
         ddF_x += 2;
         f += ddF_x;
-        setXY( x + x1, y + y1, x + x1, y + y1 );
-        writeData( usedColor );
-        setXY( x - x1, y + y1, x - x1, y + y1 );
-        writeData( usedColor );
-        setXY( x + x1, y - y1, x + x1, y - y1 );
-        writeData( usedColor );
-        setXY( x - x1, y - y1, x - x1, y - y1 );
-        writeData( usedColor );
-        setXY( x + y1, y + x1, x + y1, y + x1 );
-        writeData( usedColor );
-        setXY( x - y1, y + x1, x - y1, y + x1 );
-        writeData( usedColor );
-        setXY( x + y1, y - x1, x + y1, y - x1 );
-        writeData( usedColor );
-        setXY( x - y1, y - x1, x - y1, y - x1 );
-        writeData( usedColor );
+        SetXY( x + x1, y + y1, x + x1, y + y1 );
+        WriteData( usedColor );
+        SetXY( x - x1, y + y1, x - x1, y + y1 );
+        WriteData( usedColor );
+        SetXY( x + x1, y - y1, x + x1, y - y1 );
+        WriteData( usedColor );
+        SetXY( x - x1, y - y1, x - x1, y - y1 );
+        WriteData( usedColor );
+        SetXY( x + y1, y + x1, x + y1, y + x1 );
+        WriteData( usedColor );
+        SetXY( x - y1, y + x1, x - y1, y + x1 );
+        WriteData( usedColor );
+        SetXY( x + y1, y - x1, x + y1, y - x1 );
+        WriteData( usedColor );
+        SetXY( x - y1, y - x1, x - y1, y - x1 );
+        WriteData( usedColor );
     }
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
 void LCD::FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color )
@@ -316,11 +316,11 @@
         for ( int x1 = -radius; x1 <= radius; x1++ )
             if ( x1 * x1 + y1 * y1 <= radius * radius )
             {
-                setXY( x + x1, y + y1, x + x1, y + y1 );
-                writeData( usedColor );
+                SetXY( x + x1, y + y1, x + x1, y + y1 );
+                WriteData( usedColor );
             }
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
 void LCD::Print( const char *str, unsigned short x, unsigned short y, int fgColor, int bgColor, unsigned short deg )
@@ -346,9 +346,9 @@
 
     for ( i = 0; i < stl; i++ )
         if ( deg == 0 )
-            printChar( *str++, x + ( i * ( _font.width ) ), y, fgColor, bgColor );
+            PrintChar( *str++, x + ( i * ( _font.width ) ), y, fgColor, bgColor );
         else
-            rotateChar( *str++, x, y, i, fgColor, bgColor, deg );
+            RotateChar( *str++, x, y, i, fgColor, bgColor, deg );
 }
 
 void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, bitmap_t data, unsigned char scale )
@@ -360,9 +360,9 @@
         if ( _orientation == PORTRAIT )
         {
             _lcd_pin_cs = LOW;
-            setXY( x, y, x + sx - 1, y + sy - 1 );
+            SetXY( x, y, x + sx - 1, y + sy - 1 );
             for ( tc = 0; tc < ( sx * sy ); tc++ )
-                writeData( data[ tc ] );
+                WriteData( data[ tc ] );
             _lcd_pin_cs = HIGH;
         }
         else
@@ -370,9 +370,9 @@
             _lcd_pin_cs = LOW;
             for ( ty = 0; ty < sy; ty++ )
             {
-                setXY( x, y + ty, x + sx - 1, y + ty );
+                SetXY( x, y + ty, x + sx - 1, y + ty );
                 for ( tx = sx; tx >= 0; tx-- )
-                    writeData( data[ ( ty * sx ) + tx ] );
+                    WriteData( data[ ( ty * sx ) + tx ] );
             }
             _lcd_pin_cs = HIGH;
         }
@@ -384,11 +384,11 @@
             _lcd_pin_cs = LOW;
             for ( ty = 0; ty < sy; ty++ )
             {
-                setXY( x, y + ( ty * scale ), x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + scale );
+                SetXY( x, y + ( ty * scale ), x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + scale );
                 for ( tsy = 0; tsy < scale; tsy++ )
                     for ( tx = 0; tx < sx; tx++ )
                         for ( tsx = 0; tsx < scale; tsx++ )
-                            writeData( data[ ( ty * sx ) + tx ] );
+                            WriteData( data[ ( ty * sx ) + tx ] );
             }
             _lcd_pin_cs = HIGH;
         }
@@ -399,16 +399,16 @@
             {
                 for ( tsy = 0; tsy < scale; tsy++ )
                 {
-                    setXY( x, y + ( ty * scale ) + tsy, x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + tsy );
+                    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++ )
-                            writeData( data[ ( ty * sx ) + tx ] );
+                            WriteData( data[ ( ty * sx ) + tx ] );
                 }
             }
             _lcd_pin_cs = HIGH;
         }
     }
-    clearXY();
+    ClearXY();
 }
 
 void LCD::DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, bitmap_t data, unsigned short deg, unsigned short rox, unsigned short roy )
@@ -428,12 +428,12 @@
                 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 );
-                writeData( data[ ( ty * sx ) + tx ] );
+                SetXY( newx, newy, newx, newy );
+                WriteData( data[ ( ty * sx ) + tx ] );
             }
         _lcd_pin_cs = HIGH;
     }
-    clearXY();
+    ClearXY();
 }
 
 /*
@@ -459,10 +459,10 @@
 */
 
 inline
-void LCD::writeCmdData( unsigned short cmd, unsigned short data )
+void LCD::WriteCmdData( unsigned short cmd, unsigned short data )
 {
-    writeCmd( cmd );
-    writeData( data );
+    WriteCmd( cmd );
+    WriteData( data );
 }
 
 /*
@@ -486,39 +486,39 @@
 }
 */
 
-void LCD::clearXY()
+void LCD::ClearXY()
 {
     if ( _orientation == PORTRAIT )
-        setXY( 0, 0, _disp_width - 1, _disp_height - 1 );
+        SetXY( 0, 0, _disp_width - 1, _disp_height - 1 );
     else
-        setXY( 0, 0, _disp_height - 1, _disp_width - 1 );
+        SetXY( 0, 0, _disp_height - 1, _disp_width - 1 );
 }
 
-void LCD::drawHLine( unsigned short x, unsigned short y, unsigned short len, int color )
+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;
     
     _lcd_pin_cs = LOW;
-    setXY( x, y, x + len, y );
+    SetXY( x, y, x + len, y );
     for ( int i = 0; i < len + 1; i++ )
-        writeData( usedColor );
+        WriteData( usedColor );
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
-void LCD::drawVLine( unsigned short x, unsigned short y, unsigned short len, int color )
+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;
     
     _lcd_pin_cs = LOW;
-    setXY( x, y, x, y + len );
+    SetXY( x, y, x, y + len );
     for ( int i = 0; i < len; i++ )
-        writeData( usedColor );
+        WriteData( usedColor );
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
-void LCD::printChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
+void LCD::PrintChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
 {
     uint8_t i, ch;
     uint16_t j;
@@ -530,7 +530,7 @@
 
     if ( _orientation == PORTRAIT )
     {
-        setXY( x, y, x + _font.width - 1, y + _font.height - 1 );
+        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++ )
@@ -539,9 +539,9 @@
             for ( i = 0; i < 8; i++ )
             {
                 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
-                    writeData( usedColorFG );
+                    WriteData( usedColorFG );
                 else
-                    writeData( usedColorBG );
+                    WriteData( usedColorBG );
             }
             temp++;
         }
@@ -552,26 +552,26 @@
 
         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 ) ) );
+            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 )
-                        writeData( usedColorFG );
+                        WriteData( usedColorFG );
                     else
-                        writeData( usedColorBG );
+                        WriteData( usedColorBG );
                 }
             }
             temp += ( _font.width / 8 );
         }
     }
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }
 
-void LCD::rotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor, int bgColor, unsigned short deg )
+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;
     uint16_t temp;
@@ -595,16 +595,16 @@
                 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 );
+                SetXY( newx, newy, newx + 1, newy + 1 );
 
                 if ( ( ch & ( 1 << ( 7 - i ) ) ) != 0 )
-                    writeData( usedColorFG );
+                    WriteData( usedColorFG );
                 else
-                    writeData( usedColorBG );
+                    WriteData( usedColorBG );
             }
         }
         temp += ( _font.width / 8 );
     }
     _lcd_pin_cs = HIGH;
-    clearXY();
+    ClearXY();
 }