EasyRad / TFTLCD

Fork of TFTLCD by Todor Todorov

Files at this revision

API Documentation at this revision

Comitter:
ttodorov
Date:
Tue Dec 11 18:11:14 2012 +0000
Parent:
11:aeceefc5f9f2
Child:
13:5ceeba86bbe4
Commit message:
- integrated RGB16 and RGB18 color depth configuration/selection; - integrated HW rotation for the HX8340-B driver; - changed the bitmap drawing API; - TODO: no drawing of rotated or scaled bitmaps yet

Changed in this revision

hx8340bs.cpp Show annotated file Show diff for this revision Revisions of this file
hx8340bs.h Show annotated file Show diff for this revision Revisions of this file
lcd_base.cpp Show annotated file Show diff for this revision Revisions of this file
lcd_base.h Show annotated file Show diff for this revision Revisions of this file
ssd1289.cpp Show annotated file Show diff for this revision Revisions of this file
ssd1289.h Show annotated file Show diff for this revision Revisions of this file
st7735.cpp Show annotated file Show diff for this revision Revisions of this file
st7735.h Show annotated file Show diff for this revision Revisions of this file
--- a/hx8340bs.cpp	Tue Dec 11 16:50:09 2012 +0000
+++ b/hx8340bs.cpp	Tue Dec 11 18:11:14 2012 +0000
@@ -29,9 +29,10 @@
     else _lcd_pin_bl = 0;
 }
 
-void HX8340S_LCD::Initialize( orientation_t orientation )
+void HX8340S_LCD::Initialize( orientation_t orientation, colordepth_t colors )
 {
     _orientation = orientation;
+    _colorDepth = colors;
     
     wait_ms( 100 );
     _lcd_pin_reset = HIGH;
@@ -47,17 +48,20 @@
     wait_ms( 55 );
     
     Activate();
-    WriteCmd( 0xC1 );
+    WriteCmd( 0xC1 ); // SETEXTCMD
     WriteByteData( 0xFF );
     WriteByteData( 0x83 );
     WriteByteData( 0x40 );
-    WriteCmd( 0x11 );
+    
+    WriteCmd( 0x11 ); // SLPOUT
     wait_ms( 160 );
+    
     WriteCmd( 0xCA );
     WriteByteData( 0x70 );
     WriteByteData( 0x00 );
     WriteByteData( 0xD9 );
-    WriteCmd( 0xB0 );
+    
+    WriteCmd( 0xB0 ); // SETOSC
     WriteByteData( 0x01 );
     WriteByteData( 0x11 );
 
@@ -71,7 +75,8 @@
     WriteByteData( 0x00 );
     WriteByteData( 0x06 );
     wait_ms( 20 );
-    WriteCmd( 0xC2 );
+    
+    WriteCmd( 0xC2 ); // SETGAMMAP
     WriteByteData( 0x60 );
     WriteByteData( 0x71 );
     WriteByteData( 0x01 );
@@ -82,7 +87,7 @@
     WriteByteData( 0x31 );
     WriteByteData( 0x0A );
 
-    WriteCmd( 0xc3 );
+    WriteCmd( 0xc3 ); // SETGAMMAN
     WriteByteData( 0x67 );
     WriteByteData( 0x30 );
     WriteByteData( 0x61 );
@@ -92,31 +97,35 @@
     WriteByteData( 0x05 );
     WriteByteData( 0x33 );
     wait_ms( 10 );
-    WriteCmd( 0xB5 );
+    
+    WriteCmd( 0xB5 ); // SETPWCTR5
     WriteByteData( 0x35 );
     WriteByteData( 0x20 );
     WriteByteData( 0x45 );
 
-    WriteCmd( 0xB4 );
+    WriteCmd( 0xB4 ); // SETPWCTR4
     WriteByteData( 0x33 );
     WriteByteData( 0x25 );
     WriteByteData( 0x4c );
     wait_ms( 10 );
-    WriteCmd( 0x3a );
-    WriteByteData( 0x05 );
-    WriteCmd( 0x29 );
+    
+    WriteCmd( 0x3A ); // COLMOD == color depth: 0x05 => 16bit, 0x06 => 18bit
+    WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
+    
+    WriteCmd( 0x36 ); // MADCTL
+    switch ( _orientation )
+    {
+        case LANDSCAPE: WriteByteData( 0xB8 ); break;
+        case PORTRAIT_REV: WriteByteData( 0xDC ); break;
+        case LANDSCAPE_REV: WriteByteData( 0x6C ); break;
+        case PORTRAIT:
+        default: WriteByteData( 0x08 ); break;
+    }
+    
+    WriteCmd( 0x29 ); // DISPON
     wait_ms( 10 );
-    WriteCmd( 0x2a );
-    WriteByteData( 0x00 );
-    WriteByteData( 0x00 );
-    WriteByteData( 0x00 );
-    WriteByteData( 0xaf );
-    WriteCmd( 0x2b );
-    WriteByteData( 0x00 );
-    WriteByteData( 0x00 );
-    WriteByteData( 0x00 );
-    WriteByteData( 0xdb );
-    WriteCmd( 0x2c );
+    
+    ClearXY();
     Deactivate();
 }
 
