![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Hardware testing for M24SR-DISCOVERY demo PCB. as help to others
Set up to use MB1138 M24SR-DISCOVERY PCB http://www.st.com/content/st_com/en/products/evaluation-tools/product-evaluation-tools/st25-nfc-rfid-eval-boards/st25-nfc-rfid-eval-boards/m24sr-discovery.html with MBED system. based on https://developer.mbed.org/users/hudakz/code/STM32F103C8T6_Hello/ code and https://developer.mbed.org/users/wim/notebook/m24sr64-nfcrfid-tag-with-i2c-interface/ Which lead me to look at Peter Drescher's work on ILI9341 LCD controller
https://developer.mbed.org/users/dreschpe/code/SPI_TFT_ILI9341/
SPI_TFT_ILI9341/SPI_TFT_ILI9341.cpp@2:2033db202017, 2016-09-29 (annotated)
- Committer:
- lloydg
- Date:
- Thu Sep 29 11:07:41 2016 +0000
- Revision:
- 2:2033db202017
re jig folders
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lloydg | 2:2033db202017 | 1 | /* mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller |
lloydg | 2:2033db202017 | 2 | * Copyright (c) 2013 Peter Drescher - DC2PD |
lloydg | 2:2033db202017 | 3 | * |
lloydg | 2:2033db202017 | 4 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
lloydg | 2:2033db202017 | 5 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
lloydg | 2:2033db202017 | 6 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
lloydg | 2:2033db202017 | 7 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
lloydg | 2:2033db202017 | 8 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
lloydg | 2:2033db202017 | 9 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
lloydg | 2:2033db202017 | 10 | * THE SOFTWARE. |
lloydg | 2:2033db202017 | 11 | */ |
lloydg | 2:2033db202017 | 12 | |
lloydg | 2:2033db202017 | 13 | // 12.06.13 fork from SPI_TFT code because controller is different ... |
lloydg | 2:2033db202017 | 14 | // 14.07.13 Test with real display and bugfix |
lloydg | 2:2033db202017 | 15 | // 18.10.13 Better Circle function from Michael Ammann |
lloydg | 2:2033db202017 | 16 | // 22.10.13 Fixes for Kinetis Board - 8 bit spi |
lloydg | 2:2033db202017 | 17 | // 26.01.14 Change interface for BMP_16 to also use SD-cards |
lloydg | 2:2033db202017 | 18 | // 30.03.14 WH Added some methods & defines, Fixed typos & warnings, General define for SPI_16 selection |
lloydg | 2:2033db202017 | 19 | |
lloydg | 2:2033db202017 | 20 | #include "mbed.h" |
lloydg | 2:2033db202017 | 21 | #include "SPI_TFT_ILI9341.h" |
lloydg | 2:2033db202017 | 22 | |
lloydg | 2:2033db202017 | 23 | //extern Serial pc; |
lloydg | 2:2033db202017 | 24 | //extern DigitalOut xx; // debug !! |
lloydg | 2:2033db202017 | 25 | |
lloydg | 2:2033db202017 | 26 | SPI_TFT_ILI9341::SPI_TFT_ILI9341(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char *name) |
lloydg | 2:2033db202017 | 27 | : GraphicsDisplay(name), _spi(mosi, miso, sclk), _cs(cs), _dc(dc) |
lloydg | 2:2033db202017 | 28 | { |
lloydg | 2:2033db202017 | 29 | |
lloydg | 2:2033db202017 | 30 | // The hardware Reset pin is optional. Test and make sure whether it exists or not to prevent illegal access. |
lloydg | 2:2033db202017 | 31 | if (reset != NC) { |
lloydg | 2:2033db202017 | 32 | _reset = new DigitalOut(reset, 1); //Construct new pin, Deactivated |
lloydg | 2:2033db202017 | 33 | // _reset->write(1); //Deactivate |
lloydg | 2:2033db202017 | 34 | } |
lloydg | 2:2033db202017 | 35 | else { |
lloydg | 2:2033db202017 | 36 | // No Hardware Reset pin |
lloydg | 2:2033db202017 | 37 | _reset = NULL; //Construct dummy pin |
lloydg | 2:2033db202017 | 38 | } |
lloydg | 2:2033db202017 | 39 | |
lloydg | 2:2033db202017 | 40 | //WH clk = sclk; |
lloydg | 2:2033db202017 | 41 | //WH orientation = 0; |
lloydg | 2:2033db202017 | 42 | _origin = Origin_LeftTop; |
lloydg | 2:2033db202017 | 43 | _char_x = 0; |
lloydg | 2:2033db202017 | 44 | _char_y = 0; |
lloydg | 2:2033db202017 | 45 | _transparancy = false; |
lloydg | 2:2033db202017 | 46 | // set_font(Arial12x12); //Default font |
lloydg | 2:2033db202017 | 47 | // set_font(FONT8x8); //Default font, shame it doesnt fit format.. waste of flash space at moment |
lloydg | 2:2033db202017 | 48 | |
lloydg | 2:2033db202017 | 49 | tft_reset(); |
lloydg | 2:2033db202017 | 50 | } |
lloydg | 2:2033db202017 | 51 | |
lloydg | 2:2033db202017 | 52 | |
lloydg | 2:2033db202017 | 53 | /** Destruct a SPI_TFT LCD object |
lloydg | 2:2033db202017 | 54 | * |
lloydg | 2:2033db202017 | 55 | * @param none |
lloydg | 2:2033db202017 | 56 | * @return none |
lloydg | 2:2033db202017 | 57 | */ |
lloydg | 2:2033db202017 | 58 | SPI_TFT_ILI9341::~SPI_TFT_ILI9341() { |
lloydg | 2:2033db202017 | 59 | if (_reset != NULL) {delete _reset;} // HW Reset pin |
lloydg | 2:2033db202017 | 60 | } |
lloydg | 2:2033db202017 | 61 | |
lloydg | 2:2033db202017 | 62 | |
lloydg | 2:2033db202017 | 63 | |
lloydg | 2:2033db202017 | 64 | int SPI_TFT_ILI9341::width() |
lloydg | 2:2033db202017 | 65 | { |
lloydg | 2:2033db202017 | 66 | // if (orientation == 0 || orientation == 2) return 240; |
lloydg | 2:2033db202017 | 67 | // else return 320; |
lloydg | 2:2033db202017 | 68 | return 320; |
lloydg | 2:2033db202017 | 69 | //if (_origin == Origin_LeftTop || _origin == Origin_RightBot) return TFT_WIDTH; |
lloydg | 2:2033db202017 | 70 | // else return TFT_HEIGHT; |
lloydg | 2:2033db202017 | 71 | } |
lloydg | 2:2033db202017 | 72 | |
lloydg | 2:2033db202017 | 73 | |
lloydg | 2:2033db202017 | 74 | int SPI_TFT_ILI9341::height() |
lloydg | 2:2033db202017 | 75 | { |
lloydg | 2:2033db202017 | 76 | // if (orientation == 0 || orientation == 2) return 320; |
lloydg | 2:2033db202017 | 77 | // else return 240; |
lloydg | 2:2033db202017 | 78 | return 240; |
lloydg | 2:2033db202017 | 79 | //if (_origin == Origin_LeftTop || _origin == Origin_RightBot) return TFT_HEIGHT; |
lloydg | 2:2033db202017 | 80 | //else return TFT_WIDTH; |
lloydg | 2:2033db202017 | 81 | } |
lloydg | 2:2033db202017 | 82 | |
lloydg | 2:2033db202017 | 83 | //WH |
lloydg | 2:2033db202017 | 84 | //#if(0) |
lloydg | 2:2033db202017 | 85 | void SPI_TFT_ILI9341::set_orientation(unsigned int o) |
lloydg | 2:2033db202017 | 86 | { |
lloydg | 2:2033db202017 | 87 | int orientation = o; |
lloydg | 2:2033db202017 | 88 | wr_cmd(0x36); // MEMORY_ACCESS_CONTROL |
lloydg | 2:2033db202017 | 89 | switch (orientation) { |
lloydg | 2:2033db202017 | 90 | case 0: |
lloydg | 2:2033db202017 | 91 | _spi.write(0x48); |
lloydg | 2:2033db202017 | 92 | break; |
lloydg | 2:2033db202017 | 93 | case 1: |
lloydg | 2:2033db202017 | 94 | _spi.write(0x28); |
lloydg | 2:2033db202017 | 95 | break; |
lloydg | 2:2033db202017 | 96 | case 2: |
lloydg | 2:2033db202017 | 97 | _spi.write(0x88); |
lloydg | 2:2033db202017 | 98 | break; |
lloydg | 2:2033db202017 | 99 | case 3: |
lloydg | 2:2033db202017 | 100 | _spi.write(0xE8); |
lloydg | 2:2033db202017 | 101 | break; |
lloydg | 2:2033db202017 | 102 | } |
lloydg | 2:2033db202017 | 103 | _cs = 1; |
lloydg | 2:2033db202017 | 104 | window_max(); |
lloydg | 2:2033db202017 | 105 | } |
lloydg | 2:2033db202017 | 106 | //#else |
lloydg | 2:2033db202017 | 107 | void SPI_TFT_ILI9341::set_origin(Origin origin) { |
lloydg | 2:2033db202017 | 108 | _origin = origin; |
lloydg | 2:2033db202017 | 109 | wr_cmd(ILI9341_MAC); // MEMORY_ACCESS_CONTROL |
lloydg | 2:2033db202017 | 110 | switch (_origin) { |
lloydg | 2:2033db202017 | 111 | case Origin_LeftTop: /* Left Top of panel is origin */ |
lloydg | 2:2033db202017 | 112 | _spi.write(0x48); |
lloydg | 2:2033db202017 | 113 | break; |
lloydg | 2:2033db202017 | 114 | case Origin_RightTop: /* ok */ |
lloydg | 2:2033db202017 | 115 | _spi.write(0x28); |
lloydg | 2:2033db202017 | 116 | break; |
lloydg | 2:2033db202017 | 117 | case Origin_RightBot: /* ok */ |
lloydg | 2:2033db202017 | 118 | _spi.write(0x88); |
lloydg | 2:2033db202017 | 119 | break; |
lloydg | 2:2033db202017 | 120 | case Origin_LeftBot: /* ok */ |
lloydg | 2:2033db202017 | 121 | _spi.write(0xE8); |
lloydg | 2:2033db202017 | 122 | break; |
lloydg | 2:2033db202017 | 123 | } |
lloydg | 2:2033db202017 | 124 | _cs = 1; |
lloydg | 2:2033db202017 | 125 | window_max(); |
lloydg | 2:2033db202017 | 126 | } |
lloydg | 2:2033db202017 | 127 | //#endif |
lloydg | 2:2033db202017 | 128 | |
lloydg | 2:2033db202017 | 129 | |
lloydg | 2:2033db202017 | 130 | // background transparancy for characters |
lloydg | 2:2033db202017 | 131 | void SPI_TFT_ILI9341::set_transparancy(bool state) { |
lloydg | 2:2033db202017 | 132 | _transparancy = state; |
lloydg | 2:2033db202017 | 133 | } |
lloydg | 2:2033db202017 | 134 | |
lloydg | 2:2033db202017 | 135 | // HW Reset to tft |
lloydg | 2:2033db202017 | 136 | void SPI_TFT_ILI9341::_hwreset() |
lloydg | 2:2033db202017 | 137 | { |
lloydg | 2:2033db202017 | 138 | // _reset is an optional pin which defaults to NC. Make sure it does not hang mbed lib |
lloydg | 2:2033db202017 | 139 | if (_reset != NULL) {_reset->write(0);} //Clear _reset pin |
lloydg | 2:2033db202017 | 140 | wait_us(50); |
lloydg | 2:2033db202017 | 141 | if (_reset != NULL) {_reset->write(1);} //Set _reset pin |
lloydg | 2:2033db202017 | 142 | wait_ms(5); |
lloydg | 2:2033db202017 | 143 | } |
lloydg | 2:2033db202017 | 144 | |
lloydg | 2:2033db202017 | 145 | |
lloydg | 2:2033db202017 | 146 | // write command to tft register |
lloydg | 2:2033db202017 | 147 | void SPI_TFT_ILI9341::wr_cmd(unsigned char cmd) |
lloydg | 2:2033db202017 | 148 | { |
lloydg | 2:2033db202017 | 149 | _dc = 0; |
lloydg | 2:2033db202017 | 150 | _cs = 0; |
lloydg | 2:2033db202017 | 151 | _spi.write(cmd); // mbed lib |
lloydg | 2:2033db202017 | 152 | _dc = 1; |
lloydg | 2:2033db202017 | 153 | } |
lloydg | 2:2033db202017 | 154 | |
lloydg | 2:2033db202017 | 155 | |
lloydg | 2:2033db202017 | 156 | // write data to tft |
lloydg | 2:2033db202017 | 157 | void SPI_TFT_ILI9341::wr_dat(unsigned char dat) |
lloydg | 2:2033db202017 | 158 | { |
lloydg | 2:2033db202017 | 159 | _spi.write(dat); // mbed lib |
lloydg | 2:2033db202017 | 160 | } |
lloydg | 2:2033db202017 | 161 | |
lloydg | 2:2033db202017 | 162 | //WH |
lloydg | 2:2033db202017 | 163 | // The ILI9341 can be read |
lloydg | 2:2033db202017 | 164 | // Read not supported in M24SR |
lloydg | 2:2033db202017 | 165 | |
lloydg | 2:2033db202017 | 166 | |
lloydg | 2:2033db202017 | 167 | // Read byte |
lloydg | 2:2033db202017 | 168 | char SPI_TFT_ILI9341::rd_byte(unsigned char cmd) |
lloydg | 2:2033db202017 | 169 | { |
lloydg | 2:2033db202017 | 170 | char r; |
lloydg | 2:2033db202017 | 171 | _dc = 0; |
lloydg | 2:2033db202017 | 172 | _cs = 0; |
lloydg | 2:2033db202017 | 173 | _spi.write(cmd); // mbed lib |
lloydg | 2:2033db202017 | 174 | _cs = 1; |
lloydg | 2:2033db202017 | 175 | r = _spi.write(0xff); |
lloydg | 2:2033db202017 | 176 | _cs = 1; |
lloydg | 2:2033db202017 | 177 | return(r); |
lloydg | 2:2033db202017 | 178 | } |
lloydg | 2:2033db202017 | 179 | |
lloydg | 2:2033db202017 | 180 | // Read 32 bit |
lloydg | 2:2033db202017 | 181 | int SPI_TFT_ILI9341::rd_32(unsigned char cmd) |
lloydg | 2:2033db202017 | 182 | { |
lloydg | 2:2033db202017 | 183 | int d; |
lloydg | 2:2033db202017 | 184 | char r; |
lloydg | 2:2033db202017 | 185 | _dc = 0; |
lloydg | 2:2033db202017 | 186 | _cs = 0; |
lloydg | 2:2033db202017 | 187 | d = cmd; |
lloydg | 2:2033db202017 | 188 | d = d << 1; |
lloydg | 2:2033db202017 | 189 | |
lloydg | 2:2033db202017 | 190 | //Note WH: the 9bit format is not supported for most mbed devices...This wont work. |
lloydg | 2:2033db202017 | 191 | _spi.format(9,0); // we have to add a dummy clock cycle |
lloydg | 2:2033db202017 | 192 | |
lloydg | 2:2033db202017 | 193 | _spi.write(d); |
lloydg | 2:2033db202017 | 194 | _spi.format(8,0); |
lloydg | 2:2033db202017 | 195 | _dc = 1; |
lloydg | 2:2033db202017 | 196 | r = _spi.write(0xff); |
lloydg | 2:2033db202017 | 197 | d = r; |
lloydg | 2:2033db202017 | 198 | r = _spi.write(0xff); |
lloydg | 2:2033db202017 | 199 | d = (d << 8) | r; |
lloydg | 2:2033db202017 | 200 | r = _spi.write(0xff); |
lloydg | 2:2033db202017 | 201 | d = (d << 8) | r; |
lloydg | 2:2033db202017 | 202 | r = _spi.write(0xff); |
lloydg | 2:2033db202017 | 203 | d = (d << 8) | r; |
lloydg | 2:2033db202017 | 204 | _cs = 1; |
lloydg | 2:2033db202017 | 205 | return(d); |
lloydg | 2:2033db202017 | 206 | } |
lloydg | 2:2033db202017 | 207 | |
lloydg | 2:2033db202017 | 208 | //This may be not supported on some revisions of IL9341 |
lloydg | 2:2033db202017 | 209 | int SPI_TFT_ILI9341::Read_ID(void){ |
lloydg | 2:2033db202017 | 210 | int r; |
lloydg | 2:2033db202017 | 211 | r = rd_byte(0x0A); |
lloydg | 2:2033db202017 | 212 | r = rd_byte(0x0A); |
lloydg | 2:2033db202017 | 213 | r = rd_byte(0x0A); |
lloydg | 2:2033db202017 | 214 | r = rd_byte(0x0A); |
lloydg | 2:2033db202017 | 215 | return(r); |
lloydg | 2:2033db202017 | 216 | } |
lloydg | 2:2033db202017 | 217 | |
lloydg | 2:2033db202017 | 218 | |
lloydg | 2:2033db202017 | 219 | // Init code based on MI0283QT datasheet |
lloydg | 2:2033db202017 | 220 | void SPI_TFT_ILI9341::tft_reset() |
lloydg | 2:2033db202017 | 221 | { |
lloydg | 2:2033db202017 | 222 | //WH _spi.format(8,3); // 8 bit spi Mode 3 |
lloydg | 2:2033db202017 | 223 | _spi.format(8,0); // 8 bit spi, mode 0 |
lloydg | 2:2033db202017 | 224 | |
lloydg | 2:2033db202017 | 225 | // _spi.frequency(4000000); // 4 Mhz SPI clock |
lloydg | 2:2033db202017 | 226 | // _spi.frequency(8000000); // 8 Mhz SPI clock |
lloydg | 2:2033db202017 | 227 | _spi.frequency(10000000); // 10 Mhz SPI ... works on current version of mbed F103 lib after fix for HSI/HSE... |
lloydg | 2:2033db202017 | 228 | |
lloydg | 2:2033db202017 | 229 | _cs = 1; // cs high |
lloydg | 2:2033db202017 | 230 | _dc = 1; // dc high |
lloydg | 2:2033db202017 | 231 | |
lloydg | 2:2033db202017 | 232 | _hwreset(); // HW reset |
lloydg | 2:2033db202017 | 233 | |
lloydg | 2:2033db202017 | 234 | wr_cmd(ILI9341_DISPLAY_RST); // SW reset |
lloydg | 2:2033db202017 | 235 | wait_ms(5); |
lloydg | 2:2033db202017 | 236 | |
lloydg | 2:2033db202017 | 237 | wr_cmd(ILI9341_DISPLAY_OFF); // display off |
lloydg | 2:2033db202017 | 238 | |
lloydg | 2:2033db202017 | 239 | /* Start Initial Sequence ----------------------------------------------------*/ |
lloydg | 2:2033db202017 | 240 | wr_cmd(ILI9341_POWERB); /* Power control B register */ |
lloydg | 2:2033db202017 | 241 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 242 | _spi.write(0x83); |
lloydg | 2:2033db202017 | 243 | _spi.write(0x30); |
lloydg | 2:2033db202017 | 244 | _cs = 1; |
lloydg | 2:2033db202017 | 245 | |
lloydg | 2:2033db202017 | 246 | wr_cmd(ILI9341_POWER_SEQ); /* Power on sequence register */ |
lloydg | 2:2033db202017 | 247 | _spi.write(0x64); |
lloydg | 2:2033db202017 | 248 | _spi.write(0x03); |
lloydg | 2:2033db202017 | 249 | _spi.write(0x12); |
lloydg | 2:2033db202017 | 250 | _spi.write(0x81); |
lloydg | 2:2033db202017 | 251 | _cs = 1; |
lloydg | 2:2033db202017 | 252 | |
lloydg | 2:2033db202017 | 253 | wr_cmd(ILI9341_DTCA); /* Driver timing control A */ |
lloydg | 2:2033db202017 | 254 | _spi.write(0x85); |
lloydg | 2:2033db202017 | 255 | _spi.write(0x01); |
lloydg | 2:2033db202017 | 256 | _spi.write(0x79); |
lloydg | 2:2033db202017 | 257 | _cs = 1; |
lloydg | 2:2033db202017 | 258 | |
lloydg | 2:2033db202017 | 259 | wr_cmd(ILI9341_POWERA); /* Power control A register */ |
lloydg | 2:2033db202017 | 260 | _spi.write(0x39); |
lloydg | 2:2033db202017 | 261 | _spi.write(0x2C); |
lloydg | 2:2033db202017 | 262 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 263 | _spi.write(0x34); |
lloydg | 2:2033db202017 | 264 | _spi.write(0x02); |
lloydg | 2:2033db202017 | 265 | _cs = 1; |
lloydg | 2:2033db202017 | 266 | |
lloydg | 2:2033db202017 | 267 | wr_cmd(ILI9341_PRC); /* Pump ratio control register */ |
lloydg | 2:2033db202017 | 268 | _spi.write(0x20); |
lloydg | 2:2033db202017 | 269 | _cs = 1; |
lloydg | 2:2033db202017 | 270 | |
lloydg | 2:2033db202017 | 271 | wr_cmd(ILI9341_DTCB); /* Driver timing control B */ |
lloydg | 2:2033db202017 | 272 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 273 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 274 | _cs = 1; |
lloydg | 2:2033db202017 | 275 | |
lloydg | 2:2033db202017 | 276 | wr_cmd(ILI9341_POWER1); // POWER_CONTROL_1 |
lloydg | 2:2033db202017 | 277 | _spi.write(0x26); |
lloydg | 2:2033db202017 | 278 | _cs = 1; |
lloydg | 2:2033db202017 | 279 | |
lloydg | 2:2033db202017 | 280 | wr_cmd(ILI9341_POWER2); // POWER_CONTROL_2 |
lloydg | 2:2033db202017 | 281 | _spi.write(0x11); |
lloydg | 2:2033db202017 | 282 | _cs = 1; |
lloydg | 2:2033db202017 | 283 | |
lloydg | 2:2033db202017 | 284 | wr_cmd(ILI9341_VCOM1); // VCOM_CONTROL_1 |
lloydg | 2:2033db202017 | 285 | _spi.write(0x35); |
lloydg | 2:2033db202017 | 286 | _spi.write(0x3E); |
lloydg | 2:2033db202017 | 287 | _cs = 1; |
lloydg | 2:2033db202017 | 288 | |
lloydg | 2:2033db202017 | 289 | wr_cmd(ILI9341_VCOM2); // VCOM_CONTROL_2 |
lloydg | 2:2033db202017 | 290 | _spi.write(0xBE); |
lloydg | 2:2033db202017 | 291 | _cs = 1; |
lloydg | 2:2033db202017 | 292 | |
lloydg | 2:2033db202017 | 293 | wr_cmd(ILI9341_MAC); // MEMORY_ACCESS_CONTROL |
lloydg | 2:2033db202017 | 294 | _spi.write(0x48); // my,mx,mv,ml,BGR,mh,0,0 |
lloydg | 2:2033db202017 | 295 | _cs = 1; |
lloydg | 2:2033db202017 | 296 | |
lloydg | 2:2033db202017 | 297 | wr_cmd(ILI9341_PIXEL_FORMAT); /* Pixel Format register */ |
lloydg | 2:2033db202017 | 298 | _spi.write(0x55); // 16 bit pixel |
lloydg | 2:2033db202017 | 299 | _cs = 1; |
lloydg | 2:2033db202017 | 300 | |
lloydg | 2:2033db202017 | 301 | wr_cmd(ILI9341_FRC); /* Frame Rate Control register */ |
lloydg | 2:2033db202017 | 302 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 303 | _spi.write(0x1B); |
lloydg | 2:2033db202017 | 304 | _cs = 1; |
lloydg | 2:2033db202017 | 305 | |
lloydg | 2:2033db202017 | 306 | wr_cmd(ILI9341_3GAMMA_EN); /* 3 Gamma enable register */ |
lloydg | 2:2033db202017 | 307 | _spi.write(0x08); // Gamma Function Disable |
lloydg | 2:2033db202017 | 308 | _cs = 1; |
lloydg | 2:2033db202017 | 309 | |
lloydg | 2:2033db202017 | 310 | wr_cmd(ILI9341_GAMMA); /* Gamma register */ |
lloydg | 2:2033db202017 | 311 | _spi.write(0x01); // gamma set for curve 01/2/04/08 |
lloydg | 2:2033db202017 | 312 | _cs = 1; |
lloydg | 2:2033db202017 | 313 | |
lloydg | 2:2033db202017 | 314 | wr_cmd(ILI9341_PGAMMA); /* Positive Gamma Correction register*/ |
lloydg | 2:2033db202017 | 315 | _spi.write(0x1F); |
lloydg | 2:2033db202017 | 316 | _spi.write(0x1A); |
lloydg | 2:2033db202017 | 317 | _spi.write(0x18); |
lloydg | 2:2033db202017 | 318 | _spi.write(0x0A); |
lloydg | 2:2033db202017 | 319 | _spi.write(0x0F); |
lloydg | 2:2033db202017 | 320 | _spi.write(0x06); |
lloydg | 2:2033db202017 | 321 | _spi.write(0x45); |
lloydg | 2:2033db202017 | 322 | _spi.write(0x87); |
lloydg | 2:2033db202017 | 323 | _spi.write(0x32); |
lloydg | 2:2033db202017 | 324 | _spi.write(0x0A); |
lloydg | 2:2033db202017 | 325 | _spi.write(0x07); |
lloydg | 2:2033db202017 | 326 | _spi.write(0x02); |
lloydg | 2:2033db202017 | 327 | _spi.write(0x07); |
lloydg | 2:2033db202017 | 328 | _spi.write(0x05); |
lloydg | 2:2033db202017 | 329 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 330 | _cs = 1; |
lloydg | 2:2033db202017 | 331 | |
lloydg | 2:2033db202017 | 332 | wr_cmd(ILI9341_NGAMMA); /* Negative Gamma Correction register*/ |
lloydg | 2:2033db202017 | 333 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 334 | _spi.write(0x25); |
lloydg | 2:2033db202017 | 335 | _spi.write(0x27); |
lloydg | 2:2033db202017 | 336 | _spi.write(0x05); |
lloydg | 2:2033db202017 | 337 | _spi.write(0x10); |
lloydg | 2:2033db202017 | 338 | _spi.write(0x09); |
lloydg | 2:2033db202017 | 339 | _spi.write(0x3A); |
lloydg | 2:2033db202017 | 340 | _spi.write(0x78); |
lloydg | 2:2033db202017 | 341 | _spi.write(0x4D); |
lloydg | 2:2033db202017 | 342 | _spi.write(0x05); |
lloydg | 2:2033db202017 | 343 | _spi.write(0x18); |
lloydg | 2:2033db202017 | 344 | _spi.write(0x0D); |
lloydg | 2:2033db202017 | 345 | _spi.write(0x38); |
lloydg | 2:2033db202017 | 346 | _spi.write(0x3A); |
lloydg | 2:2033db202017 | 347 | _spi.write(0x1F); |
lloydg | 2:2033db202017 | 348 | _cs = 1; |
lloydg | 2:2033db202017 | 349 | |
lloydg | 2:2033db202017 | 350 | window_max(); |
lloydg | 2:2033db202017 | 351 | |
lloydg | 2:2033db202017 | 352 | //wr_cmd(ILI9341_TEAR_OFF); // tearing effect off |
lloydg | 2:2033db202017 | 353 | //_cs = 1; |
lloydg | 2:2033db202017 | 354 | |
lloydg | 2:2033db202017 | 355 | //wr_cmd(ILI9341_TEAR_ON); // tearing effect on |
lloydg | 2:2033db202017 | 356 | //_cs = 1; |
lloydg | 2:2033db202017 | 357 | |
lloydg | 2:2033db202017 | 358 | wr_cmd(ILI9341_ENTRY_MODE); // entry mode |
lloydg | 2:2033db202017 | 359 | _spi.write(0x07); |
lloydg | 2:2033db202017 | 360 | _cs = 1; |
lloydg | 2:2033db202017 | 361 | |
lloydg | 2:2033db202017 | 362 | wr_cmd(ILI9341_DFC); /* Display Function Control register*/ |
lloydg | 2:2033db202017 | 363 | _spi.write(0x0A); |
lloydg | 2:2033db202017 | 364 | _spi.write(0x82); |
lloydg | 2:2033db202017 | 365 | _spi.write(0x27); |
lloydg | 2:2033db202017 | 366 | _spi.write(0x00); |
lloydg | 2:2033db202017 | 367 | _cs = 1; |
lloydg | 2:2033db202017 | 368 | |
lloydg | 2:2033db202017 | 369 | wr_cmd(ILI9341_SLEEP_OUT); // sleep out |
lloydg | 2:2033db202017 | 370 | _cs = 1; |
lloydg | 2:2033db202017 | 371 | |
lloydg | 2:2033db202017 | 372 | wait_ms(100); |
lloydg | 2:2033db202017 | 373 | |
lloydg | 2:2033db202017 | 374 | wr_cmd(ILI9341_DISPLAY_ON); |
lloydg | 2:2033db202017 | 375 | _cs = 1; |
lloydg | 2:2033db202017 | 376 | |
lloydg | 2:2033db202017 | 377 | wait_ms(100); |
lloydg | 2:2033db202017 | 378 | |
lloydg | 2:2033db202017 | 379 | } |
lloydg | 2:2033db202017 | 380 | |
lloydg | 2:2033db202017 | 381 | |
lloydg | 2:2033db202017 | 382 | void SPI_TFT_ILI9341::tft_on(bool on) |
lloydg | 2:2033db202017 | 383 | { |
lloydg | 2:2033db202017 | 384 | if (on) { |
lloydg | 2:2033db202017 | 385 | wr_cmd(ILI9341_DISPLAY_ON); |
lloydg | 2:2033db202017 | 386 | } |
lloydg | 2:2033db202017 | 387 | else { |
lloydg | 2:2033db202017 | 388 | wr_cmd(ILI9341_DISPLAY_OFF); |
lloydg | 2:2033db202017 | 389 | } |
lloydg | 2:2033db202017 | 390 | _cs = 1; |
lloydg | 2:2033db202017 | 391 | } |
lloydg | 2:2033db202017 | 392 | |
lloydg | 2:2033db202017 | 393 | void SPI_TFT_ILI9341::pixel(int x, int y, int color) |
lloydg | 2:2033db202017 | 394 | { |
lloydg | 2:2033db202017 | 395 | wr_cmd(ILI9341_COLUMN_ADDR); |
lloydg | 2:2033db202017 | 396 | _spi.write(x >> 8); |
lloydg | 2:2033db202017 | 397 | _spi.write(x); |
lloydg | 2:2033db202017 | 398 | _spi.write((x+1) >> 8); //WH |
lloydg | 2:2033db202017 | 399 | _spi.write(x+1); |
lloydg | 2:2033db202017 | 400 | |
lloydg | 2:2033db202017 | 401 | _cs = 1; |
lloydg | 2:2033db202017 | 402 | |
lloydg | 2:2033db202017 | 403 | wr_cmd(ILI9341_PAGE_ADDR); |
lloydg | 2:2033db202017 | 404 | _spi.write(y >> 8); |
lloydg | 2:2033db202017 | 405 | _spi.write(y); |
lloydg | 2:2033db202017 | 406 | _spi.write((y+1) >> 8); //WH |
lloydg | 2:2033db202017 | 407 | _spi.write(y+1); |
lloydg | 2:2033db202017 | 408 | |
lloydg | 2:2033db202017 | 409 | _cs = 1; |
lloydg | 2:2033db202017 | 410 | |
lloydg | 2:2033db202017 | 411 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 412 | |
lloydg | 2:2033db202017 | 413 | // #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 414 | #if(0) // dont bother switching for only one pixel |
lloydg | 2:2033db202017 | 415 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 416 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 417 | _spi.write(color); // Write D0..D15 |
lloydg | 2:2033db202017 | 418 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 419 | #else |
lloydg | 2:2033db202017 | 420 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 421 | _spi.write(color >> 8); |
lloydg | 2:2033db202017 | 422 | _spi.write(color & 0xff); |
lloydg | 2:2033db202017 | 423 | #endif |
lloydg | 2:2033db202017 | 424 | |
lloydg | 2:2033db202017 | 425 | _cs = 1; |
lloydg | 2:2033db202017 | 426 | } |
lloydg | 2:2033db202017 | 427 | |
lloydg | 2:2033db202017 | 428 | |
lloydg | 2:2033db202017 | 429 | void SPI_TFT_ILI9341::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) |
lloydg | 2:2033db202017 | 430 | { |
lloydg | 2:2033db202017 | 431 | |
lloydg | 2:2033db202017 | 432 | wr_cmd(ILI9341_COLUMN_ADDR); |
lloydg | 2:2033db202017 | 433 | _spi.write(x >> 8); |
lloydg | 2:2033db202017 | 434 | _spi.write(x); |
lloydg | 2:2033db202017 | 435 | _spi.write((x+w-1) >> 8); |
lloydg | 2:2033db202017 | 436 | _spi.write(x+w-1); |
lloydg | 2:2033db202017 | 437 | _cs = 1; |
lloydg | 2:2033db202017 | 438 | |
lloydg | 2:2033db202017 | 439 | wr_cmd(ILI9341_PAGE_ADDR); |
lloydg | 2:2033db202017 | 440 | _spi.write(y >> 8); |
lloydg | 2:2033db202017 | 441 | _spi.write(y); |
lloydg | 2:2033db202017 | 442 | _spi.write((y+h-1) >> 8); |
lloydg | 2:2033db202017 | 443 | _spi.write(y+h-1); |
lloydg | 2:2033db202017 | 444 | _cs = 1; |
lloydg | 2:2033db202017 | 445 | } |
lloydg | 2:2033db202017 | 446 | |
lloydg | 2:2033db202017 | 447 | |
lloydg | 2:2033db202017 | 448 | void SPI_TFT_ILI9341::window_max (void) |
lloydg | 2:2033db202017 | 449 | { |
lloydg | 2:2033db202017 | 450 | window (0, 0, width(), height()); |
lloydg | 2:2033db202017 | 451 | } |
lloydg | 2:2033db202017 | 452 | |
lloydg | 2:2033db202017 | 453 | |
lloydg | 2:2033db202017 | 454 | |
lloydg | 2:2033db202017 | 455 | /** Fill the screen with _background color |
lloydg | 2:2033db202017 | 456 | * @param none |
lloydg | 2:2033db202017 | 457 | * @return none |
lloydg | 2:2033db202017 | 458 | */ |
lloydg | 2:2033db202017 | 459 | void SPI_TFT_ILI9341::cls() |
lloydg | 2:2033db202017 | 460 | { |
lloydg | 2:2033db202017 | 461 | fillrect(0, 0, width()-1, height()-1, _background); |
lloydg | 2:2033db202017 | 462 | } |
lloydg | 2:2033db202017 | 463 | |
lloydg | 2:2033db202017 | 464 | |
lloydg | 2:2033db202017 | 465 | //WH test |
lloydg | 2:2033db202017 | 466 | void SPI_TFT_ILI9341::newcls (void) |
lloydg | 2:2033db202017 | 467 | { |
lloydg | 2:2033db202017 | 468 | int pixels = height() * width(); |
lloydg | 2:2033db202017 | 469 | int i; |
lloydg | 2:2033db202017 | 470 | int color = _background; |
lloydg | 2:2033db202017 | 471 | #if (SPI_16 != 1) |
lloydg | 2:2033db202017 | 472 | int msb, lsb; |
lloydg | 2:2033db202017 | 473 | #endif |
lloydg | 2:2033db202017 | 474 | |
lloydg | 2:2033db202017 | 475 | window(0,0,width(),height()); |
lloydg | 2:2033db202017 | 476 | |
lloydg | 2:2033db202017 | 477 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 478 | |
lloydg | 2:2033db202017 | 479 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 480 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 481 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 482 | |
lloydg | 2:2033db202017 | 483 | //unroll loop in chunks of 8 pixels |
lloydg | 2:2033db202017 | 484 | for (i = 0; i < (pixels>>3); i++) { |
lloydg | 2:2033db202017 | 485 | _spi.write(color); |
lloydg | 2:2033db202017 | 486 | _spi.write(color); |
lloydg | 2:2033db202017 | 487 | _spi.write(color); |
lloydg | 2:2033db202017 | 488 | _spi.write(color); |
lloydg | 2:2033db202017 | 489 | |
lloydg | 2:2033db202017 | 490 | _spi.write(color); |
lloydg | 2:2033db202017 | 491 | _spi.write(color); |
lloydg | 2:2033db202017 | 492 | _spi.write(color); |
lloydg | 2:2033db202017 | 493 | _spi.write(color); |
lloydg | 2:2033db202017 | 494 | } |
lloydg | 2:2033db202017 | 495 | |
lloydg | 2:2033db202017 | 496 | //remainder |
lloydg | 2:2033db202017 | 497 | for (i = 0; i < (pixels & 0x07); i++) |
lloydg | 2:2033db202017 | 498 | _spi.write(color); |
lloydg | 2:2033db202017 | 499 | |
lloydg | 2:2033db202017 | 500 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 501 | #else |
lloydg | 2:2033db202017 | 502 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 503 | msb = color >> 8; |
lloydg | 2:2033db202017 | 504 | lsb = color & 0xff; |
lloydg | 2:2033db202017 | 505 | |
lloydg | 2:2033db202017 | 506 | for (i = 0; i < (pixels>>3); i+=8) { |
lloydg | 2:2033db202017 | 507 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 508 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 509 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 510 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 511 | |
lloydg | 2:2033db202017 | 512 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 513 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 514 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 515 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 516 | } |
lloydg | 2:2033db202017 | 517 | |
lloydg | 2:2033db202017 | 518 | for (i = 0; i < (pixels & 0x07); i++) { |
lloydg | 2:2033db202017 | 519 | _spi.write(msb); _spi.write(lsb); |
lloydg | 2:2033db202017 | 520 | } |
lloydg | 2:2033db202017 | 521 | #endif |
lloydg | 2:2033db202017 | 522 | |
lloydg | 2:2033db202017 | 523 | _cs = 1; |
lloydg | 2:2033db202017 | 524 | } |
lloydg | 2:2033db202017 | 525 | |
lloydg | 2:2033db202017 | 526 | |
lloydg | 2:2033db202017 | 527 | |
lloydg | 2:2033db202017 | 528 | |
lloydg | 2:2033db202017 | 529 | void SPI_TFT_ILI9341::circle(int x0, int y0, int r, int color) |
lloydg | 2:2033db202017 | 530 | { |
lloydg | 2:2033db202017 | 531 | |
lloydg | 2:2033db202017 | 532 | int x = -r, y = 0, err = 2-2*r, e2; |
lloydg | 2:2033db202017 | 533 | do { |
lloydg | 2:2033db202017 | 534 | pixel(x0-x, y0+y,color); |
lloydg | 2:2033db202017 | 535 | pixel(x0+x, y0+y,color); |
lloydg | 2:2033db202017 | 536 | pixel(x0+x, y0-y,color); |
lloydg | 2:2033db202017 | 537 | pixel(x0-x, y0-y,color); |
lloydg | 2:2033db202017 | 538 | e2 = err; |
lloydg | 2:2033db202017 | 539 | if (e2 <= y) { |
lloydg | 2:2033db202017 | 540 | err += ++y*2+1; |
lloydg | 2:2033db202017 | 541 | if (-x == y && e2 <= x) e2 = 0; |
lloydg | 2:2033db202017 | 542 | } |
lloydg | 2:2033db202017 | 543 | if (e2 > x) err += ++x*2+1; |
lloydg | 2:2033db202017 | 544 | } while (x <= 0); |
lloydg | 2:2033db202017 | 545 | |
lloydg | 2:2033db202017 | 546 | } |
lloydg | 2:2033db202017 | 547 | |
lloydg | 2:2033db202017 | 548 | void SPI_TFT_ILI9341::fillcircle(int x0, int y0, int r, int color) |
lloydg | 2:2033db202017 | 549 | { |
lloydg | 2:2033db202017 | 550 | int x = -r, y = 0, err = 2-2*r, e2; |
lloydg | 2:2033db202017 | 551 | do { |
lloydg | 2:2033db202017 | 552 | vline(x0-x, y0-y, y0+y, color); |
lloydg | 2:2033db202017 | 553 | vline(x0+x, y0-y, y0+y, color); |
lloydg | 2:2033db202017 | 554 | e2 = err; |
lloydg | 2:2033db202017 | 555 | if (e2 <= y) { |
lloydg | 2:2033db202017 | 556 | err += ++y*2+1; |
lloydg | 2:2033db202017 | 557 | if (-x == y && e2 <= x) e2 = 0; |
lloydg | 2:2033db202017 | 558 | } |
lloydg | 2:2033db202017 | 559 | if (e2 > x) err += ++x*2+1; |
lloydg | 2:2033db202017 | 560 | } while (x <= 0); |
lloydg | 2:2033db202017 | 561 | } |
lloydg | 2:2033db202017 | 562 | |
lloydg | 2:2033db202017 | 563 | |
lloydg | 2:2033db202017 | 564 | void SPI_TFT_ILI9341::oval ( int x, int y, int b, int color, float aspect ) |
lloydg | 2:2033db202017 | 565 | { |
lloydg | 2:2033db202017 | 566 | /* local variables */ |
lloydg | 2:2033db202017 | 567 | int col; /* Column. */ |
lloydg | 2:2033db202017 | 568 | int row; /* Row. */ |
lloydg | 2:2033db202017 | 569 | float aspect_square; |
lloydg | 2:2033db202017 | 570 | int a_square; |
lloydg | 2:2033db202017 | 571 | int b_square; |
lloydg | 2:2033db202017 | 572 | int two_a_square; |
lloydg | 2:2033db202017 | 573 | int two_b_square; |
lloydg | 2:2033db202017 | 574 | int four_a_square; |
lloydg | 2:2033db202017 | 575 | int four_b_square; |
lloydg | 2:2033db202017 | 576 | int d; |
lloydg | 2:2033db202017 | 577 | |
lloydg | 2:2033db202017 | 578 | aspect_square = aspect * aspect; |
lloydg | 2:2033db202017 | 579 | |
lloydg | 2:2033db202017 | 580 | b_square = b * b; |
lloydg | 2:2033db202017 | 581 | a_square = b_square / aspect_square; |
lloydg | 2:2033db202017 | 582 | row = b; |
lloydg | 2:2033db202017 | 583 | col = 0; |
lloydg | 2:2033db202017 | 584 | two_a_square = a_square << 1; |
lloydg | 2:2033db202017 | 585 | four_a_square = a_square << 2; |
lloydg | 2:2033db202017 | 586 | four_b_square = b_square << 2; |
lloydg | 2:2033db202017 | 587 | two_b_square = b_square << 1; |
lloydg | 2:2033db202017 | 588 | d = two_a_square * ((row - 1) * (row)) + a_square + two_b_square * (1 - a_square); |
lloydg | 2:2033db202017 | 589 | |
lloydg | 2:2033db202017 | 590 | while (a_square * (row) > b_square * col ) { |
lloydg | 2:2033db202017 | 591 | pixel( x + col, y + row, color ); |
lloydg | 2:2033db202017 | 592 | pixel( x + col, y - row, color ); |
lloydg | 2:2033db202017 | 593 | pixel( x - col, y + row, color ); |
lloydg | 2:2033db202017 | 594 | pixel( x - col, y - row, color ); |
lloydg | 2:2033db202017 | 595 | if ( d >= 0 ) { |
lloydg | 2:2033db202017 | 596 | row--; |
lloydg | 2:2033db202017 | 597 | d -= four_a_square * row; |
lloydg | 2:2033db202017 | 598 | } |
lloydg | 2:2033db202017 | 599 | d += two_b_square * (3 + (col << 1)); |
lloydg | 2:2033db202017 | 600 | col++; |
lloydg | 2:2033db202017 | 601 | } |
lloydg | 2:2033db202017 | 602 | |
lloydg | 2:2033db202017 | 603 | d = two_b_square * (col + 1) * col + two_a_square * (row * (row - 2) + 1) + (1 - two_a_square) * b_square; |
lloydg | 2:2033db202017 | 604 | |
lloydg | 2:2033db202017 | 605 | while ( row + 1 ) { |
lloydg | 2:2033db202017 | 606 | pixel( x + col, y + row, color ); |
lloydg | 2:2033db202017 | 607 | pixel( x + col, y - row, color ); |
lloydg | 2:2033db202017 | 608 | pixel( x - col, y + row, color ); |
lloydg | 2:2033db202017 | 609 | pixel( x - col, y - row, color ); |
lloydg | 2:2033db202017 | 610 | if ( d <= 0 ) { |
lloydg | 2:2033db202017 | 611 | col++; |
lloydg | 2:2033db202017 | 612 | d += four_b_square * col; |
lloydg | 2:2033db202017 | 613 | } |
lloydg | 2:2033db202017 | 614 | row--; |
lloydg | 2:2033db202017 | 615 | d += two_a_square * (3 - (row << 1)); |
lloydg | 2:2033db202017 | 616 | } |
lloydg | 2:2033db202017 | 617 | |
lloydg | 2:2033db202017 | 618 | } /* End oval */ |
lloydg | 2:2033db202017 | 619 | |
lloydg | 2:2033db202017 | 620 | |
lloydg | 2:2033db202017 | 621 | void SPI_TFT_ILI9341::filloval ( int x, int y, int b, int color, float aspect ) |
lloydg | 2:2033db202017 | 622 | { |
lloydg | 2:2033db202017 | 623 | /* local variables */ |
lloydg | 2:2033db202017 | 624 | int col; /* Column. */ |
lloydg | 2:2033db202017 | 625 | int row; /* Row. */ |
lloydg | 2:2033db202017 | 626 | float aspect_square; |
lloydg | 2:2033db202017 | 627 | int a_square; |
lloydg | 2:2033db202017 | 628 | int b_square; |
lloydg | 2:2033db202017 | 629 | int two_a_square; |
lloydg | 2:2033db202017 | 630 | int two_b_square; |
lloydg | 2:2033db202017 | 631 | int four_a_square; |
lloydg | 2:2033db202017 | 632 | int four_b_square; |
lloydg | 2:2033db202017 | 633 | int d; |
lloydg | 2:2033db202017 | 634 | |
lloydg | 2:2033db202017 | 635 | aspect_square = aspect * aspect; |
lloydg | 2:2033db202017 | 636 | |
lloydg | 2:2033db202017 | 637 | b_square = b * b; |
lloydg | 2:2033db202017 | 638 | a_square = b_square / aspect_square; |
lloydg | 2:2033db202017 | 639 | row = b; |
lloydg | 2:2033db202017 | 640 | col = 0; |
lloydg | 2:2033db202017 | 641 | two_a_square = a_square << 1; |
lloydg | 2:2033db202017 | 642 | four_a_square = a_square << 2; |
lloydg | 2:2033db202017 | 643 | four_b_square = b_square << 2; |
lloydg | 2:2033db202017 | 644 | two_b_square = b_square << 1; |
lloydg | 2:2033db202017 | 645 | d = two_a_square * ((row - 1) * (row)) + a_square + two_b_square * (1 - a_square); |
lloydg | 2:2033db202017 | 646 | |
lloydg | 2:2033db202017 | 647 | while (a_square * (row) > b_square * col ) { |
lloydg | 2:2033db202017 | 648 | vline(x - col, y - row, y + row, color); |
lloydg | 2:2033db202017 | 649 | vline(x + col, y - row, y + row, color); |
lloydg | 2:2033db202017 | 650 | |
lloydg | 2:2033db202017 | 651 | if ( d >= 0 ) { |
lloydg | 2:2033db202017 | 652 | row--; |
lloydg | 2:2033db202017 | 653 | d -= four_a_square * row; |
lloydg | 2:2033db202017 | 654 | } |
lloydg | 2:2033db202017 | 655 | d += two_b_square * (3 + (col << 1)); |
lloydg | 2:2033db202017 | 656 | col++; |
lloydg | 2:2033db202017 | 657 | } |
lloydg | 2:2033db202017 | 658 | |
lloydg | 2:2033db202017 | 659 | d = two_b_square * (col + 1) * col + two_a_square * (row * (row - 2) + 1) + (1 - two_a_square) * b_square; |
lloydg | 2:2033db202017 | 660 | |
lloydg | 2:2033db202017 | 661 | while ( row + 1 ) { |
lloydg | 2:2033db202017 | 662 | vline(x - col, y - row, y + row, color); |
lloydg | 2:2033db202017 | 663 | vline(x + col, y - row, y + row, color); |
lloydg | 2:2033db202017 | 664 | |
lloydg | 2:2033db202017 | 665 | if ( d <= 0 ) { |
lloydg | 2:2033db202017 | 666 | col++; |
lloydg | 2:2033db202017 | 667 | d += four_b_square * col; |
lloydg | 2:2033db202017 | 668 | } |
lloydg | 2:2033db202017 | 669 | row--; |
lloydg | 2:2033db202017 | 670 | d += two_a_square * (3 - (row << 1)); |
lloydg | 2:2033db202017 | 671 | } |
lloydg | 2:2033db202017 | 672 | |
lloydg | 2:2033db202017 | 673 | } /* End filloval */ |
lloydg | 2:2033db202017 | 674 | |
lloydg | 2:2033db202017 | 675 | |
lloydg | 2:2033db202017 | 676 | void SPI_TFT_ILI9341::hline(int x0, int x1, int y, int color) |
lloydg | 2:2033db202017 | 677 | { |
lloydg | 2:2033db202017 | 678 | int i, w; |
lloydg | 2:2033db202017 | 679 | #if (SPI_16 != 1) |
lloydg | 2:2033db202017 | 680 | int msb, lsb; |
lloydg | 2:2033db202017 | 681 | #endif |
lloydg | 2:2033db202017 | 682 | w = x1 - x0 + 1; |
lloydg | 2:2033db202017 | 683 | window(x0,y,w,1); |
lloydg | 2:2033db202017 | 684 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 685 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 686 | |
lloydg | 2:2033db202017 | 687 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 688 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 689 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 690 | for (i = 0; i < w; i++) |
lloydg | 2:2033db202017 | 691 | _spi.write(color); |
lloydg | 2:2033db202017 | 692 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 693 | #else |
lloydg | 2:2033db202017 | 694 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 695 | msb = color >> 8; |
lloydg | 2:2033db202017 | 696 | lsb = color & 0xff; |
lloydg | 2:2033db202017 | 697 | for (i = 0; i < w; i++){ |
lloydg | 2:2033db202017 | 698 | _spi.write(msb); |
lloydg | 2:2033db202017 | 699 | _spi.write(lsb); |
lloydg | 2:2033db202017 | 700 | } |
lloydg | 2:2033db202017 | 701 | #endif |
lloydg | 2:2033db202017 | 702 | |
lloydg | 2:2033db202017 | 703 | _cs = 1; |
lloydg | 2:2033db202017 | 704 | window_max(); |
lloydg | 2:2033db202017 | 705 | } |
lloydg | 2:2033db202017 | 706 | |
lloydg | 2:2033db202017 | 707 | |
lloydg | 2:2033db202017 | 708 | |
lloydg | 2:2033db202017 | 709 | void SPI_TFT_ILI9341::vline(int x, int y0, int y1, int color) |
lloydg | 2:2033db202017 | 710 | { |
lloydg | 2:2033db202017 | 711 | int i, h; |
lloydg | 2:2033db202017 | 712 | #if (SPI_16 != 1) |
lloydg | 2:2033db202017 | 713 | int msb, lsb; |
lloydg | 2:2033db202017 | 714 | #endif |
lloydg | 2:2033db202017 | 715 | |
lloydg | 2:2033db202017 | 716 | h = y1 - y0 + 1; |
lloydg | 2:2033db202017 | 717 | window(x,y0,1,h); |
lloydg | 2:2033db202017 | 718 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 719 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 720 | |
lloydg | 2:2033db202017 | 721 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 722 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 723 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 724 | for (i = 0; i < h; i++) |
lloydg | 2:2033db202017 | 725 | _spi.write(color); |
lloydg | 2:2033db202017 | 726 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 727 | #else |
lloydg | 2:2033db202017 | 728 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 729 | msb = color >> 8; |
lloydg | 2:2033db202017 | 730 | lsb = color & 0xff; |
lloydg | 2:2033db202017 | 731 | for (i = 0; i < h; i++){ |
lloydg | 2:2033db202017 | 732 | _spi.write(msb); |
lloydg | 2:2033db202017 | 733 | _spi.write(lsb); |
lloydg | 2:2033db202017 | 734 | } |
lloydg | 2:2033db202017 | 735 | #endif |
lloydg | 2:2033db202017 | 736 | |
lloydg | 2:2033db202017 | 737 | _cs = 1; |
lloydg | 2:2033db202017 | 738 | window_max(); |
lloydg | 2:2033db202017 | 739 | } |
lloydg | 2:2033db202017 | 740 | |
lloydg | 2:2033db202017 | 741 | void SPI_TFT_ILI9341::line(int x0, int y0, int x1, int y1, int color) |
lloydg | 2:2033db202017 | 742 | { |
lloydg | 2:2033db202017 | 743 | //window_max(); |
lloydg | 2:2033db202017 | 744 | int dx = 0, dy = 0; |
lloydg | 2:2033db202017 | 745 | int dx_sym = 0, dy_sym = 0; |
lloydg | 2:2033db202017 | 746 | int dx_x2 = 0, dy_x2 = 0; |
lloydg | 2:2033db202017 | 747 | int di = 0; |
lloydg | 2:2033db202017 | 748 | |
lloydg | 2:2033db202017 | 749 | dx = x1-x0; |
lloydg | 2:2033db202017 | 750 | dy = y1-y0; |
lloydg | 2:2033db202017 | 751 | |
lloydg | 2:2033db202017 | 752 | if (dx == 0) { /* vertical line */ |
lloydg | 2:2033db202017 | 753 | if (y1 > y0) vline(x0,y0,y1,color); |
lloydg | 2:2033db202017 | 754 | else vline(x0,y1,y0,color); |
lloydg | 2:2033db202017 | 755 | return; |
lloydg | 2:2033db202017 | 756 | } |
lloydg | 2:2033db202017 | 757 | |
lloydg | 2:2033db202017 | 758 | if (dx > 0) { |
lloydg | 2:2033db202017 | 759 | dx_sym = 1; |
lloydg | 2:2033db202017 | 760 | } else { |
lloydg | 2:2033db202017 | 761 | dx_sym = -1; |
lloydg | 2:2033db202017 | 762 | } |
lloydg | 2:2033db202017 | 763 | if (dy == 0) { /* horizontal line */ |
lloydg | 2:2033db202017 | 764 | if (x1 > x0) hline(x0,x1,y0,color); |
lloydg | 2:2033db202017 | 765 | else hline(x1,x0,y0,color); |
lloydg | 2:2033db202017 | 766 | return; |
lloydg | 2:2033db202017 | 767 | } |
lloydg | 2:2033db202017 | 768 | |
lloydg | 2:2033db202017 | 769 | if (dy > 0) { |
lloydg | 2:2033db202017 | 770 | dy_sym = 1; |
lloydg | 2:2033db202017 | 771 | } else { |
lloydg | 2:2033db202017 | 772 | dy_sym = -1; |
lloydg | 2:2033db202017 | 773 | } |
lloydg | 2:2033db202017 | 774 | |
lloydg | 2:2033db202017 | 775 | dx = dx_sym*dx; |
lloydg | 2:2033db202017 | 776 | dy = dy_sym*dy; |
lloydg | 2:2033db202017 | 777 | |
lloydg | 2:2033db202017 | 778 | dx_x2 = dx*2; |
lloydg | 2:2033db202017 | 779 | dy_x2 = dy*2; |
lloydg | 2:2033db202017 | 780 | |
lloydg | 2:2033db202017 | 781 | if (dx >= dy) { |
lloydg | 2:2033db202017 | 782 | di = dy_x2 - dx; |
lloydg | 2:2033db202017 | 783 | while (x0 != x1) { |
lloydg | 2:2033db202017 | 784 | |
lloydg | 2:2033db202017 | 785 | pixel(x0, y0, color); |
lloydg | 2:2033db202017 | 786 | x0 += dx_sym; |
lloydg | 2:2033db202017 | 787 | if (di<0) { |
lloydg | 2:2033db202017 | 788 | di += dy_x2; |
lloydg | 2:2033db202017 | 789 | } else { |
lloydg | 2:2033db202017 | 790 | di += dy_x2 - dx_x2; |
lloydg | 2:2033db202017 | 791 | y0 += dy_sym; |
lloydg | 2:2033db202017 | 792 | } |
lloydg | 2:2033db202017 | 793 | } |
lloydg | 2:2033db202017 | 794 | pixel(x0, y0, color); |
lloydg | 2:2033db202017 | 795 | } else { |
lloydg | 2:2033db202017 | 796 | di = dx_x2 - dy; |
lloydg | 2:2033db202017 | 797 | while (y0 != y1) { |
lloydg | 2:2033db202017 | 798 | pixel(x0, y0, color); |
lloydg | 2:2033db202017 | 799 | y0 += dy_sym; |
lloydg | 2:2033db202017 | 800 | if (di < 0) { |
lloydg | 2:2033db202017 | 801 | di += dx_x2; |
lloydg | 2:2033db202017 | 802 | } else { |
lloydg | 2:2033db202017 | 803 | di += dx_x2 - dy_x2; |
lloydg | 2:2033db202017 | 804 | x0 += dx_sym; |
lloydg | 2:2033db202017 | 805 | } |
lloydg | 2:2033db202017 | 806 | } |
lloydg | 2:2033db202017 | 807 | pixel(x0, y0, color); |
lloydg | 2:2033db202017 | 808 | } |
lloydg | 2:2033db202017 | 809 | //WH return; |
lloydg | 2:2033db202017 | 810 | } |
lloydg | 2:2033db202017 | 811 | |
lloydg | 2:2033db202017 | 812 | |
lloydg | 2:2033db202017 | 813 | void SPI_TFT_ILI9341::rect(int x0, int y0, int x1, int y1, int color) |
lloydg | 2:2033db202017 | 814 | { |
lloydg | 2:2033db202017 | 815 | |
lloydg | 2:2033db202017 | 816 | if (x1 > x0) hline(x0,x1,y0,color); |
lloydg | 2:2033db202017 | 817 | else hline(x1,x0,y0,color); |
lloydg | 2:2033db202017 | 818 | |
lloydg | 2:2033db202017 | 819 | if (y1 > y0) vline(x0,y0,y1,color); |
lloydg | 2:2033db202017 | 820 | else vline(x0,y1,y0,color); |
lloydg | 2:2033db202017 | 821 | |
lloydg | 2:2033db202017 | 822 | if (x1 > x0) hline(x0,x1,y1,color); |
lloydg | 2:2033db202017 | 823 | else hline(x1,x0,y1,color); |
lloydg | 2:2033db202017 | 824 | |
lloydg | 2:2033db202017 | 825 | if (y1 > y0) vline(x1,y0,y1,color); |
lloydg | 2:2033db202017 | 826 | else vline(x1,y1,y0,color); |
lloydg | 2:2033db202017 | 827 | |
lloydg | 2:2033db202017 | 828 | //WH return; |
lloydg | 2:2033db202017 | 829 | } |
lloydg | 2:2033db202017 | 830 | |
lloydg | 2:2033db202017 | 831 | |
lloydg | 2:2033db202017 | 832 | void SPI_TFT_ILI9341::fillrect(int x0, int y0, int x1, int y1, int color) |
lloydg | 2:2033db202017 | 833 | { |
lloydg | 2:2033db202017 | 834 | //sanity check |
lloydg | 2:2033db202017 | 835 | if ( x0 > x1 ) swap( int, x0, x1 ) |
lloydg | 2:2033db202017 | 836 | if ( y0 > y1 ) swap( int, y0, y1 ) |
lloydg | 2:2033db202017 | 837 | |
lloydg | 2:2033db202017 | 838 | int h = y1 - y0 + 1; |
lloydg | 2:2033db202017 | 839 | int w = x1 - x0 + 1; |
lloydg | 2:2033db202017 | 840 | int pixels = h * w; |
lloydg | 2:2033db202017 | 841 | int i; |
lloydg | 2:2033db202017 | 842 | #if (SPI_16 != 1) |
lloydg | 2:2033db202017 | 843 | int msb, lsb; |
lloydg | 2:2033db202017 | 844 | #endif |
lloydg | 2:2033db202017 | 845 | |
lloydg | 2:2033db202017 | 846 | window(x0,y0,w,h); |
lloydg | 2:2033db202017 | 847 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 848 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 849 | |
lloydg | 2:2033db202017 | 850 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 851 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 852 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 853 | |
lloydg | 2:2033db202017 | 854 | for (i = 0; i < pixels; i++) |
lloydg | 2:2033db202017 | 855 | _spi.write(color); |
lloydg | 2:2033db202017 | 856 | |
lloydg | 2:2033db202017 | 857 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 858 | #else |
lloydg | 2:2033db202017 | 859 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 860 | msb = color >> 8; |
lloydg | 2:2033db202017 | 861 | lsb = color & 0xff; |
lloydg | 2:2033db202017 | 862 | for (i = 0; i < pixels; i++){ |
lloydg | 2:2033db202017 | 863 | _spi.write(msb); |
lloydg | 2:2033db202017 | 864 | _spi.write(lsb); |
lloydg | 2:2033db202017 | 865 | } |
lloydg | 2:2033db202017 | 866 | #endif |
lloydg | 2:2033db202017 | 867 | |
lloydg | 2:2033db202017 | 868 | _cs = 1; |
lloydg | 2:2033db202017 | 869 | } |
lloydg | 2:2033db202017 | 870 | |
lloydg | 2:2033db202017 | 871 | |
lloydg | 2:2033db202017 | 872 | void SPI_TFT_ILI9341::roundrect( int x1, int y1, int x2, int y2, int color ) |
lloydg | 2:2033db202017 | 873 | { |
lloydg | 2:2033db202017 | 874 | //sanity check |
lloydg | 2:2033db202017 | 875 | if ( x1 > x2 ) swap( int, x1, x2 ) |
lloydg | 2:2033db202017 | 876 | if ( y1 > y2 ) swap( int, y1, y2 ) |
lloydg | 2:2033db202017 | 877 | |
lloydg | 2:2033db202017 | 878 | if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 ) |
lloydg | 2:2033db202017 | 879 | { |
lloydg | 2:2033db202017 | 880 | pixel( x1 + 1, y1 + 1, color ); |
lloydg | 2:2033db202017 | 881 | pixel( x2 - 1, y1 + 1, color ); |
lloydg | 2:2033db202017 | 882 | pixel( x1 + 1, y2 - 1, color ); |
lloydg | 2:2033db202017 | 883 | pixel( x2 - 1, y2 - 1, color ); |
lloydg | 2:2033db202017 | 884 | //x0, x1, y |
lloydg | 2:2033db202017 | 885 | hline( x1 + 2, x2 - 2 ,y1, color ); |
lloydg | 2:2033db202017 | 886 | hline( x1 + 2, x2 - 2, y2, color ); |
lloydg | 2:2033db202017 | 887 | //y0, y1, x |
lloydg | 2:2033db202017 | 888 | vline( y1 + 2, y2 - 2, x1, color ); |
lloydg | 2:2033db202017 | 889 | vline( y1 + 2, y2 - 2, x2, color ); |
lloydg | 2:2033db202017 | 890 | } |
lloydg | 2:2033db202017 | 891 | } |
lloydg | 2:2033db202017 | 892 | |
lloydg | 2:2033db202017 | 893 | |
lloydg | 2:2033db202017 | 894 | void SPI_TFT_ILI9341::fillroundrect( int x1, int y1, int x2, int y2, int color ) |
lloydg | 2:2033db202017 | 895 | { |
lloydg | 2:2033db202017 | 896 | //sanity check |
lloydg | 2:2033db202017 | 897 | if ( x1 > x2 ) swap( int, x1, x2 ) |
lloydg | 2:2033db202017 | 898 | if ( y1 > y2 ) swap( int, y1, y2 ) |
lloydg | 2:2033db202017 | 899 | |
lloydg | 2:2033db202017 | 900 | if ( ( x2 - x1 ) > 4 && ( y2 - y1 ) > 4 ) |
lloydg | 2:2033db202017 | 901 | { |
lloydg | 2:2033db202017 | 902 | for ( int i = 0; i < ( ( y2 - y1 ) / 2 ) + 1; i++ ) |
lloydg | 2:2033db202017 | 903 | { |
lloydg | 2:2033db202017 | 904 | switch ( i ) |
lloydg | 2:2033db202017 | 905 | { |
lloydg | 2:2033db202017 | 906 | case 0: |
lloydg | 2:2033db202017 | 907 | hline( x1 + 2, x2 - 2, y1 + i, color ); |
lloydg | 2:2033db202017 | 908 | hline( x1 + 2, x2 - 2, y2 - i, color ); |
lloydg | 2:2033db202017 | 909 | break; |
lloydg | 2:2033db202017 | 910 | |
lloydg | 2:2033db202017 | 911 | case 1: |
lloydg | 2:2033db202017 | 912 | hline( x1 + 1, x2 - 1, y1 + i, color ); |
lloydg | 2:2033db202017 | 913 | hline( x1 + 1, x2 - 1, y2 - i, color ); |
lloydg | 2:2033db202017 | 914 | break; |
lloydg | 2:2033db202017 | 915 | |
lloydg | 2:2033db202017 | 916 | default: |
lloydg | 2:2033db202017 | 917 | hline( x1, x2, y1 + i, color ); |
lloydg | 2:2033db202017 | 918 | hline( x1, x2, y2 - i, color ); |
lloydg | 2:2033db202017 | 919 | break; |
lloydg | 2:2033db202017 | 920 | } |
lloydg | 2:2033db202017 | 921 | } |
lloydg | 2:2033db202017 | 922 | } |
lloydg | 2:2033db202017 | 923 | } |
lloydg | 2:2033db202017 | 924 | |
lloydg | 2:2033db202017 | 925 | |
lloydg | 2:2033db202017 | 926 | |
lloydg | 2:2033db202017 | 927 | void SPI_TFT_ILI9341::locate(int x, int y) |
lloydg | 2:2033db202017 | 928 | { |
lloydg | 2:2033db202017 | 929 | _char_x = x; |
lloydg | 2:2033db202017 | 930 | _char_y = y; |
lloydg | 2:2033db202017 | 931 | } |
lloydg | 2:2033db202017 | 932 | |
lloydg | 2:2033db202017 | 933 | |
lloydg | 2:2033db202017 | 934 | |
lloydg | 2:2033db202017 | 935 | int SPI_TFT_ILI9341::columns() |
lloydg | 2:2033db202017 | 936 | { |
lloydg | 2:2033db202017 | 937 | return width() / _font[1]; |
lloydg | 2:2033db202017 | 938 | } |
lloydg | 2:2033db202017 | 939 | |
lloydg | 2:2033db202017 | 940 | |
lloydg | 2:2033db202017 | 941 | |
lloydg | 2:2033db202017 | 942 | int SPI_TFT_ILI9341::rows() |
lloydg | 2:2033db202017 | 943 | { |
lloydg | 2:2033db202017 | 944 | return height() / _font[2]; |
lloydg | 2:2033db202017 | 945 | } |
lloydg | 2:2033db202017 | 946 | |
lloydg | 2:2033db202017 | 947 | |
lloydg | 2:2033db202017 | 948 | |
lloydg | 2:2033db202017 | 949 | int SPI_TFT_ILI9341::_putc(int value) |
lloydg | 2:2033db202017 | 950 | { |
lloydg | 2:2033db202017 | 951 | if (value == '\n') { // new line |
lloydg | 2:2033db202017 | 952 | _char_x = 0; |
lloydg | 2:2033db202017 | 953 | _char_y = _char_y + _font[2]; |
lloydg | 2:2033db202017 | 954 | if (_char_y >= height() - _font[2]) { |
lloydg | 2:2033db202017 | 955 | _char_y = 0; |
lloydg | 2:2033db202017 | 956 | } |
lloydg | 2:2033db202017 | 957 | } else { |
lloydg | 2:2033db202017 | 958 | character(_char_x, _char_y, value); |
lloydg | 2:2033db202017 | 959 | } |
lloydg | 2:2033db202017 | 960 | return value; |
lloydg | 2:2033db202017 | 961 | } |
lloydg | 2:2033db202017 | 962 | |
lloydg | 2:2033db202017 | 963 | //WH |
lloydg | 2:2033db202017 | 964 | #if(0) |
lloydg | 2:2033db202017 | 965 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
lloydg | 2:2033db202017 | 966 | { |
lloydg | 2:2033db202017 | 967 | unsigned int hor,vert,offset,bpl,j,i,b; |
lloydg | 2:2033db202017 | 968 | unsigned char* zeichen; |
lloydg | 2:2033db202017 | 969 | unsigned char z,w; |
lloydg | 2:2033db202017 | 970 | |
lloydg | 2:2033db202017 | 971 | if ((c < 31) || (c > 127)) return; // test char range |
lloydg | 2:2033db202017 | 972 | |
lloydg | 2:2033db202017 | 973 | // read font parameter from start of array |
lloydg | 2:2033db202017 | 974 | offset = _font[0]; // bytes / char |
lloydg | 2:2033db202017 | 975 | hor = _font[1]; // get hor size of font |
lloydg | 2:2033db202017 | 976 | vert = _font[2]; // get vert size of font |
lloydg | 2:2033db202017 | 977 | bpl = _font[3]; // bytes per line |
lloydg | 2:2033db202017 | 978 | |
lloydg | 2:2033db202017 | 979 | if (_char_x + hor > width()) { |
lloydg | 2:2033db202017 | 980 | _char_x = 0; |
lloydg | 2:2033db202017 | 981 | _char_y = _char_y + vert; |
lloydg | 2:2033db202017 | 982 | if (_char_y >= height() - _font[2]) { |
lloydg | 2:2033db202017 | 983 | _char_y = 0; |
lloydg | 2:2033db202017 | 984 | } |
lloydg | 2:2033db202017 | 985 | } |
lloydg | 2:2033db202017 | 986 | window(_char_x, _char_y,hor,vert); // char box |
lloydg | 2:2033db202017 | 987 | wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 988 | #ifndef TARGET_KL25Z // 16 Bit SPI |
lloydg | 2:2033db202017 | 989 | _spi.format(16,0); |
lloydg | 2:2033db202017 | 990 | #endif // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 991 | zeichen = &_font[((c -32) * offset) + 4]; // start of char bitmap |
lloydg | 2:2033db202017 | 992 | w = zeichen[0]; // width of actual char |
lloydg | 2:2033db202017 | 993 | for (j=0; j<vert; j++) { // vert line |
lloydg | 2:2033db202017 | 994 | for (i=0; i<hor; i++) { // horz line |
lloydg | 2:2033db202017 | 995 | z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; |
lloydg | 2:2033db202017 | 996 | b = 1 << (j & 0x07); |
lloydg | 2:2033db202017 | 997 | if (( z & b ) == 0x00) { |
lloydg | 2:2033db202017 | 998 | #ifndef TARGET_KL25Z // 16 Bit SPI |
lloydg | 2:2033db202017 | 999 | _spi.write(_background); |
lloydg | 2:2033db202017 | 1000 | #else |
lloydg | 2:2033db202017 | 1001 | _spi.write(_background >> 8); |
lloydg | 2:2033db202017 | 1002 | _spi.write(_background & 0xff); |
lloydg | 2:2033db202017 | 1003 | #endif |
lloydg | 2:2033db202017 | 1004 | } else { |
lloydg | 2:2033db202017 | 1005 | #ifndef TARGET_KL25Z // 16 Bit SPI |
lloydg | 2:2033db202017 | 1006 | _spi.write(_foreground); |
lloydg | 2:2033db202017 | 1007 | #else |
lloydg | 2:2033db202017 | 1008 | _spi.write(_foreground >> 8); |
lloydg | 2:2033db202017 | 1009 | _spi.write(_foreground & 0xff); |
lloydg | 2:2033db202017 | 1010 | #endif |
lloydg | 2:2033db202017 | 1011 | } |
lloydg | 2:2033db202017 | 1012 | } |
lloydg | 2:2033db202017 | 1013 | } |
lloydg | 2:2033db202017 | 1014 | _cs = 1; |
lloydg | 2:2033db202017 | 1015 | #ifndef TARGET_KL25Z // 16 Bit SPI |
lloydg | 2:2033db202017 | 1016 | _spi.format(8,0); |
lloydg | 2:2033db202017 | 1017 | #endif |
lloydg | 2:2033db202017 | 1018 | window_max(); |
lloydg | 2:2033db202017 | 1019 | if ((w + 2) < hor) { // x offset to next char |
lloydg | 2:2033db202017 | 1020 | __char_x += w + 2; |
lloydg | 2:2033db202017 | 1021 | } else __char_x += hor; |
lloydg | 2:2033db202017 | 1022 | } |
lloydg | 2:2033db202017 | 1023 | #else |
lloydg | 2:2033db202017 | 1024 | |
lloydg | 2:2033db202017 | 1025 | #if (TRANSPARANCY == 1) |
lloydg | 2:2033db202017 | 1026 | //WH write foreground, write background only when not transparant mode |
lloydg | 2:2033db202017 | 1027 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
lloydg | 2:2033db202017 | 1028 | { |
lloydg | 2:2033db202017 | 1029 | unsigned int hor,vert,offset,bpl,j,i,b; |
lloydg | 2:2033db202017 | 1030 | unsigned char* symbol; |
lloydg | 2:2033db202017 | 1031 | unsigned char z,w; |
lloydg | 2:2033db202017 | 1032 | |
lloydg | 2:2033db202017 | 1033 | if ((c < 31) || (c > 127)) return; // test char range |
lloydg | 2:2033db202017 | 1034 | |
lloydg | 2:2033db202017 | 1035 | // read font parameter from start of array |
lloydg | 2:2033db202017 | 1036 | offset = _font[0]; // bytes / char |
lloydg | 2:2033db202017 | 1037 | hor = _font[1]; // get hor size of font |
lloydg | 2:2033db202017 | 1038 | vert = _font[2]; // get vert size of font |
lloydg | 2:2033db202017 | 1039 | bpl = _font[3]; // bytes per line |
lloydg | 2:2033db202017 | 1040 | |
lloydg | 2:2033db202017 | 1041 | if (_char_x + hor > width()) { |
lloydg | 2:2033db202017 | 1042 | _char_x = 0; |
lloydg | 2:2033db202017 | 1043 | _char_y = _char_y + vert; |
lloydg | 2:2033db202017 | 1044 | if (_char_y >= height() - vert) { |
lloydg | 2:2033db202017 | 1045 | _char_y = 0; |
lloydg | 2:2033db202017 | 1046 | } |
lloydg | 2:2033db202017 | 1047 | } |
lloydg | 2:2033db202017 | 1048 | |
lloydg | 2:2033db202017 | 1049 | symbol = &_font[((c - 32) * offset) + 4]; // start of char bitmap |
lloydg | 2:2033db202017 | 1050 | w = symbol[0]; // width of actual char (proportional font) |
lloydg | 2:2033db202017 | 1051 | for (j=0; j<vert; j++) { // vert line |
lloydg | 2:2033db202017 | 1052 | for (i=0; i<hor; i++) { // horz line |
lloydg | 2:2033db202017 | 1053 | z = symbol[bpl * i + ((j & 0xF8) >> 3) + 1]; |
lloydg | 2:2033db202017 | 1054 | b = 1 << (j & 0x07); |
lloydg | 2:2033db202017 | 1055 | |
lloydg | 2:2033db202017 | 1056 | // Test bit in character bitmap to write either _foreground or _background color |
lloydg | 2:2033db202017 | 1057 | if (( z & b ) == 0x00) { |
lloydg | 2:2033db202017 | 1058 | // bit is 0, write _background color |
lloydg | 2:2033db202017 | 1059 | if (!_transparancy) { // write background color only when transparancy is 'off' |
lloydg | 2:2033db202017 | 1060 | pixel(_char_x+i, _char_y+j, _background); |
lloydg | 2:2033db202017 | 1061 | } |
lloydg | 2:2033db202017 | 1062 | } |
lloydg | 2:2033db202017 | 1063 | else { |
lloydg | 2:2033db202017 | 1064 | // bit is 1, write _foreground color |
lloydg | 2:2033db202017 | 1065 | pixel(_char_x+i, _char_y+j, _foreground); |
lloydg | 2:2033db202017 | 1066 | } // if bit |
lloydg | 2:2033db202017 | 1067 | } // for i |
lloydg | 2:2033db202017 | 1068 | } // for j |
lloydg | 2:2033db202017 | 1069 | |
lloydg | 2:2033db202017 | 1070 | window_max(); |
lloydg | 2:2033db202017 | 1071 | |
lloydg | 2:2033db202017 | 1072 | if ((w + 2) < hor) { // x offset to next char |
lloydg | 2:2033db202017 | 1073 | _char_x += w + 2; |
lloydg | 2:2033db202017 | 1074 | } else _char_x += hor; |
lloydg | 2:2033db202017 | 1075 | } |
lloydg | 2:2033db202017 | 1076 | |
lloydg | 2:2033db202017 | 1077 | #else |
lloydg | 2:2033db202017 | 1078 | |
lloydg | 2:2033db202017 | 1079 | //WH write foreground and background |
lloydg | 2:2033db202017 | 1080 | void SPI_TFT_ILI9341::character(int x, int y, int c) |
lloydg | 2:2033db202017 | 1081 | { |
lloydg | 2:2033db202017 | 1082 | unsigned int hor,vert,offset,bpl,j,i,b; |
lloydg | 2:2033db202017 | 1083 | unsigned char* symbol; |
lloydg | 2:2033db202017 | 1084 | unsigned char z,w; |
lloydg | 2:2033db202017 | 1085 | |
lloydg | 2:2033db202017 | 1086 | if ((c < 31) || (c > 127)) return; // test char range |
lloydg | 2:2033db202017 | 1087 | |
lloydg | 2:2033db202017 | 1088 | // read font parameter from start of array |
lloydg | 2:2033db202017 | 1089 | offset = _font[0]; // bytes / char |
lloydg | 2:2033db202017 | 1090 | hor = _font[1]; // get hor size of font |
lloydg | 2:2033db202017 | 1091 | vert = _font[2]; // get vert size of font |
lloydg | 2:2033db202017 | 1092 | bpl = _font[3]; // bytes per line |
lloydg | 2:2033db202017 | 1093 | |
lloydg | 2:2033db202017 | 1094 | if (_char_x + hor > width()) { |
lloydg | 2:2033db202017 | 1095 | _char_x = 0; |
lloydg | 2:2033db202017 | 1096 | _char_y = _char_y + vert; |
lloydg | 2:2033db202017 | 1097 | if (_char_y >= height() - vert) { |
lloydg | 2:2033db202017 | 1098 | _char_y = 0; |
lloydg | 2:2033db202017 | 1099 | } |
lloydg | 2:2033db202017 | 1100 | } |
lloydg | 2:2033db202017 | 1101 | window(_char_x, _char_y, hor, vert); // char box |
lloydg | 2:2033db202017 | 1102 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 1103 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 1104 | |
lloydg | 2:2033db202017 | 1105 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1106 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 1107 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 1108 | #endif |
lloydg | 2:2033db202017 | 1109 | symbol = &_font[((c - 32) * offset) + 4]; // start of char bitmap |
lloydg | 2:2033db202017 | 1110 | w = symbol[0]; // width of actual char (proportional font) |
lloydg | 2:2033db202017 | 1111 | for (j=0; j<vert; j++) { // vert line |
lloydg | 2:2033db202017 | 1112 | for (i=0; i<hor; i++) { // horz line |
lloydg | 2:2033db202017 | 1113 | z = symbol[bpl * i + ((j & 0xF8) >> 3) + 1]; |
lloydg | 2:2033db202017 | 1114 | b = 1 << (j & 0x07); |
lloydg | 2:2033db202017 | 1115 | |
lloydg | 2:2033db202017 | 1116 | // Test bit in character bitmap to write either _foreground or _background color |
lloydg | 2:2033db202017 | 1117 | if (( z & b ) == 0x00) { |
lloydg | 2:2033db202017 | 1118 | // bit is 0, write _background color |
lloydg | 2:2033db202017 | 1119 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1120 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 1121 | _spi.write(_background); |
lloydg | 2:2033db202017 | 1122 | #else |
lloydg | 2:2033db202017 | 1123 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 1124 | _spi.write(_background >> 8); |
lloydg | 2:2033db202017 | 1125 | _spi.write(_background & 0xff); |
lloydg | 2:2033db202017 | 1126 | #endif |
lloydg | 2:2033db202017 | 1127 | } |
lloydg | 2:2033db202017 | 1128 | else { |
lloydg | 2:2033db202017 | 1129 | // bit is 1, write _foreground color |
lloydg | 2:2033db202017 | 1130 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1131 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 1132 | _spi.write(_foreground); |
lloydg | 2:2033db202017 | 1133 | #else |
lloydg | 2:2033db202017 | 1134 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 1135 | _spi.write(_foreground >> 8); |
lloydg | 2:2033db202017 | 1136 | _spi.write(_foreground & 0xff); |
lloydg | 2:2033db202017 | 1137 | #endif |
lloydg | 2:2033db202017 | 1138 | } // if bit |
lloydg | 2:2033db202017 | 1139 | } // for i |
lloydg | 2:2033db202017 | 1140 | } // for j |
lloydg | 2:2033db202017 | 1141 | |
lloydg | 2:2033db202017 | 1142 | _cs = 1; |
lloydg | 2:2033db202017 | 1143 | #if (SPI_16 == 1) // 16 Bit SPI |
lloydg | 2:2033db202017 | 1144 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 1145 | #endif |
lloydg | 2:2033db202017 | 1146 | window_max(); |
lloydg | 2:2033db202017 | 1147 | |
lloydg | 2:2033db202017 | 1148 | if ((w + 2) < hor) { // x offset to next char |
lloydg | 2:2033db202017 | 1149 | _char_x += w + 2; |
lloydg | 2:2033db202017 | 1150 | } else _char_x += hor; |
lloydg | 2:2033db202017 | 1151 | } |
lloydg | 2:2033db202017 | 1152 | #endif |
lloydg | 2:2033db202017 | 1153 | |
lloydg | 2:2033db202017 | 1154 | #endif |
lloydg | 2:2033db202017 | 1155 | |
lloydg | 2:2033db202017 | 1156 | |
lloydg | 2:2033db202017 | 1157 | void SPI_TFT_ILI9341::set_font(unsigned char* f) |
lloydg | 2:2033db202017 | 1158 | { |
lloydg | 2:2033db202017 | 1159 | _font = f; |
lloydg | 2:2033db202017 | 1160 | } |
lloydg | 2:2033db202017 | 1161 | |
lloydg | 2:2033db202017 | 1162 | |
lloydg | 2:2033db202017 | 1163 | |
lloydg | 2:2033db202017 | 1164 | void SPI_TFT_ILI9341::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) |
lloydg | 2:2033db202017 | 1165 | { |
lloydg | 2:2033db202017 | 1166 | unsigned int j; |
lloydg | 2:2033db202017 | 1167 | int padd; |
lloydg | 2:2033db202017 | 1168 | unsigned short *bitmap_ptr = (unsigned short *)bitmap; |
lloydg | 2:2033db202017 | 1169 | #if (SPI_16 != 1) // 16 Bit SPI |
lloydg | 2:2033db202017 | 1170 | unsigned short pix_temp; |
lloydg | 2:2033db202017 | 1171 | #endif |
lloydg | 2:2033db202017 | 1172 | |
lloydg | 2:2033db202017 | 1173 | unsigned int i; |
lloydg | 2:2033db202017 | 1174 | |
lloydg | 2:2033db202017 | 1175 | // the lines are padded to multiple of 4 bytes in a bitmap |
lloydg | 2:2033db202017 | 1176 | padd = -1; |
lloydg | 2:2033db202017 | 1177 | do { |
lloydg | 2:2033db202017 | 1178 | padd ++; |
lloydg | 2:2033db202017 | 1179 | } while (2*(w + padd)%4 != 0); |
lloydg | 2:2033db202017 | 1180 | window(x, y, w, h); |
lloydg | 2:2033db202017 | 1181 | bitmap_ptr += ((h - 1)* (w + padd)); |
lloydg | 2:2033db202017 | 1182 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 1183 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 1184 | |
lloydg | 2:2033db202017 | 1185 | #if (SPI_16 == 1) // 16 Bit SPI |
lloydg | 2:2033db202017 | 1186 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 1187 | #endif |
lloydg | 2:2033db202017 | 1188 | for (j = 0; j < h; j++) { //Lines |
lloydg | 2:2033db202017 | 1189 | for (i = 0; i < w; i++) { // one line |
lloydg | 2:2033db202017 | 1190 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1191 | // 16 Bit SPI |
lloydg | 2:2033db202017 | 1192 | _spi.write(*bitmap_ptr); // one line |
lloydg | 2:2033db202017 | 1193 | bitmap_ptr++; |
lloydg | 2:2033db202017 | 1194 | #else |
lloydg | 2:2033db202017 | 1195 | // 8 Bit SPI |
lloydg | 2:2033db202017 | 1196 | pix_temp = *bitmap_ptr; |
lloydg | 2:2033db202017 | 1197 | _spi.write(pix_temp >> 8); |
lloydg | 2:2033db202017 | 1198 | _spi.write(pix_temp); |
lloydg | 2:2033db202017 | 1199 | bitmap_ptr++; |
lloydg | 2:2033db202017 | 1200 | #endif |
lloydg | 2:2033db202017 | 1201 | } |
lloydg | 2:2033db202017 | 1202 | bitmap_ptr -= 2*w; |
lloydg | 2:2033db202017 | 1203 | bitmap_ptr -= padd; |
lloydg | 2:2033db202017 | 1204 | } |
lloydg | 2:2033db202017 | 1205 | _cs = 1; |
lloydg | 2:2033db202017 | 1206 | |
lloydg | 2:2033db202017 | 1207 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1208 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 1209 | #endif |
lloydg | 2:2033db202017 | 1210 | window_max(); |
lloydg | 2:2033db202017 | 1211 | } |
lloydg | 2:2033db202017 | 1212 | |
lloydg | 2:2033db202017 | 1213 | |
lloydg | 2:2033db202017 | 1214 | // local filesystem is not implemented in kinetis board, but you can add an SD card |
lloydg | 2:2033db202017 | 1215 | |
lloydg | 2:2033db202017 | 1216 | int SPI_TFT_ILI9341::BMP_16(unsigned int x, unsigned int y, const char *Name_BMP) |
lloydg | 2:2033db202017 | 1217 | { |
lloydg | 2:2033db202017 | 1218 | |
lloydg | 2:2033db202017 | 1219 | #define OffsetPixelWidth 18 |
lloydg | 2:2033db202017 | 1220 | #define OffsetPixelHeigh 22 |
lloydg | 2:2033db202017 | 1221 | #define OffsetFileSize 34 |
lloydg | 2:2033db202017 | 1222 | #define OffsetPixData 10 |
lloydg | 2:2033db202017 | 1223 | #define OffsetBPP 28 |
lloydg | 2:2033db202017 | 1224 | |
lloydg | 2:2033db202017 | 1225 | char filename[50]; |
lloydg | 2:2033db202017 | 1226 | unsigned char BMP_Header[54]; |
lloydg | 2:2033db202017 | 1227 | unsigned short BPP_t; |
lloydg | 2:2033db202017 | 1228 | unsigned int PixelWidth,PixelHeigh,start_data; |
lloydg | 2:2033db202017 | 1229 | unsigned int i,off; |
lloydg | 2:2033db202017 | 1230 | int padd,j; |
lloydg | 2:2033db202017 | 1231 | unsigned short *line; |
lloydg | 2:2033db202017 | 1232 | |
lloydg | 2:2033db202017 | 1233 | // get the filename |
lloydg | 2:2033db202017 | 1234 | i=0; |
lloydg | 2:2033db202017 | 1235 | while (*Name_BMP!='\0') { |
lloydg | 2:2033db202017 | 1236 | filename[i++]=*Name_BMP++; |
lloydg | 2:2033db202017 | 1237 | } |
lloydg | 2:2033db202017 | 1238 | filename[i] = 0; |
lloydg | 2:2033db202017 | 1239 | |
lloydg | 2:2033db202017 | 1240 | FILE *Image = fopen((const char *)&filename[0], "rb"); // open the bmp file |
lloydg | 2:2033db202017 | 1241 | if (!Image) { |
lloydg | 2:2033db202017 | 1242 | return(0); // error file not found ! |
lloydg | 2:2033db202017 | 1243 | } |
lloydg | 2:2033db202017 | 1244 | |
lloydg | 2:2033db202017 | 1245 | fread(&BMP_Header[0],1,54,Image); // get the BMP Header |
lloydg | 2:2033db202017 | 1246 | |
lloydg | 2:2033db202017 | 1247 | if (BMP_Header[0] != 0x42 || BMP_Header[1] != 0x4D) { // check magic byte |
lloydg | 2:2033db202017 | 1248 | fclose(Image); |
lloydg | 2:2033db202017 | 1249 | return(-1); // error no BMP file |
lloydg | 2:2033db202017 | 1250 | } |
lloydg | 2:2033db202017 | 1251 | |
lloydg | 2:2033db202017 | 1252 | BPP_t = BMP_Header[OffsetBPP] + (BMP_Header[OffsetBPP + 1] << 8); |
lloydg | 2:2033db202017 | 1253 | if (BPP_t != 0x0010) { |
lloydg | 2:2033db202017 | 1254 | fclose(Image); |
lloydg | 2:2033db202017 | 1255 | return(-2); // error no 16 bit BMP |
lloydg | 2:2033db202017 | 1256 | } |
lloydg | 2:2033db202017 | 1257 | |
lloydg | 2:2033db202017 | 1258 | PixelHeigh = BMP_Header[OffsetPixelHeigh] + (BMP_Header[OffsetPixelHeigh + 1] << 8) + (BMP_Header[OffsetPixelHeigh + 2] << 16) + (BMP_Header[OffsetPixelHeigh + 3] << 24); |
lloydg | 2:2033db202017 | 1259 | PixelWidth = BMP_Header[OffsetPixelWidth] + (BMP_Header[OffsetPixelWidth + 1] << 8) + (BMP_Header[OffsetPixelWidth + 2] << 16) + (BMP_Header[OffsetPixelWidth + 3] << 24); |
lloydg | 2:2033db202017 | 1260 | if (PixelHeigh > height() + y || PixelWidth > width() + x) { |
lloydg | 2:2033db202017 | 1261 | fclose(Image); |
lloydg | 2:2033db202017 | 1262 | return(-3); // to big |
lloydg | 2:2033db202017 | 1263 | } |
lloydg | 2:2033db202017 | 1264 | |
lloydg | 2:2033db202017 | 1265 | start_data = BMP_Header[OffsetPixData] + (BMP_Header[OffsetPixData + 1] << 8) + (BMP_Header[OffsetPixData + 2] << 16) + (BMP_Header[OffsetPixData + 3] << 24); |
lloydg | 2:2033db202017 | 1266 | |
lloydg | 2:2033db202017 | 1267 | line = (unsigned short *) malloc (2 * PixelWidth); // we need a buffer for a line |
lloydg | 2:2033db202017 | 1268 | if (line == NULL) { |
lloydg | 2:2033db202017 | 1269 | return(-4); // error no memory |
lloydg | 2:2033db202017 | 1270 | } |
lloydg | 2:2033db202017 | 1271 | |
lloydg | 2:2033db202017 | 1272 | // the bmp lines are padded to multiple of 4 bytes |
lloydg | 2:2033db202017 | 1273 | padd = -1; |
lloydg | 2:2033db202017 | 1274 | do { |
lloydg | 2:2033db202017 | 1275 | padd ++; |
lloydg | 2:2033db202017 | 1276 | } while ((PixelWidth * 2 + padd)%4 != 0); |
lloydg | 2:2033db202017 | 1277 | |
lloydg | 2:2033db202017 | 1278 | window(x, y,PixelWidth ,PixelHeigh); |
lloydg | 2:2033db202017 | 1279 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 1280 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 1281 | |
lloydg | 2:2033db202017 | 1282 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1283 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 1284 | #endif |
lloydg | 2:2033db202017 | 1285 | for (j = PixelHeigh - 1; j >= 0; j--) { //Lines bottom up |
lloydg | 2:2033db202017 | 1286 | off = j * (PixelWidth * 2 + padd) + start_data; // start of line |
lloydg | 2:2033db202017 | 1287 | fseek(Image, off ,SEEK_SET); |
lloydg | 2:2033db202017 | 1288 | fread(line,1,PixelWidth * 2,Image); // read a line - slow |
lloydg | 2:2033db202017 | 1289 | for (i = 0; i < PixelWidth; i++) { // copy pixel data to TFT |
lloydg | 2:2033db202017 | 1290 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1291 | // one 16 bit pixel |
lloydg | 2:2033db202017 | 1292 | _spi.write(line[i]); |
lloydg | 2:2033db202017 | 1293 | #else |
lloydg | 2:2033db202017 | 1294 | // only 8 Bit SPI |
lloydg | 2:2033db202017 | 1295 | _spi.write(line[i] >> 8); |
lloydg | 2:2033db202017 | 1296 | _spi.write(line[i]); |
lloydg | 2:2033db202017 | 1297 | #endif |
lloydg | 2:2033db202017 | 1298 | } |
lloydg | 2:2033db202017 | 1299 | } |
lloydg | 2:2033db202017 | 1300 | _cs = 1; |
lloydg | 2:2033db202017 | 1301 | |
lloydg | 2:2033db202017 | 1302 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1303 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 1304 | #endif |
lloydg | 2:2033db202017 | 1305 | |
lloydg | 2:2033db202017 | 1306 | free (line); |
lloydg | 2:2033db202017 | 1307 | fclose(Image); |
lloydg | 2:2033db202017 | 1308 | window_max(); |
lloydg | 2:2033db202017 | 1309 | return(1); |
lloydg | 2:2033db202017 | 1310 | } |
lloydg | 2:2033db202017 | 1311 | |
lloydg | 2:2033db202017 | 1312 | |
lloydg | 2:2033db202017 | 1313 | /******************************************************************************* |
lloydg | 2:2033db202017 | 1314 | * Function Name : WriteBMP_FAT |
lloydg | 2:2033db202017 | 1315 | * @brief Displays a bitmap picture loaded in Flash. |
lloydg | 2:2033db202017 | 1316 | * @param Xpos: specifies the X position. |
lloydg | 2:2033db202017 | 1317 | * @param Ypos: specifies the Y position. |
lloydg | 2:2033db202017 | 1318 | * @param BmpAddress: Bmp picture address in Flash. |
lloydg | 2:2033db202017 | 1319 | * @return None |
lloydg | 2:2033db202017 | 1320 | *******************************************************************************/ |
lloydg | 2:2033db202017 | 1321 | void SPI_TFT_ILI9341::WriteBMP_FAT(uint16_t Xpos, uint16_t Ypos, const char* BmpName) |
lloydg | 2:2033db202017 | 1322 | { |
lloydg | 2:2033db202017 | 1323 | uint32_t index = 0, size = 0, width=0, height=0; |
lloydg | 2:2033db202017 | 1324 | uint16_t *pBmpWord=0; |
lloydg | 2:2033db202017 | 1325 | // uint16_t data; |
lloydg | 2:2033db202017 | 1326 | |
lloydg | 2:2033db202017 | 1327 | /* Read bitmap width*/ |
lloydg | 2:2033db202017 | 1328 | width = BmpName[0]+1; |
lloydg | 2:2033db202017 | 1329 | /* Read bitmap height*/ |
lloydg | 2:2033db202017 | 1330 | height = BmpName[1]+1; |
lloydg | 2:2033db202017 | 1331 | /* Read bitmap size */ |
lloydg | 2:2033db202017 | 1332 | size = width * height; /* nb of 16 bits */ |
lloydg | 2:2033db202017 | 1333 | |
lloydg | 2:2033db202017 | 1334 | window(Xpos, Ypos, width , height); |
lloydg | 2:2033db202017 | 1335 | |
lloydg | 2:2033db202017 | 1336 | // wr_cmd(0x2C); // send pixel |
lloydg | 2:2033db202017 | 1337 | wr_cmd(ILI9341_GRAM); // send pixel |
lloydg | 2:2033db202017 | 1338 | |
lloydg | 2:2033db202017 | 1339 | /* Set WRX to send data */ |
lloydg | 2:2033db202017 | 1340 | //WH _dc = 1; |
lloydg | 2:2033db202017 | 1341 | |
lloydg | 2:2033db202017 | 1342 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1343 | _spi.format(16,0); // switch to 16 bit Mode 0 |
lloydg | 2:2033db202017 | 1344 | #endif |
lloydg | 2:2033db202017 | 1345 | |
lloydg | 2:2033db202017 | 1346 | pBmpWord = (uint16_t *) (&BmpName[5]); |
lloydg | 2:2033db202017 | 1347 | /* Send to the screen */ |
lloydg | 2:2033db202017 | 1348 | for(index = 0; index < size; index++) |
lloydg | 2:2033db202017 | 1349 | { |
lloydg | 2:2033db202017 | 1350 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1351 | // one 16 bit pixel |
lloydg | 2:2033db202017 | 1352 | _spi.write(*pBmpWord); |
lloydg | 2:2033db202017 | 1353 | #else |
lloydg | 2:2033db202017 | 1354 | // only 8 Bit SPI |
lloydg | 2:2033db202017 | 1355 | _spi.write(*pBmpWord & 0xFF); |
lloydg | 2:2033db202017 | 1356 | _spi.write((*pBmpWord>>8) & 0xFF); |
lloydg | 2:2033db202017 | 1357 | #endif |
lloydg | 2:2033db202017 | 1358 | |
lloydg | 2:2033db202017 | 1359 | pBmpWord++; |
lloydg | 2:2033db202017 | 1360 | } |
lloydg | 2:2033db202017 | 1361 | |
lloydg | 2:2033db202017 | 1362 | /* Set LCD control line(/CS) */ |
lloydg | 2:2033db202017 | 1363 | _cs = 1; |
lloydg | 2:2033db202017 | 1364 | |
lloydg | 2:2033db202017 | 1365 | #if (SPI_16 == 1) |
lloydg | 2:2033db202017 | 1366 | _spi.format(8,0); // switch back to 8 bit Mode 0 |
lloydg | 2:2033db202017 | 1367 | #endif |
lloydg | 2:2033db202017 | 1368 | |
lloydg | 2:2033db202017 | 1369 | window_max(); |
lloydg | 2:2033db202017 | 1370 | } |
lloydg | 2:2033db202017 | 1371 | |
lloydg | 2:2033db202017 | 1372 |