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

TFTLCD Library

NOTE (2013-03-25) Tested with both mbed LPC1768 and Freedom KL25Z. -todor

A TFT LCD driver library, which at the moment provides support for the following display controllers: HX8340-B (serial interface), SSD1289 (16-bit interface), ST7735-R (serial interface), ILI9325/ILI9328 (16-bit interface).

As I am acquiring and testing out new displays, I decided to combine all ported drivers into one library as with the original work done by Henning. However I also had as a goal to make the code maintenance and readability easier/better, so the code has been heavily refactored to make full use of C++ facilities as inheritance and access control. I extracted the common pieces of code into a base class, which every driver inherits and only the controller-specific side is provided in the actual driver - things like initialization, addressing, data transfer, etc.

Another nice extension is that the display's backlight can now be controlled through the driver. Either a simple on/off method of control could be selected, or the brightness can be set through use of PWM (the latter placing some restrictions on which pins can be used for this, as mbed offers hardware PWM on only 6 pins).

I also plan to add support for touch screens as part of the library. The goal is to grow this piece of software into a lightweight graphics widgets library, which can be used to construct control screens with buttons or menus in a speedy and intuitive way.

Changes

2013-07-21

  • Fixed the sleep/wake-up functions of the ILI9328 driver.

2013-06-15

  • Added driver for ILI9328 (works with ILI9325) controller, 16-bit data bus. Screen rotation works as usual with the TFTLCD library, but for now only RGB565 color depth is working and you can use both 65K and 262K color space with this driver. But for some reason the sleep function is not behaving as expected; I am working on this.
  • This is only on my to-do list for now - haven't really had the time yet - but I am going to refactor the library a bit to allow use of GPIO ports for data transfers instead of DigitalOut: faster and cleaner that way. For those who are using it already in a working design and cannot repurpose the pins anymore, the current way it's working will still be available; I am hoping not to tear up the public interfaces of the library (... too much). Anyway, since I am at it, I will also try to add support for multiple bus interfaces to drivers that support it (i.e. both 8bit and 16bit use of ILI932x or SSD1289). Thought this might be a good place to give you guys the heads-up.

2013-01-25

  • Replaced all existing fonts from the UTFT library with the free Terminus font. Two different sizes are provided: 8x12 pixels and 16x28 pixels. I found the old fonts not so good looking and then they supported only the ASCII codes in the range 30 (space) to 126 (the tilde ). The 7segment font didn't even implement anything else than the numbers from 0 to 9 - so it was unusable for anything (one couldn't even display the time or date as it lacked the colon [:] or the period [.] or the slash [/] or the space [ ] characters). So I completely revamped the fonts and added Terminus as the new default with its 2 sizes. Further more I added in both sizes most of the characters up to ASCII code 255. For any code not in there, the space character is substituted. In the case, when you already have provided your own fonts, please have a look at the API changes in the files <terminus.h> and <terminus.cpp>: I promise you whatever time you spent designing your own font, it is not wasted; you merely need to add a second array, which describes which ASCII codes are available in your font, and their byte offset in the original character bitmap array; and a struct to tie all parts together and describe the character size. I am sorry for breaking the old API, but you will like the change and new options it brings you. Now you can insert any char above 127 up to code 255 (if available, of course) with its hex representation, e.g displaying the current temperature would look something like 85\xB0 F or 26\xB0 C (the space in between degree and F or C is needed because both F and C are used in hex numbers, so \xB0F is interpreted as ASCII code 2831 instead of ASCII code 176, followed by the temperature scale denomination; if you insist on avoiding the space, you could write 85\xB0\x46 which will be displayed correctly as 85°F). You can either look up the ASCII code you need on Google or Bing, or just look at what's available - how it looks and its hex value - in the comments in <terminus.cpp>.
  • Added PWM backlight control. If you intend to use this, please make sure that control pin is either one of p21, p22, p23, p24, p25, or p26, as only they support hardware PWM. Please be aware that the mbed pins do not have much juice behind them, so if your display's backlight requires a lot of current, you are better off interfacing through as small signal transistor or a MOSFET. For the rest please consult the updated Doxygen documentation. NOTE The addition of PWM-controlled backlight will not break your existing code, the new options have default values, which initialize the used driver to behave as prior to PWM. Only if you want to use the new feature, some changes need to be made. The PWM is configured to be 120Hz (period of 8.33 milliseconds), in order to avoid noticeable flicker in the backlight. If in your opinion this value is too fine, then you can reduce the frequency in the LCD constructor in <lcd_base.cpp> by increasing the period value. My recommendation is to avoid frequencies lower than 60Hz.