@@ -170,25 +179,30 @@
 
 void HX8340S_LCD::SetXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 )
 {
-    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( 0x2a, x1 );
+    WriteCmdData( 0x2A, x1 );  // CASET
     WriteData( x2 );
-    WriteCmdData( 0x2b, y1 );
+    WriteCmdData( 0x2B, y1 );  // PASET
     WriteData( y2 );
-    WriteCmd( 0x2c );
+    WriteCmd( 0x2C ); // RAMWR
 }
 
-void HX8340S_LCD::SetPixelColor( unsigned short color )
+void HX8340S_LCD::SetPixelColor( unsigned int color )
 {
-    WriteData( color );
+    unsigned char r, g, b;
+    r = ( color >> 16 ) & 0xFF;
+    g = ( color >> 8 ) & 0xFF;
+    b = color & 0xFF;
+    if ( _colorDepth == RGB16 )
+    {
+        unsigned short clr = ( ( ( ( r ) & 0xF8 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 0x1C ) << 3 ) | ( ( b ) >> 3 ) );
+        WriteData( clr );
+    }
+    else
+    {
+        WriteByteData( r & 0xFC );
+        WriteByteData( g & 0xFC );
+        WriteByteData( b & 0xFC );
+    }
 }
 
 void HX8340S_LCD::serializeByte( unsigned char data )
--- a/hx8340bs.h	Tue Dec 11 16:50:09 2012 +0000
+++ b/hx8340bs.h	Tue Dec 11 18:11:14 2012 +0000
@@ -96,8 +96,9 @@
      * or produce garbage.
      *
      * \param oritentation The display orientation, landscape is default.
+     * \param colors The correct color depth to use for the pixel data.
      */
-    virtual void Initialize( orientation_t orientation = LANDSCAPE );
+    virtual void Initialize( orientation_t orientation = LANDSCAPE, colordepth_t colors = RGB16 );
     
     /** Puts the display to sleep.
      *
@@ -173,7 +174,7 @@
      * order to account for different color depth used by the controller.
      * \param color The color of the pixel.
      */
-    virtual void SetPixelColor( unsigned short color );
+    virtual void SetPixelColor( unsigned int color );
     
 private:
     void serializeByte( unsigned char data );
--- 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();
 
--- a/lcd_base.h	Tue Dec 11 16:50:09 2012 +0000
+++ b/lcd_base.h	Tue Dec 11 18:11:14 2012 +0000
@@ -39,14 +39,16 @@
 #endif
 
 /** \def RGB(r,g,b)
- *  \brief Creates a RGB-565 color value.
+ *  \brief Creates a RGB color from distinct bytes for the red, green and blue components.
  *
  * Displays which use 16 bits to assign colors to a specific pixel, use
  * 5 bits for the red component, 6 bits for the green component and 5
- * bits for the blue component.  This macro converts the 3 full-size
- * RGB bytes into one 16-bit RGB-565 value.
+ * bits for the blue component.  Displays which have 18-bit color depth
+ * use 6 bits for red, 6 bits for green and 6 bits for blue component.
+ * This macro preserves the full 24-bit color depth, but it is the responsibility
+ * of the respective driver to convert the color value to the correct format.
  */
