I simplified the library "ILI9225_TFT" provided by Arman Safikhani to better suit my needs in implementing a simple sliding puzzle game.
ILI9225.cpp@3:251e4d020501, 2018-04-27 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
blac3777 | 3:251e4d020501 | 1 | #include "ILI9225.h" |
blac3777 | 3:251e4d020501 | 2 | #define bitRead(value, bit) (((value) >> (bit)) & 0x01) |
blac3777 | 3:251e4d020501 | 3 | |
blac3777 | 3:251e4d020501 | 4 | int font_color, x_font, y_font; |
blac3777 | 3:251e4d020501 | 5 | |
blac3777 | 3:251e4d020501 | 6 | //Global Variables |
blac3777 | 3:251e4d020501 | 7 | int x_text = 0, y_text = 0; |
blac3777 | 3:251e4d020501 | 8 | int x_base = 0; |
blac3777 | 3:251e4d020501 | 9 | |
blac3777 | 3:251e4d020501 | 10 | //Constructor when using software SPI. All output pins are configurable. |
blac3777 | 3:251e4d020501 | 11 | ILI9225::ILI9225(PinName rst, PinName rs, PinName cs, |
blac3777 | 3:251e4d020501 | 12 | PinName sdi, PinName clk, PinName led) : |
blac3777 | 3:251e4d020501 | 13 | _rst(rst), _rs(rs), _cs(cs), _sdi(sdi), _clk(clk), spi(sdi, NC, clk), |
blac3777 | 3:251e4d020501 | 14 | _led(led), _rstInOut(_rst), _rsInOut(_rs), _csInOut(_cs), _ledInOut(_led), |
blac3777 | 3:251e4d020501 | 15 | hwSPI(true) |
blac3777 | 3:251e4d020501 | 16 | {} |
blac3777 | 3:251e4d020501 | 17 | |
blac3777 | 3:251e4d020501 | 18 | void ILI9225::_orientCoordinates(uint16_t &x1, uint16_t &y1) { |
blac3777 | 3:251e4d020501 | 19 | switch (_orientation) { |
blac3777 | 3:251e4d020501 | 20 | case 0: |
blac3777 | 3:251e4d020501 | 21 | break; |
blac3777 | 3:251e4d020501 | 22 | case 1: |
blac3777 | 3:251e4d020501 | 23 | y1 = _maxY - y1 - 1; |
blac3777 | 3:251e4d020501 | 24 | _swap(x1, y1); |
blac3777 | 3:251e4d020501 | 25 | break; |
blac3777 | 3:251e4d020501 | 26 | case 2: |
blac3777 | 3:251e4d020501 | 27 | x1 = _maxX - x1 - 1; |
blac3777 | 3:251e4d020501 | 28 | y1 = _maxY - y1 - 1; |
blac3777 | 3:251e4d020501 | 29 | break; |
blac3777 | 3:251e4d020501 | 30 | case 3: |
blac3777 | 3:251e4d020501 | 31 | x1 = _maxX - x1 - 1; |
blac3777 | 3:251e4d020501 | 32 | _swap(x1, y1); |
blac3777 | 3:251e4d020501 | 33 | break; |
blac3777 | 3:251e4d020501 | 34 | } |
blac3777 | 3:251e4d020501 | 35 | } |
blac3777 | 3:251e4d020501 | 36 | |
blac3777 | 3:251e4d020501 | 37 | void ILI9225::_setWindow(uint16_t x0, uint16_t y0, uint16_t x1, |
blac3777 | 3:251e4d020501 | 38 | uint16_t y1) { |
blac3777 | 3:251e4d020501 | 39 | _orientCoordinates(x0, y0); |
blac3777 | 3:251e4d020501 | 40 | _orientCoordinates(x1, y1); |
blac3777 | 3:251e4d020501 | 41 | |
blac3777 | 3:251e4d020501 | 42 | if (x1 < x0) |
blac3777 | 3:251e4d020501 | 43 | _swap(x0, x1); |
blac3777 | 3:251e4d020501 | 44 | if (y1 < y0) |
blac3777 | 3:251e4d020501 | 45 | _swap(y0, y1); |
blac3777 | 3:251e4d020501 | 46 | |
blac3777 | 3:251e4d020501 | 47 | _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR1, x1); |
blac3777 | 3:251e4d020501 | 48 | _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR2, x0); |
blac3777 | 3:251e4d020501 | 49 | |
blac3777 | 3:251e4d020501 | 50 | _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR1, y1); |
blac3777 | 3:251e4d020501 | 51 | _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR2, y0); |
blac3777 | 3:251e4d020501 | 52 | |
blac3777 | 3:251e4d020501 | 53 | _writeRegister(ILI9225_RAM_ADDR_SET1, x0); |
blac3777 | 3:251e4d020501 | 54 | _writeRegister(ILI9225_RAM_ADDR_SET2, y0); |
blac3777 | 3:251e4d020501 | 55 | |
blac3777 | 3:251e4d020501 | 56 | _writeCommand(0x00, 0x22); |
blac3777 | 3:251e4d020501 | 57 | } |
blac3777 | 3:251e4d020501 | 58 | |
blac3777 | 3:251e4d020501 | 59 | void ILI9225::begin() { |
blac3777 | 3:251e4d020501 | 60 | //Set up pins |
blac3777 | 3:251e4d020501 | 61 | _rsInOut.output(); |
blac3777 | 3:251e4d020501 | 62 | _csInOut.output(); |
blac3777 | 3:251e4d020501 | 63 | _rstInOut.output(); |
blac3777 | 3:251e4d020501 | 64 | |
blac3777 | 3:251e4d020501 | 65 | if (_led) |
blac3777 | 3:251e4d020501 | 66 | _ledInOut.output(); |
blac3777 | 3:251e4d020501 | 67 | if (hwSPI) { //Using hardware SPI |
blac3777 | 3:251e4d020501 | 68 | spi.frequency(16000000); |
blac3777 | 3:251e4d020501 | 69 | spi.format(8, 0); |
blac3777 | 3:251e4d020501 | 70 | } |
blac3777 | 3:251e4d020501 | 71 | else { |
blac3777 | 3:251e4d020501 | 72 | DigitalInOut _clkInOut(_clk); |
blac3777 | 3:251e4d020501 | 73 | _clkInOut.output(); |
blac3777 | 3:251e4d020501 | 74 | DigitalInOut _sdiInOut(_sdi); |
blac3777 | 3:251e4d020501 | 75 | _sdiInOut.output(); |
blac3777 | 3:251e4d020501 | 76 | } |
blac3777 | 3:251e4d020501 | 77 | |
blac3777 | 3:251e4d020501 | 78 | //Turn on backlight |
blac3777 | 3:251e4d020501 | 79 | if (_led) |
blac3777 | 3:251e4d020501 | 80 | _ledInOut = 1; |
blac3777 | 3:251e4d020501 | 81 | |
blac3777 | 3:251e4d020501 | 82 | //Initialization Code |
blac3777 | 3:251e4d020501 | 83 | _rstInOut = 1; //Put reset pin high to release ILI9225C from reset status |
blac3777 | 3:251e4d020501 | 84 | wait_ms(1); |
blac3777 | 3:251e4d020501 | 85 | _rstInOut = 0; //Put reset pin low to reset ILI9225 |
blac3777 | 3:251e4d020501 | 86 | wait_ms(10); |
blac3777 | 3:251e4d020501 | 87 | _rstInOut = 1; //Put reset pin high to release ILI9225C from reset status |
blac3777 | 3:251e4d020501 | 88 | wait_ms(50); |
blac3777 | 3:251e4d020501 | 89 | |
blac3777 | 3:251e4d020501 | 90 | //Start Initial Sequence |
blac3777 | 3:251e4d020501 | 91 | //Set SS bit and direction output from S528 to S1 |
blac3777 | 3:251e4d020501 | 92 | _writeRegister(ILI9225_POWER_CTRL1, 0x0000); //Set SAP,DSTB,STB |
blac3777 | 3:251e4d020501 | 93 | _writeRegister(ILI9225_POWER_CTRL2, 0x0000); //Set APON,PON,AON,VCI1EN,VC |
blac3777 | 3:251e4d020501 | 94 | _writeRegister(ILI9225_POWER_CTRL3, 0x0000); //Set BT,DC1,DC2,DC3 |
blac3777 | 3:251e4d020501 | 95 | _writeRegister(ILI9225_POWER_CTRL4, 0x0000); //Set GVDD |
blac3777 | 3:251e4d020501 | 96 | _writeRegister(ILI9225_POWER_CTRL5, 0x0000); //Set VCOMH/VCOML voltage |
blac3777 | 3:251e4d020501 | 97 | wait_ms(40); |
blac3777 | 3:251e4d020501 | 98 | |
blac3777 | 3:251e4d020501 | 99 | //Power-on sequence |
blac3777 | 3:251e4d020501 | 100 | _writeRegister(ILI9225_POWER_CTRL2, 0x0018); //Set APON,PON,AON,VCI1EN,VC |
blac3777 | 3:251e4d020501 | 101 | _writeRegister(ILI9225_POWER_CTRL3, 0x6121); //Set BT,DC1,DC2,DC3 |
blac3777 | 3:251e4d020501 | 102 | _writeRegister(ILI9225_POWER_CTRL4, 0x006F); //Set GVDD /*007F 0088 */ |
blac3777 | 3:251e4d020501 | 103 | _writeRegister(ILI9225_POWER_CTRL5, 0x495F); //Set VCOMH/VCOML voltage |
blac3777 | 3:251e4d020501 | 104 | _writeRegister(ILI9225_POWER_CTRL1, 0x0800); //Set SAP,DSTB,STB |
blac3777 | 3:251e4d020501 | 105 | wait_ms(10); |
blac3777 | 3:251e4d020501 | 106 | _writeRegister(ILI9225_POWER_CTRL2, 0x103B); //Set APON,PON,AON,VCI1EN,VC |
blac3777 | 3:251e4d020501 | 107 | wait_ms(50); |
blac3777 | 3:251e4d020501 | 108 | |
blac3777 | 3:251e4d020501 | 109 | //Set disp line # & disp dir |
blac3777 | 3:251e4d020501 | 110 | _writeRegister(ILI9225_DRIVER_OUTPUT_CTRL, 0x011C); |
blac3777 | 3:251e4d020501 | 111 | _writeRegister(ILI9225_LCD_AC_DRIVING_CTRL, 0x0100); //Set 1 line inversion |
blac3777 | 3:251e4d020501 | 112 | _writeRegister(ILI9225_ENTRY_MODE, 0x1030); //Set GRAM write dir & BGR=1. |
blac3777 | 3:251e4d020501 | 113 | _writeRegister(ILI9225_DISP_CTRL1, 0x0000); //Display off |
blac3777 | 3:251e4d020501 | 114 | _writeRegister(ILI9225_BLANK_PERIOD_CTRL1, 0x0808); //Set back & front porch |
blac3777 | 3:251e4d020501 | 115 | _writeRegister(ILI9225_FRAME_CYCLE_CTRL, 0x1100); //Set clocks # per line |
blac3777 | 3:251e4d020501 | 116 | _writeRegister(ILI9225_INTERFACE_CTRL, 0x0000); //CPU interface |
blac3777 | 3:251e4d020501 | 117 | _writeRegister(ILI9225_OSC_CTRL, 0x0D01); //Set Osc /*0e01*/ |
blac3777 | 3:251e4d020501 | 118 | _writeRegister(ILI9225_VCI_RECYCLING, 0x0020); //Set VCI recycling |
blac3777 | 3:251e4d020501 | 119 | _writeRegister(ILI9225_RAM_ADDR_SET1, 0x0000); //RAM Address |
blac3777 | 3:251e4d020501 | 120 | _writeRegister(ILI9225_RAM_ADDR_SET2, 0x0000); //RAM Address |
blac3777 | 3:251e4d020501 | 121 | |
blac3777 | 3:251e4d020501 | 122 | //Set GRAM area |
blac3777 | 3:251e4d020501 | 123 | _writeRegister(ILI9225_GATE_SCAN_CTRL, 0x0000); |
blac3777 | 3:251e4d020501 | 124 | _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL1, 0x00DB); |
blac3777 | 3:251e4d020501 | 125 | _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL2, 0x0000); |
blac3777 | 3:251e4d020501 | 126 | _writeRegister(ILI9225_VERTICAL_SCROLL_CTRL3, 0x0000); |
blac3777 | 3:251e4d020501 | 127 | _writeRegister(ILI9225_PARTIAL_DRIVING_POS1, 0x00DB); |
blac3777 | 3:251e4d020501 | 128 | _writeRegister(ILI9225_PARTIAL_DRIVING_POS2, 0x0000); |
blac3777 | 3:251e4d020501 | 129 | _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR1, 0x00AF); |
blac3777 | 3:251e4d020501 | 130 | _writeRegister(ILI9225_HORIZONTAL_WINDOW_ADDR2, 0x0000); |
blac3777 | 3:251e4d020501 | 131 | _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR1, 0x00DB); |
blac3777 | 3:251e4d020501 | 132 | _writeRegister(ILI9225_VERTICAL_WINDOW_ADDR2, 0x0000); |
blac3777 | 3:251e4d020501 | 133 | |
blac3777 | 3:251e4d020501 | 134 | //Set GAMMA curve |
blac3777 | 3:251e4d020501 | 135 | _writeRegister(ILI9225_GAMMA_CTRL1, 0x0000); |
blac3777 | 3:251e4d020501 | 136 | _writeRegister(ILI9225_GAMMA_CTRL2, 0x0808); |
blac3777 | 3:251e4d020501 | 137 | _writeRegister(ILI9225_GAMMA_CTRL3, 0x080A); |
blac3777 | 3:251e4d020501 | 138 | _writeRegister(ILI9225_GAMMA_CTRL4, 0x000A); |
blac3777 | 3:251e4d020501 | 139 | _writeRegister(ILI9225_GAMMA_CTRL5, 0x0A08); |
blac3777 | 3:251e4d020501 | 140 | _writeRegister(ILI9225_GAMMA_CTRL6, 0x0808); |
blac3777 | 3:251e4d020501 | 141 | _writeRegister(ILI9225_GAMMA_CTRL7, 0x0000); |
blac3777 | 3:251e4d020501 | 142 | _writeRegister(ILI9225_GAMMA_CTRL8, 0x0A00); |
blac3777 | 3:251e4d020501 | 143 | _writeRegister(ILI9225_GAMMA_CTRL9, 0x0710); |
blac3777 | 3:251e4d020501 | 144 | _writeRegister(ILI9225_GAMMA_CTRL10, 0x0710); |
blac3777 | 3:251e4d020501 | 145 | |
blac3777 | 3:251e4d020501 | 146 | _writeRegister(ILI9225_DISP_CTRL1, 0x0012); |
blac3777 | 3:251e4d020501 | 147 | wait_ms(50); |
blac3777 | 3:251e4d020501 | 148 | _writeRegister(ILI9225_DISP_CTRL1, 0x1017); |
blac3777 | 3:251e4d020501 | 149 | |
blac3777 | 3:251e4d020501 | 150 | setBacklight(true); |
blac3777 | 3:251e4d020501 | 151 | setOrientation(0); |
blac3777 | 3:251e4d020501 | 152 | |
blac3777 | 3:251e4d020501 | 153 | //Initialize variables |
blac3777 | 3:251e4d020501 | 154 | setBackgroundColor(COLOR_BLACK); |
blac3777 | 3:251e4d020501 | 155 | |
blac3777 | 3:251e4d020501 | 156 | clear(); |
blac3777 | 3:251e4d020501 | 157 | } |
blac3777 | 3:251e4d020501 | 158 | |
blac3777 | 3:251e4d020501 | 159 | void ILI9225::clear() { |
blac3777 | 3:251e4d020501 | 160 | uint8_t old = _orientation; |
blac3777 | 3:251e4d020501 | 161 | setOrientation(0); |
blac3777 | 3:251e4d020501 | 162 | fillRectangle(0, 0, _maxX - 1, _maxY - 1, COLOR_BLACK); |
blac3777 | 3:251e4d020501 | 163 | setOrientation(old); |
blac3777 | 3:251e4d020501 | 164 | wait_ms(10); |
blac3777 | 3:251e4d020501 | 165 | } |
blac3777 | 3:251e4d020501 | 166 | |
blac3777 | 3:251e4d020501 | 167 | void ILI9225::fill(uint16_t color) { |
blac3777 | 3:251e4d020501 | 168 | fillRectangle(0, 0, _maxX - 1, _maxY - 1, color); |
blac3777 | 3:251e4d020501 | 169 | } |
blac3777 | 3:251e4d020501 | 170 | |
blac3777 | 3:251e4d020501 | 171 | void ILI9225::invert(bool flag) { |
blac3777 | 3:251e4d020501 | 172 | _writeCommand(0x00, flag ? ILI9225C_INVON : ILI9225C_INVOFF); |
blac3777 | 3:251e4d020501 | 173 | } |
blac3777 | 3:251e4d020501 | 174 | |
blac3777 | 3:251e4d020501 | 175 | void ILI9225::setBacklight(bool flag) { |
blac3777 | 3:251e4d020501 | 176 | if (_led) _ledInOut = flag ? 1 : 0; |
blac3777 | 3:251e4d020501 | 177 | } |
blac3777 | 3:251e4d020501 | 178 | |
blac3777 | 3:251e4d020501 | 179 | void ILI9225::setDisplay(bool flag) { |
blac3777 | 3:251e4d020501 | 180 | if (flag) { |
blac3777 | 3:251e4d020501 | 181 | _writeRegister(0x00ff, 0x0000); |
blac3777 | 3:251e4d020501 | 182 | _writeRegister(ILI9225_POWER_CTRL1, 0x0000); |
blac3777 | 3:251e4d020501 | 183 | wait_ms(50); |
blac3777 | 3:251e4d020501 | 184 | _writeRegister(ILI9225_DISP_CTRL1, 0x1017); |
blac3777 | 3:251e4d020501 | 185 | wait_ms(200); |
blac3777 | 3:251e4d020501 | 186 | } |
blac3777 | 3:251e4d020501 | 187 | else { |
blac3777 | 3:251e4d020501 | 188 | _writeRegister(0x00ff, 0x0000); |
blac3777 | 3:251e4d020501 | 189 | _writeRegister(ILI9225_DISP_CTRL1, 0x0000); |
blac3777 | 3:251e4d020501 | 190 | wait_ms(50); |
blac3777 | 3:251e4d020501 | 191 | _writeRegister(ILI9225_POWER_CTRL1, 0x0003); |
blac3777 | 3:251e4d020501 | 192 | wait_ms(200); |
blac3777 | 3:251e4d020501 | 193 | } |
blac3777 | 3:251e4d020501 | 194 | } |
blac3777 | 3:251e4d020501 | 195 | |
blac3777 | 3:251e4d020501 | 196 | void ILI9225::setOrientation(uint8_t orientation) { |
blac3777 | 3:251e4d020501 | 197 | _orientation = orientation % 4; |
blac3777 | 3:251e4d020501 | 198 | |
blac3777 | 3:251e4d020501 | 199 | switch (_orientation) { |
blac3777 | 3:251e4d020501 | 200 | case 0: |
blac3777 | 3:251e4d020501 | 201 | _maxX = ILI9225_LCD_WIDTH; |
blac3777 | 3:251e4d020501 | 202 | _maxY = ILI9225_LCD_HEIGHT; |
blac3777 | 3:251e4d020501 | 203 | break; |
blac3777 | 3:251e4d020501 | 204 | case 1: |
blac3777 | 3:251e4d020501 | 205 | _maxX = ILI9225_LCD_HEIGHT; |
blac3777 | 3:251e4d020501 | 206 | _maxY = ILI9225_LCD_WIDTH; |
blac3777 | 3:251e4d020501 | 207 | break; |
blac3777 | 3:251e4d020501 | 208 | case 2: |
blac3777 | 3:251e4d020501 | 209 | _maxX = ILI9225_LCD_WIDTH; |
blac3777 | 3:251e4d020501 | 210 | _maxY = ILI9225_LCD_HEIGHT; |
blac3777 | 3:251e4d020501 | 211 | break; |
blac3777 | 3:251e4d020501 | 212 | case 3: |
blac3777 | 3:251e4d020501 | 213 | _maxX = ILI9225_LCD_HEIGHT; |
blac3777 | 3:251e4d020501 | 214 | _maxY = ILI9225_LCD_WIDTH; |
blac3777 | 3:251e4d020501 | 215 | break; |
blac3777 | 3:251e4d020501 | 216 | } |
blac3777 | 3:251e4d020501 | 217 | } |
blac3777 | 3:251e4d020501 | 218 | |
blac3777 | 3:251e4d020501 | 219 | void ILI9225::drawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, |
blac3777 | 3:251e4d020501 | 220 | uint16_t y2, uint16_t color) { |
blac3777 | 3:251e4d020501 | 221 | drawLine(x1, y1, x1, y2, color); |
blac3777 | 3:251e4d020501 | 222 | drawLine(x1, y1, x2, y1, color); |
blac3777 | 3:251e4d020501 | 223 | drawLine(x1, y2, x2, y2, color); |
blac3777 | 3:251e4d020501 | 224 | drawLine(x2, y1, x2, y2, color); |
blac3777 | 3:251e4d020501 | 225 | } |
blac3777 | 3:251e4d020501 | 226 | |
blac3777 | 3:251e4d020501 | 227 | void ILI9225::fillRectangle(uint16_t x1, uint16_t y1, uint16_t x2, |
blac3777 | 3:251e4d020501 | 228 | uint16_t y2, uint16_t color) { |
blac3777 | 3:251e4d020501 | 229 | _setWindow(x1, y1, x2, y2); |
blac3777 | 3:251e4d020501 | 230 | |
blac3777 | 3:251e4d020501 | 231 | for (uint16_t t = (y2 - y1 + 1) * (x2 - x1 + 1); t > 0; t--) |
blac3777 | 3:251e4d020501 | 232 | _writeData(color >> 8, color); |
blac3777 | 3:251e4d020501 | 233 | } |
blac3777 | 3:251e4d020501 | 234 | |
blac3777 | 3:251e4d020501 | 235 | void ILI9225::drawLine(uint16_t x1, uint16_t y1, uint16_t x2, |
blac3777 | 3:251e4d020501 | 236 | uint16_t y2, uint16_t color) { |
blac3777 | 3:251e4d020501 | 237 | //Bresenham algorithm |
blac3777 | 3:251e4d020501 | 238 | int16_t steep = abs(y2 - y1) > abs(x2 - x1); |
blac3777 | 3:251e4d020501 | 239 | int16_t dx, dy; |
blac3777 | 3:251e4d020501 | 240 | |
blac3777 | 3:251e4d020501 | 241 | if (steep) { |
blac3777 | 3:251e4d020501 | 242 | _swap(x1, y1); |
blac3777 | 3:251e4d020501 | 243 | _swap(x2, y2); |
blac3777 | 3:251e4d020501 | 244 | } |
blac3777 | 3:251e4d020501 | 245 | |
blac3777 | 3:251e4d020501 | 246 | if (x1 > x2) { |
blac3777 | 3:251e4d020501 | 247 | _swap(x1, x2); |
blac3777 | 3:251e4d020501 | 248 | _swap(y1, y2); |
blac3777 | 3:251e4d020501 | 249 | } |
blac3777 | 3:251e4d020501 | 250 | |
blac3777 | 3:251e4d020501 | 251 | dx = x2 - x1; |
blac3777 | 3:251e4d020501 | 252 | dy = abs(y2 - y1); |
blac3777 | 3:251e4d020501 | 253 | |
blac3777 | 3:251e4d020501 | 254 | int16_t err = dx / 2; |
blac3777 | 3:251e4d020501 | 255 | int16_t ystep; |
blac3777 | 3:251e4d020501 | 256 | |
blac3777 | 3:251e4d020501 | 257 | if (y1 < y2) |
blac3777 | 3:251e4d020501 | 258 | ystep = 1; |
blac3777 | 3:251e4d020501 | 259 | else |
blac3777 | 3:251e4d020501 | 260 | ystep = -1; |
blac3777 | 3:251e4d020501 | 261 | |
blac3777 | 3:251e4d020501 | 262 | for (; x1 <= x2; x1++) { |
blac3777 | 3:251e4d020501 | 263 | if(steep) |
blac3777 | 3:251e4d020501 | 264 | drawPixel(y1, x1, color); |
blac3777 | 3:251e4d020501 | 265 | else |
blac3777 | 3:251e4d020501 | 266 | drawPixel(x1, y1, color); |
blac3777 | 3:251e4d020501 | 267 | |
blac3777 | 3:251e4d020501 | 268 | err -= dy; |
blac3777 | 3:251e4d020501 | 269 | if (err < 0) { |
blac3777 | 3:251e4d020501 | 270 | y1 += ystep; |
blac3777 | 3:251e4d020501 | 271 | err += dx; |
blac3777 | 3:251e4d020501 | 272 | } |
blac3777 | 3:251e4d020501 | 273 | } |
blac3777 | 3:251e4d020501 | 274 | } |
blac3777 | 3:251e4d020501 | 275 | |
blac3777 | 3:251e4d020501 | 276 | void ILI9225::drawPixel(uint16_t x1, uint16_t y1, uint16_t color) { |
blac3777 | 3:251e4d020501 | 277 | if ((x1 >= _maxX) || (y1 >= _maxY)) |
blac3777 | 3:251e4d020501 | 278 | return; |
blac3777 | 3:251e4d020501 | 279 | |
blac3777 | 3:251e4d020501 | 280 | _setWindow(x1, y1, x1 + 1, y1 + 1); |
blac3777 | 3:251e4d020501 | 281 | _orientCoordinates(x1, y1); |
blac3777 | 3:251e4d020501 | 282 | _writeData(color >> 8, color); |
blac3777 | 3:251e4d020501 | 283 | } |
blac3777 | 3:251e4d020501 | 284 | |
blac3777 | 3:251e4d020501 | 285 | uint16_t ILI9225::maxX() { |
blac3777 | 3:251e4d020501 | 286 | return _maxX; |
blac3777 | 3:251e4d020501 | 287 | } |
blac3777 | 3:251e4d020501 | 288 | |
blac3777 | 3:251e4d020501 | 289 | uint16_t ILI9225::maxY() { |
blac3777 | 3:251e4d020501 | 290 | return _maxY; |
blac3777 | 3:251e4d020501 | 291 | } |
blac3777 | 3:251e4d020501 | 292 | |
blac3777 | 3:251e4d020501 | 293 | uint16_t ILI9225::setColor(uint8_t red8, uint8_t green8, uint8_t blue8) { |
blac3777 | 3:251e4d020501 | 294 | //RGB16 = red5 green6 blue5 |
blac3777 | 3:251e4d020501 | 295 | return (red8 >> 3) << 11 | (green8 >> 2) << 5 | (blue8 >> 3); |
blac3777 | 3:251e4d020501 | 296 | } |
blac3777 | 3:251e4d020501 | 297 | |
blac3777 | 3:251e4d020501 | 298 | void ILI9225::splitColor(uint16_t rgb, uint8_t &red, uint8_t &green, |
blac3777 | 3:251e4d020501 | 299 | uint8_t &blue) { |
blac3777 | 3:251e4d020501 | 300 | //RGB16 = red5 green6 blue5 |
blac3777 | 3:251e4d020501 | 301 | red = (rgb & 0xF800) >> 11 << 3; |
blac3777 | 3:251e4d020501 | 302 | green = (rgb & 0x7E0) >> 5 << 2; |
blac3777 | 3:251e4d020501 | 303 | blue = (rgb & 0x1F) << 3; |
blac3777 | 3:251e4d020501 | 304 | } |
blac3777 | 3:251e4d020501 | 305 | |
blac3777 | 3:251e4d020501 | 306 | void ILI9225::_swap(uint16_t &a, uint16_t &b) { |
blac3777 | 3:251e4d020501 | 307 | uint16_t w = a; |
blac3777 | 3:251e4d020501 | 308 | a = b; |
blac3777 | 3:251e4d020501 | 309 | b = w; |
blac3777 | 3:251e4d020501 | 310 | } |
blac3777 | 3:251e4d020501 | 311 | |
blac3777 | 3:251e4d020501 | 312 | //Utilities |
blac3777 | 3:251e4d020501 | 313 | void ILI9225::_writeCommand(uint8_t HI, uint8_t LO) { |
blac3777 | 3:251e4d020501 | 314 | _rsInOut = 0; |
blac3777 | 3:251e4d020501 | 315 | _csInOut = 0; |
blac3777 | 3:251e4d020501 | 316 | spi.write(HI); |
blac3777 | 3:251e4d020501 | 317 | spi.write(LO); |
blac3777 | 3:251e4d020501 | 318 | _csInOut = 1; |
blac3777 | 3:251e4d020501 | 319 | } |
blac3777 | 3:251e4d020501 | 320 | |
blac3777 | 3:251e4d020501 | 321 | void ILI9225::_writeData(uint8_t HI, uint8_t LO) { |
blac3777 | 3:251e4d020501 | 322 | _rsInOut = 1; |
blac3777 | 3:251e4d020501 | 323 | _csInOut = 0; |
blac3777 | 3:251e4d020501 | 324 | spi.write(HI); |
blac3777 | 3:251e4d020501 | 325 | spi.write(LO); |
blac3777 | 3:251e4d020501 | 326 | _csInOut = 1; |
blac3777 | 3:251e4d020501 | 327 | } |
blac3777 | 3:251e4d020501 | 328 | |
blac3777 | 3:251e4d020501 | 329 | void ILI9225::_writeRegister(uint16_t reg, uint16_t data) { |
blac3777 | 3:251e4d020501 | 330 | _writeCommand(reg >> 8, reg & 255); |
blac3777 | 3:251e4d020501 | 331 | _writeData(data >> 8, data & 255); |
blac3777 | 3:251e4d020501 | 332 | } |
blac3777 | 3:251e4d020501 | 333 | |
blac3777 | 3:251e4d020501 | 334 | void ILI9225::setBackgroundColor(uint16_t color) { |
blac3777 | 3:251e4d020501 | 335 | _bgColor = color; |
blac3777 | 3:251e4d020501 | 336 | } |
blac3777 | 3:251e4d020501 | 337 | |
blac3777 | 3:251e4d020501 | 338 | void ILI9225::setFont(const uint8_t* font) { |
blac3777 | 3:251e4d020501 | 339 | cfont.font = font; |
blac3777 | 3:251e4d020501 | 340 | cfont.width = font[0]; |
blac3777 | 3:251e4d020501 | 341 | cfont.height = font[1]; |
blac3777 | 3:251e4d020501 | 342 | cfont.offset = font[2]; |
blac3777 | 3:251e4d020501 | 343 | cfont.numchars = font[3]; |
blac3777 | 3:251e4d020501 | 344 | cfont.nbrows = cfont.height / 8; |
blac3777 | 3:251e4d020501 | 345 | |
blac3777 | 3:251e4d020501 | 346 | if (cfont.height % 8) |
blac3777 | 3:251e4d020501 | 347 | cfont.nbrows++; //Set # of bytes used by font height in multiples of 8. |
blac3777 | 3:251e4d020501 | 348 | } |
blac3777 | 3:251e4d020501 | 349 | |
blac3777 | 3:251e4d020501 | 350 | void ILI9225::drawText(uint16_t x, uint16_t y, char *s, uint16_t color) { |
blac3777 | 3:251e4d020501 | 351 | uint16_t currx = x; |
blac3777 | 3:251e4d020501 | 352 | |
blac3777 | 3:251e4d020501 | 353 | //Print every character in string |
blac3777 | 3:251e4d020501 | 354 | for (uint8_t k = 0; k < strlen(s); k++) |
blac3777 | 3:251e4d020501 | 355 | currx += drawChar(currx, y, s[k], color) + 1; |
blac3777 | 3:251e4d020501 | 356 | } |
blac3777 | 3:251e4d020501 | 357 | |
blac3777 | 3:251e4d020501 | 358 | uint16_t ILI9225::drawChar(uint16_t x, uint16_t y, uint16_t ch, |
blac3777 | 3:251e4d020501 | 359 | uint16_t color) { |
blac3777 | 3:251e4d020501 | 360 | uint8_t charData, charWidth; |
blac3777 | 3:251e4d020501 | 361 | uint8_t h, i, j; |
blac3777 | 3:251e4d020501 | 362 | uint16_t charOffset; |
blac3777 | 3:251e4d020501 | 363 | |
blac3777 | 3:251e4d020501 | 364 | //Bytes used by each character |
blac3777 | 3:251e4d020501 | 365 | charOffset = (cfont.width * cfont.nbrows) + 1; |
blac3777 | 3:251e4d020501 | 366 | //Char offset (add 4 for font header) |
blac3777 | 3:251e4d020501 | 367 | charOffset = (charOffset * (ch - cfont.offset)) + FONT_HEADER_SIZE; |
blac3777 | 3:251e4d020501 | 368 | //Get font width from 1st byte |
blac3777 | 3:251e4d020501 | 369 | charWidth = cfont.font[charOffset]; |
blac3777 | 3:251e4d020501 | 370 | //Increment pointer to first character data byte |
blac3777 | 3:251e4d020501 | 371 | charOffset++; |
blac3777 | 3:251e4d020501 | 372 | |
blac3777 | 3:251e4d020501 | 373 | //Loop through each font "column" (+1 blank column for spacing) |
blac3777 | 3:251e4d020501 | 374 | for (i = 0; i <= charWidth; i++) { |
blac3777 | 3:251e4d020501 | 375 | h = 0; //Keep track of char height |
blac3777 | 3:251e4d020501 | 376 | for (j = 0; j < cfont.nbrows; j++) { //Each column byte |
blac3777 | 3:251e4d020501 | 377 | if (i == charWidth) |
blac3777 | 3:251e4d020501 | 378 | charData = (uint8_t)0x0; //Insert blank column |
blac3777 | 3:251e4d020501 | 379 | else |
blac3777 | 3:251e4d020501 | 380 | charData = cfont.font[charOffset]; |
blac3777 | 3:251e4d020501 | 381 | charOffset++; |
blac3777 | 3:251e4d020501 | 382 | |
blac3777 | 3:251e4d020501 | 383 | //Process every row in font character |
blac3777 | 3:251e4d020501 | 384 | for (uint8_t k = 0; k < 8; k++) { |
blac3777 | 3:251e4d020501 | 385 | if (h >= cfont.height) |
blac3777 | 3:251e4d020501 | 386 | break; //No need to process excess bits |
blac3777 | 3:251e4d020501 | 387 | if (bitRead(charData, k)) |
blac3777 | 3:251e4d020501 | 388 | drawPixel(x + i, y + (j * 8) + k, color); |
blac3777 | 3:251e4d020501 | 389 | else |
blac3777 | 3:251e4d020501 | 390 | drawPixel(x + i, y + (j * 8) + k, _bgColor); |
blac3777 | 3:251e4d020501 | 391 | h++; |
blac3777 | 3:251e4d020501 | 392 | } |
blac3777 | 3:251e4d020501 | 393 | } |
blac3777 | 3:251e4d020501 | 394 | } |
blac3777 | 3:251e4d020501 | 395 | return charWidth; |
blac3777 | 3:251e4d020501 | 396 | } |
blac3777 | 3:251e4d020501 | 397 | |
blac3777 | 3:251e4d020501 | 398 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 399 | //************************************* ECA 2.8 inch LCD Module **************** |
blac3777 | 3:251e4d020501 | 400 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 401 | //Description : Draws a beveled figure on the screen. |
blac3777 | 3:251e4d020501 | 402 | //Input : x0, y0 - coordinate position of the upper left center |
blac3777 | 3:251e4d020501 | 403 | // : x1, y1 - coordinate position of the lower right center |
blac3777 | 3:251e4d020501 | 404 | // : rad - defines the redius of the circle, |
blac3777 | 3:251e4d020501 | 405 | // : fill - fill yes or no |
blac3777 | 3:251e4d020501 | 406 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 407 | void ILI9225::roundRectangle(int x0, int y0, int x1, int y1, int rad, |
blac3777 | 3:251e4d020501 | 408 | bool fill, int color) { |
blac3777 | 3:251e4d020501 | 409 | signed int a, b, P; |
blac3777 | 3:251e4d020501 | 410 | |
blac3777 | 3:251e4d020501 | 411 | a = 0; //Increment by 1 |
blac3777 | 3:251e4d020501 | 412 | b = rad; //Decrement by 1 using P |
blac3777 | 3:251e4d020501 | 413 | P = 1 - rad; |
blac3777 | 3:251e4d020501 | 414 | if (fill) { |
blac3777 | 3:251e4d020501 | 415 | fillRectangle(x0, y0 + rad, x1, y1 - rad, color); |
blac3777 | 3:251e4d020501 | 416 | |
blac3777 | 3:251e4d020501 | 417 | do { |
blac3777 | 3:251e4d020501 | 418 | fillRectangle(x0 - a + rad, y0 - b + rad, a + x1 - rad, |
blac3777 | 3:251e4d020501 | 419 | y0 - b + rad, color); //8 --> 1 |
blac3777 | 3:251e4d020501 | 420 | fillRectangle(x0 - b + rad, y0 - a + rad, b + x1 - rad, |
blac3777 | 3:251e4d020501 | 421 | y0 - a + rad, color); //7 --> 2 |
blac3777 | 3:251e4d020501 | 422 | fillRectangle(x0 - b + rad, a + y1 - rad, b + x1 - rad, |
blac3777 | 3:251e4d020501 | 423 | a + y1 - rad, color); //6 --> 3 |
blac3777 | 3:251e4d020501 | 424 | fillRectangle(x0 - a + rad, b + y1 - rad, a + x1 - rad, |
blac3777 | 3:251e4d020501 | 425 | b + y1 - rad, color); //5 --> 4 |
blac3777 | 3:251e4d020501 | 426 | |
blac3777 | 3:251e4d020501 | 427 | if (P < 0) |
blac3777 | 3:251e4d020501 | 428 | P += 3 + 2 * a++; |
blac3777 | 3:251e4d020501 | 429 | else |
blac3777 | 3:251e4d020501 | 430 | P += 5 + 2 * (a++ - b--); |
blac3777 | 3:251e4d020501 | 431 | |
blac3777 | 3:251e4d020501 | 432 | } while (a <= b); |
blac3777 | 3:251e4d020501 | 433 | } //Fill |
blac3777 | 3:251e4d020501 | 434 | else { |
blac3777 | 3:251e4d020501 | 435 | fillRectangle(x0 + rad, y0, x1 - rad, y0, color); //Top |
blac3777 | 3:251e4d020501 | 436 | fillRectangle(x0 + rad, y1, x1 - rad, y1, color); //Bottom |
blac3777 | 3:251e4d020501 | 437 | fillRectangle(x0, y0 + rad, x0, y1 - rad, color); //Left |
blac3777 | 3:251e4d020501 | 438 | fillRectangle(x1, y0 + rad, x1, y1 - rad, color); //Right |
blac3777 | 3:251e4d020501 | 439 | |
blac3777 | 3:251e4d020501 | 440 | do { |
blac3777 | 3:251e4d020501 | 441 | drawPixel(a + x1 - rad, y0 - b + rad, color); // `````` Segment 1 |
blac3777 | 3:251e4d020501 | 442 | drawPixel(b + x1 - rad, y0 - a + rad, color); // `````` Segment 2 |
blac3777 | 3:251e4d020501 | 443 | |
blac3777 | 3:251e4d020501 | 444 | drawPixel(b + x1 - rad, a + y1 - rad, color); // `````` Segment 3 |
blac3777 | 3:251e4d020501 | 445 | drawPixel(a + x1 - rad, b + y1 - rad, color); // `````` Segment 4 |
blac3777 | 3:251e4d020501 | 446 | |
blac3777 | 3:251e4d020501 | 447 | drawPixel(x0 - a + rad, b + y1 - rad, color); // `````` Segment 5 |
blac3777 | 3:251e4d020501 | 448 | drawPixel(x0 - b + rad, a + y1 - rad, color); // `````` Segment 6 |
blac3777 | 3:251e4d020501 | 449 | |
blac3777 | 3:251e4d020501 | 450 | drawPixel(x0 - b + rad, y0 - a + rad, color); // `````` Segment 7 |
blac3777 | 3:251e4d020501 | 451 | drawPixel(x0 - a + rad, y0 - b + rad, color); // `````` Segment 8 |
blac3777 | 3:251e4d020501 | 452 | |
blac3777 | 3:251e4d020501 | 453 | if (P < 0) |
blac3777 | 3:251e4d020501 | 454 | P += 3 + 2 * a++; |
blac3777 | 3:251e4d020501 | 455 | else |
blac3777 | 3:251e4d020501 | 456 | P += 5 + 2 * (a++ - b--); |
blac3777 | 3:251e4d020501 | 457 | } while (a <= b); |
blac3777 | 3:251e4d020501 | 458 | } //No fill |
blac3777 | 3:251e4d020501 | 459 | } //RoundRectangle |
blac3777 | 3:251e4d020501 | 460 | |
blac3777 | 3:251e4d020501 | 461 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 462 | //************************************* ECA 2.8 inch LCD Module **************** |
blac3777 | 3:251e4d020501 | 463 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 464 | //Go to a specific pont for farsi font (x:0..TS_SIZE_X , y:0..TS_SIZE_Y) |
blac3777 | 3:251e4d020501 | 465 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 466 | void ILI9225::goToXY(int x, int y) { |
blac3777 | 3:251e4d020501 | 467 | if ((x >= _maxX) || (x < 0)) { |
blac3777 | 3:251e4d020501 | 468 | x_font = 0; |
blac3777 | 3:251e4d020501 | 469 | x_base = 0; |
blac3777 | 3:251e4d020501 | 470 | } |
blac3777 | 3:251e4d020501 | 471 | else { |
blac3777 | 3:251e4d020501 | 472 | x_font = x; |
blac3777 | 3:251e4d020501 | 473 | x_base = x; |
blac3777 | 3:251e4d020501 | 474 | } |
blac3777 | 3:251e4d020501 | 475 | if ((y >= _maxY) || (y < 0)) |
blac3777 | 3:251e4d020501 | 476 | y_font = 0; |
blac3777 | 3:251e4d020501 | 477 | else |
blac3777 | 3:251e4d020501 | 478 | y_font = y; |
blac3777 | 3:251e4d020501 | 479 | } |
blac3777 | 3:251e4d020501 | 480 | |
blac3777 | 3:251e4d020501 | 481 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 482 | //************************************* ECA 2.8 inch LCD Module **************** |
blac3777 | 3:251e4d020501 | 483 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 484 | //Make an ascii string from an unicode string |
blac3777 | 3:251e4d020501 | 485 | //------------------------------------------------------------------------------ |
blac3777 | 3:251e4d020501 | 486 | |
blac3777 | 3:251e4d020501 | 487 | void ILI9225::unicode2ascii(char *uni_str, char *ascii_str) { |
blac3777 | 3:251e4d020501 | 488 | int counter = 0; |
blac3777 | 3:251e4d020501 | 489 | int Uch = 0; |
blac3777 | 3:251e4d020501 | 490 | char chl, chh; |
blac3777 | 3:251e4d020501 | 491 | |
blac3777 | 3:251e4d020501 | 492 | while (*uni_str) { |
blac3777 | 3:251e4d020501 | 493 | chl = *uni_str++; |
blac3777 | 3:251e4d020501 | 494 | chh = *uni_str++; |
blac3777 | 3:251e4d020501 | 495 | |
blac3777 | 3:251e4d020501 | 496 | Uch = 0; |
blac3777 | 3:251e4d020501 | 497 | Uch = ((Uch | chh) << 8) | chl; |
blac3777 | 3:251e4d020501 | 498 | |
blac3777 | 3:251e4d020501 | 499 | if (Uch > 1574 && Uch < 1591) |
blac3777 | 3:251e4d020501 | 500 | *(ascii_str + counter) = (char)(Uch - 1376); |
blac3777 | 3:251e4d020501 | 501 | else if (Uch > 1590 && Uch < 1595) |
blac3777 | 3:251e4d020501 | 502 | *(ascii_str + counter) = (char)(Uch - 1375); |
blac3777 | 3:251e4d020501 | 503 | else if (Uch > 1600 && Uch < 1603) |
blac3777 | 3:251e4d020501 | 504 | *(ascii_str + counter) = (char)(Uch - 1380); |
blac3777 | 3:251e4d020501 | 505 | else if (Uch == 1705) |
blac3777 | 3:251e4d020501 | 506 | *(ascii_str + counter) = (char)(Uch - 1482); |
blac3777 | 3:251e4d020501 | 507 | else if (Uch == 1604) |
blac3777 | 3:251e4d020501 | 508 | *(ascii_str + counter) = (char)(Uch - 1379); |
blac3777 | 3:251e4d020501 | 509 | else if (Uch > 1604 && Uch < 1609) |
blac3777 | 3:251e4d020501 | 510 | *(ascii_str + counter) = (char)(Uch - 1378); |
blac3777 | 3:251e4d020501 | 511 | else if (Uch == 1740) |
blac3777 | 3:251e4d020501 | 512 | *(ascii_str + counter) = (char)(Uch - 1503); |
blac3777 | 3:251e4d020501 | 513 | else if (Uch == 1574) |
blac3777 | 3:251e4d020501 | 514 | *(ascii_str + counter) = (char)(Uch - 1381); |
blac3777 | 3:251e4d020501 | 515 | else if (Uch == 1662) |
blac3777 | 3:251e4d020501 | 516 | *(ascii_str + counter) = (char)(Uch - 1533); |
blac3777 | 3:251e4d020501 | 517 | else if (Uch == 1670) |
blac3777 | 3:251e4d020501 | 518 | *(ascii_str + counter) = (char)(Uch - 1529); |
blac3777 | 3:251e4d020501 | 519 | else if (Uch == 1688) |
blac3777 | 3:251e4d020501 | 520 | *(ascii_str + counter) = (char)(Uch - 1546); |
blac3777 | 3:251e4d020501 | 521 | else if (Uch == 1711) |
blac3777 | 3:251e4d020501 | 522 | *(ascii_str + counter) = (char)(Uch - 1567); |
blac3777 | 3:251e4d020501 | 523 | else if (Uch == 1570) |
blac3777 | 3:251e4d020501 | 524 | *(ascii_str + counter) = (char)(Uch - 1376); |
blac3777 | 3:251e4d020501 | 525 | else if (Uch > 1631 && Uch < 1642) |
blac3777 | 3:251e4d020501 | 526 | *(ascii_str + counter) = (char)(Uch - 1584); |
blac3777 | 3:251e4d020501 | 527 | else if (Uch == 65536) |
blac3777 | 3:251e4d020501 | 528 | *(ascii_str + counter) = NULL; |
blac3777 | 3:251e4d020501 | 529 | else |
blac3777 | 3:251e4d020501 | 530 | *(ascii_str + counter) = (char)Uch; |
blac3777 | 3:251e4d020501 | 531 | |
blac3777 | 3:251e4d020501 | 532 | counter++; |
blac3777 | 3:251e4d020501 | 533 | } |
blac3777 | 3:251e4d020501 | 534 | *(ascii_str + counter) = NULL; |
blac3777 | 3:251e4d020501 | 535 | } |