Forked from TFTLCD_8bit by Thiha Electronics

Dependents:   GR-PEACH_TFTLCD_8bit

Fork of TFTLCD_8bit by Thiha Electronics

lcd_base.h

Committer:
ttodorov
Date:
2012-12-04
Revision:
7:5c418fc1879f
Parent:
6:059ca1648211
Child:
8:7a4791dbb065

File content as of revision 7:5c418fc1879f:

/** \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 TFTLCD_BASE_H
#define TFTLCD_BASE_H

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

#ifdef __cplusplus
extern "C" {
#endif

/** \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;

/** 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 display.
     * \param oritentation The display orientation, landscape is default.
     */
    virtual void Initialize( orientation_t orientation ) = 0;
    
    /** Puts the display to sleep.
     *
     * When the display is in sleep mode, its power consumption is
     * minimized.  Before new pixel data can be written to the display
     * memory, the controller needs to be brought out of sleep mode.
     * \sa #WakeUp( void );
     * \remarks The result of this operation might not be exactly as
     *          expected. Putting the display to sleep will cause the
     *          controller to switch to the standard color of the LCD,
     *          so depending on whether the display is normally white,
     *          or normally dark, the screen might or might not go
     *          dark.  Additional power saving can be achieved, if
     *          the backlight of the used display is not hardwired on
     *          the PCB and can be controlled via the BL pin.
     * \remarks This function is controller-specific and needs to be
     *          implemented separately for each available display.
     */
    virtual void Sleep( void ) = 0;
    
    /** Wakes up the display from sleep mode.
     *
     * This function needs to be called before any other, when the
     * display has been put into sleep mode by a previois call to
     * #Sleep( void ).
     * \remarks This function is controller-specific and needs to be
     *          implemented separately for each available display.
     */
    virtual void WakeUp( void ) = 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 imgPixelData 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, const unsigned short* imgPixelData, 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 imgPixelData 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, const unsigned short* imgPixelData, 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.
     * \param RESET Pin connected to the RESET input of the display.
     */
    LCD( unsigned short width, unsigned short height ,PinName CS, PinName RS, PinName RESET );
    
    /** Activates the display for command/data transfer.
     *
     * Usually achieved by pulling the CS pin of the display low.
     */
    virtual void Activate( void );
    
    /** Deactivates the display after data has been transmitted.
     *
     * Usually achieved by pulling the CS pin of the display high.
     */
    virtual void Deactivate( void );
    
    /** 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 );
    
    /** Draws a horizontal line.
     *
     * This is a utility function to draw horizontal-only lines
     * for reduced code complexity and faster execution.
     *
     * \param x X coordinate of the starting point of the line.
     * \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.
     */
    virtual void DrawHLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
    
    /** Draws a vertical line.
     *
     * This is a utility function to draw vertical-only lines
     * for reduced code complexity and faster execution.
     *
     * \param x X coordinate of the starting point of the line.
     * \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.
     */
    virtual void DrawVLine( unsigned short x, unsigned short y, unsigned short len, int color = -2 );
    
    /** Prints a character at the given position and using the given color.
     *
     * \param c The character.
     * \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.
     * \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.
     */
    virtual void PrintChar( char c, unsigned short x, unsigned short y, int fgColor = -2, int bgColor = -1 );
    
    /** Prints a character at the given position and using the given color and with the given rotation.
     *
     * \param c The character.
     * \param x X coordinate of the character position.
     * \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.
     * \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.
     * \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 );

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

#ifdef __cplusplus
}
#endif

#endif /* TFTLCD_BASE_H */