.
Display/TFT.cpp@7:bb0383b91104, 2015-02-17 (annotated)
- Committer:
- Geremia
- Date:
- Tue Feb 17 11:02:06 2015 +0000
- Revision:
- 7:bb0383b91104
- Parent:
- 5:b222a9461d6b
- Child:
- 8:26757296c79d
TFT: added get deviceID, scroll functions
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:713844a55c4e | 39 | // cls(); |
Geremia | 2:713844a55c4e | 40 | // locate(0,0); |
Geremia | 2:713844a55c4e | 41 | } |
Geremia | 2:713844a55c4e | 42 | 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 | 43 | : GraphicsDisplay(name), screensize_X(lcdsize_x), screensize_Y(lcdsize_y) |
Geremia | 2:713844a55c4e | 44 | { |
Geremia | 2:713844a55c4e | 45 | if(displayproto==SPI_8) |
Geremia | 2:713844a55c4e | 46 | { |
Geremia | 2:713844a55c4e | 47 | proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 48 | useNOP=false; |
Geremia | 2:713844a55c4e | 49 | } |
Geremia | 2:713844a55c4e | 50 | else if(displayproto==SPI_16) |
Geremia | 2:713844a55c4e | 51 | { |
Geremia | 2:713844a55c4e | 52 | proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 53 | useNOP=true; |
Geremia | 2:713844a55c4e | 54 | } |
Geremia | 4:12ba0ecc2c1f | 55 | scrollbugfix=0; |
Geremia | 4:12ba0ecc2c1f | 56 | mipistd=false; |
Geremia | 2:713844a55c4e | 57 | set_orientation(0); |
Geremia | 2:713844a55c4e | 58 | foreground(White); |
Geremia | 2:713844a55c4e | 59 | background(Black); |
Geremia | 2:713844a55c4e | 60 | set_auto_up(false); |
Geremia | 7:bb0383b91104 | 61 | topfixedareasize=0; |
Geremia | 7:bb0383b91104 | 62 | scrollareasize=0; |
Geremia | 2:713844a55c4e | 63 | // locate(0,0); |
Geremia | 2:713844a55c4e | 64 | } |
Geremia | 2:713844a55c4e | 65 | void TFT::wr_cmd8(unsigned char cmd) |
Geremia | 2:713844a55c4e | 66 | { |
Geremia | 2:713844a55c4e | 67 | if(useNOP) proto->wr_cmd16(cmd); // 0x0000|cmd, 00 is NOP cmd for TFT |
Geremia | 2:713844a55c4e | 68 | else proto->wr_cmd8(cmd); |
Geremia | 2:713844a55c4e | 69 | } |
Geremia | 2:713844a55c4e | 70 | void TFT::wr_data8(unsigned char data) |
Geremia | 2:713844a55c4e | 71 | { |
Geremia | 2:713844a55c4e | 72 | proto->wr_data8(data); |
Geremia | 2:713844a55c4e | 73 | } |
Geremia | 2:713844a55c4e | 74 | void TFT::wr_data16(unsigned short data) |
Geremia | 2:713844a55c4e | 75 | { |
Geremia | 2:713844a55c4e | 76 | proto->wr_data16(data); |
Geremia | 2:713844a55c4e | 77 | } |
Geremia | 4:12ba0ecc2c1f | 78 | void TFT::wr_gram(unsigned short data) |
Geremia | 4:12ba0ecc2c1f | 79 | { |
Geremia | 4:12ba0ecc2c1f | 80 | proto->wr_gram(data); |
Geremia | 4:12ba0ecc2c1f | 81 | } |
Geremia | 4:12ba0ecc2c1f | 82 | void TFT::wr_gram(unsigned short data, unsigned int count) |
Geremia | 2:713844a55c4e | 83 | { |
Geremia | 4:12ba0ecc2c1f | 84 | proto->wr_gram(data, count); |
Geremia | 4:12ba0ecc2c1f | 85 | } |
Geremia | 4:12ba0ecc2c1f | 86 | void TFT::wr_grambuf(unsigned short* data, unsigned int lenght) |
Geremia | 4:12ba0ecc2c1f | 87 | { |
Geremia | 4:12ba0ecc2c1f | 88 | proto->wr_grambuf(data, lenght); |
Geremia | 2:713844a55c4e | 89 | } |
Geremia | 5:b222a9461d6b | 90 | unsigned short TFT::rd_gram() |
Geremia | 5:b222a9461d6b | 91 | { |
Geremia | 7:bb0383b91104 | 92 | return proto->rd_gram(); |
Geremia | 7:bb0383b91104 | 93 | } |
Geremia | 7:bb0383b91104 | 94 | unsigned int TFT::rd_reg_data32(unsigned char reg) |
Geremia | 7:bb0383b91104 | 95 | { |
Geremia | 7:bb0383b91104 | 96 | return proto->rd_reg_data32(reg); |
Geremia | 7:bb0383b91104 | 97 | } |
Geremia | 7:bb0383b91104 | 98 | unsigned int TFT::rd_extcreg_data32(unsigned char reg, unsigned char SPIreadenablecmd) |
Geremia | 7:bb0383b91104 | 99 | { |
Geremia | 7:bb0383b91104 | 100 | return proto->rd_extcreg_data32(reg, SPIreadenablecmd); |
Geremia | 5:b222a9461d6b | 101 | } |
Geremia | 4:12ba0ecc2c1f | 102 | //for TFT, just send data, position counters are in hw |
Geremia | 4:12ba0ecc2c1f | 103 | void TFT::window_pushpixel(unsigned short color) |
Geremia | 4:12ba0ecc2c1f | 104 | { |
Geremia | 4:12ba0ecc2c1f | 105 | proto->wr_gram(color); |
Geremia | 4:12ba0ecc2c1f | 106 | } |
Geremia | 4:12ba0ecc2c1f | 107 | void TFT::window_pushpixel(unsigned short color, unsigned int count) |
Geremia | 4:12ba0ecc2c1f | 108 | { |
Geremia | 4:12ba0ecc2c1f | 109 | proto->wr_gram(color, count); |
Geremia | 4:12ba0ecc2c1f | 110 | } |
Geremia | 4:12ba0ecc2c1f | 111 | void TFT::window_pushpixelbuf(unsigned short* color, unsigned int lenght) |
Geremia | 2:713844a55c4e | 112 | { |
Geremia | 4:12ba0ecc2c1f | 113 | proto->wr_grambuf(color, lenght); |
Geremia | 2:713844a55c4e | 114 | } |
Geremia | 2:713844a55c4e | 115 | void TFT::hw_reset() |
Geremia | 2:713844a55c4e | 116 | { |
Geremia | 2:713844a55c4e | 117 | proto->hw_reset(); |
Geremia | 2:713844a55c4e | 118 | } |
Geremia | 2:713844a55c4e | 119 | void TFT::BusEnable(bool enable) |
Geremia | 2:713844a55c4e | 120 | { |
Geremia | 2:713844a55c4e | 121 | proto->BusEnable(enable); |
Geremia | 2:713844a55c4e | 122 | } |
Geremia | 2:713844a55c4e | 123 | // color TFT can rotate in hw (swap raw<->columns) for landscape views |
Geremia | 2:713844a55c4e | 124 | void TFT::set_orientation(int o) |
Geremia | 2:713844a55c4e | 125 | { |
Geremia | 2:713844a55c4e | 126 | orientation = o; |
Geremia | 2:713844a55c4e | 127 | wr_cmd8(0x36); |
Geremia | 2:713844a55c4e | 128 | switch (orientation) { |
Geremia | 2:713844a55c4e | 129 | case 0:// default, portrait view 0° |
Geremia | 2:713844a55c4e | 130 | if(mipistd) wr_data8(0x0A); // this is in real a vertical flip enabled, seems most displays are vertical flipped |
Geremia | 3:48f3282c2be8 | 131 | else wr_data8(0x48); //for some other ILIxxxx |
Geremia | 7:bb0383b91104 | 132 | set_width(screensize_X); |
Geremia | 7:bb0383b91104 | 133 | set_height(screensize_Y); |
Geremia | 2:713844a55c4e | 134 | break; |
Geremia | 2:713844a55c4e | 135 | case 1:// landscape view +90° |
Geremia | 2:713844a55c4e | 136 | if(mipistd) wr_data8(0x28); |
Geremia | 3:48f3282c2be8 | 137 | else wr_data8(0x29);//for some other ILIxxxx |
Geremia | 7:bb0383b91104 | 138 | set_width(screensize_Y); |
Geremia | 7:bb0383b91104 | 139 | set_height(screensize_X); |
Geremia | 2:713844a55c4e | 140 | break; |
Geremia | 2:713844a55c4e | 141 | case 2:// portrait view +180° |
Geremia | 2:713844a55c4e | 142 | if(mipistd) wr_data8(0x09); |
Geremia | 3:48f3282c2be8 | 143 | else wr_data8(0x99);//for some other ILIxxxx |
Geremia | 7:bb0383b91104 | 144 | set_width(screensize_X); |
Geremia | 7:bb0383b91104 | 145 | set_height(screensize_Y); |
Geremia | 2:713844a55c4e | 146 | break; |
Geremia | 2:713844a55c4e | 147 | case 3:// landscape view -90° |
Geremia | 2:713844a55c4e | 148 | if(mipistd) wr_data8(0x2B); |
Geremia | 3:48f3282c2be8 | 149 | else wr_data8(0xF8);//for some other ILIxxxx |
Geremia | 7:bb0383b91104 | 150 | set_width(screensize_Y); |
Geremia | 7:bb0383b91104 | 151 | set_height(screensize_X); |
Geremia | 2:713844a55c4e | 152 | break; |
Geremia | 2:713844a55c4e | 153 | } |
Geremia | 2:713844a55c4e | 154 | } |
Geremia | 7:bb0383b91104 | 155 | void TFT::invert(unsigned char o) |
Geremia | 7:bb0383b91104 | 156 | { |
Geremia | 7:bb0383b91104 | 157 | if(o == 0) wr_cmd8(0x20); |
Geremia | 7:bb0383b91104 | 158 | else wr_cmd8(0x21); |
Geremia | 7:bb0383b91104 | 159 | } |
Geremia | 2:713844a55c4e | 160 | // TFT have both column and raw autoincrement inside a window, with internal counters |
Geremia | 2:713844a55c4e | 161 | void TFT::window(int x, int y, int w, int h) |
Geremia | 2:713844a55c4e | 162 | { |
Geremia | 2:713844a55c4e | 163 | //ili9486 does not like truncated 2A/2B cmds, at least in par mode |
Geremia | 2:713844a55c4e | 164 | //setting only start column/page would speedup, but needs a windowmax() before, maybe implement later |
Geremia | 2:713844a55c4e | 165 | wr_cmd8(0x2A); |
Geremia | 2:713844a55c4e | 166 | wr_data16(x); //start column |
Geremia | 2:713844a55c4e | 167 | wr_data16(x+w-1);//end column |
Geremia | 2:713844a55c4e | 168 | |
Geremia | 2:713844a55c4e | 169 | wr_cmd8(0x2B); |
Geremia | 2:713844a55c4e | 170 | wr_data16(y); //start page |
Geremia | 2:713844a55c4e | 171 | wr_data16(y+h-1);//end page |
Geremia | 2:713844a55c4e | 172 | |
Geremia | 2:713844a55c4e | 173 | wr_cmd8(0x2C); //write mem, just send pixels color next |
Geremia | 2:713844a55c4e | 174 | } |
Geremia | 5:b222a9461d6b | 175 | void TFT::window4read(int x, int y, int w, int h) |
Geremia | 5:b222a9461d6b | 176 | { |
Geremia | 5:b222a9461d6b | 177 | wr_cmd8(0x2A); |
Geremia | 5:b222a9461d6b | 178 | wr_data16(x); //start column |
Geremia | 5:b222a9461d6b | 179 | wr_data16(x+w-1);//end column |
Geremia | 5:b222a9461d6b | 180 | |
Geremia | 5:b222a9461d6b | 181 | wr_cmd8(0x2B); |
Geremia | 5:b222a9461d6b | 182 | wr_data16(y); //start page |
Geremia | 5:b222a9461d6b | 183 | wr_data16(y+h-1);//end page |
Geremia | 5:b222a9461d6b | 184 | |
Geremia | 5:b222a9461d6b | 185 | wr_cmd8(0x2E); //read mem, just pixelread next |
Geremia | 5:b222a9461d6b | 186 | } |
Geremia | 2:713844a55c4e | 187 | void TFT::pixel(int x, int y, unsigned short color) |
Geremia | 2:713844a55c4e | 188 | { |
Geremia | 2:713844a55c4e | 189 | window(x,y,1,1); |
Geremia | 4:12ba0ecc2c1f | 190 | // proto->wr_gram(color); // 2C expects 16bit parameters |
Geremia | 4:12ba0ecc2c1f | 191 | wr_gram(color); |
Geremia | 2:713844a55c4e | 192 | } |
Geremia | 5:b222a9461d6b | 193 | unsigned short TFT::pixelread(int x, int y) |
Geremia | 5:b222a9461d6b | 194 | { |
Geremia | 5:b222a9461d6b | 195 | unsigned short color; |
Geremia | 5:b222a9461d6b | 196 | window4read(x,y,1,1); |
Geremia | 5:b222a9461d6b | 197 | // proto->wr_gram(color); // 2C expects 16bit parameters |
Geremia | 5:b222a9461d6b | 198 | color = rd_gram(); |
Geremia | 5:b222a9461d6b | 199 | if(mipistd) color = BGR2RGB(color); // in case, convert BGR to RGB (should depend on cmd36 bit3) but maybe is device specific |
Geremia | 5:b222a9461d6b | 200 | return color; |
Geremia | 5:b222a9461d6b | 201 | } |
Geremia | 7:bb0383b91104 | 202 | void TFT::setscrollarea (int startY, int areasize) // ie 0,480 for whole screen |
Geremia | 7:bb0383b91104 | 203 | { |
Geremia | 7:bb0383b91104 | 204 | unsigned int bfa; |
Geremia | 7:bb0383b91104 | 205 | topfixedareasize=startY; |
Geremia | 7:bb0383b91104 | 206 | scrollareasize=areasize; |
Geremia | 7:bb0383b91104 | 207 | wr_cmd8(0x33); |
Geremia | 7:bb0383b91104 | 208 | wr_data16(topfixedareasize); //num lines of top fixed area |
Geremia | 7:bb0383b91104 | 209 | wr_data16(scrollareasize+scrollbugfix); //num lines of vertical scroll area, +1 for ILI9481 fix |
Geremia | 7:bb0383b91104 | 210 | if((areasize+startY)>height()) bfa=0; |
Geremia | 7:bb0383b91104 | 211 | else bfa = height()-(areasize+startY); |
Geremia | 7:bb0383b91104 | 212 | wr_data16(bfa); //num lines of bottom fixed area |
Geremia | 7:bb0383b91104 | 213 | } |
Geremia | 7:bb0383b91104 | 214 | void TFT::scroll (int lines) // ie 1= scrollup 1, 479= scrolldown 1 |
Geremia | 7:bb0383b91104 | 215 | { |
Geremia | 7:bb0383b91104 | 216 | wr_cmd8(0x37); |
Geremia | 7:bb0383b91104 | 217 | wr_data16((topfixedareasize+lines)%scrollareasize); //num lines of top fixed area |
Geremia | 7:bb0383b91104 | 218 | } |
Geremia | 7:bb0383b91104 | 219 | void TFT::scrollreset() |
Geremia | 7:bb0383b91104 | 220 | { |
Geremia | 7:bb0383b91104 | 221 | wr_cmd8(0x13); //normal display mode |
Geremia | 7:bb0383b91104 | 222 | } |
Geremia | 2:713844a55c4e | 223 | void TFT::cls (void) |
Geremia | 2:713844a55c4e | 224 | { |
Geremia | 2:713844a55c4e | 225 | WindowMax(); |
Geremia | 7:bb0383b91104 | 226 | // proto->wr_gram(_background,screensize_X*screensize_Y); |
Geremia | 7:bb0383b91104 | 227 | // proto->wr_gram(0,screensize_X*screensize_Y); |
Geremia | 7:bb0383b91104 | 228 | wr_gram(_background,screensize_X*screensize_Y); |
Geremia | 7:bb0383b91104 | 229 | } |
Geremia | 7:bb0383b91104 | 230 | // try to identify display controller |
Geremia | 7:bb0383b91104 | 231 | void TFT::identify() |
Geremia | 7:bb0383b91104 | 232 | { |
Geremia | 7:bb0383b91104 | 233 | // MIPI std read ID cmd |
Geremia | 7:bb0383b91104 | 234 | tftID=rd_reg_data32(0xBF); |
Geremia | 7:bb0383b91104 | 235 | mipistd=true; |
Geremia | 7:bb0383b91104 | 236 | // debug("ID MIPI : 0x%8X\r\n",tftID); |
Geremia | 7:bb0383b91104 | 237 | if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF))) |
Geremia | 7:bb0383b91104 | 238 | { |
Geremia | 7:bb0383b91104 | 239 | mipistd=false; |
Geremia | 7:bb0383b91104 | 240 | // ILI specfic read ID cmd |
Geremia | 7:bb0383b91104 | 241 | tftID=rd_reg_data32(0xD3)>>8; |
Geremia | 7:bb0383b91104 | 242 | // debug("ID ILI : 0x%8X\r\n",tftID); |
Geremia | 7:bb0383b91104 | 243 | } |
Geremia | 7:bb0383b91104 | 244 | if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF))) |
Geremia | 7:bb0383b91104 | 245 | { |
Geremia | 7:bb0383b91104 | 246 | // ILI specfic read ID cmd with ili9341 specific spi read-in enable 0xD9 cmd |
Geremia | 7:bb0383b91104 | 247 | tftID=rd_extcreg_data32(0xD3, 0xD9); |
Geremia | 7:bb0383b91104 | 248 | // debug("ID D9 extc ILI : 0x%8X\r\n",tftID); |
Geremia | 7:bb0383b91104 | 249 | } |
Geremia | 7:bb0383b91104 | 250 | if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF))) |
Geremia | 7:bb0383b91104 | 251 | { |
Geremia | 7:bb0383b91104 | 252 | // ILI specfic read ID cmd with ili9486/88 specific spi read-in enable 0xFB cmd |
Geremia | 7:bb0383b91104 | 253 | tftID=rd_extcreg_data32(0xD3, 0xFB); |
Geremia | 7:bb0383b91104 | 254 | // debug("ID D9 extc ILI : 0x%8X\r\n",tftID); |
Geremia | 7:bb0383b91104 | 255 | } |
Geremia | 7:bb0383b91104 | 256 | if(((tftID&0xFF)==((tftID>>8)&0xFF)) && ((tftID&0xFF)==((tftID>>16)&0xFF))) tftID=0xDEAD; |
Geremia | 7:bb0383b91104 | 257 | if ((tftID&0xFFFF)==0x9481) scrollbugfix=1; |
Geremia | 7:bb0383b91104 | 258 | else scrollbugfix=0; |
Geremia | 7:bb0383b91104 | 259 | hw_reset(); // in case wrong cmds messed up important settings |
Geremia | 7:bb0383b91104 | 260 | } |
Geremia | 7:bb0383b91104 | 261 | int TFT::sizeX() |
Geremia | 7:bb0383b91104 | 262 | { |
Geremia | 7:bb0383b91104 | 263 | return screensize_X; |
Geremia | 7:bb0383b91104 | 264 | } |
Geremia | 7:bb0383b91104 | 265 | int TFT::sizeY() |
Geremia | 7:bb0383b91104 | 266 | { |
Geremia | 7:bb0383b91104 | 267 | return screensize_Y; |
Geremia | 2:713844a55c4e | 268 | } |