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