.

Dependents:   projekat

Committer:
2018US_EminaTahirovi
Date:
Sun Jun 16 20:26:22 2019 +0000
Revision:
0:1b3481785d59
.

Who changed what in which revision?

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