TFTLCD with FastIO

Fork of TFTLCD by en 129

Revision:
4:3ac4239f6c9c
Parent:
3:64a5b67d5b51
Child:
7:5c418fc1879f
--- a/lcd_base.cpp	Sun Dec 02 01:44:23 2012 +0000
+++ b/lcd_base.cpp	Sun Dec 02 05:44:52 2012 +0000
@@ -22,9 +22,13 @@
 #include "lcd_base.h"
 #include "helpers.h"
 
-LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS )
-    : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS )
-{}
+LCD::LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET )
+    : _disp_width( width ), _disp_height( height ), _lcd_pin_cs( CS ), _lcd_pin_rs( RS ), _lcd_pin_reset( RESET )
+{
+    SetForeground();
+    SetBackground();
+    _font.font = 0;
+}
 
 inline
 void LCD::SetForeground( unsigned short color )
@@ -64,12 +68,11 @@
 void LCD::FillScreen( int color )
 {
     unsigned short rgb = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
-    _lcd_pin_cs = LOW;
+    Activate();
     ClearXY();
-    _lcd_pin_rs = HIGH;
     for ( int i = 0; i < ( ( _disp_width ) * ( _disp_height ) ); i++ )
         WriteData( rgb );
-    _lcd_pin_cs = HIGH;
+    Deactivate();
 }
 
 inline
@@ -80,12 +83,11 @@
 
 void LCD::DrawPixel( unsigned short x, unsigned short y, int color )
 {
-    _lcd_pin_cs = LOW;
+    Activate();
     SetXY( x, y, x, y );
     WriteData( color == -1 ? _background :
                     color == -2 ? _foreground : color );
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 void LCD::DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
@@ -116,60 +118,58 @@
             swap( ushort, y1, y2 )
         DrawVLine( x1, y1, y2 - y1, color );
     }
-    else if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
+    else
     {
         unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
-        _lcd_pin_cs = LOW;
-        delta = ( double( y2 - y1 ) / double( x2 - x1 ) );
-        ty = double( y1 );
-        if ( x1 > x2 )
+        Activate();
+        if ( abs( x2 - x1 ) > abs( y2 - y1 ) )
         {
-            for ( int i = x1; i >= x2; i-- )
+            delta = ( double( y2 - y1 ) / double( x2 - x1 ) );
+            ty = double( y1 );
+            if ( x1 > x2 )
             {
-                SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
-                WriteData( usedColor );
-                ty = ty - delta;
+                for ( int i = x1; i >= x2; i-- )
+                {
+                    SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
+                    WriteData( usedColor );
+                    ty = ty - delta;
+                }
+            }
+            else
+            {
+                for ( int i = x1; i <= x2; i++ )
+                {
+                    SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
+                    WriteData( usedColor );
+                    ty = ty + delta;
+                }
             }
         }
         else
         {
-            for ( int i = x1; i <= x2; i++ )
+            delta = ( float( x2 - x1 ) / float( y2 - y1 ) );
+            tx = float( x1 );
+            if ( y1 > y2 )
             {
-                SetXY( i, int( ty + 0.5 ), i, int( ty + 0.5 ) );
-                WriteData( usedColor );
-                ty = ty + delta;
+                for ( int i = y2 + 1; i > y1; i-- )
+                {
+                    SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
+                    WriteData( usedColor );
+                    tx = tx + delta;
+                }
+            }
+            else
+            {
+                for ( int i = y1; i < y2 + 1; i++ )
+                {
+                    SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
+                    WriteData( usedColor );
+                    tx = tx + delta;
+                }
             }
         }
-        _lcd_pin_cs = HIGH;
+        Deactivate();
     }
-    else
-    {
-        unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
-        _lcd_pin_cs = LOW;
-        delta = ( float( x2 - x1 ) / float( y2 - y1 ) );
-        tx = float( x1 );
-        if ( y1 > y2 )
-        {
-            for ( int i = y2 + 1; i > y1; i-- )
-            {
-                SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
-                WriteData( usedColor );
-                tx = tx + delta;
-            }
-        }
-        else
-        {
-            for ( int i = y1; i < y2 + 1; i++ )
-            {
-                SetXY( int( tx + 0.5 ), i, int( tx + 0.5 ), i );
-                WriteData( usedColor );
-                tx = tx + delta;
-            }
-        }
-        _lcd_pin_cs = HIGH;
-    }
-
-    ClearXY();
 }
 
 void LCD::DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color )
@@ -263,7 +263,7 @@
     int y1 = radius;
     unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
 
-    _lcd_pin_cs = LOW;
+    Activate();
     SetXY( x, y + radius, x, y + radius );
     WriteData( usedColor );
     SetXY( x, y - radius, x, y - radius );
@@ -301,14 +301,13 @@
         SetXY( x - y1, y - x1, x - y1, y - x1 );
         WriteData( usedColor );
     }
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 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;
-    _lcd_pin_cs = LOW;
+    Activate();
     for ( int y1 = -radius; y1 <= radius; y1++ )
         for ( int x1 = -radius; x1 <= radius; x1++ )
             if ( x1 * x1 + y1 * y1 <= radius * radius )
