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:   UTFT_SSD1289

Fork of TFTLCD by Todor Todorov

lcd_base.h

Committer:
ttodorov
Date:
2012-12-01
Revision:
0:881ff0b71102
Child:
2:81ed304b7e9b

File content as of revision 0:881ff0b71102:

/** \file lcd_base.h
 *  \brief Base class for all LCD controller implementations.
 *  \copyright GNU Public License, v2. or later
 *
 * Generic object painting and screen control.
 *
 * This library is based on the Arduino/chipKIT UTFT library by Henning
 * Karlsen, http://henningkarlsen.com/electronics/library.php?id=52
 *
 * Copyright (C)2010-2012 Henning Karlsen. All right reserved.
 * Copyright (C)2012 Todor Todorov.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to:
 *
 * Free Software Foundation, Inc.
 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
 *
 *********************************************************************/
#ifndef LCD_BASE_H
#define LCD_BASE_H

#include "mbed.h"
#include "fonts.h"

#define HIGH        1
#define LOW         0

#define swap( type, a, b )      { type tmp = ( a ); ( a ) = ( b ); ( b ) = tmp; }

/** \def RGB(r,g,b)
 *  \brief Creates a RGB-565 color value.
 *
 * 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.
 */
#define RGB( r, g, b )          ( ( ( ( r ) & 248 ) | ( ( g ) >> 5 ) ) << 8 ) | ( ( ( ( g ) & 28 ) << 3 ) | ( ( b ) >> 3 ) )
/** \def COLOR_BLACK
 *  \brief Shorthand for RGB( 0, 0, 0 ).
 */
#define COLOR_BLACK             RGB( 0x00, 0x00, 0x00 )
/** \def COLOR_WHITE
 *  \brief Shorthand for RGB( 255, 255, 255 ).
 */
#define COLOR_WHITE             RGB( 0xFF, 0xFF, 0xFF )
/** \def COLOR_RED
 *  \brief Shorthand for RGB( 255, 0, 0 ).
 */
#define COLOR_RED               RGB( 0xFF, 0x00, 0x00 )
/** \def COLOR_GREEN
 *  \brief Shorthand for RGB( 0, 255, 0 ).
 */
#define COLOR_GREEN             RGB( 0x00, 0xFF, 0x00 )
/** \def COLOR_BLUE
 *  \brief Shorthand for RGB( 0, 0, 255 ).
 */
#define COLOR_BLUE              RGB( 0x00, 0x00, 0xFF )

/** \typedef orientation_t
 *  \brief Display orientation.
 */
typedef enum Orientation_enum
{
    PORTRAIT = 0, /**< Display height is bigger than its width. */ 
    LANDSCAPE = 1, /**< Display width is bigger than its height. */
} orientation_t;

/** \typedef align_t
 *  \brief Horizontal text alignment on the line.
 */
typedef enum Alignment_enum
{
    LEFT = 0, /**< Left-oriented, naturally gravitate closer to the left edge of the screen. */
    CENTER = 9998, /**< Center-oriented, try to fit in the middle of the available space with equal free space to the left and right of the text. */
    RIGHT = 9999, /**< Right-oriented, naturally gravitate closer to the right edge of the screen, leaving any remaining free space to the left of the text. */
} align_t;

/** \typedef font_metrics_t
 *  \brief Describes fonts and their properties.
 *  \sa Comments in fonts.h
 */
typedef struct Font_struct
{
    const    char* font; /**< A pointer to the first byte in the font. */
    unsigned char  width; /**< The width of each character, in pixels. */
    unsigned char  height; /**< Height of each character, in pixels. */
    unsigned char  offset; /**< Offset of the first character in the font. */
    unsigned char  numchars; /**< Count of the available characters in the font. */
} font_metrics_t;

/** \typedef bitmap_t
 *  \brief Pointer to the start of a block of pixel data, describing a picture.
 */
typedef unsigned short* bitmap_t;

/** Base class for LCD implementations.
 *
 * All separate LCD controller implementations have to subclass this one.
 *
 * \version 0.1
 * \author Todor Todorov
 */
