Skip Navigation LinksProjects > LPC1343 Reference Design > Tutorial: Using the TFT LCD API (v0.8.5)

Using the TFT LCD API (v0.8.5)

Using the included drawing routines for TFT LCDs in v0.8.5 of the LPC1343 Code Base

TFT LCD API Drawing ExamplesThe LPC1343 Code Base includes generic drawing routines that can be used with a variety of TFT LCDs.  The core drawing routines (/drivers/lcd/tft/drawing.h) are seperated from the actual HW interface (/drivers/lcd/tft/hw/*) so that the LCD can easily be changed without having to significantly modify your project code.  When possible, the underlying HW interface provides optimised methods for drawing primitive shapes such as horizontal and vertical lines, fill the LCD with one color, shifting the display contents, etc.  This document gives a summary of the generic drawing routines currently available in the LPC1343 Code Base, and is based on v0.85 which was the current release when this tutorial was written.

Supported HW: There are a number of LCD drivers present in the "drivers/lcd/tft/hw" folder, but the only complete and supported driver in the LPC1343 Code Based is for the ILI9325, for 240x320 pixel 16-bit TFT LCDs using an 8-bit interface. Other drivers should be considered experimental and are missing some features or are incomplete (as of v0.85). They may, however, serve as a starting point to developping other LCD drivers that work with the LPC1343 Code Base and can directly use the API described in this document. Our intention, of course, is to continue developping new drivers to support a number of new LCDs as well.

Configuring the LPC1343 Code Base for TFT LCDs

In order to make use of the TFT LCD in the LPC1343 Code Base, "CFG_TFTLCD" must be defined in projectconfig.h when compiling!

There are two ways to do this: assuming that you are using the LPC1343 Reference Design Base Board and the TFT LCD Expansion Board. The first way is to manually uncomment "#define CFG_TFTLCD" in projectconfig.h.  The second way to select a "global' board config setting at the top of the config file, which will configure the entire file to match a specific board combination (also including a FAT32 file system for the micro-SD connector, etc.), which may or may not be what you want. Both of the TFTLCDSTANDALONE board config settings enable the TFTLCD amongst other things:

/*=========================================================================
    BOARD SELECTION

    Because several boards use this code library with sometimes slightly
    different pin configuration, you will need to specify which board you
    are using by enabling one of the following definitions. The code base
    will then try to configure itself accordingly for that board.
    -----------------------------------------------------------------------*/
    // #define CFG_BRD_LPC1343_REFDESIGN
    #define CFG_BRD_LPC1343_TFTLCDSTANDALONE_USB
    // #define CFG_BRD_LPC1343_TFTLCDSTANDALONE_UART
    // #define CFG_BRD_LPC1343_802154USBSTICK
/*=========================================================================*/

Be sure to clean the solution ("make clean") and then rebuild it ("make all") after making these changes.

Selecting an LCD driver

While the only fully implemented and supported LCD drivers are the ILI9325/ILI9328 (which are register compatible and interchangeable with each other), you can use another beta LCD driver or specify your own LCD driver by modifying the make file as follows:

# LCD Driver (Only one can be included at a time!)
OBJS += ILI9328.o
# OBJS += ILI9325.o
# OBJS += st7735.o
# OBJS += st7783.o

Simply select the LCD driver that corresponds to your LCD, though be sure to only select one or you will get duplicate defition errors during the build process. All of the source files can be found in "/drivers/lcd/tft/hw/"

Sample Code

While most of the functions described below also include sample code, there are a number of additional code samples for the TFT LCD API in the LPC1343 Code Base (as of v0.90 and higher). Look in the "/tools/examples/lcd/tft" folder for more information.

Drawing Primitives

Basic operations like setting pixels and drawing simple shapes are covered by the following generic functions:

void drawPixel(uint16_t x, uint16_t y, uint16_t color)

Sets a single pixel at the specified x,y location using the supplied RGB565 color.

#include "drivers/lcd/tft/drawing.h"
...
// Draw a white pixel at X:20, Y:50
drawPixel(20, 50, COLOR_WHITE);
void drawFill(uint16_t color)

Fills the entire LCD screen with the specified RGB565 Color

#include "drivers/lcd/tft/drawing.h"
...
drawFill(COLOR_WHITE);
void drawLine ( uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color )

Draws a line between two locations on the LCD screen. (Note: If the line is a straight horizontal or vertical line, this function will automatically use an optimised drawing method in the specific LCD driver.)

#include "drivers/lcd/tft/drawing.h"
...
// Draw a green line from 10, 1 to 96, 118
drawLine(10, 1, 96, 118, COLOR_GREEN);
void drawLineDotted ( uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t empty, uint16_t solid, uint16_t color )

Draws a line with a fixed pattern of empty and solid pixels.

Sample button

#include "drivers/lcd/tft/drawing.h"
...
  // Background
  drawFill(COLOR_WHITE);
  // Solid line
  drawLine(10, 10, 230, 10, COLOR_BLACK);
  // 1x1 pattern
  drawLineDotted(10, 15, 230, 15, 1, 1, COLOR_BLACK);
  // 2x2 pattern
  drawLineDotted(10, 20, 230, 20, 2, 2, COLOR_BLACK);
  // 3x3 pattern
  drawLineDotted(10, 25, 230, 25, 3, 3, COLOR_BLACK);
  // 3x5 pattern
  drawLineDotted(10, 30, 230, 30, 3, 5, COLOR_BLACK);
  // 5x10 pattern
  drawLineDotted(10, 35, 230, 35, 5, 10, COLOR_BLACK);

Note: This function was added in v0.92 of the LPC1343 Code Base.

void drawCircle (uint16_t xCenter, uint16_t yCenter, uint16_t radius, uint16_t color)

Draws a circle with the specified radius using the supplied x and y co-ordinates as the center point.

#include "drivers/lcd/tft/drawing.h"
...
// Draw a white circle with a 25 pixel radius centered on X:50, Y:50
drawCircle(50, 50, 25, COLOR_WHITE);
void drawCircleFilled (uint16_t xCenter, uint16_t yCenter, uint16_t radius, uint16_t color)

Identical to drawCircle, but also fills the interioir of the circle

#include "drivers/lcd/tft/drawing.h"
...
// Draw a solid white circle with a 25 pixel radius centered on X:50, Y:50
drawCircleFilled(50, 50, 25, COLOR_WHITE);
void drawArrow(uint16_t x, uint16_t y, uint16_t size, drawDirection_t direction, uint16_t color)

Draws a rectangular 'arrow' points in the specified direction (useful for scrollbars, selection menus, etc.). Note that the arrow will be drawn starting at the 1-pixel 'tip' and will expand outward from this point until the specified size is reached. You may need to adjust the starting 'y' position to place UP and DOWN arrows side by side.

The direction of the arrow is specified with one of the following values (enum located in drawing.h):

DRAW_DIRECTION_LEFT
DRAW_DIRECTION_RIGHT
DRAW_DIRECTION_UP
DRAW_DIRECTION_DOWN
#include "drivers/lcd/tft/drawing.h"
...
// Draw left and right arrows
drawArrow(105, 65, 7, DRAW_DIRECTION_LEFT, TEXTCOLORACTIVE);
drawArrow(220, 65, 7, DRAW_DIRECTION_RIGHT, TEXTCOLORACTIVE);

NOTE: This function was added in v0.92 of the LPC1343 Code Base.

void drawRectangle ( uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)

Draws a rectangle using the specified co-ordinates

#include "drivers/lcd/tft/drawing.h"
...
// Draw a white rectangle from 10, 10 to 100, 35
drawRectangle(10, 10, 100, 35, COLOR_WHITE);
void drawRectangleFilled ( uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color)

Draws a filled rectangle using the specified co-ordinates

#include "drivers/lcd/tft/drawing.h"
...
// Draw a solid blue rectangle from 10, 10 to 100, 35
drawRectangleFilled(10, 10, 100, 25, COLOR_BLUE);
void drawRectangleRounded ( uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t color, uint16_t radius, drawRoundedCorners_t corners )

Draws a filled rectangle with rounded corners using the specified radius and location.

The following values can be used to indicate which corners should be 'rounded' (enum defined in drawing.h):

DRAW_ROUNDEDCORNERS_NONE
DRAW_ROUNDEDCORNERS_ALL
DRAW_ROUNDEDCORNERS_TOP
DRAW_ROUNDEDCORNERS_BOTTOM
DRAW_ROUNDEDCORNERS_LEFT
DRAW_ROUNDEDCORNERS_RIGHT
#include "drivers/lcd/tft/drawing.h"
...
// Draw a rectangle with all four corners rounded using a
// 10-pixel radius, solid blue fill, from 10,10 to 200,80
drawRectangleRounded(10, 10, 200, 80, COLOR_BLUE, 10, DRAW_ROUNDEDCORNERS_ALL);

NOTE: This function was added in v0.92 of the LPC1343 Code Base.

void drawTriangle(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)

Draws the outline of a triangle using the three supplied vertices.

#include "drivers/lcd/tft/drawing.h"
...
drawTriangle ( 100, 10, 20, 120, 230, 290, COLOR_WHITE);

NOTE: This function was added in v0.93 of the LPC1343 Code Base.

void drawTriangleFilled(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)

Draws a filled triangle using the three supplied vertices.

#include "drivers/lcd/tft/drawing.h"
...
drawTriangleFilled ( 100, 10, 20, 120, 230, 290, COLOR_WHITE);
// Draw black circles at each point of the triangle
drawCircleFilled(100, 10, 2, COLOR_BLACK);
drawCircleFilled(20, 120, 2, COLOR_BLACK);
drawCircleFilled(230, 290, 2, COLOR_BLACK);

NOTE: This function was added in v0.93 of the LPC1343 Code Base.

uint16_t drawRGB24toRGB565(uint8_t r, uint8_t g, uint8_t b)

Converts an RGB24 color (8 bits red, 8 bits green and 8 bits blue) to it's RGB565 equivalent (5 bits red, 6 bits green, 5 bits blue).

#include "drivers/lcd/tft/drawing.h"
...
uint16_t gray = drawRGB24toRGB565(0x33, 0x33, 0x33)

Text Rendering

These functions facilitate drawing strings of text on the display. By default, the following fonts are included with the LPC1343 Code Base (as of v0.85), all of which are based on freely available open-source font-famliies:

Font Monospace/Variable Width FONT_INFO
Bitstream Vera Mono 9 Monospace 8 bitstreamVeraSansMono9ptFontInfo
Bitstream Vera Mono 9 Bold Monospace 8 bitstreamVeraSansMonoBold9ptFontInfo
Bitstream Vera Mono 11 Monospace 9 bitstreamVeraSansMono11ptFontInfo
Bitstream Vera Mono 11 Bold Monospace 9 bitstreamVeraSansMonoBold11ptFontInfo
DejaVu Sans 9* Variable -- dejaVuSans9ptFontInfo
DejaVu Sans 9 Bold Variable -- dejaVuSansBold9ptFontInfo
DejaVu Sans Condensed 9 Variable -- dejaVuSansCondensed9ptFontInfo
DejaVu Sans Mono 8 Monospace 8 dejaVuSansMono8ptFontInfo
DejaVu Sans Mono 8 Bold Monospace 8 dejaVuSansMonoBold8ptFontInfo

*Note: In order to save valuable flash storage space, only DejaVu Sans 9 is used in the LPC1343 Code Base by default. Using any additional fonts will required extra storage space, typically 1.5-2.0 KB per font used.

New fonts can be created by using the open-source tool "The Dot Factory", which is included in the "/tools" folder of the LPC1343 Code Base for convenience sake. Some small modifications are required to the files created by this utility, but you can look at the converted fonts that are already included with the code base as a reference (see "/drivers/lcd/tft/fonts").

Text example using three different fonts

void drawString(uint16_t x, uint16_t y, uint16_t color, const FONT_INFO *fontInfo, char *str)

Draws a string of text on the screen using a specially formatted font.

#include "drivers/lcd/tft/drawing.h"
#include "drivers/lcd/tft/fonts/dejavusans9.h"
...
drawString(5, 90,  COLOR_BLACK, &dejaVuSans9ptFontInfo, "Text in DejaVu Sans 9");
uint16_t drawGetStringWidth(const FONT_INFO *fontInfo, char *str)

Calculates the width in pixels of the text when it will be rendered

#include "drivers/lcd/tft/drawing.h"
#include "drivers/lcd/tft/fonts/dejavusans9.h"
...
drawGetStringWidth(&dejaVuSans9ptFontInfo, "Text in DejaVu Sans 9");

Compound Shapes

The following functions facilitate drawing more complex compound shapes

void drawProgressBar ( uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t barBorderColor, uint16_t barFillColor, uint8_t progress )

Sample progress bar output

Draw a progress bar on the LCD to indicate a percentage between 0 and 100

#include "drivers/lcd/tft/drawing.h"
...
// Draw a 100 pixel wide and 20 pixel high progress bar starting at X: 10 and Y: 200.
// Outer border color is black, and the inner progress bar is blue
// Progress set to 90%
drawProgressBar(10, 200, 100, 20, COLOR_BLACK, COLOR_BLUE, 90);
void drawButton(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const FONT_INFO *fontInfo, uint16_t fontHeight, char* text, bool pressed)

Sample button

Draws a button on the display with a small text inlay.

#include "drivers/lcd/tft/drawing.h"
#include "drivers/lcd/tft/fonts/verasans9.h"
...
// Draw two buttons using DejaVu Sans 9
drawButton(20, 20, 200, 35, &dejaVuSans9ptFontInfo, 7, "System Settings", FALSE);
drawButton(20, 65, 200, 35, &dejaVuSans9ptFontInfo, 7, "Refresh", FALSE);
void drawIcon16(uint16_t x, uint16_t y, uint16_t color, uint16_t icon[])

Sample button

Renders a 16x16 pixel monochrome icon from the supplied uint16_t array. Some common icons can be found in "/drivers/lcd/icons16.h".

The include icons (seen in the image to the right) are converted from Gentleface Free Wireframe Toolbar Icons library. Please visit their website for license terms and details (Creative Commons Attribution-NonCommercial license).

Note: This function was added in v0.92 of the LPC1343 Code Base.

#include "drivers/lcd/tft/drawing.h"  
#include "drivers/lcd/icons16.h"
...
// Renders the info icon, which has two seperate parts ... the exterior
// and a seperate interior mask if you want to fill the contents with a
// different color
drawIcon16(132, 202, COLOR_BLUE, icons16_info);
drawIcon16(132, 202, COLOR_WHITE, icons16_info_interior);

Bitmap Functions

The following functions can be used to work with bitmap images. CFG_SDCARD must be enabled in projectconfig.h for this function to work.

bmp_error_t drawBitmapImage(uint16_t x, uint16_t y, char *filename)

Renders a bitmap image on the screen starting at the specified X,Y location

#include "drivers/lcd/tft/drawing.h"
...
// Render "/image.bmp" (in the root folder) starting at pixel 0, 0
bmp_error_t error = drawBitmapImage(0, 0, "/image.bmp");

if (error)
{
  switch (error)
  {
    case BMP_ERROR_SDINITFAIL:
      break;
    case BMP_ERROR_FILENOTFOUND:
      break;
    case BMP_ERROR_NOTABITMAP:
      // First two bytes of image not 'BM'
      break;
    case BMP_ERROR_INVALIDBITDEPTH:
      // Image is not 24-bits
      break;
    case BMP_ERROR_COMPRESSEDDATA:
      // Image contains compressed data
      break;
    case BMP_ERROR_INVALIDDIMENSIONS:
      // Width or Height is > LCD size
      break;
    case BMP_ERROR_PREMATUREEOF:
      // EOF unexpectedly reached in pixel data
      break;
  }
}

Dialogue Boxes

The following functions are used to display modal dialogue boxes to capture user input via the touch screen. (These methods all require, of course, that your LCD has an integrated touch screen.)

char* alphaShowDialogue(void)

Alpha numeric input boxRenders a full-screen text input dialogue that can be 'shifted' to display letters in both upper and lower case, numbers, and speciality characters. The dialogue will remain visible until the user selects the 'OK' button, at which point the text entered will be returned as a char array.

The layout will automatically itself depending on the screen orientation, and if necessary the button layout can easily be reorganised by modifying an array of pre-defined button values. See the source code for more details.

Note: This function is not part of drawing.c, and is defined in a seperate source file at "~/drivers/lcd/tft/dialogues/alphanumeric.c". It's described here for convenience sake since it will likely be used at the same time as the other drawing routines described above.

#include "drivers/lcd/tft/drawing.h"
#include "drivers/lcd/tft/fonts/dejavusans9.h"
#include "drivers/lcd/tft/touchscreen.h"
#include "drivers/lcd/tft/dialogues/alphanumeric.h"
...
// Show the text input dialogue
char* results = alphaShowDialogue();
// Start blinky
drawFill(COLOR_WHITE);
drawString(10, 10, COLOR_BLACK, &dejaVuSans9ptFontInfo, "You Entered:");
drawString(10, 30, COLOR_BLACK, &dejaVuSans9ptFontInfo, results);

  • Facebook
  • DZone It!
  • Digg It!
  • StumbleUpon
  • Technorati
  • Del.icio.us
  • NewsVine
  • Reddit
  • Blinklist
  • Furl it!

Comments