I simplified the library "ILI9225_TFT" provided by Arman Safikhani to better suit my needs in implementing a simple sliding puzzle game.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ILI9225.h Source File

ILI9225.h

00001 #include "mbed.h"
00002 #include "SPI.h"
00003 
00004 //ILI9225 Aspect ratio
00005 #define ILI9225_LCD_WIDTH  176
00006 #define ILI9225_LCD_HEIGHT 220
00007 
00008 //ILI9225 Registers
00009 #define ILI9225_DRIVER_OUTPUT_CTRL      (0x01u)  //Driver Output Control
00010 #define ILI9225_LCD_AC_DRIVING_CTRL     (0x02u)  //LCD AC Driving Control
00011 #define ILI9225_ENTRY_MODE              (0x03u)  //Entry Mode
00012 #define ILI9225_DISP_CTRL1              (0x07u)  //Display Control 1
00013 #define ILI9225_BLANK_PERIOD_CTRL1      (0x08u)  //Blank Period Control
00014 #define ILI9225_FRAME_CYCLE_CTRL        (0x0Bu)  //Frame Cycle Control
00015 #define ILI9225_INTERFACE_CTRL          (0x0Cu)  //Interface Control
00016 #define ILI9225_OSC_CTRL                (0x0Fu)  //Osc Control
00017 #define ILI9225_POWER_CTRL1             (0x10u)  //Power Control 1
00018 #define ILI9225_POWER_CTRL2             (0x11u)  //Power Control 2
00019 #define ILI9225_POWER_CTRL3             (0x12u)  //Power Control 3
00020 #define ILI9225_POWER_CTRL4             (0x13u)  //Power Control 4
00021 #define ILI9225_POWER_CTRL5             (0x14u)  //Power Control 5
00022 #define ILI9225_VCI_RECYCLING           (0x15u)  //VCI Recycling
00023 #define ILI9225_RAM_ADDR_SET1           (0x20u)  //Horizontal GRAM Address Set
00024 #define ILI9225_RAM_ADDR_SET2           (0x21u)  //Vertical GRAM Address Set
00025 #define ILI9225_GRAM_DATA_REG           (0x22u)  //GRAM Data Register
00026 #define ILI9225_GATE_SCAN_CTRL          (0x30u)  //Gate Scan Control Register
00027 #define ILI9225_VERTICAL_SCROLL_CTRL1   (0x31u)  //Vertical Scroll Cont 1 Reg
00028 #define ILI9225_VERTICAL_SCROLL_CTRL2   (0x32u)  //Vertical Scroll Cont 2 Reg
00029 #define ILI9225_VERTICAL_SCROLL_CTRL3   (0x33u)  //Vertical Scroll Cont 3 Reg
00030 #define ILI9225_PARTIAL_DRIVING_POS1    (0x34u)  //Partial Driving Pos 1 Reg
00031 #define ILI9225_PARTIAL_DRIVING_POS2    (0x35u)  //Partial Driving Pos 2 Reg
00032 #define ILI9225_HORIZONTAL_WINDOW_ADDR1 (0x36u)  //Horizontal Address Start Pos
00033 #define ILI9225_HORIZONTAL_WINDOW_ADDR2 (0x37u)  //Horizontal Address End Pos
00034 #define ILI9225_VERTICAL_WINDOW_ADDR1   (0x38u)  //Vertical Address Start Pos
00035 #define ILI9225_VERTICAL_WINDOW_ADDR2   (0x39u)  //Vertical Address End Pos
00036 #define ILI9225_GAMMA_CTRL1             (0x50u)  //Gamma Control 1
00037 #define ILI9225_GAMMA_CTRL2             (0x51u)  //Gamma Control 2
00038 #define ILI9225_GAMMA_CTRL3             (0x52u)  //Gamma Control 3
00039 #define ILI9225_GAMMA_CTRL4             (0x53u)  //Gamma Control 4
00040 #define ILI9225_GAMMA_CTRL5             (0x54u)  //Gamma Control 5
00041 #define ILI9225_GAMMA_CTRL6             (0x55u)  //Gamma Control 6
00042 #define ILI9225_GAMMA_CTRL7             (0x56u)  //Gamma Control 7
00043 #define ILI9225_GAMMA_CTRL8             (0x57u)  //Gamma Control 8
00044 #define ILI9225_GAMMA_CTRL9             (0x58u)  //Gamma Control 9
00045 #define ILI9225_GAMMA_CTRL10            (0x59u)  //Gamma Control 10
00046 
00047 #define ILI9225C_INVOFF  0x20
00048 #define ILI9225C_INVON   0x21
00049 
00050 //RGB 24-bits color table definition (RGB888).
00051 #define RGB888_RGB565(color) ((((color) >> 19) & 0x1f) << 11) | \
00052     ((((color) >> 10) & 0x3f) << 5) | (((color) >> 3) & 0x1f)
00053 
00054 #define COLOR_BLACK          RGB888_RGB565(0x000000u)
00055 #define COLOR_WHITE          RGB888_RGB565(0xFFFFFFu)
00056 #define COLOR_BLUE           RGB888_RGB565(0x0000FFu)
00057 #define COLOR_GREEN          RGB888_RGB565(0x00FF00u)
00058 #define COLOR_RED            RGB888_RGB565(0xFF0000u)
00059 #define COLOR_NAVY           RGB888_RGB565(0x000080u)
00060 #define COLOR_DARKBLUE       RGB888_RGB565(0x00008Bu)
00061 #define COLOR_DARKGREEN      RGB888_RGB565(0x006400u)
00062 #define COLOR_DARKCYAN       RGB888_RGB565(0x008B8Bu)
00063 #define COLOR_CYAN           RGB888_RGB565(0x00FFFFu)
00064 #define COLOR_TURQUOISE      RGB888_RGB565(0x40E0D0u)
00065 #define COLOR_INDIGO         RGB888_RGB565(0x4B0082u)
00066 #define COLOR_DARKRED        RGB888_RGB565(0x800000u)
00067 #define COLOR_OLIVE          RGB888_RGB565(0x808000u)
00068 #define COLOR_GRAY           RGB888_RGB565(0x808080u)
00069 #define COLOR_SKYBLUE        RGB888_RGB565(0x87CEEBu)
00070 #define COLOR_BLUEVIOLET     RGB888_RGB565(0x8A2BE2u)
00071 #define COLOR_LIGHTGREEN     RGB888_RGB565(0x90EE90u)
00072 #define COLOR_DARKVIOLET     RGB888_RGB565(0x9400D3u)
00073 #define COLOR_YELLOWGREEN    RGB888_RGB565(0x9ACD32u)
00074 #define COLOR_BROWN          RGB888_RGB565(0xA52A2Au)
00075 #define COLOR_DARKGRAY       RGB888_RGB565(0xA9A9A9u)
00076 #define COLOR_SIENNA         RGB888_RGB565(0xA0522Du)
00077 #define COLOR_LIGHTBLUE      RGB888_RGB565(0xADD8E6u)
00078 #define COLOR_GREENYELLOW    RGB888_RGB565(0xADFF2Fu)
00079 #define COLOR_SILVER         RGB888_RGB565(0xC0C0C0u)
00080 #define COLOR_LIGHTGREY      RGB888_RGB565(0xD3D3D3u)
00081 #define COLOR_LIGHTCYAN      RGB888_RGB565(0xE0FFFFu)
00082 #define COLOR_VIOLET         RGB888_RGB565(0xEE82EEu)
00083 #define COLOR_AZUR           RGB888_RGB565(0xF0FFFFu)
00084 #define COLOR_BEIGE          RGB888_RGB565(0xF5F5DCu)
00085 #define COLOR_MAGENTA        RGB888_RGB565(0xFF00FFu)
00086 #define COLOR_TOMATO         RGB888_RGB565(0xFF6347u)
00087 #define COLOR_GOLD           RGB888_RGB565(0xFFD700u)
00088 #define COLOR_ORANGE         RGB888_RGB565(0xFFA500u)
00089 #define COLOR_SNOW           RGB888_RGB565(0xFFFAFAu)
00090 #define COLOR_YELLOW         RGB888_RGB565(0xFFFF00u)
00091 
00092 //Font definitions
00093 #define FONT_HEADER_SIZE 4 //1: pixel width of 1 font char, 2: pixel height 
00094 #define readFontByte(x) pgm_read_byte(&cfont.font[x])
00095 
00096 extern const uint8_t Terminal12x16[];
00097 
00098 struct _currentFont {
00099     const uint8_t* font;
00100     uint8_t width;
00101     uint8_t height;
00102     uint8_t offset;
00103     uint8_t numchars;
00104     uint8_t nbrows;
00105 };
00106 
00107 //Main and core class
00108 class ILI9225 {
00109     public:
00110         ILI9225(PinName RST, PinName RS, PinName CS, PinName SDI,
00111             PinName CLK, PinName LED);
00112 
00113         //Initialization
00114         void begin(void);
00115     
00116         //Clear the screen
00117         void clear(void);
00118     
00119         //Fill the screen with color:
00120         void fill(uint16_t color);
00121     
00122         //Invert screen
00123         //@param  flag true to invert, false for normal screen
00124         void invert(bool flag);
00125     
00126         //Switch backlight on or off
00127         //@param flag true=on, false=off
00128         void setBacklight(bool flag);
00129     
00130         //Switch display on or off
00131         //@param flag true=on, false=off
00132         void setDisplay(bool flag);
00133     
00134         //Set orientation
00135         //@param orientation orientation, 0=portrait, 1=right rotated landscape,
00136         //2=reverse portrait, 3=left rotated landscape
00137         void setOrientation(uint8_t orientation);
00138     
00139         //Font size, x-axis
00140         //@return horizontal size of current font, in pixels
00141         uint8_t fontX(void);
00142     
00143         //Font size, y-axis
00144         //@return vertical size of current font, in pixels
00145         uint8_t fontY(void);
00146     
00147         //Screen size, x-axis
00148         //@return horizontal size of the screen, in pixels
00149         //@note 240 means 240 pixels and thus 0..239 coordinates (decimal)
00150         uint16_t maxX(void);
00151     
00152         //Screen size, y-axis
00153         //@return vertical size of the screen, in pixels
00154         //@note 220 means 220 pixels and thus 0..219 coordinates (decimal)
00155         uint16_t maxY(void);
00156     
00157         //Set background color
00158         //@param color background color, default=black
00159         void setBackgroundColor(uint16_t color = COLOR_BLACK);
00160     
00161         //Draw line, rectangle coordinates
00162         //@param x1 top left coordinate, x-axis
00163         //@param y1 top left coordinate, y-axis
00164         //@param x2 bottom right coordinate, x-axis
00165         //@param y2 bottom right coordinate, y-axis
00166         //@param color 16-bit color
00167         void drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
00168             uint16_t color);
00169     
00170         //Draw rectangle, rectangle coordinates
00171         //@param x1 top left coordinate, x-axis
00172         //@param y1 top left coordinate, y-axis
00173         //@param x2 bottom right coordinate, x-axis
00174         //@param y2 bottom right coordinate, y-axis
00175         //@param color 16-bit color
00176         void drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
00177             uint16_t color);
00178     
00179         //Draw solid rectangle, rectangle coordinates
00180         //@param x1 top left coordinate, x-axis
00181         //@param y1 top left coordinate, y-axis
00182         //@param x2 bottom right coordinate, x-axis
00183         //@param y2 bottom right coordinate, y-axis
00184         //@param color 16-bit color
00185         void fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
00186             uint16_t color);
00187     
00188         //Draw pixel
00189         //@param x1 point coordinate, x-axis
00190         //@param y1 point coordinate, y-axis
00191         //@param color 16-bit color
00192         void drawPixel(uint16_t x1, uint16_t y1, uint16_t color);
00193     
00194         //Draw ASCII Text (pixel coordinates)
00195         //@param x point coordinate, x-axis
00196         //@param y point coordinate, y-axis
00197         //@param s text string
00198         //@param color 16-bit color, default=white
00199         void drawText(uint16_t x, uint16_t y, char *s,
00200             uint16_t color = COLOR_WHITE);
00201     
00202         //Calculate 16-bit color from 8-bit Red-Green-Blue components
00203         //@param red red component, 0x00..0xff
00204         //@param green green component, 0x00..0xff
00205         //@param blue blue component, 0x00..0xff
00206         //@return 16-bit color
00207         uint16_t setColor(uint8_t red, uint8_t green, uint8_t blue);
00208     
00209         //Calculate 8-bit Red-Green-Blue components from 16-bit color
00210         //@param rgb 16-bit color
00211         //@param red red component, 0x00..0xff
00212         //@param green green component, 0x00..0xff
00213         //@param blue blue component, 0x00..0xff
00214         void splitColor(uint16_t rgb, uint8_t &red, uint8_t &green,
00215             uint8_t &blue);
00216     
00217         //Set current font
00218         //@param font Font name
00219         void setFont(const uint8_t* font);
00220     
00221         //Draw single character (pixel coordinates)
00222         //@param x point coordinate, x-axis
00223         //@param y point coordinate, y-axis
00224         //@param ch ASCII character
00225         //@param color 16-bit color, default=white
00226         uint16_t drawChar(uint16_t x, uint16_t y, uint16_t ch,
00227             uint16_t color = COLOR_WHITE);
00228     
00229         void goToXY(int x, int y);
00230         void unicode2ascii(char *uni_str, char *ascii_str); 
00231         void roundRectangle(int x0, int y0, int x1, int y1, int rad, bool fill,
00232             int color);
00233     
00234     private:
00235         SPI spi;
00236     
00237         void _swap(uint16_t &a, uint16_t &b),
00238             _setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),
00239             _orientCoordinates(uint16_t &x1, uint16_t &y1),
00240             _writeRegister(uint16_t reg, uint16_t data),
00241             _writeData(uint8_t HI, uint8_t LO),
00242             _writeCommand(uint8_t HI, uint8_t LO);
00243     
00244         uint16_t _maxX, _maxY, _bgColor;
00245     
00246         PinName  _rst, _rs, _cs, _sdi, _clk, _led;
00247         DigitalInOut _rsInOut;
00248         DigitalInOut _csInOut;
00249         DigitalInOut _rstInOut;
00250         DigitalInOut _ledInOut;
00251         uint8_t _orientation;
00252     
00253         bool  hwSPI;
00254     
00255         _currentFont cfont;
00256 };