2012-12-21

  • Internal-only changes in the way drivers transmit colors - done to simplify the bitmap drawing routines; client API remains unchanged.

2012-12-12

  • Added the driver for the ST7735 display controller.
  • Added the RGB18 color mode: choose between 16-bit (65K distinct colors) and 18-bit (262K distinct colors) color space [supported by all drivers]. NOTE This feature requires the image drawing functions to be changed, in order to account for differences between configured display color depth and the color depth of the image. Please review the API docs, in particular the new type bitmap_t and the DrawBitmap functions.
  • Changed display rotation to be achieved through the correspondent settings in the respective controller registers: no more software translation between width and height in different display orientations.
  • Extended the orientation options: PORTRAIT (top line to 12 o'clock/upright) and LANDSCAPE (top line to 9 o'clock) positions are the old options, PORTRAIT_REV (top line to 6 o'clock/upside-down) and LANDSCAPE_REV (top line to 3 o'clock) are the new orientations.
  • Added more pre-defined colors: available now are COLOR_BLACK, COLOR_WHITE, COLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_CYAN, COLOR_MAGENTA and COLOR_YELLOW.

TODO

  • Finish implementing PWM-controlled backlight (current-sink configuration).
  • Add a driver for the HX8352-A controller (ITDB02-3.2WD 16:9 240x400 pixel resolution display).

How to Use

The code is documented, so please review the API docs. There is a simple example to get you started...

