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

Committer:
blac3777
Date:
Fri Apr 27 07:33:56 2018 +0000
Revision:
3:251e4d020501
I simplified the library "ILI9225_TFT" provided by Arman Safikhani to suit my needs in implementing a simple sliding puzzle game.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
blac3777 3:251e4d020501 1 #include "mbed.h"
blac3777 3:251e4d020501 2 #include "SPI.h"
blac3777 3:251e4d020501 3
blac3777 3:251e4d020501 4 //ILI9225 Aspect ratio
blac3777 3:251e4d020501 5 #define ILI9225_LCD_WIDTH 176
blac3777 3:251e4d020501 6 #define ILI9225_LCD_HEIGHT 220
blac3777 3:251e4d020501 7
blac3777 3:251e4d020501 8 //ILI9225 Registers
blac3777 3:251e4d020501 9 #define ILI9225_DRIVER_OUTPUT_CTRL (0x01u) //Driver Output Control
blac3777 3:251e4d020501 10 #define ILI9225_LCD_AC_DRIVING_CTRL (0x02u) //LCD AC Driving Control
blac3777 3:251e4d020501 11 #define ILI9225_ENTRY_MODE (0x03u) //Entry Mode
blac3777 3:251e4d020501 12 #define ILI9225_DISP_CTRL1 (0x07u) //Display Control 1
blac3777 3:251e4d020501 13 #define ILI9225_BLANK_PERIOD_CTRL1 (0x08u) //Blank Period Control
blac3777 3:251e4d020501 14 #define ILI9225_FRAME_CYCLE_CTRL (0x0Bu) //Frame Cycle Control
blac3777 3:251e4d020501 15 #define ILI9225_INTERFACE_CTRL (0x0Cu) //Interface Control
blac3777 3:251e4d020501 16 #define ILI9225_OSC_CTRL (0x0Fu) //Osc Control
blac3777 3:251e4d020501 17 #define ILI9225_POWER_CTRL1 (0x10u) //Power Control 1
blac3777 3:251e4d020501 18 #define ILI9225_POWER_CTRL2 (0x11u) //Power Control 2
blac3777 3:251e4d020501 19 #define ILI9225_POWER_CTRL3 (0x12u) //Power Control 3
blac3777 3:251e4d020501 20 #define ILI9225_POWER_CTRL4 (0x13u) //Power Control 4
blac3777 3:251e4d020501 21 #define ILI9225_POWER_CTRL5 (0x14u) //Power Control 5
blac3777 3:251e4d020501 22 #define ILI9225_VCI_RECYCLING (0x15u) //VCI Recycling
blac3777 3:251e4d020501 23 #define ILI9225_RAM_ADDR_SET1 (0x20u) //Horizontal GRAM Address Set
blac3777 3:251e4d020501 24 #define ILI9225_RAM_ADDR_SET2 (0x21u) //Vertical GRAM Address Set
blac3777 3:251e4d020501 25 #define ILI9225_GRAM_DATA_REG (0x22u) //GRAM Data Register
blac3777 3:251e4d020501 26 #define ILI9225_GATE_SCAN_CTRL (0x30u) //Gate Scan Control Register
blac3777 3:251e4d020501 27 #define ILI9225_VERTICAL_SCROLL_CTRL1 (0x31u) //Vertical Scroll Cont 1 Reg
blac3777 3:251e4d020501 28 #define ILI9225_VERTICAL_SCROLL_CTRL2 (0x32u) //Vertical Scroll Cont 2 Reg
blac3777 3:251e4d020501 29 #define ILI9225_VERTICAL_SCROLL_CTRL3 (0x33u) //Vertical Scroll Cont 3 Reg
blac3777 3:251e4d020501 30 #define ILI9225_PARTIAL_DRIVING_POS1 (0x34u) //Partial Driving Pos 1 Reg
blac3777 3:251e4d020501 31 #define ILI9225_PARTIAL_DRIVING_POS2 (0x35u) //Partial Driving Pos 2 Reg
blac3777 3:251e4d020501 32 #define ILI9225_HORIZONTAL_WINDOW_ADDR1 (0x36u) //Horizontal Address Start Pos
blac3777 3:251e4d020501 33 #define ILI9225_HORIZONTAL_WINDOW_ADDR2 (0x37u) //Horizontal Address End Pos
blac3777 3:251e4d020501 34 #define ILI9225_VERTICAL_WINDOW_ADDR1 (0x38u) //Vertical Address Start Pos
blac3777 3:251e4d020501 35 #define ILI9225_VERTICAL_WINDOW_ADDR2 (0x39u) //Vertical Address End Pos
blac3777 3:251e4d020501 36 #define ILI9225_GAMMA_CTRL1 (0x50u) //Gamma Control 1
blac3777 3:251e4d020501 37 #define ILI9225_GAMMA_CTRL2 (0x51u) //Gamma Control 2
blac3777 3:251e4d020501 38 #define ILI9225_GAMMA_CTRL3 (0x52u) //Gamma Control 3
blac3777 3:251e4d020501 39 #define ILI9225_GAMMA_CTRL4 (0x53u) //Gamma Control 4
blac3777 3:251e4d020501 40 #define ILI9225_GAMMA_CTRL5 (0x54u) //Gamma Control 5
blac3777 3:251e4d020501 41 #define ILI9225_GAMMA_CTRL6 (0x55u) //Gamma Control 6
blac3777 3:251e4d020501 42 #define ILI9225_GAMMA_CTRL7 (0x56u) //Gamma Control 7
blac3777 3:251e4d020501 43 #define ILI9225_GAMMA_CTRL8 (0x57u) //Gamma Control 8
blac3777 3:251e4d020501 44 #define ILI9225_GAMMA_CTRL9 (0x58u) //Gamma Control 9
blac3777 3:251e4d020501 45 #define ILI9225_GAMMA_CTRL10 (0x59u) //Gamma Control 10
blac3777 3:251e4d020501 46
blac3777 3:251e4d020501 47 #define ILI9225C_INVOFF 0x20
blac3777 3:251e4d020501 48 #define ILI9225C_INVON 0x21
blac3777 3:251e4d020501 49
blac3777 3:251e4d020501 50 //RGB 24-bits color table definition (RGB888).
blac3777 3:251e4d020501 51 #define RGB888_RGB565(color) ((((color) >> 19) & 0x1f) << 11) | \
blac3777 3:251e4d020501 52 ((((color) >> 10) & 0x3f) << 5) | (((color) >> 3) & 0x1f)
blac3777 3:251e4d020501 53
blac3777 3:251e4d020501 54 #define COLOR_BLACK RGB888_RGB565(0x000000u)
blac3777 3:251e4d020501 55 #define COLOR_WHITE RGB888_RGB565(0xFFFFFFu)
blac3777 3:251e4d020501 56 #define COLOR_BLUE RGB888_RGB565(0x0000FFu)
blac3777 3:251e4d020501 57 #define COLOR_GREEN RGB888_RGB565(0x00FF00u)
blac3777 3:251e4d020501 58 #define COLOR_RED RGB888_RGB565(0xFF0000u)
blac3777 3:251e4d020501 59 #define COLOR_NAVY RGB888_RGB565(0x000080u)
blac3777 3:251e4d020501 60 #define COLOR_DARKBLUE RGB888_RGB565(0x00008Bu)
blac3777 3:251e4d020501 61 #define COLOR_DARKGREEN RGB888_RGB565(0x006400u)
blac3777 3:251e4d020501 62 #define COLOR_DARKCYAN RGB888_RGB565(0x008B8Bu)
blac3777 3:251e4d020501 63 #define COLOR_CYAN RGB888_RGB565(0x00FFFFu)
blac3777 3:251e4d020501 64 #define COLOR_TURQUOISE RGB888_RGB565(0x40E0D0u)
blac3777 3:251e4d020501 65 #define COLOR_INDIGO RGB888_RGB565(0x4B0082u)
blac3777 3:251e4d020501 66 #define COLOR_DARKRED RGB888_RGB565(0x800000u)
blac3777 3:251e4d020501 67 #define COLOR_OLIVE RGB888_RGB565(0x808000u)
blac3777 3:251e4d020501 68 #define COLOR_GRAY RGB888_RGB565(0x808080u)
blac3777 3:251e4d020501 69 #define COLOR_SKYBLUE RGB888_RGB565(0x87CEEBu)
blac3777 3:251e4d020501 70 #define COLOR_BLUEVIOLET RGB888_RGB565(0x8A2BE2u)
blac3777 3:251e4d020501 71 #define COLOR_LIGHTGREEN RGB888_RGB565(0x90EE90u)
blac3777 3:251e4d020501 72 #define COLOR_DARKVIOLET RGB888_RGB565(0x9400D3u)
blac3777 3:251e4d020501 73 #define COLOR_YELLOWGREEN RGB888_RGB565(0x9ACD32u)
blac3777 3:251e4d020501 74 #define COLOR_BROWN RGB888_RGB565(0xA52A2Au)
blac3777 3:251e4d020501 75 #define COLOR_DARKGRAY RGB888_RGB565(0xA9A9A9u)
blac3777 3:251e4d020501 76 #define COLOR_SIENNA RGB888_RGB565(0xA0522Du)
blac3777 3:251e4d020501 77 #define COLOR_LIGHTBLUE RGB888_RGB565(0xADD8E6u)
blac3777 3:251e4d020501 78 #define COLOR_GREENYELLOW RGB888_RGB565(0xADFF2Fu)
blac3777 3:251e4d020501 79 #define COLOR_SILVER RGB888_RGB565(0xC0C0C0u)
blac3777 3:251e4d020501 80 #define COLOR_LIGHTGREY RGB888_RGB565(0xD3D3D3u)
blac3777 3:251e4d020501 81 #define COLOR_LIGHTCYAN RGB888_RGB565(0xE0FFFFu)
blac3777 3:251e4d020501 82 #define COLOR_VIOLET RGB888_RGB565(0xEE82EEu)
blac3777 3:251e4d020501 83 #define COLOR_AZUR RGB888_RGB565(0xF0FFFFu)
blac3777 3:251e4d020501 84 #define COLOR_BEIGE RGB888_RGB565(0xF5F5DCu)
blac3777 3:251e4d020501 85 #define COLOR_MAGENTA RGB888_RGB565(0xFF00FFu)
blac3777 3:251e4d020501 86 #define COLOR_TOMATO RGB888_RGB565(0xFF6347u)
blac3777 3:251e4d020501 87 #define COLOR_GOLD RGB888_RGB565(0xFFD700u)
blac3777 3:251e4d020501 88 #define COLOR_ORANGE RGB888_RGB565(0xFFA500u)
blac3777 3:251e4d020501 89 #define COLOR_SNOW RGB888_RGB565(0xFFFAFAu)
blac3777 3:251e4d020501 90 #define COLOR_YELLOW RGB888_RGB565(0xFFFF00u)
blac3777 3:251e4d020501 91
blac3777 3:251e4d020501 92 //Font definitions
blac3777 3:251e4d020501 93 #define FONT_HEADER_SIZE 4 //1: pixel width of 1 font char, 2: pixel height
blac3777 3:251e4d020501 94 #define readFontByte(x) pgm_read_byte(&cfont.font[x])
blac3777 3:251e4d020501 95
blac3777 3:251e4d020501 96 extern const uint8_t Terminal12x16[];
blac3777 3:251e4d020501 97
blac3777 3:251e4d020501 98 struct _currentFont {
blac3777 3:251e4d020501 99 const uint8_t* font;
blac3777 3:251e4d020501 100 uint8_t width;
blac3777 3:251e4d020501 101 uint8_t height;
blac3777 3:251e4d020501 102 uint8_t offset;
blac3777 3:251e4d020501 103 uint8_t numchars;
blac3777 3:251e4d020501 104 uint8_t nbrows;
blac3777 3:251e4d020501 105 };
blac3777 3:251e4d020501 106
blac3777 3:251e4d020501 107 //Main and core class
blac3777 3:251e4d020501 108 class ILI9225 {
blac3777 3:251e4d020501 109 public:
blac3777 3:251e4d020501 110 ILI9225(PinName RST, PinName RS, PinName CS, PinName SDI,
blac3777 3:251e4d020501 111 PinName CLK, PinName LED);
blac3777 3:251e4d020501 112
blac3777 3:251e4d020501 113 //Initialization
blac3777 3:251e4d020501 114 void begin(void);
blac3777 3:251e4d020501 115
blac3777 3:251e4d020501 116 //Clear the screen
blac3777 3:251e4d020501 117 void clear(void);
blac3777 3:251e4d020501 118
blac3777 3:251e4d020501 119 //Fill the screen with color:
blac3777 3:251e4d020501 120 void fill(uint16_t color);
blac3777 3:251e4d020501 121
blac3777 3:251e4d020501 122 //Invert screen
blac3777 3:251e4d020501 123 //@param flag true to invert, false for normal screen
blac3777 3:251e4d020501 124 void invert(bool flag);
blac3777 3:251e4d020501 125
blac3777 3:251e4d020501 126 //Switch backlight on or off
blac3777 3:251e4d020501 127 //@param flag true=on, false=off
blac3777 3:251e4d020501 128 void setBacklight(bool flag);
blac3777 3:251e4d020501 129
blac3777 3:251e4d020501 130 //Switch display on or off
blac3777 3:251e4d020501 131 //@param flag true=on, false=off
blac3777 3:251e4d020501 132 void setDisplay(bool flag);
blac3777 3:251e4d020501 133
blac3777 3:251e4d020501 134 //Set orientation
blac3777 3:251e4d020501 135 //@param orientation orientation, 0=portrait, 1=right rotated landscape,
blac3777 3:251e4d020501 136 //2=reverse portrait, 3=left rotated landscape
blac3777 3:251e4d020501 137 void setOrientation(uint8_t orientation);
blac3777 3:251e4d020501 138
blac3777 3:251e4d020501 139 //Font size, x-axis
blac3777 3:251e4d020501 140 //@return horizontal size of current font, in pixels
blac3777 3:251e4d020501 141 uint8_t fontX(void);
blac3777 3:251e4d020501 142
blac3777 3:251e4d020501 143 //Font size, y-axis
blac3777 3:251e4d020501 144 //@return vertical size of current font, in pixels
blac3777 3:251e4d020501 145 uint8_t fontY(void);
blac3777 3:251e4d020501 146
blac3777 3:251e4d020501 147 //Screen size, x-axis
blac3777 3:251e4d020501 148 //@return horizontal size of the screen, in pixels
blac3777 3:251e4d020501 149 //@note 240 means 240 pixels and thus 0..239 coordinates (decimal)
blac3777 3:251e4d020501 150 uint16_t maxX(void);
blac3777 3:251e4d020501 151
blac3777 3:251e4d020501 152 //Screen size, y-axis
blac3777 3:251e4d020501 153 //@return vertical size of the screen, in pixels
blac3777 3:251e4d020501 154 //@note 220 means 220 pixels and thus 0..219 coordinates (decimal)
blac3777 3:251e4d020501 155 uint16_t maxY(void);
blac3777 3:251e4d020501 156
blac3777 3:251e4d020501 157 //Set background color
blac3777 3:251e4d020501 158 //@param color background color, default=black
blac3777 3:251e4d020501 159 void setBackgroundColor(uint16_t color = COLOR_BLACK);
blac3777 3:251e4d020501 160
blac3777 3:251e4d020501 161 //Draw line, rectangle coordinates
blac3777 3:251e4d020501 162 //@param x1 top left coordinate, x-axis
blac3777 3:251e4d020501 163 //@param y1 top left coordinate, y-axis
blac3777 3:251e4d020501 164 //@param x2 bottom right coordinate, x-axis
blac3777 3:251e4d020501 165 //@param y2 bottom right coordinate, y-axis
blac3777 3:251e4d020501 166 //@param color 16-bit color
blac3777 3:251e4d020501 167 void drawLine(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
blac3777 3:251e4d020501 168 uint16_t color);
blac3777 3:251e4d020501 169
blac3777 3:251e4d020501 170 //Draw rectangle, rectangle coordinates
blac3777 3:251e4d020501 171 //@param x1 top left coordinate, x-axis
blac3777 3:251e4d020501 172 //@param y1 top left coordinate, y-axis
blac3777 3:251e4d020501 173 //@param x2 bottom right coordinate, x-axis
blac3777 3:251e4d020501 174 //@param y2 bottom right coordinate, y-axis
blac3777 3:251e4d020501 175 //@param color 16-bit color
blac3777 3:251e4d020501 176 void drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
blac3777 3:251e4d020501 177 uint16_t color);
blac3777 3:251e4d020501 178
blac3777 3:251e4d020501 179 //Draw solid rectangle, rectangle coordinates
blac3777 3:251e4d020501 180 //@param x1 top left coordinate, x-axis
blac3777 3:251e4d020501 181 //@param y1 top left coordinate, y-axis
blac3777 3:251e4d020501 182 //@param x2 bottom right coordinate, x-axis
blac3777 3:251e4d020501 183 //@param y2 bottom right coordinate, y-axis
blac3777 3:251e4d020501 184 //@param color 16-bit color
blac3777 3:251e4d020501 185 void fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,
blac3777 3:251e4d020501 186 uint16_t color);
blac3777 3:251e4d020501 187
blac3777 3:251e4d020501 188 //Draw pixel
blac3777 3:251e4d020501 189 //@param x1 point coordinate, x-axis
blac3777 3:251e4d020501 190 //@param y1 point coordinate, y-axis
blac3777 3:251e4d020501 191 //@param color 16-bit color
blac3777 3:251e4d020501 192 void drawPixel(uint16_t x1, uint16_t y1, uint16_t color);
blac3777 3:251e4d020501 193
blac3777 3:251e4d020501 194 //Draw ASCII Text (pixel coordinates)
blac3777 3:251e4d020501 195 //@param x point coordinate, x-axis
blac3777 3:251e4d020501 196 //@param y point coordinate, y-axis
blac3777 3:251e4d020501 197 //@param s text string
blac3777 3:251e4d020501 198 //@param color 16-bit color, default=white
blac3777 3:251e4d020501 199 void drawText(uint16_t x, uint16_t y, char *s,
blac3777 3:251e4d020501 200 uint16_t color = COLOR_WHITE);
blac3777 3:251e4d020501 201
blac3777 3:251e4d020501 202 //Calculate 16-bit color from 8-bit Red-Green-Blue components
blac3777 3:251e4d020501 203 //@param red red component, 0x00..0xff
blac3777 3:251e4d020501 204 //@param green green component, 0x00..0xff
blac3777 3:251e4d020501 205 //@param blue blue component, 0x00..0xff
blac3777 3:251e4d020501 206 //@return 16-bit color
blac3777 3:251e4d020501 207 uint16_t setColor(uint8_t red, uint8_t green, uint8_t blue);
blac3777 3:251e4d020501 208
blac3777 3:251e4d020501 209 //Calculate 8-bit Red-Green-Blue components from 16-bit color
blac3777 3:251e4d020501 210 //@param rgb 16-bit color
blac3777 3:251e4d020501 211 //@param red red component, 0x00..0xff
blac3777 3:251e4d020501 212 //@param green green component, 0x00..0xff
blac3777 3:251e4d020501 213 //@param blue blue component, 0x00..0xff
blac3777 3:251e4d020501 214 void splitColor(uint16_t rgb, uint8_t &red, uint8_t &green,
blac3777 3:251e4d020501 215 uint8_t &blue);
blac3777 3:251e4d020501 216
blac3777 3:251e4d020501 217 //Set current font
blac3777 3:251e4d020501 218 //@param font Font name
blac3777 3:251e4d020501 219 void setFont(const uint8_t* font);
blac3777 3:251e4d020501 220
blac3777 3:251e4d020501 221 //Draw single character (pixel coordinates)
blac3777 3:251e4d020501 222 //@param x point coordinate, x-axis
blac3777 3:251e4d020501 223 //@param y point coordinate, y-axis
blac3777 3:251e4d020501 224 //@param ch ASCII character
blac3777 3:251e4d020501 225 //@param color 16-bit color, default=white
blac3777 3:251e4d020501 226 uint16_t drawChar(uint16_t x, uint16_t y, uint16_t ch,
blac3777 3:251e4d020501 227 uint16_t color = COLOR_WHITE);
blac3777 3:251e4d020501 228
blac3777 3:251e4d020501 229 void goToXY(int x, int y);
blac3777 3:251e4d020501 230 void unicode2ascii(char *uni_str, char *ascii_str);
blac3777 3:251e4d020501 231 void roundRectangle(int x0, int y0, int x1, int y1, int rad, bool fill,
blac3777 3:251e4d020501 232 int color);
blac3777 3:251e4d020501 233
blac3777 3:251e4d020501 234 private:
blac3777 3:251e4d020501 235 SPI spi;
blac3777 3:251e4d020501 236
blac3777 3:251e4d020501 237 void _swap(uint16_t &a, uint16_t &b),
blac3777 3:251e4d020501 238 _setWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1),
blac3777 3:251e4d020501 239 _orientCoordinates(uint16_t &x1, uint16_t &y1),
blac3777 3:251e4d020501 240 _writeRegister(uint16_t reg, uint16_t data),
blac3777 3:251e4d020501 241 _writeData(uint8_t HI, uint8_t LO),
blac3777 3:251e4d020501 242 _writeCommand(uint8_t HI, uint8_t LO);
blac3777 3:251e4d020501 243
blac3777 3:251e4d020501 244 uint16_t _maxX, _maxY, _bgColor;
blac3777 3:251e4d020501 245
blac3777 3:251e4d020501 246 PinName _rst, _rs, _cs, _sdi, _clk, _led;
blac3777 3:251e4d020501 247 DigitalInOut _rsInOut;
blac3777 3:251e4d020501 248 DigitalInOut _csInOut;
blac3777 3:251e4d020501 249 DigitalInOut _rstInOut;
blac3777 3:251e4d020501 250 DigitalInOut _ledInOut;
blac3777 3:251e4d020501 251 uint8_t _orientation;
blac3777 3:251e4d020501 252
blac3777 3:251e4d020501 253 bool hwSPI;
blac3777 3:251e4d020501 254
blac3777 3:251e4d020501 255 _currentFont cfont;
blac3777 3:251e4d020501 256 };