-#define RGB( r, g, b )          ( ( ( ( r ) & 248 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 28 ) << 3 ) | ( ( b ) >> 3 ) )
+#define RGB( r, g, b )          ( ( r ) << 16 ) | ( ( g ) << 8 ) | ( b )
 /** \def COLOR_BLACK
  *  \brief Shorthand for RGB( 0, 0, 0 ).
  */
@@ -73,8 +75,10 @@
  */
 enum Orientation_enum
 {
-    PORTRAIT = 0, /**< Display height is bigger than its width. */ 
-    LANDSCAPE = 1, /**< Display width is bigger than its height. */
+    PORTRAIT = 0, /**< Display height is bigger than its width. View at 12 o'clock. */ 
+    LANDSCAPE = 1, /**< Display width is bigger than its height. View at 9 o'clock. */
+    PORTRAIT_REV = 2, /**< Display height is bigger than its width. View at 6 o'clock. */
+    LANDSCAPE_REV = 3, /**< Display width is bigger than its height. View at 3 o'clock. */
 };
 /** \typedef orientation_t
  *  \brief Convenience shortcut for display orientation.
@@ -125,6 +129,21 @@
  */
 typedef struct Font_struct font_metrics_t;
 
+/** \struct Bitmap_struct
+ *  \brief Describes an image.
+ */
+struct Bitmap_struct
+{
+    colordepth_t Format; /**< Color depth of the image. */
+    unsigned short Width; /**< Width of the image in pixels. */
+    unsigned short Height; /**< Height of the image in pixels. */
+    const void* PixelData; /**< Image pixel data. */
+};
+/** \typedef bitmap_t
+ *  \brief Convenience shortcut bitmap type.
+ */
+typedef struct Bitmap_struct bitmap_t;
+
 
 /** Base class for LCD implementations.
  *
@@ -148,8 +167,9 @@
      * This function is controller-specific and needs to be implemented
      * separately for each available display.
      * \param oritentation The display orientation, landscape is default.
+     * \param colors The correct color depth to use for the pixel data.
      */
-    virtual void Initialize( orientation_t orientation ) = 0;
+    virtual void Initialize( orientation_t orientation, colordepth_t colors ) = 0;
     
     /** Puts the display to sleep.
      *
@@ -187,10 +207,10 @@
      * in place, the new setting will be used for that single operation only and
      * will not change this value.
      *
-     * \param color The color to be used. The value must be in RGB-565 format.
+     * \param color The color to be used (24-bit color depth).
      * \sa #RGB(r,g,b)
      */
-    virtual void SetForeground( unsigned short color = COLOR_WHITE );
+    virtual void SetForeground( unsigned int color = COLOR_WHITE );
     
     /** Set the background color for painting.
      *
@@ -199,10 +219,10 @@
      * when the function is called, the new value will be used only for this
      * single call and will not change this setting.
      *
-     * \param color The background color as RGB-565 value.
+     * \param color The background color (24-bit color depth).
      * \sa #RGB(r,g,b)
      */
-    virtual void SetBackground( unsigned short color = COLOR_BLACK );
+    virtual void SetBackground( unsigned int color = COLOR_BLACK );
     
     /** Sets the font to be used for painting of text on the screen.
      * \param font A pointer to the font data.
@@ -240,7 +260,7 @@
      *
      * \param x The horizontal offset of the pixel from the upper left corner of the screen.
      * \param y The vertical offset of the pixel from the upper left corner of the screen.
-     * \param color The color to be used. Must be in RGB-565 format, or -1 for background and -2 for foreground color.
+     * \param color The color to be used. Use a custom color, or -1 for background and -2 for foreground color.
      */
     virtual void DrawPixel( unsigned short x, unsigned short y, int color = -2 );
     