class LCD
{
public:

    /** Initialize display.
     *
     * Wakes up the display from sleep, initializes power parameters.
     * This function must be called first, befor any painting on the
     * display is done, otherwise the positioning of graphical elements
     * will not work properly and any paynt operation will not be visible
     * or produce garbage.
     *
     * This function is controller-specific and needs to be implemented
     * separately for each available implementation.
     * \param oritentation The display orientation, landscape is default.
     */
    virtual void Initialize( orientation_t orientation ) = 0;
    
    /** Set the foreground color for painting.
     *
     * This is the default foreground color to be used in painting operations.
     * If a specific output function allows for a different color to be specified
     * 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.
     * \sa #RGB(r,g,b)
     */
    virtual void SetForeground( unsigned short color = COLOR_WHITE );
    
    /** Set the background color for painting.
     *
     * This is the default color to be used for "empty" pixels while painting.
     * If a particular function allows for a different value to be specified
     * 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.
     * \sa #RGB(r,g,b)
     */
    virtual void SetBackground( unsigned short color = COLOR_BLACK );
    
    /** Sets the font to be used for painting of text on the screen.
     * \param font A pointer to the font data.
     * \sa Comments in file fonts.h
     */
    virtual void SetFont( const char* font );
    
    /** Gets the display width.
     *  \return Display width in pixels.
     */
    unsigned short GetWidth( void );
    
    /** Gets the display height.
     *  \return Display height in pixels.
     */
    unsigned short GetHeight( void );
    
    /** Fills the whole screen with a single color.
     * \param color The color to be used. The value must be in RGB-565 format.
     * \remarks The special values -1 and -2 signify the preset background and foreground colors, respectively.
     *          The backround color is the default.
     */
    virtual void FillScreen( int color = -1 );
    
    /** Clears the screen.
     *
     * This is the same as calling #FillScreen() or #FillScreen( -1 ) to use the background color.
     */
    virtual void ClearScreen( void );
    
    /** Draws a pixel at the specified location.
     *
     * By default the function will use the preset foreground color, but the background
     * or a custom color could be used as well.
     *
     * \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.
     */
    virtual void DrawPixel( unsigned short x, unsigned short y, int color = -2 );
    
    /** Draws a line.
     *
     * \param x1 Horizontal offset of the beginning point of the line.
     * \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.
     */
    virtual void DrawLine( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
    
    /** Paints a rectangle.
     *
     * \param x1 The horizontal offset of the beginning point of one of the rectangle's diagonals.
     * \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.
     */
    virtual void DrawRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
    
    /** Paints a rectangle and fills it with the paint color.
     *
     * \param x1 The horizontal offset of the beginning point of one of the rectangle's diagonals.
     * \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.
     */
    virtual void DrawRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
    
    /** Paints a rectangle with rounded corners.
     *
     * \param x1 The horizontal offset of the beginning point of one of the rectangle's diagonals.
     * \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.
     */
    virtual void FillRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
    
    /** Paints a rectangle with rounded corners and fills it with the paint color.
     *
     * \param x1 The horizontal offset of the beginning point of one of the rectangle's diagonals.
     * \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.
     */
    virtual void FillRoundRect( unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2, int color = -2 );
    
    /** Paints a circle.
     *
     * \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.
     */
    virtual void DrawCircle( unsigned short x, unsigned short y, unsigned short radius, int color = -2 );
    
    /** Paints a circle and fills it with the paint color.
     *
     * \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.
     */
    virtual void FillCircle( unsigned short x, unsigned short y, unsigned short radius, int color = -2 );
    
    /** Print a text on the screen.
     *
     * \param str The text.
     * \param x The horizontal offset form the left edge of the screen. The special values LEFT, CENTER,
     *          or RIGHT can be used instead of pixel offset to indicate the text's horizontal alignment.
     * \param y The vertical offset of the text from the top of the screen.
     * \param fgColor The foreground to use for painting the text; -1 indicates background color, -2 the foreground setting, or custom color.
     * \param bgColor The color to use for painting the empty pixels; -1 indicates the background color, -2 the foreground setting, or custom color.
     * \param deg If different than 0, the text will be rotated at an angle this many degrees around its starting point. Default is not to ratate.
     */
    virtual void Print( const char *str, unsigned short x, unsigned short y, int fgColor = -2, int bgColor = -1, unsigned short deg = 0 );
    
    /** Draw an image on the screen.
     *
     * The pixels of the picture must be in the RGB-565 format.  The data can be provided
     * as an array in a source or a header file.  To convert an image file to the appropriate
     * format, a special utility must be utilized.  One such tool is provided by Henning Karlsen,
     * the author of the UTFT display liberary and can be downloaded for free from his web site:
     * 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 data Image pixel array.
     * \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, bitmap_t data, unsigned char scale = 1 );
    
    /** Draw an image on the screen.
     *
     * The pixels of the picture must be in the RGB-565 format.  The data can be provided
     * as an array in a source or a header file.  To convert an image file to the appropriate
     * format, a special utility must be utilized.  One such tool is provided by Henning Karlsen,
     * the author of the UTFT display liberary and can be downloaded for free from his web site:
     * 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 data Image pixel array.
     * \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, bitmap_t data, unsigned short deg, unsigned short rox, unsigned short roy );
    
protected:
    /** Creates an instance of the class.
     *
     * \param width Width of the display in pixels.
     * \param height Height of the display in pixels.
     * \param CS Pin connected to the CS input of the display.
     * \param RS Pin connected to the RS input of the display.
     */
    LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS );
    
