Displays a large title and some small text with a blue rectangle Constantly refreshes 3 variable values on the screen. NO TOUCH SUPPORT AS OF YET

Dependencies:   mbed

Committer:
EmbeddedSam
Date:
Sat Dec 05 23:41:01 2015 +0000
Revision:
0:38cf064697d7
First commit, working with displaying messages; no touch support as of yet

Who changed what in which revision?

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