@@ -250,7 +270,7 @@
      * \param y1 Vertical offset of the beginning point of the line.
      * \param x2 Horizontal offset of the end point of the line.
      * \param y2 Verical offset of the end point of the line.
-     * \param color The color to use for painting, in RGB-565 format, or -1 for background, or -2 for foreground.
+     * \param color The color to use for painting, or -1 for background, or -2 for foreground.
      */
     virtual void DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
     
@@ -260,7 +280,7 @@
      * \param y1 The vertical offset of the beginning point of one of the rectangle's diagonals.
      * \param x2 The horizontal offset of the end point of the same of the rectangle's diagonals.
      * \param y2 The vertical offset of the end point of the same of the rectangle's diagonals.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
     
@@ -270,7 +290,7 @@
      * \param y1 The vertical offset of the beginning point of one of the rectangle's diagonals.
      * \param x2 The horizontal offset of the end point of the same of the rectangle's diagonals.
      * \param y2 The vertical offset of the end point of the same of the rectangle's diagonals.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void DrawRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
     
@@ -280,7 +300,7 @@
      * \param y1 The vertical offset of the beginning point of one of the rectangle's diagonals.
      * \param x2 The horizontal offset of the end point of the same of the rectangle's diagonals.
      * \param y2 The vertical offset of the end point of the same of the rectangle's diagonals.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void FillRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
     
@@ -290,7 +310,7 @@
      * \param y1 The vertical offset of the beginning point of one of the rectangle's diagonals.
      * \param x2 The horizontal offset of the end point of the same of the rectangle's diagonals.
      * \param y2 The vertical offset of the end point of the same of the rectangle's diagonals.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void FillRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
     
@@ -299,7 +319,7 @@
      * \param x The offset of the circle's center from the left edge of the screen.
      * \param y The offset of the circle's center from the top edge of the screen.
      * \param radius The circle's radius.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void DrawCircle( unsigned short x, unsigned short y, unsigned short radius, int color = -2 );
     
@@ -308,7 +328,7 @@
      * \param x The offset of the circle's center from the left edge of the screen.
      * \param y The offset of the circle's center from the top edge of the screen.
      * \param radius The circle's radius.
-     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color in RGB-565 format.
+     * \param color The color to use for painting. -1 indicated background, -2 foreground, or custom color.
      */
     virtual void FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color = -2 );
     
@@ -333,13 +353,11 @@
      * http://henningkarlsen.com/electronics/library.php?id=52
      *
      * \param x Horizontal offset of the first pixel of the image.
-     * \param y Vertical offset of the first pixel of the image
-     * \param sx Width of the image.
-     * \param sy Height of the image.
-     * \param imgPixelData Image pixel array.
+     * \param y Vertical offset of the first pixel of the image.
+     * \param img Image data pointer.
      * \param scale A value of 1 will produce an image with its original size, while a different value will scale the image.
      */
-    virtual void DrawBitmap( unsigned short x, unsigned short y, unsigned short sx, unsigned short sy, const unsigned short* imgPixelData, unsigned char scale = 1 );
+    virtual void DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned char scale = 1 );
     
     /** Draw an image on the screen.
      *
@@ -350,15 +368,13 @@
      * http://henningkarlsen.com/electronics/library.php?id=52
      *
      * \param x Horizontal offset of the first pixel of the image.
-     * \param y Vertical offset of the first pixel of the image
-     * \param sx Width of the image.
-     * \param sy Height of the image.
-     * \param imgPixelData Image pixel array.
+     * \param y Vertical offset of the first pixel of the image.
+     * \param img Image data pointer.
      * \param deg Angle to rotate the image before painting on screen, in degrees.
      * \param rox
      * \param roy
      */
-    virtual void 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 );
+    virtual void DrawBitmap( unsigned short x, unsigned short y, const bitmap_t* img, unsigned short deg, unsigned short rox, unsigned short roy );
     
 protected:
     /** Creates an instance of the class.
@@ -434,10 +450,10 @@
     /** Sets the color of the pixel at the address pointer of the controller.
      *
      * This function is to be provided by each implementation separately in
-     * order to account for different color depth used by the controller.
+     * order to account for different color depths used by the controller.
      * \param color The color of the pixel.
      */