    /** Sends a command to the display.
     *
     * \param cmd The display command.
     * \remarks Commands are controller-specific and this function needs to
     *          be implemented separately for each available controller.
     */
    virtual void writeCmd( unsigned short cmd ) = 0;
    
    /** Sends pixel data to the display.
     *
     * \param data The display data.
     * \remarks Sendin data is controller-specific and this function needs to
     *          be implemented separately for each available controller.
     */
    virtual void writeData( unsigned short data ) = 0;
    
    /** Sends both command and data to the display controller.
     *
     * This is a helper utility function which combines the 2 functions above
     * into one single convenience step.
     *
     * \param cmd The display command.
     * \param data The display pixel data.
     */
    virtual void writeCmdData( unsigned short cmd, unsigned short data );
    
    /** Assigns a chunk of the display memory to receive data.
     *
     * When data is sent to the display after this function completes, the opertion will
     * start from the begining of the assigned address (pixel position) and the pointer
     * will be automatically incremented so that the next data write operation will continue
     * with the next pixel from the memory block.  If more data is written than available
     * pixels, at the end of the block the pointer will jump back to its beginning and
     * commence again, until the next address change command is sent to the display.
     *
     * \param x1 The X coordinate of the pixel at the beginning of the block.
     * \param y1 The Y coordinate of the pixel at the beginning of the block.
     * \param x2 The X coordinate of the pixel at the end of the block.
     * \param y2 The Y coordinate of the pixel at the end of the block.
     * \remarks Addressing commands are controller-specific and this function needs to be
     *          implemented separately for each available controller.
     */
    virtual void setXY( uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2 ) = 0;
    
    /** Resets the memory address for the next display write operation to the screen origins (0,0).
     */
    virtual void clearXY( void );
    
    virtual void drawHLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
    virtual void drawVLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
    virtual void printChar( char c, unsigned short x, unsigned short y, int fgColor = -2, int bgColor = -1 );
    virtual void rotateChar( char c, unsigned short x, unsigned short y, int pos, int fgColor = -2, int bgColor = -1, unsigned short deg = 0 );

protected:
    unsigned short _disp_width, _disp_height;
    DigitalOut _lcd_pin_cs, _lcd_pin_rs;
    orientation_t _orientation;
    unsigned short _foreground, _background;
    font_metrics_t _font;
};

#endif /* LCD_BASE_H */