Added methods and features
Fork of SPI_TFT_ILI9341 by
SPI_TFT_ILI9341.cpp@8:8593d3668153, 2014-04-02 (annotated)
- Committer:
- wim
- Date:
- Wed Apr 02 19:20:09 2014 +0000
- Revision:
- 8:8593d3668153
- Parent:
- 7:4c30bea883bc
- Child:
- 9:6d30a225a5c7
test van updates op SPI van Peter, werkt
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:da1bf437cbc1 | 1 | /* mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller |
dreschpe | 0:da1bf437cbc1 | 2 | * Copyright (c) 2013 Peter Drescher - DC2PD |
dreschpe | 0:da1bf437cbc1 | 3 | * |
dreschpe | 0:da1bf437cbc1 | 4 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dreschpe | 0:da1bf437cbc1 | 5 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dreschpe | 0:da1bf437cbc1 | 6 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dreschpe | 0:da1bf437cbc1 | 7 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dreschpe | 0:da1bf437cbc1 | 8 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dreschpe | 0:da1bf437cbc1 | 9 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dreschpe | 0:da1bf437cbc1 | 10 | * THE SOFTWARE. |
dreschpe | 0:da1bf437cbc1 | 11 | */ |
dreschpe | 0:da1bf437cbc1 | 12 | |
dreschpe | 0:da1bf437cbc1 | 13 | // 12.06.13 fork from SPI_TFT code because controller is different ... |
dreschpe | 2:0a16083193a4 | 14 | // 14.07.13 Test with real display and bugfix |
dreschpe | 4:f018e272220b | 15 | // 18.10.13 Better Circle function from Michael Ammann |
dreschpe | 5:55aed13f2630 | 16 | // 22.10.13 Fixes for Kinetis Board - 8 bit spi |
dreschpe | 6:fe07ae8329f7 | 17 | // 26.01.14 Change interface for BMP_16 to also use SD-cards |
wim | 8:8593d3668153 | 18 | // 30.03.14 WH Added some methods for M24SR F103, Fixed typos & warnings, General define for SPI_16 selection |
dreschpe | 0:da1bf437cbc1 | 19 | |
dreschpe | 0:da1bf437cbc1 | 20 | #include "SPI_TFT_ILI9341.h" |
dreschpe | 0:da1bf437cbc1 | 21 | #include "mbed.h" |
dreschpe | 0:da1bf437cbc1 | 22 | |
dreschpe | 0:da1bf437cbc1 | 23 | #define BPP 16 // Bits per pixel |
dreschpe | 6:fe07ae8329f7 | 24 | |
dreschpe | 0:da1bf437cbc1 | 25 | //extern Serial pc; |
dreschpe | 0:da1bf437cbc1 | 26 | //extern DigitalOut xx; // debug !! |
dreschpe | 0:da1bf437cbc1 | 27 | |
dreschpe | 0:da1bf437cbc1 | 28 | SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name) |
wim | 8:8593d3668153 | 29 | //WH : _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc), GraphicsDisplay(name) |
wim | 8:8593d3668153 | 30 | : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _reset(reset), _dc(dc) |
dreschpe | 0:da1bf437cbc1 | 31 | { |
wim | 8:8593d3668153 | 32 | //WH clk = sclk; |
wim | 8:8593d3668153 | 33 | //WH orientation = 0; |
wim | 8:8593d3668153 | 34 | _origin = Origin_LeftTop; |
dreschpe | 0:da1bf437cbc1 | 35 | char_x = 0; |
dreschpe | 0:da1bf437cbc1 | 36 | tft_reset(); |
dreschpe | 0:da1bf437cbc1 | 37 | } |
dreschpe | 0:da1bf437cbc1 | 38 | |
dreschpe | 0:da1bf437cbc1 | 39 | int SPI_TFT_ILI9341::width() |
dreschpe | 0:da1bf437cbc1 | 40 | { |
wim | 8:8593d3668153 | 41 | // if (orientation == 0 || orientation == 2) return 240; |
wim | 8:8593d3668153 | 42 | // else return 320; |
wim | 8:8593d3668153 | 43 | |
wim | 8:8593d3668153 | 44 | if (_origin == Origin_LeftTop || _origin == Origin_RightBot) return TFT_WIDTH; |
wim | 8:8593d3668153 | 45 | else return TFT_HEIGHT; |
dreschpe | 0:da1bf437cbc1 | 46 | } |
dreschpe | 0:da1bf437cbc1 | 47 | |
dreschpe | 0:da1bf437cbc1 | 48 | |
dreschpe | 0:da1bf437cbc1 | 49 | int SPI_TFT_ILI9341::height() |
dreschpe | 0:da1bf437cbc1 | 50 | { |
wim | 8:8593d3668153 | 51 | // if (orientation == 0 || orientation == 2) return 320; |
wim | 8:8593d3668153 | 52 | // else return 240; |
wim | 8:8593d3668153 | 53 | |
wim | 8:8593d3668153 | 54 | if (_origin == Origin_LeftTop || _origin == Origin_RightBot) return TFT_HEIGHT; |
wim | 8:8593d3668153 | 55 | else return TFT_WIDTH; |
dreschpe | 0:da1bf437cbc1 | 56 | } |
dreschpe | 0:da1bf437cbc1 | 57 | |
wim | 8:8593d3668153 | 58 | //WH |
wim | 8:8593d3668153 | 59 | #if(0) |
dreschpe | 2:0a16083193a4 | 60 | void SPI_TFT_ILI9341::set_orientation(unsigned int o) |
dreschpe | 0:da1bf437cbc1 | 61 | { |
dreschpe | 0:da1bf437cbc1 | 62 | orientation = o; |
dreschpe | 2:0a16083193a4 | 63 | wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
dreschpe | 0:da1bf437cbc1 | 64 | switch (orientation) { |
dreschpe | 0:da1bf437cbc1 | 65 | case 0: |
dreschpe | 2:0a16083193a4 | 66 | _spi.write(0x48); |
dreschpe | 0:da1bf437cbc1 | 67 | break; |
dreschpe | 0:da1bf437cbc1 | 68 | case 1: |
dreschpe | 2:0a16083193a4 | 69 | _spi.write(0x28); |
dreschpe | 0:da1bf437cbc1 | 70 | break; |
dreschpe | 0:da1bf437cbc1 | 71 | case 2: |
dreschpe | 2:0a16083193a4 | 72 | _spi.write(0x88); |
dreschpe | 0:da1bf437cbc1 | 73 | break; |
dreschpe | 0:da1bf437cbc1 | 74 | case 3: |
dreschpe | 2:0a16083193a4 | 75 | _spi.write(0xE8); |
dreschpe | 0:da1bf437cbc1 | 76 | break; |
dreschpe | 0:da1bf437cbc1 | 77 | } |
dreschpe | 2:0a16083193a4 | 78 | _cs = 1; |
wim | 8:8593d3668153 | 79 | window_max(); |
dreschpe | 2:0a16083193a4 | 80 | } |
wim | 8:8593d3668153 | 81 | #else |
wim | 8:8593d3668153 | 82 | void SPI_TFT_ILI9341::set_origin(Origin origin) { |
wim | 8:8593d3668153 | 83 | _origin = origin; |
wim | 8:8593d3668153 | 84 | wr_cmd(ILI9341_MAC); // MEMORY_ACCESS_CONTROL |
wim | 8:8593d3668153 | 85 | switch (_origin) { |
wim | 8:8593d3668153 | 86 | case Origin_LeftTop: /* Left Top of panel is origin */ |
wim | 8:8593d3668153 | 87 | _spi.write(0x48); |
wim | 8:8593d3668153 | 88 | break; |
wim | 8:8593d3668153 | 89 | case Origin_RightTop: |
wim | 8:8593d3668153 | 90 | _spi.write(0x28); |
wim | 8:8593d3668153 | 91 | break; |
wim | 8:8593d3668153 | 92 | case Origin_RightBot: |
wim | 8:8593d3668153 | 93 | _spi.write(0x88); |
wim | 8:8593d3668153 | 94 | break; |
wim | 8:8593d3668153 | 95 | case Origin_LeftBot: |
wim | 8:8593d3668153 | 96 | _spi.write(0xE8); |
wim | 8:8593d3668153 | 97 | break; |
wim | 8:8593d3668153 | 98 | } |
wim | 8:8593d3668153 | 99 | _cs = 1; |
wim | 8:8593d3668153 | 100 | window_max(); |
wim | 8:8593d3668153 | 101 | } |
wim | 8:8593d3668153 | 102 | #endif |
dreschpe | 0:da1bf437cbc1 | 103 | |
dreschpe | 0:da1bf437cbc1 | 104 | |
dreschpe | 0:da1bf437cbc1 | 105 | // write command to tft register |
dreschpe | 0:da1bf437cbc1 | 106 | |
dreschpe | 0:da1bf437cbc1 | 107 | void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd) |
dreschpe | 0:da1bf437cbc1 | 108 | { |
dreschpe | 0:da1bf437cbc1 | 109 | _dc = 0; |
dreschpe | 0:da1bf437cbc1 | 110 | _cs = 0; |
dreschpe | 0:da1bf437cbc1 | 111 | _spi.write(cmd); // mbed lib |
dreschpe | 0:da1bf437cbc1 | 112 | _dc = 1; |
dreschpe | 0:da1bf437cbc1 | 113 | } |
dreschpe | 0:da1bf437cbc1 | 114 | |
dreschpe | 0:da1bf437cbc1 | 115 | |
dreschpe | 0:da1bf437cbc1 | 116 | |
dreschpe | 0:da1bf437cbc1 | 117 | void SPI_TFT_ILI9341::wr_dat(unsigned char dat) |
dreschpe | 0:da1bf437cbc1 | 118 | { |
dreschpe | 0:da1bf437cbc1 | 119 | _spi.write(dat); // mbed lib |
dreschpe | 0:da1bf437cbc1 | 120 | } |
dreschpe | 0:da1bf437cbc1 | 121 | |
dreschpe | 0:da1bf437cbc1 | 122 | |
wim | 8:8593d3668153 | 123 | //WH |
wim | 8:8593d3668153 | 124 | //Read not supported in M24SR |
wim | 8:8593d3668153 | 125 | #if(0) |
dreschpe | 6:fe07ae8329f7 | 126 | // the ILI9341 can read |
dreschpe | 6:fe07ae8329f7 | 127 | |
dreschpe | 6:fe07ae8329f7 | 128 | char SPI_TFT_ILI9341::rd_byte(unsigned char cmd) |
dreschpe | 6:fe07ae8329f7 | 129 | { |
dreschpe | 6:fe07ae8329f7 | 130 | char r; |
dreschpe | 6:fe07ae8329f7 | 131 | _dc = 0; |
dreschpe | 6:fe07ae8329f7 | 132 | _cs = 0; |
dreschpe | 6:fe07ae8329f7 | 133 | _spi.write(cmd); // mbed lib |
dreschpe | 6:fe07ae8329f7 | 134 | _cs = 1; |
dreschpe | 6:fe07ae8329f7 | 135 | r = _spi.write(0xff); |
dreschpe | 6:fe07ae8329f7 | 136 | _cs = 1; |
dreschpe | 6:fe07ae8329f7 | 137 | return(r); |
dreschpe | 6:fe07ae8329f7 | 138 | } |
dreschpe | 0:da1bf437cbc1 | 139 | |
dreschpe | 6:fe07ae8329f7 | 140 | // read 32 bit |
dreschpe | 6:fe07ae8329f7 | 141 | int SPI_TFT_ILI9341::rd_32(unsigned char cmd) |
dreschpe | 6:fe07ae8329f7 | 142 | { |
dreschpe | 6:fe07ae8329f7 | 143 | int d; |
dreschpe | 6:fe07ae8329f7 | 144 | char r; |
dreschpe | 6:fe07ae8329f7 | 145 | _dc = 0; |
dreschpe | 6:fe07ae8329f7 | 146 | _cs = 0; |
dreschpe | 6:fe07ae8329f7 | 147 | d = cmd; |
dreschpe | 6:fe07ae8329f7 | 148 | d = d << 1; |
wim | 8:8593d3668153 | 149 | _spi.format(9,0); // we have to add a dummy clock cycle |
dreschpe | 6:fe07ae8329f7 | 150 | _spi.write(d); |
wim | 8:8593d3668153 | 151 | _spi.format(8,0); |
dreschpe | 6:fe07ae8329f7 | 152 | _dc = 1; |
dreschpe | 6:fe07ae8329f7 | 153 | r = _spi.write(0xff); |
dreschpe | 6:fe07ae8329f7 | 154 | d = r; |
dreschpe | 6:fe07ae8329f7 | 155 | r = _spi.write(0xff); |
dreschpe | 6:fe07ae8329f7 | 156 | d = (d << 8) | r; |
dreschpe | 6:fe07ae8329f7 | 157 | r = _spi.write(0xff); |
dreschpe | 6:fe07ae8329f7 | 158 | d = (d << 8) | r; |
dreschpe | 6:fe07ae8329f7 | 159 | r = _spi.write(0xff); |
dreschpe | 6:fe07ae8329f7 | 160 | d = (d << 8) | r; |
dreschpe | 6:fe07ae8329f7 | 161 | _cs = 1; |
dreschpe | 6:fe07ae8329f7 | 162 | return(d); |
dreschpe | 6:fe07ae8329f7 | 163 | } |
dreschpe | 0:da1bf437cbc1 | 164 | |
dreschpe | 6:fe07ae8329f7 | 165 | int SPI_TFT_ILI9341::Read_ID(void){ |
dreschpe | 6:fe07ae8329f7 | 166 | int r; |
dreschpe | 6:fe07ae8329f7 | 167 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 168 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 169 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 170 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 171 | return(r); |
dreschpe | 6:fe07ae8329f7 | 172 | } |
wim | 8:8593d3668153 | 173 | #endif |
wim | 8:8593d3668153 | 174 | |
dreschpe | 0:da1bf437cbc1 | 175 | |
dreschpe | 0:da1bf437cbc1 | 176 | |
dreschpe | 1:6d6125e88de7 | 177 | // Init code based on MI0283QT datasheet |
dreschpe | 1:6d6125e88de7 | 178 | |
dreschpe | 0:da1bf437cbc1 | 179 | void SPI_TFT_ILI9341::tft_reset() |
dreschpe | 0:da1bf437cbc1 | 180 | { |
wim | 8:8593d3668153 | 181 | //WH _spi.format(8,0); // 8 bit spi Mode 0 |
wim | 8:8593d3668153 | 182 | _spi.format(8,0); // 8 bit spi mode 0 |
wim | 8:8593d3668153 | 183 | |
wim | 8:8593d3668153 | 184 | //WH _spi.frequency(10000000); // 10 Mhz SPI doesnt work on current version of mbed F103 lib due to issue with HSI/HSE... |
wim | 8:8593d3668153 | 185 | // _spi.frequency(4000000); // 4 Mhz SPI clock ==> 2.2Mhz |
wim | 8:8593d3668153 | 186 | _spi.frequency(8000000); // 8 Mhz SPI clock ==> Mhz |
wim | 8:8593d3668153 | 187 | |
wim | 8:8593d3668153 | 188 | |
dreschpe | 0:da1bf437cbc1 | 189 | _cs = 1; // cs high |
dreschpe | 0:da1bf437cbc1 | 190 | _dc = 1; // dc high |
dreschpe | 0:da1bf437cbc1 | 191 | _reset = 0; // display reset |
dreschpe | 0:da1bf437cbc1 | 192 | |
dreschpe | 0:da1bf437cbc1 | 193 | wait_us(50); |
dreschpe | 1:6d6125e88de7 | 194 | _reset = 1; // end hardware reset |
dreschpe | 0:da1bf437cbc1 | 195 | wait_ms(5); |
dreschpe | 1:6d6125e88de7 | 196 | |
wim | 8:8593d3668153 | 197 | //WH wr_cmd(0x01); // SW reset |
wim | 8:8593d3668153 | 198 | wr_cmd(ILI9341_DISPLAY_RST); // SW reset |
dreschpe | 1:6d6125e88de7 | 199 | wait_ms(5); |
wim | 8:8593d3668153 | 200 | //WH wr_cmd(0x28); // display off |
wim | 8:8593d3668153 | 201 | wr_cmd(ILI9341_DISPLAY_OFF); // display off |
dreschpe | 0:da1bf437cbc1 | 202 | |
dreschpe | 0:da1bf437cbc1 | 203 | /* Start Initial Sequence ----------------------------------------------------*/ |
wim | 8:8593d3668153 | 204 | // wr_cmd(0xCF); |
wim | 8:8593d3668153 | 205 | wr_cmd(ILI9341_POWERB); /* Power control B register */ |
dreschpe | 1:6d6125e88de7 | 206 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 207 | _spi.write(0x83); |
dreschpe | 1:6d6125e88de7 | 208 | _spi.write(0x30); |
dreschpe | 1:6d6125e88de7 | 209 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 210 | |
wim | 8:8593d3668153 | 211 | // wr_cmd(0xED); |
wim | 8:8593d3668153 | 212 | wr_cmd(ILI9341_POWER_SEQ); /* Power on sequence register */ |
dreschpe | 1:6d6125e88de7 | 213 | _spi.write(0x64); |
dreschpe | 1:6d6125e88de7 | 214 | _spi.write(0x03); |
dreschpe | 1:6d6125e88de7 | 215 | _spi.write(0x12); |
dreschpe | 1:6d6125e88de7 | 216 | _spi.write(0x81); |
dreschpe | 1:6d6125e88de7 | 217 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 218 | |
wim | 8:8593d3668153 | 219 | // wr_cmd(0xE8); |
wim | 8:8593d3668153 | 220 | wr_cmd(ILI9341_DTCA); /* Driver timing control A */ |
dreschpe | 1:6d6125e88de7 | 221 | _spi.write(0x85); |
dreschpe | 1:6d6125e88de7 | 222 | _spi.write(0x01); |
dreschpe | 1:6d6125e88de7 | 223 | _spi.write(0x79); |
dreschpe | 1:6d6125e88de7 | 224 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 225 | |
wim | 8:8593d3668153 | 226 | // wr_cmd(0xCB); |
wim | 8:8593d3668153 | 227 | wr_cmd(ILI9341_POWERA); /* Power control A register */ |
dreschpe | 0:da1bf437cbc1 | 228 | _spi.write(0x39); |
dreschpe | 0:da1bf437cbc1 | 229 | _spi.write(0x2C); |
dreschpe | 0:da1bf437cbc1 | 230 | _spi.write(0x00); |
dreschpe | 0:da1bf437cbc1 | 231 | _spi.write(0x34); |
dreschpe | 0:da1bf437cbc1 | 232 | _spi.write(0x02); |
dreschpe | 0:da1bf437cbc1 | 233 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 234 | |
wim | 8:8593d3668153 | 235 | // wr_cmd(0xF7); |
wim | 8:8593d3668153 | 236 | wr_cmd(ILI9341_PRC); /* Pump ratio control register */ |
dreschpe | 1:6d6125e88de7 | 237 | _spi.write(0x20); |
dreschpe | 0:da1bf437cbc1 | 238 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 239 | |
wim | 8:8593d3668153 | 240 | // wr_cmd(0xEA); |
wim | 8:8593d3668153 | 241 | wr_cmd(ILI9341_DTCB); /* Driver timing control B */ |
dreschpe | 1:6d6125e88de7 | 242 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 243 | _spi.write(0x00); |
dreschpe | 0:da1bf437cbc1 | 244 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 245 | |
wim | 8:8593d3668153 | 246 | // wr_cmd(0xC0); // POWER_CONTROL_1 |
wim | 8:8593d3668153 | 247 | wr_cmd(ILI9341_POWER1); // POWER_CONTROL_1 |
dreschpe | 1:6d6125e88de7 | 248 | _spi.write(0x26); |
dreschpe | 0:da1bf437cbc1 | 249 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 250 | |
wim | 8:8593d3668153 | 251 | // wr_cmd(0xC1); // POWER_CONTROL_2 |
wim | 8:8593d3668153 | 252 | wr_cmd(ILI9341_POWER2); // POWER_CONTROL_2 |
dreschpe | 0:da1bf437cbc1 | 253 | _spi.write(0x11); |
dreschpe | 0:da1bf437cbc1 | 254 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 255 | |
wim | 8:8593d3668153 | 256 | // wr_cmd(0xC5); // VCOM_CONTROL_1 |
wim | 8:8593d3668153 | 257 | wr_cmd(ILI9341_VCOM1); // VCOM_CONTROL_1 |
dreschpe | 1:6d6125e88de7 | 258 | _spi.write(0x35); |
dreschpe | 1:6d6125e88de7 | 259 | _spi.write(0x3E); |
dreschpe | 0:da1bf437cbc1 | 260 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 261 | |
wim | 8:8593d3668153 | 262 | // wr_cmd(0xC7); // VCOM_CONTROL_2 |
wim | 8:8593d3668153 | 263 | wr_cmd(ILI9341_VCOM2); // VCOM_CONTROL_2 |
dreschpe | 1:6d6125e88de7 | 264 | _spi.write(0xBE); |
dreschpe | 0:da1bf437cbc1 | 265 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 266 | |
wim | 8:8593d3668153 | 267 | // wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
wim | 8:8593d3668153 | 268 | wr_cmd(ILI9341_MAC); // MEMORY_ACCESS_CONTROL |
dreschpe | 0:da1bf437cbc1 | 269 | _spi.write(0x48); |
dreschpe | 0:da1bf437cbc1 | 270 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 271 | |
wim | 8:8593d3668153 | 272 | // wr_cmd(0x3A); // COLMOD_PIXEL_FORMAT_SET |
wim | 8:8593d3668153 | 273 | wr_cmd(ILI9341_PIXEL_FORMAT); /* Pixel Format register */ |
dreschpe | 1:6d6125e88de7 | 274 | _spi.write(0x55); // 16 bit pixel |
dreschpe | 1:6d6125e88de7 | 275 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 276 | |
wim | 8:8593d3668153 | 277 | // wr_cmd(0xB1); // Frame Rate |
wim | 8:8593d3668153 | 278 | wr_cmd(ILI9341_FRC); /* Frame Rate Control register */ |
dreschpe | 1:6d6125e88de7 | 279 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 280 | _spi.write(0x1B); |
dreschpe | 1:6d6125e88de7 | 281 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 282 | |
wim | 8:8593d3668153 | 283 | // wr_cmd(0xF2); // Gamma Function Disable |
wim | 8:8593d3668153 | 284 | wr_cmd(ILI9341_3GAMMA_EN); /* 3 Gamma enable register */ |
wim | 8:8593d3668153 | 285 | _spi.write(0x08); // Gamma Function Disable |
dreschpe | 1:6d6125e88de7 | 286 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 287 | |
wim | 8:8593d3668153 | 288 | // wr_cmd(0x26); |
wim | 8:8593d3668153 | 289 | wr_cmd(ILI9341_GAMMA); /* Gamma register */ |
dreschpe | 1:6d6125e88de7 | 290 | _spi.write(0x01); // gamma set for curve 01/2/04/08 |
dreschpe | 1:6d6125e88de7 | 291 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 292 | |
wim | 8:8593d3668153 | 293 | // wr_cmd(0xE0); // positive gamma correction |
wim | 8:8593d3668153 | 294 | wr_cmd(ILI9341_PGAMMA); /* Positive Gamma Correction register*/ |
dreschpe | 1:6d6125e88de7 | 295 | _spi.write(0x1F); |
dreschpe | 1:6d6125e88de7 | 296 | _spi.write(0x1A); |
dreschpe | 1:6d6125e88de7 | 297 | _spi.write(0x18); |
dreschpe | 1:6d6125e88de7 | 298 | _spi.write(0x0A); |
dreschpe | 1:6d6125e88de7 | 299 | _spi.write(0x0F); |
dreschpe | 1:6d6125e88de7 | 300 | _spi.write(0x06); |
dreschpe | 1:6d6125e88de7 | 301 | _spi.write(0x45); |
dreschpe | 1:6d6125e88de7 | 302 | _spi.write(0x87); |
dreschpe | 1:6d6125e88de7 | 303 | _spi.write(0x32); |
dreschpe | 1:6d6125e88de7 | 304 | _spi.write(0x0A); |
dreschpe | 1:6d6125e88de7 | 305 | _spi.write(0x07); |
dreschpe | 1:6d6125e88de7 | 306 | _spi.write(0x02); |
dreschpe | 1:6d6125e88de7 | 307 | _spi.write(0x07); |
dreschpe | 1:6d6125e88de7 | 308 | _spi.write(0x05); |
dreschpe | 1:6d6125e88de7 | 309 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 310 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 311 | |
wim | 8:8593d3668153 | 312 | // wr_cmd(0xE1); // negativ gamma correction |
wim | 8:8593d3668153 | 313 | wr_cmd(ILI9341_NGAMMA); /* Negative Gamma Correction register*/ |
dreschpe | 1:6d6125e88de7 | 314 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 315 | _spi.write(0x25); |
dreschpe | 1:6d6125e88de7 | 316 | _spi.write(0x27); |
dreschpe | 1:6d6125e88de7 | 317 | _spi.write(0x05); |
dreschpe | 1:6d6125e88de7 | 318 | _spi.write(0x10); |
dreschpe | 1:6d6125e88de7 | 319 | _spi.write(0x09); |
dreschpe | 1:6d6125e88de7 | 320 | _spi.write(0x3A); |
dreschpe | 1:6d6125e88de7 | 321 | _spi.write(0x78); |
dreschpe | 1:6d6125e88de7 | 322 | _spi.write(0x4D); |
dreschpe | 1:6d6125e88de7 | 323 | _spi.write(0x05); |
dreschpe | 1:6d6125e88de7 | 324 | _spi.write(0x18); |
dreschpe | 1:6d6125e88de7 | 325 | _spi.write(0x0D); |
dreschpe | 1:6d6125e88de7 | 326 | _spi.write(0x38); |
dreschpe | 1:6d6125e88de7 | 327 | _spi.write(0x3A); |
dreschpe | 1:6d6125e88de7 | 328 | _spi.write(0x1F); |
dreschpe | 1:6d6125e88de7 | 329 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 330 | |
wim | 8:8593d3668153 | 331 | window_max(); |
wim | 8:8593d3668153 | 332 | |
wim | 8:8593d3668153 | 333 | //wr_cmd(ILI9341_TEAR_OFF); // tearing effect off |
dreschpe | 1:6d6125e88de7 | 334 | //_cs = 1; |
dreschpe | 1:6d6125e88de7 | 335 | |
wim | 8:8593d3668153 | 336 | //wr_cmd(ILI9341_TEAR_ON); // tearing effect on |
dreschpe | 1:6d6125e88de7 | 337 | //_cs = 1; |
dreschpe | 1:6d6125e88de7 | 338 | |
wim | 8:8593d3668153 | 339 | //WH wr_cmd(0xB7); // entry mode |
wim | 8:8593d3668153 | 340 | wr_cmd(ILI9341_ENTRY_MODE); // entry mode |
dreschpe | 1:6d6125e88de7 | 341 | _spi.write(0x07); |
dreschpe | 1:6d6125e88de7 | 342 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 343 | |
wim | 8:8593d3668153 | 344 | // wr_cmd(0xB6); // display function control |
wim | 8:8593d3668153 | 345 | wr_cmd(ILI9341_DFC); /* Display Function Control register*/ |
dreschpe | 1:6d6125e88de7 | 346 | _spi.write(0x0A); |
dreschpe | 1:6d6125e88de7 | 347 | _spi.write(0x82); |
dreschpe | 1:6d6125e88de7 | 348 | _spi.write(0x27); |
dreschpe | 1:6d6125e88de7 | 349 | _spi.write(0x00); |
dreschpe | 1:6d6125e88de7 | 350 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 351 | |
wim | 8:8593d3668153 | 352 | // wr_cmd(0x11); // sleep out |
wim | 8:8593d3668153 | 353 | wr_cmd(ILI9341_SLEEP_OUT); // sleep out |
dreschpe | 1:6d6125e88de7 | 354 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 355 | |
dreschpe | 1:6d6125e88de7 | 356 | wait_ms(100); |
dreschpe | 1:6d6125e88de7 | 357 | |
wim | 8:8593d3668153 | 358 | // wr_cmd(0x29); // display on |
wim | 8:8593d3668153 | 359 | wr_cmd(ILI9341_DISPLAY_ON); |
dreschpe | 1:6d6125e88de7 | 360 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 361 | |
dreschpe | 1:6d6125e88de7 | 362 | wait_ms(100); |
dreschpe | 1:6d6125e88de7 | 363 | |
dreschpe | 1:6d6125e88de7 | 364 | } |
dreschpe | 0:da1bf437cbc1 | 365 | |
dreschpe | 0:da1bf437cbc1 | 366 | |
dreschpe | 0:da1bf437cbc1 | 367 | void SPI_TFT_ILI9341::pixel(int x, int y, int color) |
dreschpe | 0:da1bf437cbc1 | 368 | { |
wim | 8:8593d3668153 | 369 | // wr_cmd(0x2A); |
wim | 8:8593d3668153 | 370 | wr_cmd(ILI9341_COLUMN_ADDR); |
dreschpe | 0:da1bf437cbc1 | 371 | _spi.write(x >> 8); |
dreschpe | 0:da1bf437cbc1 | 372 | _spi.write(x); |
dreschpe | 0:da1bf437cbc1 | 373 | _cs = 1; |
wim | 8:8593d3668153 | 374 | |
wim | 8:8593d3668153 | 375 | // wr_cmd(0x2B); |
wim | 8:8593d3668153 | 376 | wr_cmd(ILI9341_PAGE_ADDR); |
dreschpe | 0:da1bf437cbc1 | 377 | _spi.write(y >> 8); |
dreschpe | 0:da1bf437cbc1 | 378 | _spi.write(y); |
dreschpe | 0:da1bf437cbc1 | 379 | _cs = 1; |
wim | 8:8593d3668153 | 380 | |
wim | 8:8593d3668153 | 381 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 382 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 383 | |
wim | 8:8593d3668153 | 384 | //WH |
wim | 8:8593d3668153 | 385 | #if(0) |
dreschpe | 5:55aed13f2630 | 386 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 387 | _spi.write(color >> 8); |
dreschpe | 5:55aed13f2630 | 388 | _spi.write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 389 | #else |
wim | 8:8593d3668153 | 390 | _spi.format(16,0); // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 391 | _spi.write(color); // Write D0..D15 |
wim | 8:8593d3668153 | 392 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 393 | #endif |
wim | 8:8593d3668153 | 394 | #endif |
wim | 8:8593d3668153 | 395 | |
wim | 8:8593d3668153 | 396 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 397 | // 16 Bit SPI |
wim | 8:8593d3668153 | 398 | _spi.format(16,0); // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 399 | _spi.write(color); // Write D0..D15 |
wim | 8:8593d3668153 | 400 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
wim | 8:8593d3668153 | 401 | #else |
wim | 8:8593d3668153 | 402 | // 8 Bit SPI |
wim | 8:8593d3668153 | 403 | _spi.write(color >> 8); |
wim | 8:8593d3668153 | 404 | _spi.write(color & 0xff); |
wim | 8:8593d3668153 | 405 | #endif |
wim | 8:8593d3668153 | 406 | |
dreschpe | 0:da1bf437cbc1 | 407 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 408 | } |
dreschpe | 0:da1bf437cbc1 | 409 | |
dreschpe | 0:da1bf437cbc1 | 410 | |
dreschpe | 0:da1bf437cbc1 | 411 | void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) |
dreschpe | 0:da1bf437cbc1 | 412 | { |
wim | 8:8593d3668153 | 413 | |
wim | 8:8593d3668153 | 414 | // wr_cmd(0x2A); |
wim | 8:8593d3668153 | 415 | wr_cmd(ILI9341_COLUMN_ADDR); |
dreschpe | 0:da1bf437cbc1 | 416 | _spi.write(x >> 8); |
dreschpe | 0:da1bf437cbc1 | 417 | _spi.write(x); |
dreschpe | 0:da1bf437cbc1 | 418 | _spi.write((x+w-1) >> 8); |
wim | 8:8593d3668153 | 419 | _spi.write(x+w-1); |
dreschpe | 0:da1bf437cbc1 | 420 | _cs = 1; |
wim | 8:8593d3668153 | 421 | |
wim | 8:8593d3668153 | 422 | // wr_cmd(0x2B); |
wim | 8:8593d3668153 | 423 | wr_cmd(ILI9341_PAGE_ADDR); |
dreschpe | 0:da1bf437cbc1 | 424 | _spi.write(y >> 8); |
dreschpe | 0:da1bf437cbc1 | 425 | _spi.write(y); |
dreschpe | 0:da1bf437cbc1 | 426 | _spi.write((y+h-1) >> 8); |
dreschpe | 0:da1bf437cbc1 | 427 | _spi.write(y+h-1); |
dreschpe | 0:da1bf437cbc1 | 428 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 429 | } |
dreschpe | 0:da1bf437cbc1 | 430 | |
dreschpe | 0:da1bf437cbc1 | 431 | |
wim | 8:8593d3668153 | 432 | void SPI_TFT_ILI9341::window_max (void) |
dreschpe | 0:da1bf437cbc1 | 433 | { |
wim | 8:8593d3668153 | 434 | window (0, 0, width(), height()); |
dreschpe | 0:da1bf437cbc1 | 435 | } |
dreschpe | 0:da1bf437cbc1 | 436 | |
dreschpe | 0:da1bf437cbc1 | 437 | |
wim | 8:8593d3668153 | 438 | //WH |
wim | 8:8593d3668153 | 439 | #if(0) |
dreschpe | 0:da1bf437cbc1 | 440 | void SPI_TFT_ILI9341::cls (void) |
dreschpe | 0:da1bf437cbc1 | 441 | { |
dreschpe | 0:da1bf437cbc1 | 442 | int pixel = ( width() * height()); |
wim | 8:8593d3668153 | 443 | window_max(); |
dreschpe | 5:55aed13f2630 | 444 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 445 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 446 | unsigned int i; |
dreschpe | 5:55aed13f2630 | 447 | for (i = 0; i < ( width() * height()); i++){ |
dreschpe | 5:55aed13f2630 | 448 | _spi.write(_background >> 8); |
dreschpe | 5:55aed13f2630 | 449 | _spi.write(_background & 0xff); |
dreschpe | 5:55aed13f2630 | 450 | } |
dreschpe | 5:55aed13f2630 | 451 | |
dreschpe | 5:55aed13f2630 | 452 | #else |
wim | 8:8593d3668153 | 453 | _spi.format(16,0); // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 454 | unsigned int i; |
dreschpe | 0:da1bf437cbc1 | 455 | for (i = 0; i < ( width() * height()); i++) |
dreschpe | 5:55aed13f2630 | 456 | _spi.write(_background); |
wim | 8:8593d3668153 | 457 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 458 | #endif |
dreschpe | 5:55aed13f2630 | 459 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 460 | } |
wim | 8:8593d3668153 | 461 | #else |
dreschpe | 0:da1bf437cbc1 | 462 | |
wim | 8:8593d3668153 | 463 | /** Fill the screen with _background color |
wim | 8:8593d3668153 | 464 | * @param none |
wim | 8:8593d3668153 | 465 | * @return none |
wim | 8:8593d3668153 | 466 | */ |
wim | 8:8593d3668153 | 467 | void SPI_TFT_ILI9341::cls() |
wim | 8:8593d3668153 | 468 | { |
wim | 8:8593d3668153 | 469 | fillrect(0, 0, width()-1, height()-1, _background); |
wim | 8:8593d3668153 | 470 | } |
wim | 8:8593d3668153 | 471 | |
wim | 8:8593d3668153 | 472 | #endif |
dreschpe | 0:da1bf437cbc1 | 473 | |
dreschpe | 0:da1bf437cbc1 | 474 | void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color) |
dreschpe | 0:da1bf437cbc1 | 475 | { |
dreschpe | 0:da1bf437cbc1 | 476 | |
mazgch | 3:3d7298360e45 | 477 | int x = -r, y = 0, err = 2-2*r, e2; |
mazgch | 3:3d7298360e45 | 478 | do { |
mazgch | 3:3d7298360e45 | 479 | pixel(x0-x, y0+y,color); |
mazgch | 3:3d7298360e45 | 480 | pixel(x0+x, y0+y,color); |
mazgch | 3:3d7298360e45 | 481 | pixel(x0+x, y0-y,color); |
mazgch | 3:3d7298360e45 | 482 | pixel(x0-x, y0-y,color); |
mazgch | 3:3d7298360e45 | 483 | e2 = err; |
mazgch | 3:3d7298360e45 | 484 | if (e2 <= y) { |
mazgch | 3:3d7298360e45 | 485 | err += ++y*2+1; |
mazgch | 3:3d7298360e45 | 486 | if (-x == y && e2 <= x) e2 = 0; |
mazgch | 3:3d7298360e45 | 487 | } |
mazgch | 3:3d7298360e45 | 488 | if (e2 > x) err += ++x*2+1; |
mazgch | 3:3d7298360e45 | 489 | } while (x <= 0); |
dreschpe | 4:f018e272220b | 490 | |
dreschpe | 0:da1bf437cbc1 | 491 | } |
dreschpe | 0:da1bf437cbc1 | 492 | |
mazgch | 3:3d7298360e45 | 493 | void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color) |
dreschpe | 0:da1bf437cbc1 | 494 | { |
mazgch | 3:3d7298360e45 | 495 | int x = -r, y = 0, err = 2-2*r, e2; |
mazgch | 3:3d7298360e45 | 496 | do { |
mazgch | 3:3d7298360e45 | 497 | vline(x0-x, y0-y, y0+y, color); |
mazgch | 3:3d7298360e45 | 498 | vline(x0+x, y0-y, y0+y, color); |
mazgch | 3:3d7298360e45 | 499 | e2 = err; |
mazgch | 3:3d7298360e45 | 500 | if (e2 <= y) { |
mazgch | 3:3d7298360e45 | 501 | err += ++y*2+1; |
mazgch | 3:3d7298360e45 | 502 | if (-x == y && e2 <= x) e2 = 0; |
mazgch | 3:3d7298360e45 | 503 | } |
mazgch | 3:3d7298360e45 | 504 | if (e2 > x) err += ++x*2+1; |
mazgch | 3:3d7298360e45 | 505 | } while (x <= 0); |
dreschpe | 0:da1bf437cbc1 | 506 | } |
dreschpe | 0:da1bf437cbc1 | 507 | |
wim | 8:8593d3668153 | 508 | //WH |
wim | 8:8593d3668153 | 509 | #if(0) |
dreschpe | 0:da1bf437cbc1 | 510 | void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) |
dreschpe | 0:da1bf437cbc1 | 511 | { |
dreschpe | 0:da1bf437cbc1 | 512 | int w; |
dreschpe | 0:da1bf437cbc1 | 513 | w = x1 - x0 + 1; |
dreschpe | 0:da1bf437cbc1 | 514 | window(x0,y,w,1); |
dreschpe | 5:55aed13f2630 | 515 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 516 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 517 | int j; |
dreschpe | 5:55aed13f2630 | 518 | for (j=0; j<w; j++) { |
dreschpe | 5:55aed13f2630 | 519 | _spi.write(color >> 8); |
dreschpe | 5:55aed13f2630 | 520 | _spi.write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 521 | } |
dreschpe | 5:55aed13f2630 | 522 | #else |
wim | 8:8593d3668153 | 523 | _spi.format(16,0); // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 524 | int j; |
dreschpe | 0:da1bf437cbc1 | 525 | for (j=0; j<w; j++) { |
dreschpe | 0:da1bf437cbc1 | 526 | _spi.write(color); |
dreschpe | 0:da1bf437cbc1 | 527 | } |
wim | 8:8593d3668153 | 528 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 529 | #endif |
dreschpe | 0:da1bf437cbc1 | 530 | _cs = 1; |
wim | 8:8593d3668153 | 531 | window_max(); |
dreschpe | 0:da1bf437cbc1 | 532 | return; |
dreschpe | 0:da1bf437cbc1 | 533 | } |
wim | 8:8593d3668153 | 534 | #else |
wim | 8:8593d3668153 | 535 | void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) |
wim | 8:8593d3668153 | 536 | { |
wim | 8:8593d3668153 | 537 | int i, w; |
wim | 8:8593d3668153 | 538 | int msb, lsb; |
wim | 8:8593d3668153 | 539 | w = x1 - x0 + 1; |
wim | 8:8593d3668153 | 540 | window(x0,y,w,1); |
wim | 8:8593d3668153 | 541 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 542 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 543 | |
wim | 8:8593d3668153 | 544 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 545 | // 16 Bit SPI |
wim | 8:8593d3668153 | 546 | _spi.format(16,0); // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 547 | for (i = 0; i < w; i++) |
wim | 8:8593d3668153 | 548 | _spi.write(color); |
wim | 8:8593d3668153 | 549 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
wim | 8:8593d3668153 | 550 | #else |
wim | 8:8593d3668153 | 551 | // 8 Bit SPI |
wim | 8:8593d3668153 | 552 | msb = color >> 8; |
wim | 8:8593d3668153 | 553 | lsb = color & 0xff; |
wim | 8:8593d3668153 | 554 | for (i = 0; i < w; i++){ |
wim | 8:8593d3668153 | 555 | _spi.write(msb); |
wim | 8:8593d3668153 | 556 | _spi.write(lsb); |
wim | 8:8593d3668153 | 557 | } |
wim | 8:8593d3668153 | 558 | #endif |
dreschpe | 0:da1bf437cbc1 | 559 | |
wim | 8:8593d3668153 | 560 | _cs = 1; |
wim | 8:8593d3668153 | 561 | window_max(); |
wim | 8:8593d3668153 | 562 | } |
wim | 8:8593d3668153 | 563 | #endif |
wim | 8:8593d3668153 | 564 | |
wim | 8:8593d3668153 | 565 | |
wim | 8:8593d3668153 | 566 | |
wim | 8:8593d3668153 | 567 | //WH |
wim | 8:8593d3668153 | 568 | #if(0) |
dreschpe | 0:da1bf437cbc1 | 569 | void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 570 | { |
dreschpe | 0:da1bf437cbc1 | 571 | int h; |
dreschpe | 0:da1bf437cbc1 | 572 | h = y1 - y0 + 1; |
dreschpe | 0:da1bf437cbc1 | 573 | window(x,y0,1,h); |
dreschpe | 5:55aed13f2630 | 574 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 575 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 576 | for (int y=0; y<h; y++) { |
dreschpe | 5:55aed13f2630 | 577 | _spi.write(color >> 8); |
dreschpe | 5:55aed13f2630 | 578 | _spi.write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 579 | } |
dreschpe | 5:55aed13f2630 | 580 | #else |
wim | 8:8593d3668153 | 581 | _spi.format(16,0); // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 582 | for (int y=0; y<h; y++) { |
dreschpe | 0:da1bf437cbc1 | 583 | _spi.write(color); |
dreschpe | 0:da1bf437cbc1 | 584 | } |
wim | 8:8593d3668153 | 585 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 586 | #endif |
dreschpe | 0:da1bf437cbc1 | 587 | _cs = 1; |
wim | 8:8593d3668153 | 588 | window_max(); |
dreschpe | 0:da1bf437cbc1 | 589 | return; |
dreschpe | 0:da1bf437cbc1 | 590 | } |
wim | 8:8593d3668153 | 591 | #else |
wim | 8:8593d3668153 | 592 | void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) |
wim | 8:8593d3668153 | 593 | { |
wim | 8:8593d3668153 | 594 | int i, h; |
wim | 8:8593d3668153 | 595 | int msb, lsb; |
wim | 8:8593d3668153 | 596 | |
wim | 8:8593d3668153 | 597 | h = y1 - y0 + 1; |
wim | 8:8593d3668153 | 598 | window(x,y0,1,h); |
wim | 8:8593d3668153 | 599 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 600 | wr_cmd(ILI9341_GRAM); // send pixel |
dreschpe | 0:da1bf437cbc1 | 601 | |
wim | 8:8593d3668153 | 602 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 603 | // 16 Bit SPI |
wim | 8:8593d3668153 | 604 | _spi.format(16,0); // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 605 | for (i = 0; i < h; i++) |
wim | 8:8593d3668153 | 606 | _spi.write(color); |
wim | 8:8593d3668153 | 607 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
wim | 8:8593d3668153 | 608 | #else |
wim | 8:8593d3668153 | 609 | // 8 Bit SPI |
wim | 8:8593d3668153 | 610 | msb = color >> 8; |
wim | 8:8593d3668153 | 611 | lsb = color & 0xff; |
wim | 8:8593d3668153 | 612 | for (i = 0; i < h; i++){ |
wim | 8:8593d3668153 | 613 | _spi.write(msb); |
wim | 8:8593d3668153 | 614 | _spi.write(lsb); |
wim | 8:8593d3668153 | 615 | } |
wim | 8:8593d3668153 | 616 | #endif |
wim | 8:8593d3668153 | 617 | |
wim | 8:8593d3668153 | 618 | _cs = 1; |
wim | 8:8593d3668153 | 619 | window_max(); |
wim | 8:8593d3668153 | 620 | } |
wim | 8:8593d3668153 | 621 | #endif |
dreschpe | 0:da1bf437cbc1 | 622 | |
dreschpe | 0:da1bf437cbc1 | 623 | void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 624 | { |
wim | 8:8593d3668153 | 625 | //window_max(); |
dreschpe | 0:da1bf437cbc1 | 626 | int dx = 0, dy = 0; |
dreschpe | 0:da1bf437cbc1 | 627 | int dx_sym = 0, dy_sym = 0; |
dreschpe | 0:da1bf437cbc1 | 628 | int dx_x2 = 0, dy_x2 = 0; |
dreschpe | 0:da1bf437cbc1 | 629 | int di = 0; |
dreschpe | 0:da1bf437cbc1 | 630 | |
dreschpe | 0:da1bf437cbc1 | 631 | dx = x1-x0; |
dreschpe | 0:da1bf437cbc1 | 632 | dy = y1-y0; |
dreschpe | 0:da1bf437cbc1 | 633 | |
dreschpe | 0:da1bf437cbc1 | 634 | if (dx == 0) { /* vertical line */ |
dreschpe | 0:da1bf437cbc1 | 635 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 636 | else vline(x0,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 637 | return; |
dreschpe | 0:da1bf437cbc1 | 638 | } |
dreschpe | 0:da1bf437cbc1 | 639 | |
dreschpe | 0:da1bf437cbc1 | 640 | if (dx > 0) { |
dreschpe | 0:da1bf437cbc1 | 641 | dx_sym = 1; |
dreschpe | 0:da1bf437cbc1 | 642 | } else { |
dreschpe | 0:da1bf437cbc1 | 643 | dx_sym = -1; |
dreschpe | 0:da1bf437cbc1 | 644 | } |
dreschpe | 0:da1bf437cbc1 | 645 | if (dy == 0) { /* horizontal line */ |
dreschpe | 0:da1bf437cbc1 | 646 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 647 | else hline(x1,x0,y0,color); |
dreschpe | 0:da1bf437cbc1 | 648 | return; |
dreschpe | 0:da1bf437cbc1 | 649 | } |
dreschpe | 0:da1bf437cbc1 | 650 | |
dreschpe | 0:da1bf437cbc1 | 651 | if (dy > 0) { |
dreschpe | 0:da1bf437cbc1 | 652 | dy_sym = 1; |
dreschpe | 0:da1bf437cbc1 | 653 | } else { |
dreschpe | 0:da1bf437cbc1 | 654 | dy_sym = -1; |
dreschpe | 0:da1bf437cbc1 | 655 | } |
dreschpe | 0:da1bf437cbc1 | 656 | |
dreschpe | 0:da1bf437cbc1 | 657 | dx = dx_sym*dx; |
dreschpe | 0:da1bf437cbc1 | 658 | dy = dy_sym*dy; |
dreschpe | 0:da1bf437cbc1 | 659 | |
dreschpe | 0:da1bf437cbc1 | 660 | dx_x2 = dx*2; |
dreschpe | 0:da1bf437cbc1 | 661 | dy_x2 = dy*2; |
dreschpe | 0:da1bf437cbc1 | 662 | |
dreschpe | 0:da1bf437cbc1 | 663 | if (dx >= dy) { |
dreschpe | 0:da1bf437cbc1 | 664 | di = dy_x2 - dx; |
dreschpe | 0:da1bf437cbc1 | 665 | while (x0 != x1) { |
dreschpe | 0:da1bf437cbc1 | 666 | |
dreschpe | 0:da1bf437cbc1 | 667 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 668 | x0 += dx_sym; |
dreschpe | 0:da1bf437cbc1 | 669 | if (di<0) { |
dreschpe | 0:da1bf437cbc1 | 670 | di += dy_x2; |
dreschpe | 0:da1bf437cbc1 | 671 | } else { |
dreschpe | 0:da1bf437cbc1 | 672 | di += dy_x2 - dx_x2; |
dreschpe | 0:da1bf437cbc1 | 673 | y0 += dy_sym; |
dreschpe | 0:da1bf437cbc1 | 674 | } |
dreschpe | 0:da1bf437cbc1 | 675 | } |
dreschpe | 0:da1bf437cbc1 | 676 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 677 | } else { |
dreschpe | 0:da1bf437cbc1 | 678 | di = dx_x2 - dy; |
dreschpe | 0:da1bf437cbc1 | 679 | while (y0 != y1) { |
dreschpe | 0:da1bf437cbc1 | 680 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 681 | y0 += dy_sym; |
dreschpe | 0:da1bf437cbc1 | 682 | if (di < 0) { |
dreschpe | 0:da1bf437cbc1 | 683 | di += dx_x2; |
dreschpe | 0:da1bf437cbc1 | 684 | } else { |
dreschpe | 0:da1bf437cbc1 | 685 | di += dx_x2 - dy_x2; |
dreschpe | 0:da1bf437cbc1 | 686 | x0 += dx_sym; |
dreschpe | 0:da1bf437cbc1 | 687 | } |
dreschpe | 0:da1bf437cbc1 | 688 | } |
dreschpe | 0:da1bf437cbc1 | 689 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 690 | } |
wim | 8:8593d3668153 | 691 | //WH return; |
dreschpe | 0:da1bf437cbc1 | 692 | } |
dreschpe | 0:da1bf437cbc1 | 693 | |
dreschpe | 0:da1bf437cbc1 | 694 | |
dreschpe | 0:da1bf437cbc1 | 695 | void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 696 | { |
dreschpe | 0:da1bf437cbc1 | 697 | |
dreschpe | 0:da1bf437cbc1 | 698 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 699 | else hline(x1,x0,y0,color); |
dreschpe | 0:da1bf437cbc1 | 700 | |
dreschpe | 0:da1bf437cbc1 | 701 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 702 | else vline(x0,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 703 | |
dreschpe | 0:da1bf437cbc1 | 704 | if (x1 > x0) hline(x0,x1,y1,color); |
dreschpe | 0:da1bf437cbc1 | 705 | else hline(x1,x0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 706 | |
dreschpe | 0:da1bf437cbc1 | 707 | if (y1 > y0) vline(x1,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 708 | else vline(x1,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 709 | |
wim | 8:8593d3668153 | 710 | //WH return; |
dreschpe | 0:da1bf437cbc1 | 711 | } |
dreschpe | 0:da1bf437cbc1 | 712 | |
dreschpe | 0:da1bf437cbc1 | 713 | |
wim | 8:8593d3668153 | 714 | //WH |
wim | 8:8593d3668153 | 715 | #if(0) |
dreschpe | 0:da1bf437cbc1 | 716 | void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 717 | { |
dreschpe | 0:da1bf437cbc1 | 718 | int h = y1 - y0 + 1; |
dreschpe | 0:da1bf437cbc1 | 719 | int w = x1 - x0 + 1; |
dreschpe | 0:da1bf437cbc1 | 720 | int pixel = h * w; |
dreschpe | 0:da1bf437cbc1 | 721 | window(x0,y0,w,h); |
dreschpe | 0:da1bf437cbc1 | 722 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 723 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 724 | for (int p=0; p<pixel; p++) { |
dreschpe | 5:55aed13f2630 | 725 | _spi.write(color >> 8); |
dreschpe | 5:55aed13f2630 | 726 | _spi.write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 727 | } |
dreschpe | 5:55aed13f2630 | 728 | #else |
wim | 8:8593d3668153 | 729 | _spi.format(16,0); // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 730 | for (int p=0; p<pixel; p++) { |
dreschpe | 0:da1bf437cbc1 | 731 | _spi.write(color); |
dreschpe | 0:da1bf437cbc1 | 732 | } |
wim | 8:8593d3668153 | 733 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 734 | #endif |
dreschpe | 0:da1bf437cbc1 | 735 | _cs = 1; |
wim | 8:8593d3668153 | 736 | window_max(); |
dreschpe | 0:da1bf437cbc1 | 737 | return; |
dreschpe | 0:da1bf437cbc1 | 738 | } |
dreschpe | 0:da1bf437cbc1 | 739 | |
wim | 8:8593d3668153 | 740 | #else |
wim | 8:8593d3668153 | 741 | |
wim | 8:8593d3668153 | 742 | void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color) |
wim | 8:8593d3668153 | 743 | { |
wim | 8:8593d3668153 | 744 | int h = y1 - y0 + 1; //may want to add a sanity check on y1 > y0 |
wim | 8:8593d3668153 | 745 | int w = x1 - x0 + 1; //may want to add a sanity check on x1 > x0 |
wim | 8:8593d3668153 | 746 | int pixels = h * w; |
wim | 8:8593d3668153 | 747 | int i, msb, lsb; |
wim | 8:8593d3668153 | 748 | |
wim | 8:8593d3668153 | 749 | window(x0,y0,w,h); |
wim | 8:8593d3668153 | 750 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 751 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 752 | |
wim | 8:8593d3668153 | 753 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 754 | // 16 Bit SPI |
wim | 8:8593d3668153 | 755 | _spi.format(16,0); // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 756 | for (i = 0; i < pixels; i++) |
wim | 8:8593d3668153 | 757 | _spi.write(color); |
wim | 8:8593d3668153 | 758 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
wim | 8:8593d3668153 | 759 | #else |
wim | 8:8593d3668153 | 760 | // 8 Bit SPI |
wim | 8:8593d3668153 | 761 | msb = color >> 8; |
wim | 8:8593d3668153 | 762 | lsb = color & 0xff; |
wim | 8:8593d3668153 | 763 | for (i = 0; i < pixels; i++){ |
wim | 8:8593d3668153 | 764 | _spi.write(msb); |
wim | 8:8593d3668153 | 765 | _spi.write(lsb); |
wim | 8:8593d3668153 | 766 | } |
wim | 8:8593d3668153 | 767 | #endif |
wim | 8:8593d3668153 | 768 | |
wim | 8:8593d3668153 | 769 | _cs = 1; |
wim | 8:8593d3668153 | 770 | } |
wim | 8:8593d3668153 | 771 | #endif |
wim | 8:8593d3668153 | 772 | |
wim | 8:8593d3668153 | 773 | |
dreschpe | 0:da1bf437cbc1 | 774 | |
dreschpe | 0:da1bf437cbc1 | 775 | void SPI_TFT_ILI9341::locate(int x, int y) |
dreschpe | 0:da1bf437cbc1 | 776 | { |
dreschpe | 0:da1bf437cbc1 | 777 | char_x = x; |
dreschpe | 0:da1bf437cbc1 | 778 | char_y = y; |
dreschpe | 0:da1bf437cbc1 | 779 | } |
dreschpe | 0:da1bf437cbc1 | 780 | |
dreschpe | 0:da1bf437cbc1 | 781 | |
dreschpe | 0:da1bf437cbc1 | 782 | |
dreschpe | 0:da1bf437cbc1 | 783 | int SPI_TFT_ILI9341::columns() |
dreschpe | 0:da1bf437cbc1 | 784 | { |
wim | 8:8593d3668153 | 785 | return width() / _font[1]; |
dreschpe | 0:da1bf437cbc1 | 786 | } |
dreschpe | 0:da1bf437cbc1 | 787 | |
dreschpe | 0:da1bf437cbc1 | 788 | |
dreschpe | 0:da1bf437cbc1 | 789 | |
dreschpe | 0:da1bf437cbc1 | 790 | int SPI_TFT_ILI9341::rows() |
dreschpe | 0:da1bf437cbc1 | 791 | { |
wim | 8:8593d3668153 | 792 | return height() / _font[2]; |
dreschpe | 0:da1bf437cbc1 | 793 | } |
dreschpe | 0:da1bf437cbc1 | 794 | |
dreschpe | 0:da1bf437cbc1 | 795 | |
dreschpe | 0:da1bf437cbc1 | 796 | |
dreschpe | 0:da1bf437cbc1 | 797 | int SPI_TFT_ILI9341::_putc(int value) |
dreschpe | 0:da1bf437cbc1 | 798 | { |
dreschpe | 0:da1bf437cbc1 | 799 | if (value == '\n') { // new line |
dreschpe | 0:da1bf437cbc1 | 800 | char_x = 0; |
wim | 8:8593d3668153 | 801 | char_y = char_y + _font[2]; |
wim | 8:8593d3668153 | 802 | if (char_y >= height() - _font[2]) { |
dreschpe | 0:da1bf437cbc1 | 803 | char_y = 0; |
dreschpe | 0:da1bf437cbc1 | 804 | } |
dreschpe | 0:da1bf437cbc1 | 805 | } else { |
dreschpe | 0:da1bf437cbc1 | 806 | character(char_x, char_y, value); |
dreschpe | 0:da1bf437cbc1 | 807 | } |
dreschpe | 0:da1bf437cbc1 | 808 | return value; |
dreschpe | 0:da1bf437cbc1 | 809 | } |
dreschpe | 0:da1bf437cbc1 | 810 | |
wim | 8:8593d3668153 | 811 | //WH |
wim | 8:8593d3668153 | 812 | #if(0) |
dreschpe | 0:da1bf437cbc1 | 813 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
dreschpe | 0:da1bf437cbc1 | 814 | { |
dreschpe | 0:da1bf437cbc1 | 815 | unsigned int hor,vert,offset,bpl,j,i,b; |
dreschpe | 0:da1bf437cbc1 | 816 | unsigned char* zeichen; |
dreschpe | 0:da1bf437cbc1 | 817 | unsigned char z,w; |
dreschpe | 0:da1bf437cbc1 | 818 | |
dreschpe | 0:da1bf437cbc1 | 819 | if ((c < 31) || (c > 127)) return; // test char range |
dreschpe | 0:da1bf437cbc1 | 820 | |
dreschpe | 0:da1bf437cbc1 | 821 | // read font parameter from start of array |
wim | 8:8593d3668153 | 822 | offset = _font[0]; // bytes / char |
wim | 8:8593d3668153 | 823 | hor = _font[1]; // get hor size of font |
wim | 8:8593d3668153 | 824 | vert = _font[2]; // get vert size of font |
wim | 8:8593d3668153 | 825 | bpl = _font[3]; // bytes per line |
dreschpe | 0:da1bf437cbc1 | 826 | |
dreschpe | 0:da1bf437cbc1 | 827 | if (char_x + hor > width()) { |
dreschpe | 0:da1bf437cbc1 | 828 | char_x = 0; |
dreschpe | 0:da1bf437cbc1 | 829 | char_y = char_y + vert; |
wim | 8:8593d3668153 | 830 | if (char_y >= height() - _font[2]) { |
dreschpe | 0:da1bf437cbc1 | 831 | char_y = 0; |
dreschpe | 0:da1bf437cbc1 | 832 | } |
dreschpe | 0:da1bf437cbc1 | 833 | } |
dreschpe | 0:da1bf437cbc1 | 834 | window(char_x, char_y,hor,vert); // char box |
dreschpe | 5:55aed13f2630 | 835 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 836 | #ifndef TARGET_KL25Z // 16 Bit SPI |
wim | 8:8593d3668153 | 837 | _spi.format(16,0); |
wim | 8:8593d3668153 | 838 | #endif // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 839 | zeichen = &_font[((c -32) * offset) + 4]; // start of char bitmap |
dreschpe | 0:da1bf437cbc1 | 840 | w = zeichen[0]; // width of actual char |
dreschpe | 0:da1bf437cbc1 | 841 | for (j=0; j<vert; j++) { // vert line |
dreschpe | 0:da1bf437cbc1 | 842 | for (i=0; i<hor; i++) { // horz line |
dreschpe | 0:da1bf437cbc1 | 843 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
dreschpe | 0:da1bf437cbc1 | 844 | b = 1 << (j & 0x07); |
dreschpe | 0:da1bf437cbc1 | 845 | if (( z & b ) == 0x00) { |
dreschpe | 5:55aed13f2630 | 846 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 0:da1bf437cbc1 | 847 | _spi.write(_background); |
dreschpe | 5:55aed13f2630 | 848 | #else |
dreschpe | 5:55aed13f2630 | 849 | _spi.write(_background >> 8); |
dreschpe | 5:55aed13f2630 | 850 | _spi.write(_background & 0xff); |
dreschpe | 5:55aed13f2630 | 851 | #endif |
dreschpe | 0:da1bf437cbc1 | 852 | } else { |
dreschpe | 5:55aed13f2630 | 853 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 0:da1bf437cbc1 | 854 | _spi.write(_foreground); |
dreschpe | 5:55aed13f2630 | 855 | #else |
dreschpe | 5:55aed13f2630 | 856 | _spi.write(_foreground >> 8); |
dreschpe | 5:55aed13f2630 | 857 | _spi.write(_foreground & 0xff); |
dreschpe | 5:55aed13f2630 | 858 | #endif |
dreschpe | 0:da1bf437cbc1 | 859 | } |
dreschpe | 0:da1bf437cbc1 | 860 | } |
dreschpe | 0:da1bf437cbc1 | 861 | } |
dreschpe | 0:da1bf437cbc1 | 862 | _cs = 1; |
dreschpe | 5:55aed13f2630 | 863 | #ifndef TARGET_KL25Z // 16 Bit SPI |
wim | 8:8593d3668153 | 864 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 865 | #endif |
wim | 8:8593d3668153 | 866 | window_max(); |
wim | 8:8593d3668153 | 867 | if ((w + 2) < hor) { // x offset to next char |
wim | 8:8593d3668153 | 868 | char_x += w + 2; |
wim | 8:8593d3668153 | 869 | } else char_x += hor; |
wim | 8:8593d3668153 | 870 | } |
wim | 8:8593d3668153 | 871 | #else |
wim | 8:8593d3668153 | 872 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
wim | 8:8593d3668153 | 873 | { |
wim | 8:8593d3668153 | 874 | unsigned int hor,vert,offset,bpl,j,i,b; |
wim | 8:8593d3668153 | 875 | unsigned char* symbol; |
wim | 8:8593d3668153 | 876 | unsigned char z,w; |
wim | 8:8593d3668153 | 877 | |
wim | 8:8593d3668153 | 878 | if ((c < 31) || (c > 127)) return; // test char range |
wim | 8:8593d3668153 | 879 | |
wim | 8:8593d3668153 | 880 | // read font parameter from start of array |
wim | 8:8593d3668153 | 881 | offset = _font[0]; // bytes / char |
wim | 8:8593d3668153 | 882 | hor = _font[1]; // get hor size of font |
wim | 8:8593d3668153 | 883 | vert = _font[2]; // get vert size of font |
wim | 8:8593d3668153 | 884 | bpl = _font[3]; // bytes per line |
wim | 8:8593d3668153 | 885 | |
wim | 8:8593d3668153 | 886 | if (char_x + hor > width()) { |
wim | 8:8593d3668153 | 887 | char_x = 0; |
wim | 8:8593d3668153 | 888 | char_y = char_y + vert; |
wim | 8:8593d3668153 | 889 | if (char_y >= height() - _font[2]) { |
wim | 8:8593d3668153 | 890 | char_y = 0; |
wim | 8:8593d3668153 | 891 | } |
wim | 8:8593d3668153 | 892 | } |
wim | 8:8593d3668153 | 893 | window(char_x, char_y, hor, vert); // char box |
wim | 8:8593d3668153 | 894 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 895 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 896 | |
wim | 8:8593d3668153 | 897 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 898 | // 16 Bit SPI |
wim | 8:8593d3668153 | 899 | _spi.format(16,0); // switch to 16 bit Mode 0 |
wim | 8:8593d3668153 | 900 | #endif |
wim | 8:8593d3668153 | 901 | symbol = &_font[((c - 32) * offset) + 4]; // start of char bitmap |
wim | 8:8593d3668153 | 902 | w = symbol[0]; // width of actual char |
wim | 8:8593d3668153 | 903 | for (j=0; j<vert; j++) { // vert line |
wim | 8:8593d3668153 | 904 | for (i=0; i<hor; i++) { // horz line |
wim | 8:8593d3668153 | 905 | z = symbol[bpl * i + ((j & 0xF8) >> 3)+1]; |
wim | 8:8593d3668153 | 906 | b = 1 << (j & 0x07); |
wim | 8:8593d3668153 | 907 | if (( z & b ) == 0x00) { |
wim | 8:8593d3668153 | 908 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 909 | // 16 Bit SPI |
wim | 8:8593d3668153 | 910 | _spi.write(_background); |
wim | 8:8593d3668153 | 911 | #else |
wim | 8:8593d3668153 | 912 | // 8 Bit SPI |
wim | 8:8593d3668153 | 913 | _spi.write(_background >> 8); |
wim | 8:8593d3668153 | 914 | _spi.write(_background & 0xff); |
wim | 8:8593d3668153 | 915 | #endif |
wim | 8:8593d3668153 | 916 | } |
wim | 8:8593d3668153 | 917 | else { |
wim | 8:8593d3668153 | 918 | #if (SPI_16 == 1) |
wim | 8:8593d3668153 | 919 | // 16 Bit SPI |
wim | 8:8593d3668153 | 920 | _spi.write(_foreground); |
wim | 8:8593d3668153 | 921 | #else |
wim | 8:8593d3668153 | 922 | // 8 Bit SPI |
wim | 8:8593d3668153 | 923 | _spi.write(_foreground >> 8); |
wim | 8:8593d3668153 | 924 | _spi.write(_foreground & 0xff); |
wim | 8:8593d3668153 | 925 | #endif |
wim | 8:8593d3668153 | 926 | } // if |
wim | 8:8593d3668153 | 927 | } // for i |
wim | 8:8593d3668153 | 928 | } // for j |
wim | 8:8593d3668153 | 929 | |
wim | 8:8593d3668153 | 930 | _cs = 1; |
wim | 8:8593d3668153 | 931 | #if (SPI_16 == 1) // 16 Bit SPI |
wim | 8:8593d3668153 | 932 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
wim | 8:8593d3668153 | 933 | #endif |
wim | 8:8593d3668153 | 934 | window_max(); |
wim | 8:8593d3668153 | 935 | |
dreschpe | 0:da1bf437cbc1 | 936 | if ((w + 2) < hor) { // x offset to next char |
dreschpe | 0:da1bf437cbc1 | 937 | char_x += w + 2; |
dreschpe | 0:da1bf437cbc1 | 938 | } else char_x += hor; |
dreschpe | 0:da1bf437cbc1 | 939 | } |
dreschpe | 0:da1bf437cbc1 | 940 | |
wim | 8:8593d3668153 | 941 | #endif |
wim | 8:8593d3668153 | 942 | |
dreschpe | 0:da1bf437cbc1 | 943 | |
dreschpe | 0:da1bf437cbc1 | 944 | void SPI_TFT_ILI9341::set_font(unsigned char* f) |
dreschpe | 0:da1bf437cbc1 | 945 | { |
wim | 8:8593d3668153 | 946 | _font = f; |
dreschpe | 0:da1bf437cbc1 | 947 | } |
dreschpe | 0:da1bf437cbc1 | 948 | |
dreschpe | 0:da1bf437cbc1 | 949 | |
dreschpe | 0:da1bf437cbc1 | 950 | |
dreschpe | 0:da1bf437cbc1 | 951 | void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) |
dreschpe | 0:da1bf437cbc1 | 952 | { |
dreschpe | 0:da1bf437cbc1 | 953 | unsigned int j; |
dreschpe | 0:da1bf437cbc1 | 954 | int padd; |
dreschpe | 0:da1bf437cbc1 | 955 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
dreschpe | 5:55aed13f2630 | 956 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 957 | unsigned short pix_temp; |
dreschpe | 5:55aed13f2630 | 958 | #endif |
dreschpe | 5:55aed13f2630 | 959 | |
dreschpe | 2:0a16083193a4 | 960 | unsigned int i; |
dreschpe | 2:0a16083193a4 | 961 | |
dreschpe | 0:da1bf437cbc1 | 962 | // the lines are padded to multiple of 4 bytes in a bitmap |
dreschpe | 0:da1bf437cbc1 | 963 | padd = -1; |
dreschpe | 0:da1bf437cbc1 | 964 | do { |
dreschpe | 0:da1bf437cbc1 | 965 | padd ++; |
dreschpe | 0:da1bf437cbc1 | 966 | } while (2*(w + padd)%4 != 0); |
dreschpe | 0:da1bf437cbc1 | 967 | window(x, y, w, h); |
dreschpe | 2:0a16083193a4 | 968 | bitmap_ptr += ((h - 1)* (w + padd)); |
wim | 8:8593d3668153 | 969 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 970 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 971 | |
dreschpe | 5:55aed13f2630 | 972 | #ifndef TARGET_KL25Z // 16 Bit SPI |
wim | 8:8593d3668153 | 973 | _spi.format(16,0); |
wim | 8:8593d3668153 | 974 | #endif // switch to 16 bit Mode 0 |
dreschpe | 2:0a16083193a4 | 975 | for (j = 0; j < h; j++) { //Lines |
dreschpe | 2:0a16083193a4 | 976 | for (i = 0; i < w; i++) { // one line |
dreschpe | 5:55aed13f2630 | 977 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 978 | pix_temp = *bitmap_ptr; |
dreschpe | 5:55aed13f2630 | 979 | _spi.write(pix_temp >> 8); |
dreschpe | 5:55aed13f2630 | 980 | _spi.write(pix_temp); |
dreschpe | 5:55aed13f2630 | 981 | bitmap_ptr++; |
dreschpe | 5:55aed13f2630 | 982 | #else |
dreschpe | 5:55aed13f2630 | 983 | _spi.write(*bitmap_ptr); // one line |
dreschpe | 5:55aed13f2630 | 984 | bitmap_ptr++; |
dreschpe | 5:55aed13f2630 | 985 | #endif |
dreschpe | 0:da1bf437cbc1 | 986 | } |
dreschpe | 0:da1bf437cbc1 | 987 | bitmap_ptr -= 2*w; |
dreschpe | 0:da1bf437cbc1 | 988 | bitmap_ptr -= padd; |
dreschpe | 0:da1bf437cbc1 | 989 | } |
dreschpe | 0:da1bf437cbc1 | 990 | _cs = 1; |
dreschpe | 5:55aed13f2630 | 991 | #ifndef TARGET_KL25Z // 16 Bit SPI |
wim | 8:8593d3668153 | 992 | _spi.format(8,0); |
dreschpe | 5:55aed13f2630 | 993 | #endif |
wim | 8:8593d3668153 | 994 | window_max(); |
dreschpe | 0:da1bf437cbc1 | 995 | } |
dreschpe | 0:da1bf437cbc1 | 996 | |
dreschpe | 0:da1bf437cbc1 | 997 | |
dreschpe | 6:fe07ae8329f7 | 998 | // local filesystem is not implemented in kinetis board , but you can add a SD card |
dreschpe | 5:55aed13f2630 | 999 | |
dreschpe | 0:da1bf437cbc1 | 1000 | int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) |
dreschpe | 0:da1bf437cbc1 | 1001 | { |
dreschpe | 0:da1bf437cbc1 | 1002 | |
dreschpe | 0:da1bf437cbc1 | 1003 | #define OffsetPixelWidth 18 |
dreschpe | 0:da1bf437cbc1 | 1004 | #define OffsetPixelHeigh 22 |
dreschpe | 0:da1bf437cbc1 | 1005 | #define OffsetFileSize 34 |
dreschpe | 0:da1bf437cbc1 | 1006 | #define OffsetPixData 10 |
dreschpe | 0:da1bf437cbc1 | 1007 | #define OffsetBPP 28 |
dreschpe | 0:da1bf437cbc1 | 1008 | |
dreschpe | 0:da1bf437cbc1 | 1009 | char filename[50]; |
dreschpe | 0:da1bf437cbc1 | 1010 | unsigned char BMP_Header[54]; |
dreschpe | 0:da1bf437cbc1 | 1011 | unsigned short BPP_t; |
dreschpe | 0:da1bf437cbc1 | 1012 | unsigned int PixelWidth,PixelHeigh,start_data; |
dreschpe | 0:da1bf437cbc1 | 1013 | unsigned int i,off; |
dreschpe | 0:da1bf437cbc1 | 1014 | int padd,j; |
dreschpe | 0:da1bf437cbc1 | 1015 | unsigned short *line; |
dreschpe | 0:da1bf437cbc1 | 1016 | |
dreschpe | 0:da1bf437cbc1 | 1017 | // get the filename |
dreschpe | 6:fe07ae8329f7 | 1018 | i=0; |
dreschpe | 0:da1bf437cbc1 | 1019 | while (*Name_BMP!='\0') { |
dreschpe | 0:da1bf437cbc1 | 1020 | filename[i++]=*Name_BMP++; |
dreschpe | 0:da1bf437cbc1 | 1021 | } |
dreschpe | 6:fe07ae8329f7 | 1022 | filename[i] = 0; |
dreschpe | 6:fe07ae8329f7 | 1023 | |
dreschpe | 0:da1bf437cbc1 | 1024 | FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file |
dreschpe | 0:da1bf437cbc1 | 1025 | if (!Image) { |
dreschpe | 0:da1bf437cbc1 | 1026 | return(0); // error file not found ! |
dreschpe | 0:da1bf437cbc1 | 1027 | } |
dreschpe | 0:da1bf437cbc1 | 1028 | |
dreschpe | 0:da1bf437cbc1 | 1029 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
dreschpe | 0:da1bf437cbc1 | 1030 | |
dreschpe | 0:da1bf437cbc1 | 1031 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
dreschpe | 0:da1bf437cbc1 | 1032 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 1033 | return(-1); // error no BMP file |
dreschpe | 0:da1bf437cbc1 | 1034 | } |
dreschpe | 0:da1bf437cbc1 | 1035 | |
dreschpe | 0:da1bf437cbc1 | 1036 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
dreschpe | 0:da1bf437cbc1 | 1037 | if (BPP_t != 0x0010) { |
dreschpe | 0:da1bf437cbc1 | 1038 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 1039 | return(-2); // error no 16 bit BMP |
dreschpe | 0:da1bf437cbc1 | 1040 | } |
dreschpe | 0:da1bf437cbc1 | 1041 | |
dreschpe | 0:da1bf437cbc1 | 1042 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 1043 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 1044 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
dreschpe | 0:da1bf437cbc1 | 1045 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 1046 | return(-3); // to big |
dreschpe | 0:da1bf437cbc1 | 1047 | } |
dreschpe | 0:da1bf437cbc1 | 1048 | |
dreschpe | 0:da1bf437cbc1 | 1049 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 1050 | |
dreschpe | 0:da1bf437cbc1 | 1051 | line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line |
dreschpe | 0:da1bf437cbc1 | 1052 | if (line == NULL) { |
dreschpe | 0:da1bf437cbc1 | 1053 | return(-4); // error no memory |
dreschpe | 0:da1bf437cbc1 | 1054 | } |
dreschpe | 0:da1bf437cbc1 | 1055 | |
dreschpe | 0:da1bf437cbc1 | 1056 | // the bmp lines are padded to multiple of 4 bytes |
dreschpe | 0:da1bf437cbc1 | 1057 | padd = -1; |
dreschpe | 0:da1bf437cbc1 | 1058 | do { |
dreschpe | 0:da1bf437cbc1 | 1059 | padd ++; |
dreschpe | 0:da1bf437cbc1 | 1060 | } while ((PixelWidth * 2 + padd)%4 != 0); |
dreschpe | 0:da1bf437cbc1 | 1061 | |
dreschpe | 0:da1bf437cbc1 | 1062 | window(x, y,PixelWidth ,PixelHeigh); |
wim | 8:8593d3668153 | 1063 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 1064 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 1065 | |
dreschpe | 6:fe07ae8329f7 | 1066 | #ifndef TARGET_KL25Z // only 8 Bit SPI |
wim | 8:8593d3668153 | 1067 | _spi.format(16,0); |
wim | 8:8593d3668153 | 1068 | #endif // switch to 16 bit Mode 0 |
dreschpe | 0:da1bf437cbc1 | 1069 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
dreschpe | 0:da1bf437cbc1 | 1070 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
dreschpe | 0:da1bf437cbc1 | 1071 | fseek(Image, off ,SEEK_SET); |
dreschpe | 6:fe07ae8329f7 | 1072 | fread(line,1,PixelWidth * 2,Image); // read a line - slow |
dreschpe | 0:da1bf437cbc1 | 1073 | for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
dreschpe | 6:fe07ae8329f7 | 1074 | #ifndef TARGET_KL25Z // only 8 Bit SPI |
dreschpe | 0:da1bf437cbc1 | 1075 | _spi.write(line[i]); // one 16 bit pixel |
dreschpe | 6:fe07ae8329f7 | 1076 | #else |
dreschpe | 6:fe07ae8329f7 | 1077 | _spi.write(line[i] >> 8); |
dreschpe | 6:fe07ae8329f7 | 1078 | _spi.write(line[i]); |
dreschpe | 6:fe07ae8329f7 | 1079 | #endif |
dreschpe | 0:da1bf437cbc1 | 1080 | } |
dreschpe | 0:da1bf437cbc1 | 1081 | } |
dreschpe | 0:da1bf437cbc1 | 1082 | _cs = 1; |
wim | 8:8593d3668153 | 1083 | _spi.format(8,0); |
dreschpe | 0:da1bf437cbc1 | 1084 | free (line); |
dreschpe | 0:da1bf437cbc1 | 1085 | fclose(Image); |
wim | 8:8593d3668153 | 1086 | window_max(); |
dreschpe | 0:da1bf437cbc1 | 1087 | return(1); |
dreschpe | 5:55aed13f2630 | 1088 | } |
wim | 8:8593d3668153 | 1089 | |
wim | 8:8593d3668153 | 1090 | |
wim | 8:8593d3668153 | 1091 | /******************************************************************************* |
wim | 8:8593d3668153 | 1092 | * Function Name : WriteBMP_FAT |
wim | 8:8593d3668153 | 1093 | * @brief Displays a bitmap picture loaded in Flash. |
wim | 8:8593d3668153 | 1094 | * @param Xpos: specifies the X position. |
wim | 8:8593d3668153 | 1095 | * @param Ypos: specifies the Y position. |
wim | 8:8593d3668153 | 1096 | * @param BmpAddress: Bmp picture address in Flash. |
wim | 8:8593d3668153 | 1097 | * @return None |
wim | 8:8593d3668153 | 1098 | *******************************************************************************/ |
wim | 8:8593d3668153 | 1099 | void SPI_TFT_ILI9341::WriteBMP_FAT(uint16_t Xpos, uint16_t Ypos, const char* BmpName) |
wim | 8:8593d3668153 | 1100 | { |
wim | 8:8593d3668153 | 1101 | uint32_t index = 0, size = 0, width=0, height=0; |
wim | 8:8593d3668153 | 1102 | uint16_t *pBmpWord=0; |
wim | 8:8593d3668153 | 1103 | uint16_t data; |
wim | 8:8593d3668153 | 1104 | |
wim | 8:8593d3668153 | 1105 | /* Read bitmap width*/ |
wim | 8:8593d3668153 | 1106 | width = BmpName[0]+1; |
wim | 8:8593d3668153 | 1107 | /* Read bitmap height*/ |
wim | 8:8593d3668153 | 1108 | height = BmpName[1]+1; |
wim | 8:8593d3668153 | 1109 | /* Read bitmap size */ |
wim | 8:8593d3668153 | 1110 | size = width * height; /* nb of 16 bits */ |
wim | 8:8593d3668153 | 1111 | |
wim | 8:8593d3668153 | 1112 | window(Xpos, Ypos, width , height); |
wim | 8:8593d3668153 | 1113 | |
wim | 8:8593d3668153 | 1114 | // wr_cmd(0x2C); // send pixel |
wim | 8:8593d3668153 | 1115 | wr_cmd(ILI9341_GRAM); // send pixel |
wim | 8:8593d3668153 | 1116 | |
wim | 8:8593d3668153 | 1117 | /* Set WRX to send data */ |
wim | 8:8593d3668153 | 1118 | _dc = 1; |
wim | 8:8593d3668153 | 1119 | |
wim | 8:8593d3668153 | 1120 | pBmpWord = (uint16_t *) (&BmpName[5]); |
wim | 8:8593d3668153 | 1121 | /* Send them on the screen */ |
wim | 8:8593d3668153 | 1122 | for(index = 0; index < size; index++) |
wim | 8:8593d3668153 | 1123 | { |
wim | 8:8593d3668153 | 1124 | _cs = 0; |
wim | 8:8593d3668153 | 1125 | |
wim | 8:8593d3668153 | 1126 | data = (*pBmpWord & 0x00FF); |
wim | 8:8593d3668153 | 1127 | _spi.write(data); |
wim | 8:8593d3668153 | 1128 | |
wim | 8:8593d3668153 | 1129 | data = (*pBmpWord & 0xFF00)>>8; |
wim | 8:8593d3668153 | 1130 | _spi.write(data); |
wim | 8:8593d3668153 | 1131 | |
wim | 8:8593d3668153 | 1132 | _cs = 1; |
wim | 8:8593d3668153 | 1133 | |
wim | 8:8593d3668153 | 1134 | pBmpWord++; |
wim | 8:8593d3668153 | 1135 | } |
wim | 8:8593d3668153 | 1136 | |
wim | 8:8593d3668153 | 1137 | /* Set LCD control line(/CS) */ |
wim | 8:8593d3668153 | 1138 | _cs = 1; |
wim | 8:8593d3668153 | 1139 | |
wim | 8:8593d3668153 | 1140 | window_max(); |
wim | 8:8593d3668153 | 1141 | } |
wim | 8:8593d3668153 | 1142 | |
wim | 8:8593d3668153 | 1143 |