-    virtual void SetPixelColor( unsigned short color ) = 0;
+    virtual void SetPixelColor( unsigned int color ) = 0;
     
     /** Draws a horizontal line.
      *
@@ -448,7 +464,7 @@
      * \param y Y coordinate of the starting point of the line.
      * \param len Length of the line.
      * \param color The color to use to draw the line. By default the global foreground color is used ( -2 ),
-     *              -1 switches to the default background color, or any other RGB-565 color can be used.
+     *              -1 switches to the default background color, or any custom color can be used.
      */
     virtual void DrawHLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
     
@@ -461,7 +477,7 @@
      * \param y Y coordinate of the starting point of the line.
      * \param len Height of the line.
      * \param color The color to use to draw the line. By default the global foreground color is used ( -2 ),
-     *              -1 switches to the default background color, or any other RGB-565 color can be used.
+     *              -1 switches to the default background color, or any custom color can be used.
      */
     virtual void DrawVLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
     
@@ -471,9 +487,9 @@
      * \param x X coordinate of the character position.
      * \param y Y coordinate of the character position.
      * \param fgColor Foreground color for drawing. By default the global foreground color is used ( -2 ),
-     *                -1 switches to the default background color, or any other RGB-565 color can be used.
+     *                -1 switches to the default background color, or any custom color can be used.
      * \param bgColor Background color for drawing. By default the global background color is used ( -1 ),
-     *                -2 switches to the default foreground color, or any other RGB-565 color can be used.
+     *                -2 switches to the default foreground color, or any custom color can be used.
      */
     virtual void PrintChar( char c, unsigned short x, unsigned short y, int fgColor = -2, int bgColor = -1 );
     
@@ -484,9 +500,9 @@
      * \param y Y coordinate of the character position.
      * \param pos Position of the character in the string from which it originates (used to rotate a whole string).
      * \param fgColor Foreground color for drawing. By default the global foreground color is used ( -2 ),
-     *                -1 switches to the default background color, or any other RGB-565 color can be used.
+     *                -1 switches to the default background color, or any custom color can be used.
      * \param bgColor Background color for drawing. By default the global background color is used ( -1 ),
-     *                -2 switches to the default foreground color, or any other RGB-565 color can be used.
+     *                -2 switches to the default foreground color, or any custom color can be used.
      * \param deg The angle at which to rotate. 
      */
     virtual void RotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor = -2, int bgColor = -1, unsigned short deg = 0 );
@@ -495,7 +511,8 @@
     unsigned short  _disp_width, _disp_height;
     DigitalOut      _lcd_pin_cs, _lcd_pin_rs, _lcd_pin_reset;
     orientation_t   _orientation;
-    unsigned short  _foreground, _background;
+    colordepth_t    _colorDepth;
+    unsigned int    _foreground, _background;
     font_metrics_t  _font;
 };
 
--- a/ssd1289.cpp	Tue Dec 11 16:50:09 2012 +0000
+++ b/ssd1289.cpp	Tue Dec 11 18:11:14 2012 +0000
@@ -32,9 +32,10 @@
     else _lcd_pin_rd = 0;
 }
 
-void SSD1289_LCD::Initialize( orientation_t orientation )
+void SSD1289_LCD::Initialize( orientation_t orientation, colordepth_t colors )
 {
     _orientation = orientation;
+    _colorDepth = RGB16;
     
     _lcd_pin_reset = HIGH;
     wait_ms( 5 );
@@ -142,7 +143,12 @@
     WriteCmd( 0x22 );
 }
 
-void SSD1289_LCD::SetPixelColor( unsigned short color )
+void SSD1289_LCD::SetPixelColor( unsigned int color )
 {
-    WriteData( color );
+    unsigned char r, g, b;
+    r = ( color >> 16 ) & 0xFF;
+    g = ( color >> 8 ) & 0xFF;
+    b = color & 0xFF;
+    unsigned short clr = ( ( ( ( r ) & 0xF8 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 0x1C ) << 3 ) | ( ( b ) >> 3 ) );
+    WriteData( clr );
 }
