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:   test SDCard capstone_display capstone_display_2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ssd1289.cpp Source File

ssd1289.cpp

00001 /*
00002  * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
00003  * Copyright (C)2012 Todor Todorov.
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Lesser General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2.1 of the License, or (at your option) any later version.
00009  *
00010  * This library is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Lesser General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Lesser General Public
00016  * License along with this library; if not, write to:
00017  *
00018  * Free Software Foundation, Inc.
00019  * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
00020  *
00021  *********************************************************************/
00022 #include "ssd1289.h"
00023 #include "helpers.h"
00024 
00025 SSD1289_LCD::SSD1289_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
00026     : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
00027 {
00028     _lcd_port = DATA_PORT;
00029     if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
00030     else _lcd_pin_rd = 0;
00031 }
00032 
00033 void SSD1289_LCD::Initialize( orientation_t orientation, colordepth_t colors )
00034 {
00035     _orientation = orientation;
00036     _colorDepth = colors;
00037     
00038     _lcd_pin_reset = HIGH;
00039     wait_ms( 5 );
00040     _lcd_pin_reset = LOW;
00041     wait_ms( 15 );
00042     _lcd_pin_reset = HIGH;
00043     _lcd_pin_cs = HIGH;
00044     if ( _lcd_pin_bl != 0 )
00045         *_lcd_pin_bl = HIGH;
00046     else if ( _bl_pwm != 0 )
00047         *_bl_pwm = _bl_pwm_default;
00048     if ( _lcd_pin_rd != 0 )
00049         *_lcd_pin_rd = HIGH;
00050     _lcd_pin_wr = HIGH;
00051     wait_ms( 15 );
00052     
00053     Activate();
00054     WriteCmdData( 0x00, 0x0001 ); // oscillator: 1 = on, 0 = off
00055     wait_ms( 1 );
00056     WriteCmdData( 0x03, 0xA8A4 ); // power control
00057     wait_ms( 1 );
00058     WriteCmdData( 0x0C, 0x0000 ); // power control 2
00059     wait_ms( 1 );
00060     WriteCmdData( 0x0D, 0x080C ); // power control 3
00061     wait_ms( 1 );
00062     WriteCmdData( 0x0E, 0x2B00 ); // power control 4
00063     wait_ms( 1 );
00064     WriteCmdData( 0x1E, 0x00B7 ); // power control 5
00065     wait_ms( 1 );
00066     WriteCmdData( 0x02, 0x0600 ); // driving waveform control
00067     wait_ms( 1 );
00068     WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
00069     wait_ms( 1 );
00070     if ( _colorDepth == RGB16 )
00071     {
00072         switch ( _orientation )
00073         {
00074             case LANDSCAPE: // works
00075                 WriteCmdData( 0x01, 0x293F ); // driver output control
00076                 wait_ms( 1 );
00077                 WriteCmdData( 0x11, 0x6078 ); // entry mode
00078                 break;
00079                 
00080             case PORTRAIT_REV: // works
00081                 WriteCmdData( 0x01, 0x693F ); // driver output control
00082                 wait_ms( 1 );
00083                 WriteCmdData( 0x11, 0x6070 ); // entry mode
00084                 break;
00085                 
00086             case LANDSCAPE_REV: // works
00087                 WriteCmdData( 0x01, 0x6B3F ); // driver output control
00088                 wait_ms( 1 );
00089                 WriteCmdData( 0x11, 0x6078 ); // entry mode
00090                 break;
00091                 
00092             case PORTRAIT: // works
00093             default:
00094                 WriteCmdData( 0x01, 0x2B3F ); // driver output control
00095                 wait_ms( 1 );
00096                 WriteCmdData( 0x11, 0x6070 ); // entry mode
00097                 break;
00098         }
00099     }
00100     else if ( _colorDepth == RGB18 )
00101     {
00102         switch ( _orientation )
00103         {
00104             case LANDSCAPE: // works
00105                 WriteCmdData( 0x01, 0x293F ); // driver output control
00106                 wait_ms( 1 );
00107                 WriteCmdData( 0x11, 0x4078 ); // entry mode
00108                 break;
00109                 
00110             case PORTRAIT_REV: // works
00111                 WriteCmdData( 0x01, 0x693F ); // driver output control
00112                 wait_ms( 1 );
00113                 WriteCmdData( 0x11, 0x4070 ); // entry mode
00114                 break;
00115                 
00116             case LANDSCAPE_REV: // works
00117                 WriteCmdData( 0x01, 0x6B3F ); // driver output control
00118                 wait_ms( 1 );
00119                 WriteCmdData( 0x11, 0x4078 ); // entry mode
00120                 break;
00121                 
00122             case PORTRAIT: // works
00123             default:
00124                 WriteCmdData( 0x01, 0x2B3F ); // driver output control
00125                 wait_ms( 1 );
00126                 WriteCmdData( 0x11, 0x4070 ); // entry mode
00127                 break;
00128         }
00129     }
00130     wait_ms( 1 );
00131     WriteCmdData( 0x05, 0x0000 ); // compare register
00132     wait_ms( 1 );
00133     WriteCmdData( 0x06, 0x0000 ); // compare register
00134     wait_ms( 1 );
00135     WriteCmdData( 0x16, 0xEF1C ); // horizontal porch
00136     wait_ms( 1 );
00137     WriteCmdData( 0x17, 0x0003 ); // vertical porch
00138     wait_ms( 1 );
00139     WriteCmdData( 0x07, 0x0233 ); // display control
00140     wait_ms( 1 );
00141     WriteCmdData( 0x0B, 0x0000 ); // frame cycle control
00142     wait_ms( 1 );
00143     WriteCmdData( 0x0F, 0x0000 ); // gate scan position
00144     wait_ms( 1 );
00145     WriteCmdData( 0x41, 0x0000 ); // vertical scroll control
00146     wait_ms( 1 );
00147     WriteCmdData( 0x42, 0x0000 ); // vertical scroll control
00148     wait_ms( 1 );
00149     WriteCmdData( 0x48, 0x0000 ); // 1st screen driving position
00150     wait_ms( 1 );
00151     WriteCmdData( 0x49, 0x013F ); // 1st screen driving position
00152     wait_ms( 1 );
00153     WriteCmdData( 0x4A, 0x0000 ); // 2nd screen driving position
00154     wait_ms( 1 );
00155     WriteCmdData( 0x4B, 0x0000 ); // 2nd screen driving position
00156     wait_ms( 1 );
00157     WriteCmdData( 0x44, 0xEF00 ); // horizontal ram address position
00158     wait_ms( 1 );
00159     WriteCmdData( 0x45, 0x0000 ); // vertical ram address position
00160     wait_ms( 1 );
00161     WriteCmdData( 0x46, 0x013F ); // vertical ram address position
00162     wait_ms( 1 );
00163     WriteCmdData( 0x30, 0x0707 ); // gamma control
00164     wait_ms( 1 );
00165     WriteCmdData( 0x31, 0x0204 ); // gamma control
00166     wait_ms( 1 );
00167     WriteCmdData( 0x32, 0x0204 ); // gamma control
00168     wait_ms( 1 );
00169     WriteCmdData( 0x33, 0x0502 ); // gamma control
00170     wait_ms( 1 );
00171     WriteCmdData( 0x34, 0x0507 ); // gamma control
00172     wait_ms( 1 );
00173     WriteCmdData( 0x35, 0x0204 ); // gamma control
00174     wait_ms( 1 );
00175     WriteCmdData( 0x36, 0x0204 ); // gamma control
00176     wait_ms( 1 );
00177     WriteCmdData( 0x37, 0x0502 ); // gamma control
00178     wait_ms( 1 );
00179     WriteCmdData( 0x3A, 0x0302 ); // gamma control
00180     wait_ms( 1 );
00181     WriteCmdData( 0x3B, 0x0302 ); // gamma control
00182     wait_ms( 1 );
00183     WriteCmdData( 0x23, 0x0000 ); // GRAM write mask for red and green pins
00184     wait_ms( 1 );
00185     WriteCmdData( 0x24, 0x0000 ); // GRAM write mask for blue pins
00186     wait_ms( 1 );
00187     WriteCmdData( 0x25, 0x8000 ); // frame frequency control
00188     wait_ms( 1 );
00189     WriteCmdData( 0x4e, 0x0000 ); // ram address set
00190     wait_ms( 1 );
00191     WriteCmdData( 0x4f, 0x0000 ); // ram address set
00192     wait_ms( 1 );
00193     WriteCmd( 0x22 ); // write GRAM
00194     Deactivate();
00195 }
00196 
00197 void SSD1289_LCD::Sleep( void )
00198 {
00199     WriteCmdData( 0x10, 0x0001 ); // sleep mode: 0 = exit, 1 = enter
00200     LCD::Sleep();
00201 }
00202 
00203 void SSD1289_LCD::WakeUp( void )
00204 {
00205     WriteCmdData( 0x10, 0x0000 ); // sleep mode: 0 = exit, 1 = enter
00206     LCD::WakeUp();
00207 }
00208 
00209 void SSD1289_LCD::WriteCmd( unsigned short cmd )
00210 {
00211     _lcd_pin_rs = LOW;
00212     _lcd_port->write( cmd );
00213     pulseLow( _lcd_pin_wr );
00214 }
00215 
00216 void SSD1289_LCD::WriteData( unsigned short data )
00217 {
00218     _lcd_pin_rs = HIGH;
00219     _lcd_port->write( data );
00220     pulseLow( _lcd_pin_wr );
00221 }
00222 
00223 void SSD1289_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
00224 {
00225     if ( _orientation == PORTRAIT || _orientation == PORTRAIT_REV )
00226     {
00227         WriteCmdData( 0x44, ( x2 << 8 ) + x1 );
00228         WriteCmdData( 0x45, y1 );
00229         WriteCmdData( 0x46, y2 );
00230         WriteCmdData( 0x4e, x1 );
00231         WriteCmdData( 0x4f, y1 );
00232     }
00233     else
00234     {
00235         WriteCmdData( 0x44, ( y2 << 8 ) + y1 );
00236         WriteCmdData( 0x45, x1 );
00237         WriteCmdData( 0x46, x2 );
00238         WriteCmdData( 0x4e, y1 );
00239         WriteCmdData( 0x4f, x1 );
00240     }
00241     WriteCmd( 0x22 );
00242 }
00243 
00244 void SSD1289_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
00245 {
00246     unsigned char r, g, b;
00247     unsigned short clr;
00248     if ( _colorDepth == RGB16 )
00249     {
00250         switch ( mode )
00251         {
00252             case RGB16:
00253                 WriteData( color & 0xFFFF );
00254                 break;
00255             case RGB18:
00256                 r = ( color >> 10 ) & 0xF8;
00257                 g = ( color >> 4 ) & 0xFC;
00258                 b = ( color >> 1 ) & 0x1F;
00259                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
00260                 WriteData( clr );
00261                 break;
00262             case RGB24:
00263                 r = ( color >> 16 ) & 0xF8;
00264                 g = ( color >> 8 ) & 0xFC;
00265                 b = color & 0xF8;
00266                 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
00267                 WriteData( clr );
00268                 break;
00269         }
00270     }
00271     else if ( _colorDepth == RGB18 )
00272     {
00273         switch ( mode )
00274         {
00275             case RGB16:
00276                 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
00277                 g = ( color >> 3 ) & 0xFC;
00278                 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
00279                 WriteData( ( r << 8 ) | g );
00280                 WriteData( b );
00281                 break;
00282             case RGB18:
00283                 b = ( color << 2 ) & 0xFC;
00284                 g = ( color >> 4 ) & 0xFC;
00285                 r = ( color >> 10 ) & 0xFC;
00286                 WriteData( ( r << 8 ) | g );
00287                 WriteData( b );
00288                 break;
00289             case RGB24:
00290                 r = ( color >> 16 ) & 0xFC;
00291                 g = ( color >> 8 ) & 0xFC;
00292                 b = color & 0xFC;
00293                 WriteData( ( r << 8 ) | g );
00294                 WriteData( b );
00295                 break;
00296         }
00297     }
00298 }