@@ -316,8 +315,7 @@
                 SetXY( x + x1, y + y1, x + x1, y + y1 );
                 WriteData( usedColor );
             }
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 void LCD::Print( const char *str, unsigned short x, unsigned short y, int fgColor, int bgColor, unsigned short deg )
@@ -352,33 +350,29 @@
 {
     int tx, ty, tc, tsx, tsy;
 
+    Activate();
     if ( scale == 1 )
     {
         if ( _orientation == PORTRAIT )
         {
-            _lcd_pin_cs = LOW;
             SetXY( x, y, x + sx - 1, y + sy - 1 );
             for ( tc = 0; tc < ( sx * sy ); tc++ )
                 WriteData( data[ tc ] );
-            _lcd_pin_cs = HIGH;
         }
         else
         {
-            _lcd_pin_cs = LOW;
             for ( ty = 0; ty < sy; ty++ )
             {
                 SetXY( x, y + ty, x + sx - 1, y + ty );
                 for ( tx = sx; tx >= 0; tx-- )
                     WriteData( data[ ( ty * sx ) + tx ] );
             }
-            _lcd_pin_cs = HIGH;
         }
     }
     else
     {
         if ( _orientation == PORTRAIT )
         {
-            _lcd_pin_cs = LOW;
             for ( ty = 0; ty < sy; ty++ )
             {
                 SetXY( x, y + ( ty * scale ), x + ( ( sx * scale ) - 1 ), y + ( ty * scale ) + scale );
@@ -387,11 +381,9 @@
                         for ( tsx = 0; tsx < scale; tsx++ )
                             WriteData( data[ ( ty * sx ) + tx ] );
             }
-            _lcd_pin_cs = HIGH;
         }
         else
         {
-            _lcd_pin_cs = LOW;
             for ( ty = 0; ty < sy; ty++ )
             {
                 for ( tsy = 0; tsy < scale; tsy++ )
@@ -402,10 +394,9 @@
                             WriteData( data[ ( ty * sx ) + tx ] );
                 }
             }
-            _lcd_pin_cs = HIGH;
         }
     }
-    ClearXY();
+    Deactivate();
 }
 
 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 )
@@ -418,7 +409,7 @@
         DrawBitmap( x, y, sx, sy, data );
     else
     {
-        _lcd_pin_cs = LOW;
+        Activate();
         for ( ty = 0; ty < sy; ty++ )
             for ( tx = 0; tx < sx; tx++ )
             {
@@ -428,30 +419,37 @@
                 SetXY( newx, newy, newx, newy );
                 WriteData( data[ ( ty * sx ) + tx ] );
             }
-        _lcd_pin_cs = HIGH;
+        Deactivate();
     }
-    ClearXY();
+}
+
+inline
+void LCD::Activate( void )
+{
+    _lcd_pin_cs = LOW;
+}
+
+inline
+void LCD::Deactivate( void )
+{
+    _lcd_pin_cs = HIGH;
 }
 
 /*
-void LCD::writeCmd( unsigned short cmd )
+void LCD::WriteCmd( unsigned short cmd )
 {
     _lcd_pin_rs = LOW;
-    _lcd_pin_cs = LOW;
     _lcd_port->write( cmd );
     pulseLow( _lcd_pin_wr );
-    _lcd_pin_cs = HIGH;
 }
 */
 
 /*
-void LCD::writeData( unsigned short data )
+void LCD::WriteData( unsigned short data )
 {
     _lcd_pin_rs = HIGH;
-    _lcd_pin_cs = LOW;
     _lcd_port->write( data );
     pulseLow( _lcd_pin_wr );
-    _lcd_pin_cs = HIGH;
 }
 */
 
@@ -463,7 +461,7 @@
 }
 
 /*
-void LCD::setXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
+void LCD::SetXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
 {
     if ( _orientation == LANDSCAPE )
     {
@@ -474,12 +472,12 @@
         swap( uint16_t, y1, y2 )
     }
 
-    writeCmdData( 0x44, ( x2 << 8 ) + x1 );
-    writeCmdData( 0x45, y1 );
-    writeCmdData( 0x46, y2 );
-    writeCmdData( 0x4e, x1 );
-    writeCmdData( 0x4f, y1 );
-    writeCmd( 0x22 );
+    WriteCmdData( 0x44, ( x2 << 8 ) + x1 );
+    WriteCmdData( 0x45, y1 );
+    WriteCmdData( 0x46, y2 );
+    WriteCmdData( 0x4e, x1 );
+    WriteCmdData( 0x4f, y1 );
+    WriteCmd( 0x22 );
 }
 */
 
@@ -495,24 +493,22 @@
 {
     unsigned short usedColor = color == -1 ? _background : color == -2 ? _foreground : ( unsigned short ) color;
     
-    _lcd_pin_cs = LOW;
+    Activate();
     SetXY( x, y, x + len, y );
     for ( int i = 0; i < len + 1; i++ )
         WriteData( usedColor );
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 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;
+    Activate();
     SetXY( x, y, x, y + len );
     for ( int i = 0; i < len; i++ )
         WriteData( usedColor );
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 void LCD::PrintChar( char c, unsigned short x, unsigned short y, int fgColor, int bgColor )
@@ -523,7 +519,7 @@
     unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
     unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
 
-    _lcd_pin_cs = LOW;
+    Activate();
 
     if ( _orientation == PORTRAIT )
     {
@@ -564,8 +560,7 @@
             temp += ( _font.width / 8 );
         }
     }
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }
 
 void LCD::RotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor, int bgColor, unsigned short deg )
@@ -579,7 +574,7 @@
     unsigned short usedColorFG = fgColor == -1 ? _background : fgColor == -2 ? _foreground : ( unsigned short ) fgColor;
     unsigned short usedColorBG = bgColor == -1 ? _background : bgColor == -2 ? _foreground : ( unsigned short ) bgColor;
 
-    _lcd_pin_cs = LOW;
+    Activate();
 
     temp = ( ( c - _font.offset ) * ( ( _font.width / 8 ) * _font.height ) ) + 4;
     for ( j = 0; j < _font.height; j++ )
@@ -602,6 +597,5 @@
         }
         temp += ( _font.width / 8 );
     }
-    _lcd_pin_cs = HIGH;
-    ClearXY();
+    Deactivate();
 }