.

Committer:
Geremia
Date:
Thu Feb 19 00:33:27 2015 +0000
Revision:
10:668cf78ff93a
Parent:
8:26757296c79d
Child:
11:b842b8e332cb
Added FastWindow for TFT (truncated set page/column cmds), which increases speed when plotting single pixels (around 20-25% faster?!?!)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 4:12ba0ecc2c1f 1 /* mbed UniGraphic library - universal TFT driver class
Geremia 4:12ba0ecc2c1f 2 * Copyright (c) 2015 Giuliano Dianda
Geremia 4:12ba0ecc2c1f 3 * Released under the MIT License: http://mbed.org/license/mit
Geremia 4:12ba0ecc2c1f 4 *
Geremia 4:12ba0ecc2c1f 5 * Derived work of:
Geremia 4:12ba0ecc2c1f 6 *
Geremia 4:12ba0ecc2c1f 7 * mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
Geremia 3:48f3282c2be8 8 * Copyright (c) 2013 Peter Drescher - DC2PD
Geremia 3:48f3282c2be8 9 *
Geremia 3:48f3282c2be8 10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Geremia 3:48f3282c2be8 11 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Geremia 3:48f3282c2be8 12 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Geremia 3:48f3282c2be8 13 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Geremia 3:48f3282c2be8 14 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Geremia 3:48f3282c2be8 15 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Geremia 3:48f3282c2be8 16 * THE SOFTWARE.
Geremia 3:48f3282c2be8 17 */
Geremia 3:48f3282c2be8 18
Geremia 2:713844a55c4e 19 #include "TFT.h"
Geremia 2:713844a55c4e 20
Geremia 2:713844a55c4e 21 //#include "mbed_debug.h"
Geremia 2:713844a55c4e 22
Geremia 2:713844a55c4e 23 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
Geremia 2:713844a55c4e 24
Geremia 2:713844a55c4e 25 TFT::TFT(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const int lcdsize_x, const int lcdsize_y, const char *name)
Geremia 7:bb0383b91104 26 : GraphicsDisplay(name), screensize_X(lcdsize_x), screensize_Y(lcdsize_y)
Geremia 2:713844a55c4e 27 {
Geremia 2:713844a55c4e 28 if(displayproto==PAR_8) proto = new PAR8(port, CS, reset, DC, WR, RD);
Geremia 4:12ba0ecc2c1f 29 else if(displayproto==PAR_16) proto = new PAR16(port, CS, reset, DC, WR, RD);
Geremia 2:713844a55c4e 30 useNOP=false;
Geremia 4:12ba0ecc2c1f 31 scrollbugfix=0;
Geremia 4:12ba0ecc2c1f 32 mipistd=false;
Geremia 2:713844a55c4e 33 set_orientation(0);
Geremia 2:713844a55c4e 34 foreground(White);
Geremia 2:713844a55c4e 35 background(Black);
Geremia 2:713844a55c4e 36 set_auto_up(false); //we don't have framebuffer
Geremia 7:bb0383b91104 37 topfixedareasize=0;
Geremia 7:bb0383b91104 38 scrollareasize=0;
Geremia 10:668cf78ff93a 39 usefastwindow=false;
Geremia 10:668cf78ff93a 40 fastwindowready=false;
Geremia 2:713844a55c4e 41 // cls();
Geremia 2:713844a55c4e 42 // locate(0,0);
Geremia 2:713844a55c4e 43 }
Geremia 2:713844a55c4e 44 TFT::TFT(proto_t displayproto, int Hz, PinName mosi, PinName miso, PinName sclk, PinName CS, PinName reset, PinName DC, const int lcdsize_x, const int lcdsize_y, const char *name)
Geremia 7:bb0383b91104 45 : GraphicsDisplay(name), screensize_X(lcdsize_x), screensize_Y(lcdsize_y)
Geremia 2:713844a55c4e 46 {
Geremia 2:713844a55c4e 47 if(displayproto==SPI_8)
Geremia 2:713844a55c4e 48 {
Geremia 2:713844a55c4e 49 proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 2:713844a55c4e 50 useNOP=false;
Geremia 2:713844a55c4e 51 }
Geremia 2:713844a55c4e 52 else if(displayproto==SPI_16)
Geremia 2:713844a55c4e 53 {
Geremia 2:713844a55c4e 54 proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 2:713844a55c4e 55 useNOP=true;
Geremia 2:713844a55c4e 56 }
Geremia 4:12ba0ecc2c1f 57 scrollbugfix=0;
Geremia 4:12ba0ecc2c1f 58 mipistd=false;
Geremia 2:713844a55c4e 59 set_orientation(0);
Geremia 2:713844a55c4e 60 foreground(White);
Geremia 2:713844a55c4e 61 background(Black);
Geremia 2:713844a55c4e 62 set_auto_up(false);
Geremia 7:bb0383b91104 63 topfixedareasize=0;
Geremia 7:bb0383b91104 64 scrollareasize=0;
Geremia 10:668cf78ff93a 65 usefastwindow=false;
Geremia 10:668cf78ff93a 66 fastwindowready=false;
Geremia 2:713844a55c4e 67 // locate(0,0);
Geremia 2:713844a55c4e 68 }
Geremia 2:713844a55c4e 69 void TFT::wr_cmd8(unsigned char cmd)
Geremia 2:713844a55c4e 70 {
Geremia 2:713844a55c4e 71 if(useNOP) proto->wr_cmd16(cmd); // 0x0000|cmd, 00 is NOP cmd for TFT
Geremia 2:713844a55c4e 72 else proto->wr_cmd8(cmd);
Geremia 2:713844a55c4e 73 }
Geremia 2:713844a55c4e 74 void TFT::wr_data8(unsigned char data)
Geremia 2:713844a55c4e 75 {
Geremia 2:713844a55c4e 76 proto->wr_data8(data);
Geremia 2:713844a55c4e 77 }
Geremia 2:713844a55c4e 78 void TFT::wr_data16(unsigned short data)
Geremia 2:713844a55c4e 79 {
Geremia 2:713844a55c4e 80 proto->wr_data16(data);
Geremia 2:713844a55c4e 81 }
Geremia 4:12ba0ecc2c1f 82 void TFT::wr_gram(unsigned short data)
Geremia 4:12ba0ecc2c1f 83 {
Geremia 4:12ba0ecc2c1f 84 proto->wr_gram(data);
Geremia 4:12ba0ecc2c1f 85 }
Geremia 4:12ba0ecc2c1f 86 void TFT::wr_gram(unsigned short data, unsigned int count)
Geremia 2:713844a55c4e 87 {
Geremia 4:12ba0ecc2c1f 88 proto->wr_gram(data, count);
Geremia 4:12ba0ecc2c1f 89 }
Geremia 4:12ba0ecc2c1f 90 void TFT::wr_grambuf(unsigned short* data, unsigned int lenght)
Geremia 4:12ba0ecc2c1f 91 {
Geremia 4:12ba0ecc2c1f 92 proto->wr_grambuf(data, lenght);
Geremia 2:713844a55c4e 93 }
Geremia 5:b222a9461d6b 94 unsigned short TFT::rd_gram()
Geremia 5:b222a9461d6b 95 {
Geremia 7:bb0383b91104 96 return proto->rd_gram();
Geremia 7:bb0383b91104 97 }
Geremia 7:bb0383b91104 98 unsigned int TFT::rd_reg_data32(unsigned char reg)
Geremia 7:bb0383b91104 99 {
Geremia 7:bb0383b91104 100 return proto->rd_reg_data32(reg);
Geremia 7:bb0383b91104 101 }
Geremia 7:bb0383b91104 102 unsigned int TFT::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd)
Geremia 7:bb0383b91104 103 {
Geremia 7:bb0383b91104 104 return proto->rd_extcreg_data32(reg, SPIreadenablecmd);
Geremia 5:b222a9461d6b 105 }
Geremia 4:12ba0ecc2c1f 106 //for TFT, just send data, position counters are in hw
Geremia 4:12ba0ecc2c1f 107 void TFT::window_pushpixel(unsigned short color)
Geremia 4:12ba0ecc2c1f 108 {
Geremia 4:12ba0ecc2c1f 109 proto->wr_gram(color);
Geremia 4:12ba0ecc2c1f 110 }
Geremia 4:12ba0ecc2c1f 111 void TFT::window_pushpixel(unsigned short color, unsigned int count)
Geremia 4:12ba0ecc2c1f 112 {
Geremia 4:12ba0ecc2c1f 113 proto->wr_gram(color, count);
Geremia 4:12ba0ecc2c1f 114 }
Geremia 4:12ba0ecc2c1f 115 void TFT::window_pushpixelbuf(unsigned short* color, unsigned int lenght)
Geremia 2:713844a55c4e 116 {
Geremia 4:12ba0ecc2c1f 117 proto->wr_grambuf(color, lenght);
Geremia 2:713844a55c4e 118 }
Geremia 2:713844a55c4e 119 void TFT::hw_reset()
Geremia 2:713844a55c4e 120 {
Geremia 2:713844a55c4e 121 proto->hw_reset();
Geremia 2:713844a55c4e 122 }
Geremia 2:713844a55c4e 123 void TFT::BusEnable(bool enable)
Geremia 2:713844a55c4e 124 {
Geremia 2:713844a55c4e 125 proto->BusEnable(enable);
Geremia 2:713844a55c4e 126 }
Geremia 2:713844a55c4e 127 // color TFT can rotate in hw (swap raw<->columns) for landscape views
Geremia 2:713844a55c4e 128 void TFT::set_orientation(int o)
Geremia 2:713844a55c4e 129 {
Geremia 2:713844a55c4e 130 orientation = o;
Geremia 2:713844a55c4e 131 wr_cmd8(0x36);
Geremia 2:713844a55c4e 132 switch (orientation) {
Geremia 2:713844a55c4e 133 case 0:// default, portrait view 0°
Geremia 2:713844a55c4e 134 if(mipistd) wr_data8(0x0A); // this is in real a vertical flip enabled, seems most displays are vertical flipped
Geremia 3:48f3282c2be8 135 else wr_data8(0x48); //for some other ILIxxxx
Geremia 7:bb0383b91104 136 set_width(screensize_X);
Geremia 7:bb0383b91104 137 set_height(screensize_Y);
Geremia 2:713844a55c4e 138 break;
Geremia 2:713844a55c4e 139 case 1:// landscape view +90°
Geremia 2:713844a55c4e 140 if(mipistd) wr_data8(0x28);
Geremia 3:48f3282c2be8 141 else wr_data8(0x29);//for some other ILIxxxx
Geremia 7:bb0383b91104 142 set_width(screensize_Y);
Geremia 7:bb0383b91104 143 set_height(screensize_X);
Geremia 2:713844a55c4e 144 break;
Geremia 2:713844a55c4e 145 case 2:// portrait view +180°
Geremia 2:713844a55c4e 146 if(mipistd) wr_data8(0x09);
Geremia 3:48f3282c2be8 147 else wr_data8(0x99);//for some other ILIxxxx
Geremia 7:bb0383b91104 148 set_width(screensize_X);
Geremia 7:bb0383b91104 149 set_height(screensize_Y);
Geremia 2:713844a55c4e 150 break;
Geremia 2:713844a55c4e 151 case 3:// landscape view -90°
Geremia 2:713844a55c4e 152 if(mipistd) wr_data8(0x2B);
Geremia 3:48f3282c2be8 153 else wr_data8(0xF8);//for some other ILIxxxx
Geremia 7:bb0383b91104 154 set_width(screensize_Y);
Geremia 7:bb0383b91104 155 set_height(screensize_X);
Geremia 2:713844a55c4e 156 break;
Geremia 2:713844a55c4e 157 }
Geremia 2:713844a55c4e 158 }
Geremia 7:bb0383b91104 159 void TFT::invert(unsigned char o)
Geremia 7:bb0383b91104 160 {
Geremia 7:bb0383b91104 161 if(o == 0) wr_cmd8(0x20);
Geremia 7:bb0383b91104 162 else wr_cmd8(0x21);
Geremia 7:bb0383b91104 163 }
Geremia 10:668cf78ff93a 164 void TFT::FastWindow(bool enable)
Geremia 10:668cf78ff93a 165 {
Geremia 10:668cf78ff93a 166 usefastwindow=enable;
Geremia 10:668cf78ff93a 167 }
Geremia 2:713844a55c4e 168 // TFT have both column and raw autoincrement inside a window, with internal counters
Geremia 2:713844a55c4e 169 void TFT::window(int x, int y, int w, int h)
Geremia 2:713844a55c4e 170 {
Geremia 10:668cf78ff93a 171 fastwindowready=false; // end raw/column going to be set to lower value than bottom-right corner
Geremia 2:713844a55c4e 172 wr_cmd8(0x2A);
Geremia 2:713844a55c4e 173 wr_data16(x); //start column
Geremia 2:713844a55c4e 174 wr_data16(x+w-1);//end column
Geremia 2:713844a55c4e 175
Geremia 2:713844a55c4e 176 wr_cmd8(0x2B);
Geremia 2:713844a55c4e 177 wr_data16(y); //start page
Geremia 2:713844a55c4e 178 wr_data16(y+h-1);//end page
Geremia 2:713844a55c4e 179
Geremia 2:713844a55c4e 180 wr_cmd8(0x2C); //write mem, just send pixels color next
Geremia 2:713844a55c4e 181 }
Geremia 5:b222a9461d6b 182 void TFT::window4read(int x, int y, int w, int h)
Geremia 5:b222a9461d6b 183 {
Geremia 10:668cf78ff93a 184 fastwindowready=false;
Geremia 5:b222a9461d6b 185 wr_cmd8(0x2A);
Geremia 5:b222a9461d6b 186 wr_data16(x); //start column
Geremia 5:b222a9461d6b 187 wr_data16(x+w-1);//end column
Geremia 5:b222a9461d6b 188
Geremia 5:b222a9461d6b 189 wr_cmd8(0x2B);
Geremia 5:b222a9461d6b 190 wr_data16(y); //start page
Geremia 5:b222a9461d6b 191 wr_data16(y+h-1);//end page
Geremia 5:b222a9461d6b 192
Geremia 5:b222a9461d6b 193 wr_cmd8(0x2E); //read mem, just pixelread next
Geremia 5:b222a9461d6b 194 }
Geremia 2:713844a55c4e 195 void TFT::pixel(int x, int y, unsigned short color)
Geremia 2:713844a55c4e 196 {
Geremia 10:668cf78ff93a 197 if(usefastwindow) //ili9486 does not like truncated 2A/2B cmds, at least in par mode
Geremia 10:668cf78ff93a 198 {
Geremia 10:668cf78ff93a 199 if(fastwindowready) //setting only start column/page does speedup, but needs end raw/column previously set to bottom-right corner
Geremia 10:668cf78ff93a 200 {
Geremia 10:668cf78ff93a 201 wr_cmd8(0x2A);
Geremia 10:668cf78ff93a 202 wr_data16(x); //start column only
Geremia 10:668cf78ff93a 203 wr_cmd8(0x2B);
Geremia 10:668cf78ff93a 204 wr_data16(y); //start page only
Geremia 10:668cf78ff93a 205 wr_cmd8(0x2C); //write mem, just send pixels color next
Geremia 10:668cf78ff93a 206 }
Geremia 10:668cf78ff93a 207 else
Geremia 10:668cf78ff93a 208 {
Geremia 10:668cf78ff93a 209 window(x,y,width()-x,height()-y); // set also end raw/column to bottom-right corner
Geremia 10:668cf78ff93a 210 fastwindowready=true;
Geremia 10:668cf78ff93a 211 }
Geremia 10:668cf78ff93a 212 }
Geremia 10:668cf78ff93a 213 else window(x,y,1,1);
Geremia 4:12ba0ecc2c1f 214 // proto->wr_gram(color); // 2C expects 16bit parameters
Geremia 4:12ba0ecc2c1f 215 wr_gram(color);
Geremia 2:713844a55c4e 216 }
Geremia 5:b222a9461d6b 217 unsigned short TFT::pixelread(int x, int y)
Geremia 5:b222a9461d6b 218 {
Geremia 10:668cf78ff93a 219 if(usefastwindow) //ili9486 does not like truncated 2A/2B cmds, at least in par mode
Geremia 10:668cf78ff93a 220 {
Geremia 10:668cf78ff93a 221 if(fastwindowready) //setting only start column/page does speedup, but needs end raw/column previously set to bottom-right corner
Geremia 10:668cf78ff93a 222 {
Geremia 10:668cf78ff93a 223 wr_cmd8(0x2A);
Geremia 10:668cf78ff93a 224 wr_data16(x); //start column only
Geremia 10:668cf78ff93a 225 wr_cmd8(0x2B);
Geremia 10:668cf78ff93a 226 wr_data16(y); //start page only
Geremia 10:668cf78ff93a 227 wr_cmd8(0x2E); //read mem, just pixelread next
Geremia 10:668cf78ff93a 228 }
Geremia 10:668cf78ff93a 229 else
Geremia 10:668cf78ff93a 230 {
Geremia 10:668cf78ff93a 231 window4read(x,y,width()-x,height()-y); // set also end raw/column to bottom-right corner
Geremia 10:668cf78ff93a 232 fastwindowready=true;
Geremia 10:668cf78ff93a 233 }
Geremia 10:668cf78ff93a 234 }
Geremia 10:668cf78ff93a 235 else window4read(x,y,1,1);
Geremia 10:668cf78ff93a 236
Geremia 5:b222a9461d6b 237 unsigned short color;
Geremia 5:b222a9461d6b 238 // proto->wr_gram(color); // 2C expects 16bit parameters
Geremia 5:b222a9461d6b 239 color = rd_gram();
Geremia 5:b222a9461d6b 240 if(mipistd) color = BGR2RGB(color); // in case, convert BGR to RGB (should depend on cmd36 bit3) but maybe is device specific
Geremia 5:b222a9461d6b 241 return color;
Geremia 5:b222a9461d6b 242 }
Geremia 7:bb0383b91104 243 void TFT::setscrollarea (int startY, int areasize) // ie 0,480 for whole screen
Geremia 7:bb0383b91104 244 {
Geremia 7:bb0383b91104 245 unsigned int bfa;
Geremia 7:bb0383b91104 246 topfixedareasize=startY;
Geremia 7:bb0383b91104 247 scrollareasize=areasize;
Geremia 7:bb0383b91104 248 wr_cmd8(0x33);
Geremia 7:bb0383b91104 249 wr_data16(topfixedareasize); //num lines of top fixed area
Geremia 7:bb0383b91104 250 wr_data16(scrollareasize+scrollbugfix); //num lines of vertical scroll area, +1 for ILI9481 fix
Geremia 8:26757296c79d 251 if((areasize+startY)>screensize_Y) bfa=0;
Geremia 8:26757296c79d 252 else bfa = screensize_Y-(areasize+startY);
Geremia 7:bb0383b91104 253 wr_data16(bfa); //num lines of bottom fixed area
Geremia 7:bb0383b91104 254 }
Geremia 7:bb0383b91104 255 void TFT::scroll (int lines) // ie 1= scrollup 1, 479= scrolldown 1
Geremia 7:bb0383b91104 256 {
Geremia 7:bb0383b91104 257 wr_cmd8(0x37);
Geremia 8:26757296c79d 258 wr_data16(topfixedareasize+(lines%scrollareasize)); // select the (absolute)line which will be displayed as first scrollarea line
Geremia 7:bb0383b91104 259 }
Geremia 7:bb0383b91104 260 void TFT::scrollreset()
Geremia 7:bb0383b91104 261 {
Geremia 7:bb0383b91104 262 wr_cmd8(0x13); //normal display mode
Geremia 7:bb0383b91104 263 }
Geremia 2:713844a55c4e 264 void TFT::cls (void)
Geremia 2:713844a55c4e 265 {
Geremia 2:713844a55c4e 266 WindowMax();
Geremia 7:bb0383b91104 267 // proto->wr_gram(_background,screensize_X*screensize_Y);
Geremia 7:bb0383b91104 268 // proto->wr_gram(0,screensize_X*screensize_Y);
Geremia 7:bb0383b91104 269 wr_gram(_background,screensize_X*screensize_Y);
Geremia 7:bb0383b91104 270 }
Geremia 7:bb0383b91104 271 // try to identify display controller
Geremia 7:bb0383b91104 272 void TFT::identify()
Geremia 7:bb0383b91104 273 {
Geremia 7:bb0383b91104 274 // MIPI std read ID cmd
Geremia 7:bb0383b91104 275 tftID=rd_reg_data32(0xBF);
Geremia 7:bb0383b91104 276 mipistd=true;
Geremia 7:bb0383b91104 277 // debug("ID MIPI : 0x%8X\r\n",tftID);
Geremia 7:bb0383b91104 278 if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF)))
Geremia 7:bb0383b91104 279 {
Geremia 7:bb0383b91104 280 mipistd=false;
Geremia 7:bb0383b91104 281 // ILI specfic read ID cmd
Geremia 7:bb0383b91104 282 tftID=rd_reg_data32(0xD3)>>8;
Geremia 7:bb0383b91104 283 // debug("ID ILI : 0x%8X\r\n",tftID);
Geremia 7:bb0383b91104 284 }
Geremia 7:bb0383b91104 285 if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF)))
Geremia 7:bb0383b91104 286 {
Geremia 7:bb0383b91104 287 // ILI specfic read ID cmd with ili9341 specific spi read-in enable 0xD9 cmd
Geremia 7:bb0383b91104 288 tftID=rd_extcreg_data32(0xD3, 0xD9);
Geremia 7:bb0383b91104 289 // debug("ID D9 extc ILI : 0x%8X\r\n",tftID);
Geremia 7:bb0383b91104 290 }
Geremia 7:bb0383b91104 291 if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF)))
Geremia 7:bb0383b91104 292 {
Geremia 7:bb0383b91104 293 // ILI specfic read ID cmd with ili9486/88 specific spi read-in enable 0xFB cmd
Geremia 7:bb0383b91104 294 tftID=rd_extcreg_data32(0xD3, 0xFB);
Geremia 7:bb0383b91104 295 // debug("ID D9 extc ILI : 0x%8X\r\n",tftID);
Geremia 7:bb0383b91104 296 }
Geremia 7:bb0383b91104 297 if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF))) tftID=0xDEAD;
Geremia 7:bb0383b91104 298 if ((tftID&0xFFFF)==0x9481) scrollbugfix=1;
Geremia 7:bb0383b91104 299 else scrollbugfix=0;
Geremia 7:bb0383b91104 300 hw_reset(); // in case wrong cmds messed up important settings
Geremia 7:bb0383b91104 301 }
Geremia 7:bb0383b91104 302 int TFT::sizeX()
Geremia 7:bb0383b91104 303 {
Geremia 7:bb0383b91104 304 return screensize_X;
Geremia 7:bb0383b91104 305 }
Geremia 7:bb0383b91104 306 int TFT::sizeY()
Geremia 7:bb0383b91104 307 {
Geremia 7:bb0383b91104 308 return screensize_Y;
Geremia 2:713844a55c4e 309 }