Committer:
ttodorov
Date:
Mon Jul 22 01:48:06 2013 +0000
Revision:
27:26491d710e72
Parent:
26:28f64fbcf7df
- fix sleep/wakeup of ILI9328 driver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ttodorov 23:eca4414196ca 1 /*
ttodorov 23:eca4414196ca 2 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
ttodorov 23:eca4414196ca 3 * Copyright (C)2012-2013 Todor Todorov.
ttodorov 23:eca4414196ca 4 *
ttodorov 23:eca4414196ca 5 * This library is free software; you can redistribute it and/or
ttodorov 23:eca4414196ca 6 * modify it under the terms of the GNU Lesser General Public
ttodorov 23:eca4414196ca 7 * License as published by the Free Software Foundation; either
ttodorov 23:eca4414196ca 8 * version 2.1 of the License, or (at your option) any later version.
ttodorov 23:eca4414196ca 9 *
ttodorov 23:eca4414196ca 10 * This library is distributed in the hope that it will be useful,
ttodorov 23:eca4414196ca 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
ttodorov 23:eca4414196ca 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
ttodorov 23:eca4414196ca 13 * Lesser General Public License for more details.
ttodorov 23:eca4414196ca 14 *
ttodorov 23:eca4414196ca 15 * You should have received a copy of the GNU Lesser General Public
ttodorov 23:eca4414196ca 16 * License along with this library; if not, write to:
ttodorov 23:eca4414196ca 17 *
ttodorov 23:eca4414196ca 18 * Free Software Foundation, Inc.
ttodorov 23:eca4414196ca 19 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
ttodorov 23:eca4414196ca 20 *
ttodorov 23:eca4414196ca 21 *********************************************************************/
ttodorov 23:eca4414196ca 22 #include "ili9328.h"
ttodorov 23:eca4414196ca 23 #include "helpers.h"
ttodorov 23:eca4414196ca 24
ttodorov 23:eca4414196ca 25 ILI9328_LCD::ILI9328_LCD( PinName CS, PinName RESET, PinName RS, PinName WR, BusOut* DATA_PORT, PinName BL, PinName RD, backlight_t blType, float defaultBackLightLevel )
ttodorov 23:eca4414196ca 26 : LCD( 240, 320, CS, RS, RESET, BL, blType, defaultBackLightLevel ), _lcd_pin_wr( WR )
ttodorov 23:eca4414196ca 27 {
ttodorov 23:eca4414196ca 28 _lcd_port = DATA_PORT;
ttodorov 23:eca4414196ca 29 if ( RD != NC ) _lcd_pin_rd = new DigitalOut( RD );
ttodorov 23:eca4414196ca 30 else _lcd_pin_rd = 0;
ttodorov 23:eca4414196ca 31 }
ttodorov 23:eca4414196ca 32
ttodorov 23:eca4414196ca 33 void ILI9328_LCD::Initialize( orientation_t orientation, colordepth_t colors )
ttodorov 23:eca4414196ca 34 {
ttodorov 23:eca4414196ca 35 _orientation = orientation;
ttodorov 23:eca4414196ca 36 _colorDepth = colors;
ttodorov 23:eca4414196ca 37
ttodorov 23:eca4414196ca 38 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 39 wait_ms( 50 );
ttodorov 23:eca4414196ca 40 _lcd_pin_reset = LOW;
ttodorov 23:eca4414196ca 41 wait_ms( 100 );
ttodorov 23:eca4414196ca 42 _lcd_pin_reset = HIGH;
ttodorov 23:eca4414196ca 43 wait_ms( 1000 );
ttodorov 23:eca4414196ca 44 _lcd_pin_cs = HIGH;
ttodorov 23:eca4414196ca 45 if ( _lcd_pin_bl != 0 )
ttodorov 23:eca4414196ca 46 *_lcd_pin_bl = HIGH;
ttodorov 23:eca4414196ca 47 else if ( _bl_pwm != 0 )
ttodorov 23:eca4414196ca 48 *_bl_pwm = _bl_pwm_default;
ttodorov 23:eca4414196ca 49 if ( _lcd_pin_rd != 0 )
ttodorov 23:eca4414196ca 50 *_lcd_pin_rd = HIGH;
ttodorov 23:eca4414196ca 51 _lcd_pin_wr = HIGH;
ttodorov 23:eca4414196ca 52 wait_ms( 15 );
ttodorov 23:eca4414196ca 53
ttodorov 23:eca4414196ca 54 Activate();
ttodorov 25:6cffb758c075 55
ttodorov 25:6cffb758c075 56 short drivOut = 0;
ttodorov 25:6cffb758c075 57 short entryMod = 0;
ttodorov 25:6cffb758c075 58 short gateScan = 0x2700;
ttodorov 24:ac6e35658037 59 switch ( _orientation )
ttodorov 24:ac6e35658037 60 {
ttodorov 25:6cffb758c075 61 case LANDSCAPE:
ttodorov 25:6cffb758c075 62 drivOut = 0x0100;
ttodorov 25:6cffb758c075 63 entryMod |= 0x0038;
ttodorov 25:6cffb758c075 64 gateScan |= 0x0000;
ttodorov 25:6cffb758c075 65 break;
ttodorov 25:6cffb758c075 66
ttodorov 25:6cffb758c075 67 case LANDSCAPE_REV:
ttodorov 25:6cffb758c075 68 drivOut = 0x0000;
ttodorov 25:6cffb758c075 69 entryMod |= 0x0038;
ttodorov 25:6cffb758c075 70 gateScan |= 0x8000;
ttodorov 25:6cffb758c075 71 break;
ttodorov 25:6cffb758c075 72
ttodorov 25:6cffb758c075 73 case PORTRAIT_REV:
ttodorov 25:6cffb758c075 74 drivOut = 0x0000;
ttodorov 25:6cffb758c075 75 entryMod |= 0x0030;
ttodorov 25:6cffb758c075 76 gateScan |= 0x0000;
ttodorov 25:6cffb758c075 77 break;
ttodorov 25:6cffb758c075 78
ttodorov 24:ac6e35658037 79 case PORTRAIT:
ttodorov 24:ac6e35658037 80 default:
ttodorov 25:6cffb758c075 81 drivOut = 0x0100;
ttodorov 25:6cffb758c075 82 entryMod |= 0x0030;
ttodorov 25:6cffb758c075 83 gateScan |= 0x8000;
ttodorov 24:ac6e35658037 84 break;
ttodorov 24:ac6e35658037 85 }
ttodorov 24:ac6e35658037 86 switch ( _colorDepth )
ttodorov 24:ac6e35658037 87 {
ttodorov 26:28f64fbcf7df 88 case RGB18:
ttodorov 26:28f64fbcf7df 89 entryMod |= 0x9000;
ttodorov 26:28f64fbcf7df 90 break;
ttodorov 26:28f64fbcf7df 91
ttodorov 24:ac6e35658037 92 case RGB16:
ttodorov 24:ac6e35658037 93 default:
ttodorov 25:6cffb758c075 94 entryMod |= 0x1000;
ttodorov 24:ac6e35658037 95 break;
ttodorov 24:ac6e35658037 96 }
ttodorov 24:ac6e35658037 97
ttodorov 24:ac6e35658037 98 WriteCmdData( 0xE5, 0x78F0 ); // set SRAM internal timing
ttodorov 25:6cffb758c075 99 WriteCmdData( 0x01, drivOut ); // set Driver Output Control
ttodorov 24:ac6e35658037 100 WriteCmdData( 0x02, 0x0200 ); // set 1 line inversion
ttodorov 25:6cffb758c075 101 WriteCmdData( 0x03, entryMod ); // set GRAM write direction and BGR=1.
ttodorov 24:ac6e35658037 102 WriteCmdData( 0x04, 0x0000 ); // Resize register
ttodorov 24:ac6e35658037 103 WriteCmdData( 0x08, 0x0207 ); // set the back porch and front porch
ttodorov 24:ac6e35658037 104 WriteCmdData( 0x09, 0x0000 ); // set non-display area refresh cycle ISC[3:0]
ttodorov 24:ac6e35658037 105 WriteCmdData( 0x0A, 0x0000 ); // FMARK function
ttodorov 24:ac6e35658037 106 WriteCmdData( 0x0C, 0x0000 ); // RGB interface setting
ttodorov 24:ac6e35658037 107 WriteCmdData( 0x0D, 0x0000 ); // Frame marker Position
ttodorov 24:ac6e35658037 108 WriteCmdData( 0x0F, 0x0000 ); // RGB interface polarity
ttodorov 24:ac6e35658037 109 // ----------- Power On sequence ----------- //
ttodorov 24:ac6e35658037 110 WriteCmdData( 0x10, 0x0000 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
ttodorov 24:ac6e35658037 111 WriteCmdData( 0x11, 0x0007 ); // DC1[2:0], DC0[2:0], VC[2:0]
ttodorov 24:ac6e35658037 112 WriteCmdData( 0x12, 0x0000 ); // VREG1OUT voltage
ttodorov 24:ac6e35658037 113 WriteCmdData( 0x13, 0x0000 ); // VDV[4:0] for VCOM amplitude
ttodorov 24:ac6e35658037 114 WriteCmdData( 0x07, 0x0001 );
ttodorov 24:ac6e35658037 115 wait_ms( 200 ); // Dis-charge capacitor power voltage
ttodorov 24:ac6e35658037 116 WriteCmdData( 0x10, 0x1690 ); // SAP, BT[3:0], AP, DSTB, SLP, STB
ttodorov 24:ac6e35658037 117 WriteCmdData( 0x11, 0x0227 ); // Set DC1[2:0], DC0[2:0], VC[2:0]
ttodorov 24:ac6e35658037 118 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 119 WriteCmdData( 0x12, 0x000D ); // 0012
ttodorov 24:ac6e35658037 120 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 121 WriteCmdData( 0x13, 0x1200 ); // VDV[4:0] for VCOM amplitude
ttodorov 24:ac6e35658037 122 WriteCmdData( 0x29, 0x000A ); // 04 VCM[5:0] for VCOMH
ttodorov 24:ac6e35658037 123 WriteCmdData( 0x2B, 0x000D ); // Set Frame Rate
ttodorov 24:ac6e35658037 124 wait_ms( 50 ); // Delay 50ms
ttodorov 24:ac6e35658037 125 WriteCmdData( 0x20, 0x0000 ); // GRAM horizontal Address
ttodorov 24:ac6e35658037 126 WriteCmdData( 0x21, 0x0000 ); // GRAM Vertical Address
ttodorov 24:ac6e35658037 127 // ----------- Adjust the Gamma Curve ----------//
ttodorov 24:ac6e35658037 128 WriteCmdData( 0x30, 0x0000 );
ttodorov 24:ac6e35658037 129 WriteCmdData( 0x31, 0x0404 );
ttodorov 24:ac6e35658037 130 WriteCmdData( 0x32, 0x0003 );
ttodorov 24:ac6e35658037 131 WriteCmdData( 0x35, 0x0405 );
ttodorov 24:ac6e35658037 132 WriteCmdData( 0x36, 0x0808 );
ttodorov 24:ac6e35658037 133 WriteCmdData( 0x37, 0x0407 );
ttodorov 24:ac6e35658037 134 WriteCmdData( 0x38, 0x0303 );
ttodorov 24:ac6e35658037 135 WriteCmdData( 0x39, 0x0707 );
ttodorov 24:ac6e35658037 136 WriteCmdData( 0x3C, 0x0504 );
ttodorov 24:ac6e35658037 137 WriteCmdData( 0x3D, 0x0808 );
ttodorov 24:ac6e35658037 138 //------------------ Set GRAM area ---------------//
ttodorov 24:ac6e35658037 139 WriteCmdData( 0x50, 0x0000 ); // Horizontal GRAM Start Address
ttodorov 24:ac6e35658037 140 WriteCmdData( 0x51, 0x00EF ); // Horizontal GRAM End Address
ttodorov 24:ac6e35658037 141 WriteCmdData( 0x52, 0x0000 ); // Vertical GRAM Start Address
ttodorov 24:ac6e35658037 142 WriteCmdData( 0x53, 0x013F ); // Vertical GRAM Start Address
ttodorov 25:6cffb758c075 143 WriteCmdData( 0x60, gateScan ); // Gate Scan Line (0xA700)
ttodorov 24:ac6e35658037 144 WriteCmdData( 0x61, 0x0000 ); // NDL,VLE, REV
ttodorov 24:ac6e35658037 145 WriteCmdData( 0x6A, 0x0000 ); // set scrolling line
ttodorov 24:ac6e35658037 146 //-------------- Partial Display Control ---------//
ttodorov 24:ac6e35658037 147 WriteCmdData( 0x80, 0x0000 );
ttodorov 24:ac6e35658037 148 WriteCmdData( 0x81, 0x0000 );
ttodorov 24:ac6e35658037 149 WriteCmdData( 0x82, 0x0000 );
ttodorov 24:ac6e35658037 150 WriteCmdData( 0x83, 0x0000 );
ttodorov 24:ac6e35658037 151 WriteCmdData( 0x84, 0x0000 );
ttodorov 24:ac6e35658037 152 WriteCmdData( 0x85, 0x0000 );
ttodorov 24:ac6e35658037 153 //-------------- Panel Control -------------------//
ttodorov 24:ac6e35658037 154 WriteCmdData( 0x90, 0x0010 );
ttodorov 24:ac6e35658037 155 WriteCmdData( 0x92, 0x0000 );
ttodorov 24:ac6e35658037 156 WriteCmdData( 0x07, 0x0133 ); // 262K color and display ON
ttodorov 24:ac6e35658037 157
ttodorov 23:eca4414196ca 158 Deactivate();
ttodorov 23:eca4414196ca 159 }
ttodorov 23:eca4414196ca 160
ttodorov 23:eca4414196ca 161 void ILI9328_LCD::Sleep( void )
ttodorov 23:eca4414196ca 162 {
ttodorov 27:26491d710e72 163 Activate();
ttodorov 27:26491d710e72 164 WriteCmdData( 0x10, 0x1692 ); // enter sleep mode
ttodorov 27:26491d710e72 165 wait_ms( 200 );
ttodorov 23:eca4414196ca 166 LCD::Sleep();
ttodorov 27:26491d710e72 167 Deactivate();
ttodorov 23:eca4414196ca 168 }
ttodorov 23:eca4414196ca 169
ttodorov 23:eca4414196ca 170 void ILI9328_LCD::WakeUp( void )
ttodorov 23:eca4414196ca 171 {
ttodorov 27:26491d710e72 172 Activate();
ttodorov 27:26491d710e72 173 WriteCmdData( 0x10, 0x1690 ); // exit sleep mode
ttodorov 27:26491d710e72 174 wait_ms( 200 );
ttodorov 23:eca4414196ca 175 LCD::WakeUp();
ttodorov 27:26491d710e72 176 Deactivate();
ttodorov 23:eca4414196ca 177 }
ttodorov 23:eca4414196ca 178
ttodorov 23:eca4414196ca 179 void ILI9328_LCD::WriteCmd( unsigned short cmd )
ttodorov 23:eca4414196ca 180 {
ttodorov 23:eca4414196ca 181 _lcd_pin_rs = LOW;
ttodorov 23:eca4414196ca 182 _lcd_port->write( cmd );
ttodorov 23:eca4414196ca 183 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 184 }
ttodorov 23:eca4414196ca 185
ttodorov 23:eca4414196ca 186 void ILI9328_LCD::WriteData( unsigned short data )
ttodorov 23:eca4414196ca 187 {
ttodorov 23:eca4414196ca 188 _lcd_pin_rs = HIGH;
ttodorov 23:eca4414196ca 189 _lcd_port->write( data );
ttodorov 23:eca4414196ca 190 pulseLow( _lcd_pin_wr );
ttodorov 23:eca4414196ca 191 }
ttodorov 23:eca4414196ca 192
ttodorov 23:eca4414196ca 193 void ILI9328_LCD::SetXY( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2 )
ttodorov 23:eca4414196ca 194 {
ttodorov 25:6cffb758c075 195 switch ( _orientation )
ttodorov 25:6cffb758c075 196 {
ttodorov 25:6cffb758c075 197 case LANDSCAPE:
ttodorov 25:6cffb758c075 198 case LANDSCAPE_REV:
ttodorov 25:6cffb758c075 199 WriteCmdData( 0x20, y1 );
ttodorov 25:6cffb758c075 200 WriteCmdData( 0x21, x1 );
ttodorov 25:6cffb758c075 201 WriteCmdData( 0x50, y1 );
ttodorov 25:6cffb758c075 202 WriteCmdData( 0x52, x1 );
ttodorov 25:6cffb758c075 203 WriteCmdData( 0x51, y2 );
ttodorov 25:6cffb758c075 204 WriteCmdData( 0x53, x2 );
ttodorov 25:6cffb758c075 205 break;
ttodorov 25:6cffb758c075 206
ttodorov 25:6cffb758c075 207 case PORTRAIT_REV:
ttodorov 25:6cffb758c075 208 case PORTRAIT:
ttodorov 25:6cffb758c075 209 default:
ttodorov 25:6cffb758c075 210 WriteCmdData( 0x20, x1 );
ttodorov 25:6cffb758c075 211 WriteCmdData( 0x21, y1 );
ttodorov 25:6cffb758c075 212 WriteCmdData( 0x50, x1 );
ttodorov 25:6cffb758c075 213 WriteCmdData( 0x52, y1 );
ttodorov 25:6cffb758c075 214 WriteCmdData( 0x51, x2 );
ttodorov 25:6cffb758c075 215 WriteCmdData( 0x53, y2 );
ttodorov 25:6cffb758c075 216 break;
ttodorov 25:6cffb758c075 217 }
ttodorov 23:eca4414196ca 218 WriteCmd( 0x22 );
ttodorov 23:eca4414196ca 219 }
ttodorov 23:eca4414196ca 220
ttodorov 23:eca4414196ca 221 void ILI9328_LCD::SetPixelColor( unsigned int color, colordepth_t mode )
ttodorov 23:eca4414196ca 222 {
ttodorov 23:eca4414196ca 223 unsigned char r, g, b;
ttodorov 23:eca4414196ca 224 unsigned short clr;
ttodorov 26:28f64fbcf7df 225 r = g = b = 0;
ttodorov 23:eca4414196ca 226 if ( _colorDepth == RGB16 )
ttodorov 23:eca4414196ca 227 {
ttodorov 23:eca4414196ca 228 switch ( mode )
ttodorov 23:eca4414196ca 229 {
ttodorov 23:eca4414196ca 230 case RGB16:
ttodorov 23:eca4414196ca 231 WriteData( color & 0xFFFF );
ttodorov 23:eca4414196ca 232 break;
ttodorov 23:eca4414196ca 233 case RGB18:
ttodorov 23:eca4414196ca 234 r = ( color >> 10 ) & 0xF8;
ttodorov 23:eca4414196ca 235 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 236 b = ( color >> 1 ) & 0x1F;
ttodorov 23:eca4414196ca 237 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | b );
ttodorov 23:eca4414196ca 238 WriteData( clr );
ttodorov 23:eca4414196ca 239 break;
ttodorov 23:eca4414196ca 240 case RGB24:
ttodorov 23:eca4414196ca 241 r = ( color >> 16 ) & 0xF8;
ttodorov 23:eca4414196ca 242 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 243 b = color & 0xF8;
ttodorov 23:eca4414196ca 244 clr = ( ( r | ( g >> 5 ) ) << 8 ) | ( ( g << 3 ) | ( b >> 3 ) );
ttodorov 23:eca4414196ca 245 WriteData( clr );
ttodorov 23:eca4414196ca 246 break;
ttodorov 23:eca4414196ca 247 }
ttodorov 23:eca4414196ca 248 }
ttodorov 23:eca4414196ca 249 else if ( _colorDepth == RGB18 )
ttodorov 23:eca4414196ca 250 {
ttodorov 23:eca4414196ca 251 switch ( mode )
ttodorov 23:eca4414196ca 252 {
ttodorov 23:eca4414196ca 253 case RGB16:
ttodorov 23:eca4414196ca 254 r = ( ( color >> 8 ) & 0xF8 ) | ( ( color & 0x8000 ) >> 13 );
ttodorov 23:eca4414196ca 255 g = ( color >> 3 ) & 0xFC;
ttodorov 23:eca4414196ca 256 b = ( ( color << 3 ) & 0xFC ) | ( ( color >> 3 ) & 0x01 );
ttodorov 23:eca4414196ca 257 break;
ttodorov 23:eca4414196ca 258 case RGB18:
ttodorov 23:eca4414196ca 259 b = ( color << 2 ) & 0xFC;
ttodorov 23:eca4414196ca 260 g = ( color >> 4 ) & 0xFC;
ttodorov 23:eca4414196ca 261 r = ( color >> 10 ) & 0xFC;
ttodorov 23:eca4414196ca 262 break;
ttodorov 23:eca4414196ca 263 case RGB24:
ttodorov 23:eca4414196ca 264 r = ( color >> 16 ) & 0xFC;
ttodorov 23:eca4414196ca 265 g = ( color >> 8 ) & 0xFC;
ttodorov 23:eca4414196ca 266 b = color & 0xFC;
ttodorov 23:eca4414196ca 267 break;
ttodorov 23:eca4414196ca 268 }
ttodorov 26:28f64fbcf7df 269 clr = ( r << 8 ) | ( g << 2 ) | ( b >> 4 );
ttodorov 26:28f64fbcf7df 270 WriteData( clr );
ttodorov 26:28f64fbcf7df 271 WriteData( b << 4 );
ttodorov 23:eca4414196ca 272 }
ttodorov 23:eca4414196ca 273 }