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