Lib for the new LCD Display with ILI9341 controller with rounded (outlined and filled) rectangles added

Fork of SPI_TFT_ILI9341 by Peter Drescher

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?

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