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