--- a/ssd1289.h	Tue Dec 11 16:50:09 2012 +0000
+++ b/ssd1289.h	Tue Dec 11 18:11:14 2012 +0000
@@ -103,8 +103,9 @@
      * or produce garbage.
      *
      * \param oritentation The display orientation, landscape is default.
+     * \param colors The correct color depth to use for the pixel data. Value is disregarded.
      */
-    virtual void Initialize( orientation_t orientation = LANDSCAPE );
+    virtual void Initialize( orientation_t orientation = LANDSCAPE, colordepth_t colors = RGB16 );
     
     /** Puts the display to sleep.
      *
@@ -172,7 +173,7 @@
      * order to account for different color depth used by the controller.
      * \param color The color of the pixel.
      */
-    virtual void SetPixelColor( unsigned short color );
+    virtual void SetPixelColor( unsigned int color );
     
 private:
     DigitalOut  _lcd_pin_wr;
--- a/st7735.cpp	Tue Dec 11 16:50:09 2012 +0000
+++ b/st7735.cpp	Tue Dec 11 18:11:14 2012 +0000
@@ -29,9 +29,10 @@
     else _lcd_pin_bl = 0;
 }
 
-void ST7735_LCD::Initialize( orientation_t orientation )
+void ST7735_LCD::Initialize( orientation_t orientation, colordepth_t colors )
 {
     _orientation = orientation;
+    _colorDepth = colors;
     
     wait_ms( 100 );
     _lcd_pin_reset = HIGH;
@@ -148,8 +149,8 @@
     WriteCmd( 0xF6 ); // disable ram power save mode
     WriteByteData( 0x00 );
 
-    WriteCmd( 0x3A ); // interface pixel format (color mode)
-    WriteByteData( 0x05 ); // 65k mode
+    WriteCmd( 0x3A ); // interface pixel format (color mode): 0x05 => RGB16, 0x06 => RGB18
+    WriteByteData( _colorDepth == RGB16 ? 0x05 : 0x06 );
     WriteCmd( 0x29 ); // display on
 
     Deactivate();
@@ -216,9 +217,23 @@
     WriteCmd( 0x2c );
 }
 
-void ST7735_LCD::SetPixelColor( unsigned short color )
+void ST7735_LCD::SetPixelColor( unsigned int color )
 {
-    WriteData( color );
+    unsigned char r, g, b;
+    r = ( color >> 16 ) & 0xFF;
+    g = ( color >> 8 ) & 0xFF;
+    b = color & 0xFF;
+    if ( _colorDepth == RGB16 )
+    {
+        unsigned short clr = ( ( ( ( r ) & 0xF8 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 0x1C ) << 3 ) | ( ( b ) >> 3 ) );
+        WriteData( clr );
+    }
+    else
+    {
+        WriteByteData( r & 0xFC );
+        WriteByteData( g & 0xFC );
+        WriteByteData( b & 0xFC );
+    }
 }
 
 void ST7735_LCD::serializeByte( unsigned char data )
--- a/st7735.h	Tue Dec 11 16:50:09 2012 +0000
+++ b/st7735.h	Tue Dec 11 18:11:14 2012 +0000
@@ -97,8 +97,9 @@
      * or produce garbage.
      *
      * \param oritentation The display orientation, landscape is default.
+     * \param colors The correct color depth to use for the pixel data.
      */
-    virtual void Initialize( orientation_t orientation = LANDSCAPE );
+    virtual void Initialize( orientation_t orientation = LANDSCAPE, colordepth_t colors = RGB16 );
     
     /** Puts the display to sleep.
      *
@@ -174,7 +175,7 @@
      * order to account for different color depth used by the controller.
      * \param color The color of the pixel.
      */
-    virtual void SetPixelColor( unsigned short color );
+    virtual void SetPixelColor( unsigned int color );
     
 private:
     void serializeByte( unsigned char data );