Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of SPI_TFT_ILI9341 by
SPI_TFT_ILI9341.cpp@14:70665f0a182f, 2015-03-24 (annotated)
- Committer:
- JackB
- Date:
- Tue Mar 24 01:33:24 2015 +0000
- Revision:
- 14:70665f0a182f
- Parent:
- 13:b2b3e5430f81
Rounded rectangles, rounded filled rectangles added
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 |
dreschpe | 9:423e6a952472 | 18 | // 23.06.14 switch back to old Version - fork for L152 |
dreschpe | 11:59eca2723ec5 | 19 | // 24.06.14 Add compiler flag for optimized L152 version |
dreschpe | 12:98cc5c193ecd | 20 | // 25.06.14 Add optimized F103 version |
dreschpe | 11:59eca2723ec5 | 21 | |
dreschpe | 11:59eca2723ec5 | 22 | // exclude this file for platforms with optimized version |
dreschpe | 13:b2b3e5430f81 | 23 | #if defined TARGET_NUCLEO_L152RE || defined TARGET_NUCLEO_F103RB || defined TARGET_LPC1768 |
dreschpe | 12:98cc5c193ecd | 24 | // this platforms are supported by special version in different source file |
dreschpe | 12:98cc5c193ecd | 25 | #else |
dreschpe | 0:da1bf437cbc1 | 26 | |
dreschpe | 0:da1bf437cbc1 | 27 | #include "SPI_TFT_ILI9341.h" |
dreschpe | 0:da1bf437cbc1 | 28 | #include "mbed.h" |
dreschpe | 0:da1bf437cbc1 | 29 | |
dreschpe | 0:da1bf437cbc1 | 30 | #define BPP 16 // Bits per pixel |
dreschpe | 6:fe07ae8329f7 | 31 | |
dreschpe | 0:da1bf437cbc1 | 32 | //extern Serial pc; |
dreschpe | 0:da1bf437cbc1 | 33 | //extern DigitalOut xx; // debug !! |
dreschpe | 0:da1bf437cbc1 | 34 | |
dreschpe | 0:da1bf437cbc1 | 35 | SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name) |
dreschpe | 11:59eca2723ec5 | 36 | : GraphicsDisplay(name), SPI(mosi, miso, sclk,NC), _cs(cs), _reset(reset), _dc(dc) |
dreschpe | 0:da1bf437cbc1 | 37 | { |
dreschpe | 11:59eca2723ec5 | 38 | |
dreschpe | 0:da1bf437cbc1 | 39 | orientation = 0; |
dreschpe | 0:da1bf437cbc1 | 40 | char_x = 0; |
dreschpe | 11:59eca2723ec5 | 41 | SPI::format(8,3); // 8 bit spi mode 3 |
dreschpe | 11:59eca2723ec5 | 42 | SPI::frequency(10000000); // 10 Mhz SPI clock |
dreschpe | 0:da1bf437cbc1 | 43 | tft_reset(); |
dreschpe | 0:da1bf437cbc1 | 44 | } |
dreschpe | 0:da1bf437cbc1 | 45 | |
dreschpe | 0:da1bf437cbc1 | 46 | int SPI_TFT_ILI9341::width() |
dreschpe | 0:da1bf437cbc1 | 47 | { |
dreschpe | 0:da1bf437cbc1 | 48 | if (orientation == 0 || orientation == 2) return 240; |
dreschpe | 0:da1bf437cbc1 | 49 | else return 320; |
dreschpe | 0:da1bf437cbc1 | 50 | } |
dreschpe | 0:da1bf437cbc1 | 51 | |
dreschpe | 0:da1bf437cbc1 | 52 | |
dreschpe | 0:da1bf437cbc1 | 53 | int SPI_TFT_ILI9341::height() |
dreschpe | 0:da1bf437cbc1 | 54 | { |
dreschpe | 0:da1bf437cbc1 | 55 | if (orientation == 0 || orientation == 2) return 320; |
dreschpe | 0:da1bf437cbc1 | 56 | else return 240; |
dreschpe | 0:da1bf437cbc1 | 57 | } |
dreschpe | 0:da1bf437cbc1 | 58 | |
dreschpe | 0:da1bf437cbc1 | 59 | |
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 | 11:59eca2723ec5 | 66 | SPI::write(0x48); |
dreschpe | 0:da1bf437cbc1 | 67 | break; |
dreschpe | 0:da1bf437cbc1 | 68 | case 1: |
dreschpe | 11:59eca2723ec5 | 69 | SPI::write(0x28); |
dreschpe | 0:da1bf437cbc1 | 70 | break; |
dreschpe | 0:da1bf437cbc1 | 71 | case 2: |
dreschpe | 11:59eca2723ec5 | 72 | SPI::write(0x88); |
dreschpe | 0:da1bf437cbc1 | 73 | break; |
dreschpe | 0:da1bf437cbc1 | 74 | case 3: |
dreschpe | 11:59eca2723ec5 | 75 | SPI::write(0xE8); |
dreschpe | 0:da1bf437cbc1 | 76 | break; |
dreschpe | 0:da1bf437cbc1 | 77 | } |
dreschpe | 2:0a16083193a4 | 78 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 79 | WindowMax(); |
dreschpe | 2:0a16083193a4 | 80 | } |
dreschpe | 0:da1bf437cbc1 | 81 | |
dreschpe | 0:da1bf437cbc1 | 82 | |
dreschpe | 0:da1bf437cbc1 | 83 | // write command to tft register |
dreschpe | 0:da1bf437cbc1 | 84 | |
dreschpe | 0:da1bf437cbc1 | 85 | void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd) |
dreschpe | 0:da1bf437cbc1 | 86 | { |
dreschpe | 0:da1bf437cbc1 | 87 | _dc = 0; |
dreschpe | 0:da1bf437cbc1 | 88 | _cs = 0; |
dreschpe | 11:59eca2723ec5 | 89 | SPI::write(cmd); // mbed lib |
dreschpe | 0:da1bf437cbc1 | 90 | _dc = 1; |
dreschpe | 0:da1bf437cbc1 | 91 | } |
dreschpe | 0:da1bf437cbc1 | 92 | |
dreschpe | 0:da1bf437cbc1 | 93 | |
dreschpe | 0:da1bf437cbc1 | 94 | |
dreschpe | 0:da1bf437cbc1 | 95 | void SPI_TFT_ILI9341::wr_dat(unsigned char dat) |
dreschpe | 0:da1bf437cbc1 | 96 | { |
dreschpe | 11:59eca2723ec5 | 97 | SPI::write(dat); // mbed lib |
dreschpe | 0:da1bf437cbc1 | 98 | } |
dreschpe | 0:da1bf437cbc1 | 99 | |
dreschpe | 0:da1bf437cbc1 | 100 | |
dreschpe | 0:da1bf437cbc1 | 101 | |
dreschpe | 6:fe07ae8329f7 | 102 | // the ILI9341 can read |
dreschpe | 6:fe07ae8329f7 | 103 | |
dreschpe | 6:fe07ae8329f7 | 104 | char SPI_TFT_ILI9341::rd_byte(unsigned char cmd) |
dreschpe | 6:fe07ae8329f7 | 105 | { |
dreschpe | 6:fe07ae8329f7 | 106 | char r; |
dreschpe | 6:fe07ae8329f7 | 107 | _dc = 0; |
dreschpe | 6:fe07ae8329f7 | 108 | _cs = 0; |
dreschpe | 11:59eca2723ec5 | 109 | SPI::write(cmd); // mbed lib |
dreschpe | 6:fe07ae8329f7 | 110 | _cs = 1; |
dreschpe | 11:59eca2723ec5 | 111 | r = SPI::write(0xff); |
dreschpe | 6:fe07ae8329f7 | 112 | _cs = 1; |
dreschpe | 6:fe07ae8329f7 | 113 | return(r); |
dreschpe | 6:fe07ae8329f7 | 114 | } |
dreschpe | 0:da1bf437cbc1 | 115 | |
dreschpe | 6:fe07ae8329f7 | 116 | // read 32 bit |
dreschpe | 6:fe07ae8329f7 | 117 | int SPI_TFT_ILI9341::rd_32(unsigned char cmd) |
dreschpe | 6:fe07ae8329f7 | 118 | { |
dreschpe | 6:fe07ae8329f7 | 119 | int d; |
dreschpe | 6:fe07ae8329f7 | 120 | char r; |
dreschpe | 6:fe07ae8329f7 | 121 | _dc = 0; |
dreschpe | 6:fe07ae8329f7 | 122 | _cs = 0; |
dreschpe | 6:fe07ae8329f7 | 123 | d = cmd; |
dreschpe | 6:fe07ae8329f7 | 124 | d = d << 1; |
dreschpe | 11:59eca2723ec5 | 125 | SPI::format(9,3); // we have to add a dummy clock cycle |
dreschpe | 11:59eca2723ec5 | 126 | SPI::write(d); |
dreschpe | 11:59eca2723ec5 | 127 | SPI::format(8,3); |
dreschpe | 6:fe07ae8329f7 | 128 | _dc = 1; |
dreschpe | 11:59eca2723ec5 | 129 | r = SPI::write(0xff); |
dreschpe | 6:fe07ae8329f7 | 130 | d = r; |
dreschpe | 11:59eca2723ec5 | 131 | r = SPI::write(0xff); |
dreschpe | 6:fe07ae8329f7 | 132 | d = (d << 8) | r; |
dreschpe | 11:59eca2723ec5 | 133 | r = SPI::write(0xff); |
dreschpe | 6:fe07ae8329f7 | 134 | d = (d << 8) | r; |
dreschpe | 11:59eca2723ec5 | 135 | r = SPI::write(0xff); |
dreschpe | 6:fe07ae8329f7 | 136 | d = (d << 8) | r; |
dreschpe | 6:fe07ae8329f7 | 137 | _cs = 1; |
dreschpe | 6:fe07ae8329f7 | 138 | return(d); |
dreschpe | 6:fe07ae8329f7 | 139 | } |
dreschpe | 0:da1bf437cbc1 | 140 | |
dreschpe | 6:fe07ae8329f7 | 141 | int SPI_TFT_ILI9341::Read_ID(void){ |
dreschpe | 6:fe07ae8329f7 | 142 | int r; |
dreschpe | 6:fe07ae8329f7 | 143 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 144 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 145 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 146 | r = rd_byte(0x0A); |
dreschpe | 6:fe07ae8329f7 | 147 | return(r); |
dreschpe | 6:fe07ae8329f7 | 148 | } |
dreschpe | 0:da1bf437cbc1 | 149 | |
dreschpe | 0:da1bf437cbc1 | 150 | |
dreschpe | 1:6d6125e88de7 | 151 | // Init code based on MI0283QT datasheet |
dreschpe | 1:6d6125e88de7 | 152 | |
dreschpe | 0:da1bf437cbc1 | 153 | void SPI_TFT_ILI9341::tft_reset() |
dreschpe | 0:da1bf437cbc1 | 154 | { |
dreschpe | 0:da1bf437cbc1 | 155 | _cs = 1; // cs high |
dreschpe | 0:da1bf437cbc1 | 156 | _dc = 1; // dc high |
dreschpe | 0:da1bf437cbc1 | 157 | _reset = 0; // display reset |
dreschpe | 0:da1bf437cbc1 | 158 | |
dreschpe | 0:da1bf437cbc1 | 159 | wait_us(50); |
dreschpe | 1:6d6125e88de7 | 160 | _reset = 1; // end hardware reset |
dreschpe | 0:da1bf437cbc1 | 161 | wait_ms(5); |
dreschpe | 1:6d6125e88de7 | 162 | |
dreschpe | 1:6d6125e88de7 | 163 | wr_cmd(0x01); // SW reset |
dreschpe | 1:6d6125e88de7 | 164 | wait_ms(5); |
dreschpe | 1:6d6125e88de7 | 165 | wr_cmd(0x28); // display off |
dreschpe | 0:da1bf437cbc1 | 166 | |
dreschpe | 0:da1bf437cbc1 | 167 | /* Start Initial Sequence ----------------------------------------------------*/ |
dreschpe | 1:6d6125e88de7 | 168 | wr_cmd(0xCF); |
dreschpe | 11:59eca2723ec5 | 169 | SPI::write(0x00); |
dreschpe | 11:59eca2723ec5 | 170 | SPI::write(0x83); |
dreschpe | 11:59eca2723ec5 | 171 | SPI::write(0x30); |
dreschpe | 1:6d6125e88de7 | 172 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 173 | |
dreschpe | 1:6d6125e88de7 | 174 | wr_cmd(0xED); |
dreschpe | 11:59eca2723ec5 | 175 | SPI::write(0x64); |
dreschpe | 11:59eca2723ec5 | 176 | SPI::write(0x03); |
dreschpe | 11:59eca2723ec5 | 177 | SPI::write(0x12); |
dreschpe | 11:59eca2723ec5 | 178 | SPI::write(0x81); |
dreschpe | 1:6d6125e88de7 | 179 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 180 | |
dreschpe | 1:6d6125e88de7 | 181 | wr_cmd(0xE8); |
dreschpe | 11:59eca2723ec5 | 182 | SPI::write(0x85); |
dreschpe | 11:59eca2723ec5 | 183 | SPI::write(0x01); |
dreschpe | 11:59eca2723ec5 | 184 | SPI::write(0x79); |
dreschpe | 1:6d6125e88de7 | 185 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 186 | |
dreschpe | 1:6d6125e88de7 | 187 | wr_cmd(0xCB); |
dreschpe | 11:59eca2723ec5 | 188 | SPI::write(0x39); |
dreschpe | 11:59eca2723ec5 | 189 | SPI::write(0x2C); |
dreschpe | 11:59eca2723ec5 | 190 | SPI::write(0x00); |
dreschpe | 11:59eca2723ec5 | 191 | SPI::write(0x34); |
dreschpe | 11:59eca2723ec5 | 192 | SPI::write(0x02); |
dreschpe | 0:da1bf437cbc1 | 193 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 194 | |
dreschpe | 1:6d6125e88de7 | 195 | wr_cmd(0xF7); |
dreschpe | 11:59eca2723ec5 | 196 | SPI::write(0x20); |
dreschpe | 0:da1bf437cbc1 | 197 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 198 | |
dreschpe | 1:6d6125e88de7 | 199 | wr_cmd(0xEA); |
dreschpe | 11:59eca2723ec5 | 200 | SPI::write(0x00); |
dreschpe | 11:59eca2723ec5 | 201 | SPI::write(0x00); |
dreschpe | 0:da1bf437cbc1 | 202 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 203 | |
dreschpe | 0:da1bf437cbc1 | 204 | wr_cmd(0xC0); // POWER_CONTROL_1 |
dreschpe | 11:59eca2723ec5 | 205 | SPI::write(0x26); |
dreschpe | 0:da1bf437cbc1 | 206 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 207 | |
dreschpe | 0:da1bf437cbc1 | 208 | wr_cmd(0xC1); // POWER_CONTROL_2 |
dreschpe | 11:59eca2723ec5 | 209 | SPI::write(0x11); |
dreschpe | 0:da1bf437cbc1 | 210 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 211 | |
dreschpe | 0:da1bf437cbc1 | 212 | wr_cmd(0xC5); // VCOM_CONTROL_1 |
dreschpe | 11:59eca2723ec5 | 213 | SPI::write(0x35); |
dreschpe | 11:59eca2723ec5 | 214 | SPI::write(0x3E); |
dreschpe | 0:da1bf437cbc1 | 215 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 216 | |
dreschpe | 0:da1bf437cbc1 | 217 | wr_cmd(0xC7); // VCOM_CONTROL_2 |
dreschpe | 11:59eca2723ec5 | 218 | SPI::write(0xBE); |
dreschpe | 0:da1bf437cbc1 | 219 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 220 | |
dreschpe | 0:da1bf437cbc1 | 221 | wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
dreschpe | 11:59eca2723ec5 | 222 | SPI::write(0x48); |
dreschpe | 0:da1bf437cbc1 | 223 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 224 | |
dreschpe | 1:6d6125e88de7 | 225 | wr_cmd(0x3A); // COLMOD_PIXEL_FORMAT_SET |
dreschpe | 11:59eca2723ec5 | 226 | SPI::write(0x55); // 16 bit pixel |
dreschpe | 1:6d6125e88de7 | 227 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 228 | |
dreschpe | 1:6d6125e88de7 | 229 | wr_cmd(0xB1); // Frame Rate |
dreschpe | 11:59eca2723ec5 | 230 | SPI::write(0x00); |
dreschpe | 11:59eca2723ec5 | 231 | SPI::write(0x1B); |
dreschpe | 1:6d6125e88de7 | 232 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 233 | |
dreschpe | 1:6d6125e88de7 | 234 | wr_cmd(0xF2); // Gamma Function Disable |
dreschpe | 11:59eca2723ec5 | 235 | SPI::write(0x08); |
dreschpe | 1:6d6125e88de7 | 236 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 237 | |
dreschpe | 1:6d6125e88de7 | 238 | wr_cmd(0x26); |
dreschpe | 11:59eca2723ec5 | 239 | SPI::write(0x01); // gamma set for curve 01/2/04/08 |
dreschpe | 1:6d6125e88de7 | 240 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 241 | |
dreschpe | 1:6d6125e88de7 | 242 | wr_cmd(0xE0); // positive gamma correction |
dreschpe | 11:59eca2723ec5 | 243 | SPI::write(0x1F); |
dreschpe | 11:59eca2723ec5 | 244 | SPI::write(0x1A); |
dreschpe | 11:59eca2723ec5 | 245 | SPI::write(0x18); |
dreschpe | 11:59eca2723ec5 | 246 | SPI::write(0x0A); |
dreschpe | 11:59eca2723ec5 | 247 | SPI::write(0x0F); |
dreschpe | 11:59eca2723ec5 | 248 | SPI::write(0x06); |
dreschpe | 11:59eca2723ec5 | 249 | SPI::write(0x45); |
dreschpe | 11:59eca2723ec5 | 250 | SPI::write(0x87); |
dreschpe | 11:59eca2723ec5 | 251 | SPI::write(0x32); |
dreschpe | 11:59eca2723ec5 | 252 | SPI::write(0x0A); |
dreschpe | 11:59eca2723ec5 | 253 | SPI::write(0x07); |
dreschpe | 11:59eca2723ec5 | 254 | SPI::write(0x02); |
dreschpe | 11:59eca2723ec5 | 255 | SPI::write(0x07); |
dreschpe | 11:59eca2723ec5 | 256 | SPI::write(0x05); |
dreschpe | 11:59eca2723ec5 | 257 | SPI::write(0x00); |
dreschpe | 1:6d6125e88de7 | 258 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 259 | |
dreschpe | 1:6d6125e88de7 | 260 | wr_cmd(0xE1); // negativ gamma correction |
dreschpe | 11:59eca2723ec5 | 261 | SPI::write(0x00); |
dreschpe | 11:59eca2723ec5 | 262 | SPI::write(0x25); |
dreschpe | 11:59eca2723ec5 | 263 | SPI::write(0x27); |
dreschpe | 11:59eca2723ec5 | 264 | SPI::write(0x05); |
dreschpe | 11:59eca2723ec5 | 265 | SPI::write(0x10); |
dreschpe | 11:59eca2723ec5 | 266 | SPI::write(0x09); |
dreschpe | 11:59eca2723ec5 | 267 | SPI::write(0x3A); |
dreschpe | 11:59eca2723ec5 | 268 | SPI::write(0x78); |
dreschpe | 11:59eca2723ec5 | 269 | SPI::write(0x4D); |
dreschpe | 11:59eca2723ec5 | 270 | SPI::write(0x05); |
dreschpe | 11:59eca2723ec5 | 271 | SPI::write(0x18); |
dreschpe | 11:59eca2723ec5 | 272 | SPI::write(0x0D); |
dreschpe | 11:59eca2723ec5 | 273 | SPI::write(0x38); |
dreschpe | 11:59eca2723ec5 | 274 | SPI::write(0x3A); |
dreschpe | 11:59eca2723ec5 | 275 | SPI::write(0x1F); |
dreschpe | 1:6d6125e88de7 | 276 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 277 | |
dreschpe | 1:6d6125e88de7 | 278 | WindowMax (); |
dreschpe | 1:6d6125e88de7 | 279 | |
dreschpe | 1:6d6125e88de7 | 280 | //wr_cmd(0x34); // tearing effect off |
dreschpe | 1:6d6125e88de7 | 281 | //_cs = 1; |
dreschpe | 1:6d6125e88de7 | 282 | |
dreschpe | 1:6d6125e88de7 | 283 | //wr_cmd(0x35); // tearing effect on |
dreschpe | 1:6d6125e88de7 | 284 | //_cs = 1; |
dreschpe | 1:6d6125e88de7 | 285 | |
dreschpe | 1:6d6125e88de7 | 286 | wr_cmd(0xB7); // entry mode |
dreschpe | 11:59eca2723ec5 | 287 | SPI::write(0x07); |
dreschpe | 1:6d6125e88de7 | 288 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 289 | |
dreschpe | 1:6d6125e88de7 | 290 | wr_cmd(0xB6); // display function control |
dreschpe | 11:59eca2723ec5 | 291 | SPI::write(0x0A); |
dreschpe | 11:59eca2723ec5 | 292 | SPI::write(0x82); |
dreschpe | 11:59eca2723ec5 | 293 | SPI::write(0x27); |
dreschpe | 11:59eca2723ec5 | 294 | SPI::write(0x00); |
dreschpe | 1:6d6125e88de7 | 295 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 296 | |
dreschpe | 1:6d6125e88de7 | 297 | wr_cmd(0x11); // sleep out |
dreschpe | 1:6d6125e88de7 | 298 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 299 | |
dreschpe | 1:6d6125e88de7 | 300 | wait_ms(100); |
dreschpe | 1:6d6125e88de7 | 301 | |
dreschpe | 1:6d6125e88de7 | 302 | wr_cmd(0x29); // display on |
dreschpe | 1:6d6125e88de7 | 303 | _cs = 1; |
dreschpe | 1:6d6125e88de7 | 304 | |
dreschpe | 1:6d6125e88de7 | 305 | wait_ms(100); |
dreschpe | 1:6d6125e88de7 | 306 | |
dreschpe | 1:6d6125e88de7 | 307 | } |
dreschpe | 0:da1bf437cbc1 | 308 | |
dreschpe | 0:da1bf437cbc1 | 309 | |
dreschpe | 0:da1bf437cbc1 | 310 | void SPI_TFT_ILI9341::pixel(int x, int y, int color) |
dreschpe | 0:da1bf437cbc1 | 311 | { |
dreschpe | 0:da1bf437cbc1 | 312 | wr_cmd(0x2A); |
dreschpe | 11:59eca2723ec5 | 313 | SPI::write(x >> 8); |
dreschpe | 11:59eca2723ec5 | 314 | SPI::write(x); |
dreschpe | 0:da1bf437cbc1 | 315 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 316 | wr_cmd(0x2B); |
dreschpe | 11:59eca2723ec5 | 317 | SPI::write(y >> 8); |
dreschpe | 11:59eca2723ec5 | 318 | SPI::write(y); |
dreschpe | 0:da1bf437cbc1 | 319 | _cs = 1; |
dreschpe | 5:55aed13f2630 | 320 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 321 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 11:59eca2723ec5 | 322 | SPI::write(color >> 8); |
dreschpe | 11:59eca2723ec5 | 323 | SPI::write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 324 | #else |
dreschpe | 11:59eca2723ec5 | 325 | SPI::format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 11:59eca2723ec5 | 326 | SPI::write(color); // Write D0..D15 |
dreschpe | 11:59eca2723ec5 | 327 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 328 | #endif |
dreschpe | 0:da1bf437cbc1 | 329 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 330 | } |
dreschpe | 0:da1bf437cbc1 | 331 | |
dreschpe | 0:da1bf437cbc1 | 332 | |
dreschpe | 0:da1bf437cbc1 | 333 | void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) |
dreschpe | 0:da1bf437cbc1 | 334 | { |
dreschpe | 0:da1bf437cbc1 | 335 | wr_cmd(0x2A); |
dreschpe | 11:59eca2723ec5 | 336 | SPI::write(x >> 8); |
dreschpe | 11:59eca2723ec5 | 337 | SPI::write(x); |
dreschpe | 11:59eca2723ec5 | 338 | SPI::write((x+w-1) >> 8); |
dreschpe | 11:59eca2723ec5 | 339 | SPI::write(x+w-1); |
dreschpe | 0:da1bf437cbc1 | 340 | |
dreschpe | 0:da1bf437cbc1 | 341 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 342 | wr_cmd(0x2B); |
dreschpe | 11:59eca2723ec5 | 343 | SPI::write(y >> 8); |
dreschpe | 11:59eca2723ec5 | 344 | SPI::write(y); |
dreschpe | 11:59eca2723ec5 | 345 | SPI::write((y+h-1) >> 8); |
dreschpe | 11:59eca2723ec5 | 346 | SPI::write(y+h-1); |
dreschpe | 0:da1bf437cbc1 | 347 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 348 | } |
dreschpe | 0:da1bf437cbc1 | 349 | |
dreschpe | 0:da1bf437cbc1 | 350 | |
dreschpe | 0:da1bf437cbc1 | 351 | void SPI_TFT_ILI9341::WindowMax (void) |
dreschpe | 0:da1bf437cbc1 | 352 | { |
dreschpe | 0:da1bf437cbc1 | 353 | window (0, 0, width(), height()); |
dreschpe | 0:da1bf437cbc1 | 354 | } |
dreschpe | 0:da1bf437cbc1 | 355 | |
dreschpe | 0:da1bf437cbc1 | 356 | |
dreschpe | 0:da1bf437cbc1 | 357 | |
dreschpe | 0:da1bf437cbc1 | 358 | void SPI_TFT_ILI9341::cls (void) |
dreschpe | 0:da1bf437cbc1 | 359 | { |
dreschpe | 12:98cc5c193ecd | 360 | // we can use the fillrect function |
dreschpe | 12:98cc5c193ecd | 361 | fillrect(0,0,width()-1,height()-1,_background); |
dreschpe | 0:da1bf437cbc1 | 362 | } |
dreschpe | 0:da1bf437cbc1 | 363 | |
dreschpe | 0:da1bf437cbc1 | 364 | |
dreschpe | 0:da1bf437cbc1 | 365 | void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color) |
dreschpe | 0:da1bf437cbc1 | 366 | { |
dreschpe | 0:da1bf437cbc1 | 367 | |
mazgch | 3:3d7298360e45 | 368 | int x = -r, y = 0, err = 2-2*r, e2; |
mazgch | 3:3d7298360e45 | 369 | do { |
mazgch | 3:3d7298360e45 | 370 | pixel(x0-x, y0+y,color); |
mazgch | 3:3d7298360e45 | 371 | pixel(x0+x, y0+y,color); |
mazgch | 3:3d7298360e45 | 372 | pixel(x0+x, y0-y,color); |
mazgch | 3:3d7298360e45 | 373 | pixel(x0-x, y0-y,color); |
mazgch | 3:3d7298360e45 | 374 | e2 = err; |
mazgch | 3:3d7298360e45 | 375 | if (e2 <= y) { |
mazgch | 3:3d7298360e45 | 376 | err += ++y*2+1; |
mazgch | 3:3d7298360e45 | 377 | if (-x == y && e2 <= x) e2 = 0; |
mazgch | 3:3d7298360e45 | 378 | } |
mazgch | 3:3d7298360e45 | 379 | if (e2 > x) err += ++x*2+1; |
mazgch | 3:3d7298360e45 | 380 | } while (x <= 0); |
dreschpe | 4:f018e272220b | 381 | |
dreschpe | 0:da1bf437cbc1 | 382 | } |
dreschpe | 0:da1bf437cbc1 | 383 | |
JackB | 14:70665f0a182f | 384 | void SPI_TFT_ILI9341::circleGFX(int x0, int y0, int r, int color) |
JackB | 14:70665f0a182f | 385 | { |
JackB | 14:70665f0a182f | 386 | int16_t f = 1 - r; |
JackB | 14:70665f0a182f | 387 | int16_t ddF_x = 1; |
JackB | 14:70665f0a182f | 388 | int16_t ddF_y = -2 * r; |
JackB | 14:70665f0a182f | 389 | int16_t x = 0; |
JackB | 14:70665f0a182f | 390 | int16_t y = r; |
JackB | 14:70665f0a182f | 391 | |
JackB | 14:70665f0a182f | 392 | pixel(x0, y0+r, color); |
JackB | 14:70665f0a182f | 393 | pixel(x0, y0-r, color); |
JackB | 14:70665f0a182f | 394 | pixel(x0+r, y0, color); |
JackB | 14:70665f0a182f | 395 | pixel(x0-r, y0, color); |
JackB | 14:70665f0a182f | 396 | |
JackB | 14:70665f0a182f | 397 | while (x<y) |
JackB | 14:70665f0a182f | 398 | { |
JackB | 14:70665f0a182f | 399 | if (f >= 0) |
JackB | 14:70665f0a182f | 400 | { |
JackB | 14:70665f0a182f | 401 | y--; |
JackB | 14:70665f0a182f | 402 | ddF_y += 2; |
JackB | 14:70665f0a182f | 403 | f += ddF_y; |
JackB | 14:70665f0a182f | 404 | } |
JackB | 14:70665f0a182f | 405 | x++; |
JackB | 14:70665f0a182f | 406 | ddF_x += 2; |
JackB | 14:70665f0a182f | 407 | f += ddF_x; |
JackB | 14:70665f0a182f | 408 | |
JackB | 14:70665f0a182f | 409 | pixel(x0 + x, y0 + y, color); |
JackB | 14:70665f0a182f | 410 | pixel(x0 - x, y0 + y, color); |
JackB | 14:70665f0a182f | 411 | pixel(x0 + x, y0 - y, color); |
JackB | 14:70665f0a182f | 412 | pixel(x0 - x, y0 - y, color); |
JackB | 14:70665f0a182f | 413 | pixel(x0 + y, y0 + x, color); |
JackB | 14:70665f0a182f | 414 | pixel(x0 - y, y0 + x, color); |
JackB | 14:70665f0a182f | 415 | pixel(x0 + y, y0 - x, color); |
JackB | 14:70665f0a182f | 416 | pixel(x0 - y, y0 - x, color); |
JackB | 14:70665f0a182f | 417 | } |
JackB | 14:70665f0a182f | 418 | } |
JackB | 14:70665f0a182f | 419 | |
mazgch | 3:3d7298360e45 | 420 | void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color) |
dreschpe | 0:da1bf437cbc1 | 421 | { |
mazgch | 3:3d7298360e45 | 422 | int x = -r, y = 0, err = 2-2*r, e2; |
mazgch | 3:3d7298360e45 | 423 | do { |
mazgch | 3:3d7298360e45 | 424 | vline(x0-x, y0-y, y0+y, color); |
mazgch | 3:3d7298360e45 | 425 | vline(x0+x, y0-y, y0+y, color); |
mazgch | 3:3d7298360e45 | 426 | e2 = err; |
mazgch | 3:3d7298360e45 | 427 | if (e2 <= y) { |
mazgch | 3:3d7298360e45 | 428 | err += ++y*2+1; |
mazgch | 3:3d7298360e45 | 429 | if (-x == y && e2 <= x) e2 = 0; |
mazgch | 3:3d7298360e45 | 430 | } |
mazgch | 3:3d7298360e45 | 431 | if (e2 > x) err += ++x*2+1; |
mazgch | 3:3d7298360e45 | 432 | } while (x <= 0); |
dreschpe | 0:da1bf437cbc1 | 433 | } |
dreschpe | 0:da1bf437cbc1 | 434 | |
dreschpe | 0:da1bf437cbc1 | 435 | |
dreschpe | 0:da1bf437cbc1 | 436 | void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) |
dreschpe | 0:da1bf437cbc1 | 437 | { |
dreschpe | 0:da1bf437cbc1 | 438 | int w; |
dreschpe | 0:da1bf437cbc1 | 439 | w = x1 - x0 + 1; |
dreschpe | 0:da1bf437cbc1 | 440 | window(x0,y,w,1); |
dreschpe | 5:55aed13f2630 | 441 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 442 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 443 | int j; |
dreschpe | 5:55aed13f2630 | 444 | for (j=0; j<w; j++) { |
dreschpe | 11:59eca2723ec5 | 445 | SPI::write(color >> 8); |
dreschpe | 11:59eca2723ec5 | 446 | SPI::write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 447 | } |
dreschpe | 5:55aed13f2630 | 448 | #else |
dreschpe | 11:59eca2723ec5 | 449 | SPI::format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:da1bf437cbc1 | 450 | int j; |
dreschpe | 0:da1bf437cbc1 | 451 | for (j=0; j<w; j++) { |
dreschpe | 11:59eca2723ec5 | 452 | SPI::write(color); |
dreschpe | 0:da1bf437cbc1 | 453 | } |
dreschpe | 11:59eca2723ec5 | 454 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 455 | #endif |
dreschpe | 0:da1bf437cbc1 | 456 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 457 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 458 | return; |
dreschpe | 0:da1bf437cbc1 | 459 | } |
dreschpe | 0:da1bf437cbc1 | 460 | |
dreschpe | 0:da1bf437cbc1 | 461 | void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 462 | { |
dreschpe | 0:da1bf437cbc1 | 463 | int h; |
dreschpe | 0:da1bf437cbc1 | 464 | h = y1 - y0 + 1; |
dreschpe | 0:da1bf437cbc1 | 465 | window(x,y0,1,h); |
dreschpe | 5:55aed13f2630 | 466 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 467 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 468 | for (int y=0; y<h; y++) { |
dreschpe | 11:59eca2723ec5 | 469 | SPI::write(color >> 8); |
dreschpe | 11:59eca2723ec5 | 470 | SPI::write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 471 | } |
dreschpe | 5:55aed13f2630 | 472 | #else |
dreschpe | 11:59eca2723ec5 | 473 | SPI::format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:da1bf437cbc1 | 474 | for (int y=0; y<h; y++) { |
dreschpe | 11:59eca2723ec5 | 475 | SPI::write(color); |
dreschpe | 0:da1bf437cbc1 | 476 | } |
dreschpe | 11:59eca2723ec5 | 477 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 478 | #endif |
dreschpe | 0:da1bf437cbc1 | 479 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 480 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 481 | return; |
dreschpe | 0:da1bf437cbc1 | 482 | } |
dreschpe | 0:da1bf437cbc1 | 483 | |
dreschpe | 0:da1bf437cbc1 | 484 | |
dreschpe | 0:da1bf437cbc1 | 485 | |
dreschpe | 0:da1bf437cbc1 | 486 | void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 487 | { |
dreschpe | 0:da1bf437cbc1 | 488 | //WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 489 | int dx = 0, dy = 0; |
dreschpe | 0:da1bf437cbc1 | 490 | int dx_sym = 0, dy_sym = 0; |
dreschpe | 0:da1bf437cbc1 | 491 | int dx_x2 = 0, dy_x2 = 0; |
dreschpe | 0:da1bf437cbc1 | 492 | int di = 0; |
dreschpe | 0:da1bf437cbc1 | 493 | |
dreschpe | 0:da1bf437cbc1 | 494 | dx = x1-x0; |
dreschpe | 0:da1bf437cbc1 | 495 | dy = y1-y0; |
dreschpe | 0:da1bf437cbc1 | 496 | |
dreschpe | 0:da1bf437cbc1 | 497 | if (dx == 0) { /* vertical line */ |
dreschpe | 0:da1bf437cbc1 | 498 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 499 | else vline(x0,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 500 | return; |
dreschpe | 0:da1bf437cbc1 | 501 | } |
dreschpe | 0:da1bf437cbc1 | 502 | |
dreschpe | 0:da1bf437cbc1 | 503 | if (dx > 0) { |
dreschpe | 0:da1bf437cbc1 | 504 | dx_sym = 1; |
dreschpe | 0:da1bf437cbc1 | 505 | } else { |
dreschpe | 0:da1bf437cbc1 | 506 | dx_sym = -1; |
dreschpe | 0:da1bf437cbc1 | 507 | } |
dreschpe | 0:da1bf437cbc1 | 508 | if (dy == 0) { /* horizontal line */ |
dreschpe | 0:da1bf437cbc1 | 509 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 510 | else hline(x1,x0,y0,color); |
dreschpe | 0:da1bf437cbc1 | 511 | return; |
dreschpe | 0:da1bf437cbc1 | 512 | } |
dreschpe | 0:da1bf437cbc1 | 513 | |
dreschpe | 0:da1bf437cbc1 | 514 | if (dy > 0) { |
dreschpe | 0:da1bf437cbc1 | 515 | dy_sym = 1; |
dreschpe | 0:da1bf437cbc1 | 516 | } else { |
dreschpe | 0:da1bf437cbc1 | 517 | dy_sym = -1; |
dreschpe | 0:da1bf437cbc1 | 518 | } |
dreschpe | 0:da1bf437cbc1 | 519 | |
dreschpe | 0:da1bf437cbc1 | 520 | dx = dx_sym*dx; |
dreschpe | 0:da1bf437cbc1 | 521 | dy = dy_sym*dy; |
dreschpe | 0:da1bf437cbc1 | 522 | |
dreschpe | 0:da1bf437cbc1 | 523 | dx_x2 = dx*2; |
dreschpe | 0:da1bf437cbc1 | 524 | dy_x2 = dy*2; |
dreschpe | 0:da1bf437cbc1 | 525 | |
dreschpe | 0:da1bf437cbc1 | 526 | if (dx >= dy) { |
dreschpe | 0:da1bf437cbc1 | 527 | di = dy_x2 - dx; |
dreschpe | 0:da1bf437cbc1 | 528 | while (x0 != x1) { |
dreschpe | 0:da1bf437cbc1 | 529 | |
dreschpe | 0:da1bf437cbc1 | 530 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 531 | x0 += dx_sym; |
dreschpe | 0:da1bf437cbc1 | 532 | if (di<0) { |
dreschpe | 0:da1bf437cbc1 | 533 | di += dy_x2; |
dreschpe | 0:da1bf437cbc1 | 534 | } else { |
dreschpe | 0:da1bf437cbc1 | 535 | di += dy_x2 - dx_x2; |
dreschpe | 0:da1bf437cbc1 | 536 | y0 += dy_sym; |
dreschpe | 0:da1bf437cbc1 | 537 | } |
dreschpe | 0:da1bf437cbc1 | 538 | } |
dreschpe | 0:da1bf437cbc1 | 539 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 540 | } else { |
dreschpe | 0:da1bf437cbc1 | 541 | di = dx_x2 - dy; |
dreschpe | 0:da1bf437cbc1 | 542 | while (y0 != y1) { |
dreschpe | 0:da1bf437cbc1 | 543 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 544 | y0 += dy_sym; |
dreschpe | 0:da1bf437cbc1 | 545 | if (di < 0) { |
dreschpe | 0:da1bf437cbc1 | 546 | di += dx_x2; |
dreschpe | 0:da1bf437cbc1 | 547 | } else { |
dreschpe | 0:da1bf437cbc1 | 548 | di += dx_x2 - dy_x2; |
dreschpe | 0:da1bf437cbc1 | 549 | x0 += dx_sym; |
dreschpe | 0:da1bf437cbc1 | 550 | } |
dreschpe | 0:da1bf437cbc1 | 551 | } |
dreschpe | 0:da1bf437cbc1 | 552 | pixel(x0, y0, color); |
dreschpe | 0:da1bf437cbc1 | 553 | } |
dreschpe | 0:da1bf437cbc1 | 554 | return; |
dreschpe | 0:da1bf437cbc1 | 555 | } |
dreschpe | 0:da1bf437cbc1 | 556 | |
dreschpe | 0:da1bf437cbc1 | 557 | |
dreschpe | 0:da1bf437cbc1 | 558 | void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 559 | { |
dreschpe | 0:da1bf437cbc1 | 560 | |
dreschpe | 0:da1bf437cbc1 | 561 | if (x1 > x0) hline(x0,x1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 562 | else hline(x1,x0,y0,color); |
dreschpe | 0:da1bf437cbc1 | 563 | |
dreschpe | 0:da1bf437cbc1 | 564 | if (y1 > y0) vline(x0,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 565 | else vline(x0,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 566 | |
dreschpe | 0:da1bf437cbc1 | 567 | if (x1 > x0) hline(x0,x1,y1,color); |
dreschpe | 0:da1bf437cbc1 | 568 | else hline(x1,x0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 569 | |
dreschpe | 0:da1bf437cbc1 | 570 | if (y1 > y0) vline(x1,y0,y1,color); |
dreschpe | 0:da1bf437cbc1 | 571 | else vline(x1,y1,y0,color); |
dreschpe | 0:da1bf437cbc1 | 572 | |
dreschpe | 0:da1bf437cbc1 | 573 | return; |
dreschpe | 0:da1bf437cbc1 | 574 | } |
dreschpe | 0:da1bf437cbc1 | 575 | |
dreschpe | 0:da1bf437cbc1 | 576 | |
dreschpe | 0:da1bf437cbc1 | 577 | |
dreschpe | 0:da1bf437cbc1 | 578 | void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color) |
dreschpe | 0:da1bf437cbc1 | 579 | { |
dreschpe | 0:da1bf437cbc1 | 580 | |
dreschpe | 0:da1bf437cbc1 | 581 | int h = y1 - y0 + 1; |
dreschpe | 0:da1bf437cbc1 | 582 | int w = x1 - x0 + 1; |
dreschpe | 0:da1bf437cbc1 | 583 | int pixel = h * w; |
dreschpe | 0:da1bf437cbc1 | 584 | window(x0,y0,w,h); |
dreschpe | 0:da1bf437cbc1 | 585 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 586 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 587 | for (int p=0; p<pixel; p++) { |
dreschpe | 11:59eca2723ec5 | 588 | SPI::write(color >> 8); |
dreschpe | 11:59eca2723ec5 | 589 | SPI::write(color & 0xff); |
dreschpe | 5:55aed13f2630 | 590 | } |
dreschpe | 5:55aed13f2630 | 591 | #else |
dreschpe | 11:59eca2723ec5 | 592 | SPI::format(16,3); // switch to 16 bit Mode 3 |
dreschpe | 0:da1bf437cbc1 | 593 | for (int p=0; p<pixel; p++) { |
dreschpe | 11:59eca2723ec5 | 594 | SPI::write(color); |
dreschpe | 0:da1bf437cbc1 | 595 | } |
dreschpe | 11:59eca2723ec5 | 596 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 597 | #endif |
dreschpe | 0:da1bf437cbc1 | 598 | _cs = 1; |
dreschpe | 0:da1bf437cbc1 | 599 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 600 | return; |
dreschpe | 0:da1bf437cbc1 | 601 | } |
dreschpe | 0:da1bf437cbc1 | 602 | |
dreschpe | 0:da1bf437cbc1 | 603 | |
dreschpe | 0:da1bf437cbc1 | 604 | void SPI_TFT_ILI9341::locate(int x, int y) |
dreschpe | 0:da1bf437cbc1 | 605 | { |
dreschpe | 0:da1bf437cbc1 | 606 | char_x = x; |
dreschpe | 0:da1bf437cbc1 | 607 | char_y = y; |
dreschpe | 0:da1bf437cbc1 | 608 | } |
dreschpe | 0:da1bf437cbc1 | 609 | |
dreschpe | 0:da1bf437cbc1 | 610 | |
dreschpe | 0:da1bf437cbc1 | 611 | |
dreschpe | 0:da1bf437cbc1 | 612 | int SPI_TFT_ILI9341::columns() |
dreschpe | 0:da1bf437cbc1 | 613 | { |
dreschpe | 0:da1bf437cbc1 | 614 | return width() / font[1]; |
dreschpe | 0:da1bf437cbc1 | 615 | } |
dreschpe | 0:da1bf437cbc1 | 616 | |
dreschpe | 0:da1bf437cbc1 | 617 | |
dreschpe | 0:da1bf437cbc1 | 618 | |
dreschpe | 0:da1bf437cbc1 | 619 | int SPI_TFT_ILI9341::rows() |
dreschpe | 0:da1bf437cbc1 | 620 | { |
dreschpe | 0:da1bf437cbc1 | 621 | return height() / font[2]; |
dreschpe | 0:da1bf437cbc1 | 622 | } |
dreschpe | 0:da1bf437cbc1 | 623 | |
dreschpe | 0:da1bf437cbc1 | 624 | |
dreschpe | 0:da1bf437cbc1 | 625 | |
dreschpe | 0:da1bf437cbc1 | 626 | int SPI_TFT_ILI9341::_putc(int value) |
dreschpe | 0:da1bf437cbc1 | 627 | { |
dreschpe | 0:da1bf437cbc1 | 628 | if (value == '\n') { // new line |
dreschpe | 0:da1bf437cbc1 | 629 | char_x = 0; |
dreschpe | 0:da1bf437cbc1 | 630 | char_y = char_y + font[2]; |
dreschpe | 0:da1bf437cbc1 | 631 | if (char_y >= height() - font[2]) { |
dreschpe | 0:da1bf437cbc1 | 632 | char_y = 0; |
dreschpe | 0:da1bf437cbc1 | 633 | } |
dreschpe | 0:da1bf437cbc1 | 634 | } else { |
dreschpe | 0:da1bf437cbc1 | 635 | character(char_x, char_y, value); |
dreschpe | 0:da1bf437cbc1 | 636 | } |
dreschpe | 0:da1bf437cbc1 | 637 | return value; |
dreschpe | 0:da1bf437cbc1 | 638 | } |
dreschpe | 0:da1bf437cbc1 | 639 | |
dreschpe | 0:da1bf437cbc1 | 640 | |
dreschpe | 0:da1bf437cbc1 | 641 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
dreschpe | 0:da1bf437cbc1 | 642 | { |
dreschpe | 0:da1bf437cbc1 | 643 | unsigned int hor,vert,offset,bpl,j,i,b; |
dreschpe | 0:da1bf437cbc1 | 644 | unsigned char* zeichen; |
dreschpe | 0:da1bf437cbc1 | 645 | unsigned char z,w; |
dreschpe | 0:da1bf437cbc1 | 646 | |
dreschpe | 0:da1bf437cbc1 | 647 | if ((c < 31) || (c > 127)) return; // test char range |
dreschpe | 0:da1bf437cbc1 | 648 | |
dreschpe | 0:da1bf437cbc1 | 649 | // read font parameter from start of array |
dreschpe | 0:da1bf437cbc1 | 650 | offset = font[0]; // bytes / char |
dreschpe | 0:da1bf437cbc1 | 651 | hor = font[1]; // get hor size of font |
dreschpe | 0:da1bf437cbc1 | 652 | vert = font[2]; // get vert size of font |
dreschpe | 0:da1bf437cbc1 | 653 | bpl = font[3]; // bytes per line |
dreschpe | 0:da1bf437cbc1 | 654 | |
dreschpe | 0:da1bf437cbc1 | 655 | if (char_x + hor > width()) { |
dreschpe | 0:da1bf437cbc1 | 656 | char_x = 0; |
dreschpe | 0:da1bf437cbc1 | 657 | char_y = char_y + vert; |
dreschpe | 0:da1bf437cbc1 | 658 | if (char_y >= height() - font[2]) { |
dreschpe | 0:da1bf437cbc1 | 659 | char_y = 0; |
dreschpe | 0:da1bf437cbc1 | 660 | } |
dreschpe | 0:da1bf437cbc1 | 661 | } |
dreschpe | 0:da1bf437cbc1 | 662 | window(char_x, char_y,hor,vert); // char box |
dreschpe | 5:55aed13f2630 | 663 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 664 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 665 | SPI::format(16,3); |
dreschpe | 5:55aed13f2630 | 666 | #endif // switch to 16 bit Mode 3 |
dreschpe | 0:da1bf437cbc1 | 667 | zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap |
dreschpe | 0:da1bf437cbc1 | 668 | w = zeichen[0]; // width of actual char |
dreschpe | 0:da1bf437cbc1 | 669 | for (j=0; j<vert; j++) { // vert line |
dreschpe | 0:da1bf437cbc1 | 670 | for (i=0; i<hor; i++) { // horz line |
dreschpe | 0:da1bf437cbc1 | 671 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
dreschpe | 0:da1bf437cbc1 | 672 | b = 1 << (j & 0x07); |
dreschpe | 0:da1bf437cbc1 | 673 | if (( z & b ) == 0x00) { |
dreschpe | 5:55aed13f2630 | 674 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 675 | SPI::write(_background); |
dreschpe | 5:55aed13f2630 | 676 | #else |
dreschpe | 11:59eca2723ec5 | 677 | SPI::write(_background >> 8); |
dreschpe | 11:59eca2723ec5 | 678 | SPI::write(_background & 0xff); |
dreschpe | 5:55aed13f2630 | 679 | #endif |
dreschpe | 0:da1bf437cbc1 | 680 | } else { |
dreschpe | 5:55aed13f2630 | 681 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 682 | SPI::write(_foreground); |
dreschpe | 5:55aed13f2630 | 683 | #else |
dreschpe | 11:59eca2723ec5 | 684 | SPI::write(_foreground >> 8); |
dreschpe | 11:59eca2723ec5 | 685 | SPI::write(_foreground & 0xff); |
dreschpe | 5:55aed13f2630 | 686 | #endif |
dreschpe | 0:da1bf437cbc1 | 687 | } |
dreschpe | 0:da1bf437cbc1 | 688 | } |
dreschpe | 0:da1bf437cbc1 | 689 | } |
dreschpe | 0:da1bf437cbc1 | 690 | _cs = 1; |
dreschpe | 5:55aed13f2630 | 691 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 692 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 693 | #endif |
dreschpe | 0:da1bf437cbc1 | 694 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 695 | if ((w + 2) < hor) { // x offset to next char |
dreschpe | 0:da1bf437cbc1 | 696 | char_x += w + 2; |
dreschpe | 0:da1bf437cbc1 | 697 | } else char_x += hor; |
dreschpe | 0:da1bf437cbc1 | 698 | } |
dreschpe | 0:da1bf437cbc1 | 699 | |
dreschpe | 0:da1bf437cbc1 | 700 | |
dreschpe | 0:da1bf437cbc1 | 701 | void SPI_TFT_ILI9341::set_font(unsigned char* f) |
dreschpe | 0:da1bf437cbc1 | 702 | { |
dreschpe | 0:da1bf437cbc1 | 703 | font = f; |
dreschpe | 0:da1bf437cbc1 | 704 | } |
dreschpe | 0:da1bf437cbc1 | 705 | |
dreschpe | 0:da1bf437cbc1 | 706 | |
dreschpe | 0:da1bf437cbc1 | 707 | |
dreschpe | 0:da1bf437cbc1 | 708 | void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) |
dreschpe | 0:da1bf437cbc1 | 709 | { |
dreschpe | 0:da1bf437cbc1 | 710 | unsigned int j; |
dreschpe | 0:da1bf437cbc1 | 711 | int padd; |
dreschpe | 0:da1bf437cbc1 | 712 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
dreschpe | 5:55aed13f2630 | 713 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 714 | unsigned short pix_temp; |
dreschpe | 5:55aed13f2630 | 715 | #endif |
dreschpe | 5:55aed13f2630 | 716 | |
dreschpe | 2:0a16083193a4 | 717 | unsigned int i; |
dreschpe | 2:0a16083193a4 | 718 | |
dreschpe | 0:da1bf437cbc1 | 719 | // the lines are padded to multiple of 4 bytes in a bitmap |
dreschpe | 0:da1bf437cbc1 | 720 | padd = -1; |
dreschpe | 0:da1bf437cbc1 | 721 | do { |
dreschpe | 0:da1bf437cbc1 | 722 | padd ++; |
dreschpe | 0:da1bf437cbc1 | 723 | } while (2*(w + padd)%4 != 0); |
dreschpe | 0:da1bf437cbc1 | 724 | window(x, y, w, h); |
dreschpe | 2:0a16083193a4 | 725 | bitmap_ptr += ((h - 1)* (w + padd)); |
dreschpe | 5:55aed13f2630 | 726 | wr_cmd(0x2C); // send pixel |
dreschpe | 5:55aed13f2630 | 727 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 728 | SPI::format(16,3); |
dreschpe | 5:55aed13f2630 | 729 | #endif // switch to 16 bit Mode 3 |
dreschpe | 2:0a16083193a4 | 730 | for (j = 0; j < h; j++) { //Lines |
dreschpe | 2:0a16083193a4 | 731 | for (i = 0; i < w; i++) { // one line |
dreschpe | 5:55aed13f2630 | 732 | #if defined TARGET_KL25Z // 8 Bit SPI |
dreschpe | 5:55aed13f2630 | 733 | pix_temp = *bitmap_ptr; |
dreschpe | 11:59eca2723ec5 | 734 | SPI::write(pix_temp >> 8); |
dreschpe | 11:59eca2723ec5 | 735 | SPI::write(pix_temp); |
dreschpe | 5:55aed13f2630 | 736 | bitmap_ptr++; |
dreschpe | 5:55aed13f2630 | 737 | #else |
dreschpe | 11:59eca2723ec5 | 738 | SPI::write(*bitmap_ptr); // one line |
dreschpe | 5:55aed13f2630 | 739 | bitmap_ptr++; |
dreschpe | 5:55aed13f2630 | 740 | #endif |
dreschpe | 0:da1bf437cbc1 | 741 | } |
dreschpe | 0:da1bf437cbc1 | 742 | bitmap_ptr -= 2*w; |
dreschpe | 0:da1bf437cbc1 | 743 | bitmap_ptr -= padd; |
dreschpe | 0:da1bf437cbc1 | 744 | } |
dreschpe | 0:da1bf437cbc1 | 745 | _cs = 1; |
dreschpe | 5:55aed13f2630 | 746 | #ifndef TARGET_KL25Z // 16 Bit SPI |
dreschpe | 11:59eca2723ec5 | 747 | SPI::format(8,3); |
dreschpe | 5:55aed13f2630 | 748 | #endif |
dreschpe | 0:da1bf437cbc1 | 749 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 750 | } |
dreschpe | 0:da1bf437cbc1 | 751 | |
dreschpe | 0:da1bf437cbc1 | 752 | |
dreschpe | 6:fe07ae8329f7 | 753 | // local filesystem is not implemented in kinetis board , but you can add a SD card |
dreschpe | 5:55aed13f2630 | 754 | |
dreschpe | 0:da1bf437cbc1 | 755 | int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) |
dreschpe | 0:da1bf437cbc1 | 756 | { |
dreschpe | 0:da1bf437cbc1 | 757 | |
dreschpe | 0:da1bf437cbc1 | 758 | #define OffsetPixelWidth 18 |
dreschpe | 0:da1bf437cbc1 | 759 | #define OffsetPixelHeigh 22 |
dreschpe | 0:da1bf437cbc1 | 760 | #define OffsetFileSize 34 |
dreschpe | 0:da1bf437cbc1 | 761 | #define OffsetPixData 10 |
dreschpe | 0:da1bf437cbc1 | 762 | #define OffsetBPP 28 |
dreschpe | 0:da1bf437cbc1 | 763 | |
dreschpe | 0:da1bf437cbc1 | 764 | char filename[50]; |
dreschpe | 0:da1bf437cbc1 | 765 | unsigned char BMP_Header[54]; |
dreschpe | 0:da1bf437cbc1 | 766 | unsigned short BPP_t; |
dreschpe | 0:da1bf437cbc1 | 767 | unsigned int PixelWidth,PixelHeigh,start_data; |
dreschpe | 0:da1bf437cbc1 | 768 | unsigned int i,off; |
dreschpe | 0:da1bf437cbc1 | 769 | int padd,j; |
dreschpe | 0:da1bf437cbc1 | 770 | unsigned short *line; |
dreschpe | 0:da1bf437cbc1 | 771 | |
dreschpe | 0:da1bf437cbc1 | 772 | // get the filename |
dreschpe | 6:fe07ae8329f7 | 773 | i=0; |
dreschpe | 0:da1bf437cbc1 | 774 | while (*Name_BMP!='\0') { |
dreschpe | 0:da1bf437cbc1 | 775 | filename[i++]=*Name_BMP++; |
dreschpe | 0:da1bf437cbc1 | 776 | } |
dreschpe | 6:fe07ae8329f7 | 777 | filename[i] = 0; |
dreschpe | 6:fe07ae8329f7 | 778 | |
dreschpe | 0:da1bf437cbc1 | 779 | FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file |
dreschpe | 0:da1bf437cbc1 | 780 | if (!Image) { |
dreschpe | 0:da1bf437cbc1 | 781 | return(0); // error file not found ! |
dreschpe | 0:da1bf437cbc1 | 782 | } |
dreschpe | 0:da1bf437cbc1 | 783 | |
dreschpe | 0:da1bf437cbc1 | 784 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
dreschpe | 0:da1bf437cbc1 | 785 | |
dreschpe | 0:da1bf437cbc1 | 786 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
dreschpe | 0:da1bf437cbc1 | 787 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 788 | return(-1); // error no BMP file |
dreschpe | 0:da1bf437cbc1 | 789 | } |
dreschpe | 0:da1bf437cbc1 | 790 | |
dreschpe | 0:da1bf437cbc1 | 791 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
dreschpe | 0:da1bf437cbc1 | 792 | if (BPP_t != 0x0010) { |
dreschpe | 0:da1bf437cbc1 | 793 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 794 | return(-2); // error no 16 bit BMP |
dreschpe | 0:da1bf437cbc1 | 795 | } |
dreschpe | 0:da1bf437cbc1 | 796 | |
dreschpe | 0:da1bf437cbc1 | 797 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 798 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 799 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
dreschpe | 0:da1bf437cbc1 | 800 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 801 | return(-3); // to big |
dreschpe | 0:da1bf437cbc1 | 802 | } |
dreschpe | 0:da1bf437cbc1 | 803 | |
dreschpe | 0:da1bf437cbc1 | 804 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
dreschpe | 0:da1bf437cbc1 | 805 | |
dreschpe | 0:da1bf437cbc1 | 806 | line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line |
dreschpe | 0:da1bf437cbc1 | 807 | if (line == NULL) { |
dreschpe | 0:da1bf437cbc1 | 808 | return(-4); // error no memory |
dreschpe | 0:da1bf437cbc1 | 809 | } |
dreschpe | 0:da1bf437cbc1 | 810 | |
dreschpe | 0:da1bf437cbc1 | 811 | // the bmp lines are padded to multiple of 4 bytes |
dreschpe | 0:da1bf437cbc1 | 812 | padd = -1; |
dreschpe | 0:da1bf437cbc1 | 813 | do { |
dreschpe | 0:da1bf437cbc1 | 814 | padd ++; |
dreschpe | 0:da1bf437cbc1 | 815 | } while ((PixelWidth * 2 + padd)%4 != 0); |
dreschpe | 0:da1bf437cbc1 | 816 | |
dreschpe | 0:da1bf437cbc1 | 817 | window(x, y,PixelWidth ,PixelHeigh); |
dreschpe | 6:fe07ae8329f7 | 818 | wr_cmd(0x2C); // send pixel |
dreschpe | 6:fe07ae8329f7 | 819 | #ifndef TARGET_KL25Z // only 8 Bit SPI |
dreschpe | 11:59eca2723ec5 | 820 | SPI::format(16,3); |
dreschpe | 6:fe07ae8329f7 | 821 | #endif // switch to 16 bit Mode 3 |
dreschpe | 0:da1bf437cbc1 | 822 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
dreschpe | 0:da1bf437cbc1 | 823 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
dreschpe | 0:da1bf437cbc1 | 824 | fseek(Image, off ,SEEK_SET); |
dreschpe | 6:fe07ae8329f7 | 825 | fread(line,1,PixelWidth * 2,Image); // read a line - slow |
dreschpe | 0:da1bf437cbc1 | 826 | for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
dreschpe | 6:fe07ae8329f7 | 827 | #ifndef TARGET_KL25Z // only 8 Bit SPI |
dreschpe | 11:59eca2723ec5 | 828 | SPI::write(line[i]); // one 16 bit pixel |
dreschpe | 6:fe07ae8329f7 | 829 | #else |
dreschpe | 11:59eca2723ec5 | 830 | SPI::write(line[i] >> 8); |
dreschpe | 11:59eca2723ec5 | 831 | SPI::write(line[i]); |
dreschpe | 6:fe07ae8329f7 | 832 | #endif |
dreschpe | 0:da1bf437cbc1 | 833 | } |
dreschpe | 0:da1bf437cbc1 | 834 | } |
dreschpe | 0:da1bf437cbc1 | 835 | _cs = 1; |
dreschpe | 11:59eca2723ec5 | 836 | SPI::format(8,3); |
dreschpe | 0:da1bf437cbc1 | 837 | free (line); |
dreschpe | 0:da1bf437cbc1 | 838 | fclose(Image); |
dreschpe | 0:da1bf437cbc1 | 839 | WindowMax(); |
dreschpe | 0:da1bf437cbc1 | 840 | return(1); |
dreschpe | 5:55aed13f2630 | 841 | } |
dreschpe | 11:59eca2723ec5 | 842 | |
dreschpe | 11:59eca2723ec5 | 843 | #endif |