.
Display/TFT.cpp@5:b222a9461d6b, 2015-02-16 (annotated)
- Committer:
- Geremia
- Date:
- Mon Feb 16 00:52:24 2015 +0000
- Revision:
- 5:b222a9461d6b
- Parent:
- 4:12ba0ecc2c1f
- Child:
- 7:bb0383b91104
Added pixelread for TFTs
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 | 2:713844a55c4e | 26 | : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_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 | 2:713844a55c4e | 37 | // cls(); |
Geremia | 2:713844a55c4e | 38 | // locate(0,0); |
Geremia | 2:713844a55c4e | 39 | } |
Geremia | 2:713844a55c4e | 40 | 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 | 2:713844a55c4e | 41 | : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y) |
Geremia | 2:713844a55c4e | 42 | { |
Geremia | 2:713844a55c4e | 43 | if(displayproto==SPI_8) |
Geremia | 2:713844a55c4e | 44 | { |
Geremia | 2:713844a55c4e | 45 | proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 46 | useNOP=false; |
Geremia | 2:713844a55c4e | 47 | } |
Geremia | 2:713844a55c4e | 48 | else if(displayproto==SPI_16) |
Geremia | 2:713844a55c4e | 49 | { |
Geremia | 2:713844a55c4e | 50 | proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 51 | useNOP=true; |
Geremia | 2:713844a55c4e | 52 | } |
Geremia | 4:12ba0ecc2c1f | 53 | scrollbugfix=0; |
Geremia | 4:12ba0ecc2c1f | 54 | mipistd=false; |
Geremia | 2:713844a55c4e | 55 | set_orientation(0); |
Geremia | 2:713844a55c4e | 56 | foreground(White); |
Geremia | 2:713844a55c4e | 57 | background(Black); |
Geremia | 2:713844a55c4e | 58 | set_auto_up(false); |
Geremia | 2:713844a55c4e | 59 | // locate(0,0); |
Geremia | 2:713844a55c4e | 60 | } |
Geremia | 2:713844a55c4e | 61 | void TFT::wr_cmd8(unsigned char cmd) |
Geremia | 2:713844a55c4e | 62 | { |
Geremia | 2:713844a55c4e | 63 | if(useNOP) proto->wr_cmd16(cmd); // 0x0000|cmd, 00 is NOP cmd for TFT |
Geremia | 2:713844a55c4e | 64 | else proto->wr_cmd8(cmd); |
Geremia | 2:713844a55c4e | 65 | } |
Geremia | 2:713844a55c4e | 66 | void TFT::wr_data8(unsigned char data) |
Geremia | 2:713844a55c4e | 67 | { |
Geremia | 2:713844a55c4e | 68 | proto->wr_data8(data); |
Geremia | 2:713844a55c4e | 69 | } |
Geremia | 2:713844a55c4e | 70 | void TFT::wr_data16(unsigned short data) |
Geremia | 2:713844a55c4e | 71 | { |
Geremia | 2:713844a55c4e | 72 | proto->wr_data16(data); |
Geremia | 2:713844a55c4e | 73 | } |
Geremia | 4:12ba0ecc2c1f | 74 | void TFT::wr_gram(unsigned short data) |
Geremia | 4:12ba0ecc2c1f | 75 | { |
Geremia | 4:12ba0ecc2c1f | 76 | proto->wr_gram(data); |
Geremia | 4:12ba0ecc2c1f | 77 | } |
Geremia | 4:12ba0ecc2c1f | 78 | void TFT::wr_gram(unsigned short data, unsigned int count) |
Geremia | 2:713844a55c4e | 79 | { |
Geremia | 4:12ba0ecc2c1f | 80 | proto->wr_gram(data, count); |
Geremia | 4:12ba0ecc2c1f | 81 | } |
Geremia | 4:12ba0ecc2c1f | 82 | void TFT::wr_grambuf(unsigned short* data, unsigned int lenght) |
Geremia | 4:12ba0ecc2c1f | 83 | { |
Geremia | 4:12ba0ecc2c1f | 84 | proto->wr_grambuf(data, lenght); |
Geremia | 2:713844a55c4e | 85 | } |
Geremia | 5:b222a9461d6b | 86 | unsigned int TFT::rd_data32_wdummy() |
Geremia | 5:b222a9461d6b | 87 | { |
Geremia | 5:b222a9461d6b | 88 | return proto->rd_data32_wdummy(); |
Geremia | 5:b222a9461d6b | 89 | } |
Geremia | 5:b222a9461d6b | 90 | unsigned short TFT::rd_gram() |
Geremia | 5:b222a9461d6b | 91 | { |
Geremia | 5:b222a9461d6b | 92 | return (proto->rd_gram()); |
Geremia | 5:b222a9461d6b | 93 | } |
Geremia | 4:12ba0ecc2c1f | 94 | //for TFT, just send data, position counters are in hw |
Geremia | 4:12ba0ecc2c1f | 95 | void TFT::window_pushpixel(unsigned short color) |
Geremia | 4:12ba0ecc2c1f | 96 | { |
Geremia | 4:12ba0ecc2c1f | 97 | proto->wr_gram(color); |
Geremia | 4:12ba0ecc2c1f | 98 | } |
Geremia | 4:12ba0ecc2c1f | 99 | void TFT::window_pushpixel(unsigned short color, unsigned int count) |
Geremia | 4:12ba0ecc2c1f | 100 | { |
Geremia | 4:12ba0ecc2c1f | 101 | proto->wr_gram(color, count); |
Geremia | 4:12ba0ecc2c1f | 102 | } |
Geremia | 4:12ba0ecc2c1f | 103 | void TFT::window_pushpixelbuf(unsigned short* color, unsigned int lenght) |
Geremia | 2:713844a55c4e | 104 | { |
Geremia | 4:12ba0ecc2c1f | 105 | proto->wr_grambuf(color, lenght); |
Geremia | 2:713844a55c4e | 106 | } |
Geremia | 2:713844a55c4e | 107 | void TFT::hw_reset() |
Geremia | 2:713844a55c4e | 108 | { |
Geremia | 2:713844a55c4e | 109 | proto->hw_reset(); |
Geremia | 2:713844a55c4e | 110 | } |
Geremia | 2:713844a55c4e | 111 | void TFT::BusEnable(bool enable) |
Geremia | 2:713844a55c4e | 112 | { |
Geremia | 2:713844a55c4e | 113 | proto->BusEnable(enable); |
Geremia | 2:713844a55c4e | 114 | } |
Geremia | 2:713844a55c4e | 115 | // color TFT can rotate in hw (swap raw<->columns) for landscape views |
Geremia | 2:713844a55c4e | 116 | void TFT::set_orientation(int o) |
Geremia | 2:713844a55c4e | 117 | { |
Geremia | 2:713844a55c4e | 118 | orientation = o; |
Geremia | 2:713844a55c4e | 119 | wr_cmd8(0x36); |
Geremia | 2:713844a55c4e | 120 | switch (orientation) { |
Geremia | 2:713844a55c4e | 121 | case 0:// default, portrait view 0° |
Geremia | 2:713844a55c4e | 122 | if(mipistd) wr_data8(0x0A); // this is in real a vertical flip enabled, seems most displays are vertical flipped |
Geremia | 3:48f3282c2be8 | 123 | else wr_data8(0x48); //for some other ILIxxxx |
Geremia | 2:713844a55c4e | 124 | set_width(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 125 | set_height(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 126 | break; |
Geremia | 2:713844a55c4e | 127 | case 1:// landscape view +90° |
Geremia | 2:713844a55c4e | 128 | if(mipistd) wr_data8(0x28); |
Geremia | 3:48f3282c2be8 | 129 | else wr_data8(0x29);//for some other ILIxxxx |
Geremia | 2:713844a55c4e | 130 | set_width(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 131 | set_height(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 132 | break; |
Geremia | 2:713844a55c4e | 133 | case 2:// portrait view +180° |
Geremia | 2:713844a55c4e | 134 | if(mipistd) wr_data8(0x09); |
Geremia | 3:48f3282c2be8 | 135 | else wr_data8(0x99);//for some other ILIxxxx |
Geremia | 2:713844a55c4e | 136 | set_width(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 137 | set_height(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 138 | break; |
Geremia | 2:713844a55c4e | 139 | case 3:// landscape view -90° |
Geremia | 2:713844a55c4e | 140 | if(mipistd) wr_data8(0x2B); |
Geremia | 3:48f3282c2be8 | 141 | else wr_data8(0xF8);//for some other ILIxxxx |
Geremia | 2:713844a55c4e | 142 | set_width(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 143 | set_height(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 144 | break; |
Geremia | 2:713844a55c4e | 145 | } |
Geremia | 2:713844a55c4e | 146 | } |
Geremia | 2:713844a55c4e | 147 | // TFT have both column and raw autoincrement inside a window, with internal counters |
Geremia | 2:713844a55c4e | 148 | void TFT::window(int x, int y, int w, int h) |
Geremia | 2:713844a55c4e | 149 | { |
Geremia | 2:713844a55c4e | 150 | //ili9486 does not like truncated 2A/2B cmds, at least in par mode |
Geremia | 2:713844a55c4e | 151 | //setting only start column/page would speedup, but needs a windowmax() before, maybe implement later |
Geremia | 2:713844a55c4e | 152 | wr_cmd8(0x2A); |
Geremia | 2:713844a55c4e | 153 | wr_data16(x); //start column |
Geremia | 2:713844a55c4e | 154 | wr_data16(x+w-1);//end column |
Geremia | 2:713844a55c4e | 155 | |
Geremia | 2:713844a55c4e | 156 | wr_cmd8(0x2B); |
Geremia | 2:713844a55c4e | 157 | wr_data16(y); //start page |
Geremia | 2:713844a55c4e | 158 | wr_data16(y+h-1);//end page |
Geremia | 2:713844a55c4e | 159 | |
Geremia | 2:713844a55c4e | 160 | wr_cmd8(0x2C); //write mem, just send pixels color next |
Geremia | 2:713844a55c4e | 161 | } |
Geremia | 5:b222a9461d6b | 162 | void TFT::window4read(int x, int y, int w, int h) |
Geremia | 5:b222a9461d6b | 163 | { |
Geremia | 5:b222a9461d6b | 164 | wr_cmd8(0x2A); |
Geremia | 5:b222a9461d6b | 165 | wr_data16(x); //start column |
Geremia | 5:b222a9461d6b | 166 | wr_data16(x+w-1);//end column |
Geremia | 5:b222a9461d6b | 167 | |
Geremia | 5:b222a9461d6b | 168 | wr_cmd8(0x2B); |
Geremia | 5:b222a9461d6b | 169 | wr_data16(y); //start page |
Geremia | 5:b222a9461d6b | 170 | wr_data16(y+h-1);//end page |
Geremia | 5:b222a9461d6b | 171 | |
Geremia | 5:b222a9461d6b | 172 | wr_cmd8(0x2E); //read mem, just pixelread next |
Geremia | 5:b222a9461d6b | 173 | } |
Geremia | 2:713844a55c4e | 174 | void TFT::pixel(int x, int y, unsigned short color) |
Geremia | 2:713844a55c4e | 175 | { |
Geremia | 2:713844a55c4e | 176 | window(x,y,1,1); |
Geremia | 4:12ba0ecc2c1f | 177 | // proto->wr_gram(color); // 2C expects 16bit parameters |
Geremia | 4:12ba0ecc2c1f | 178 | wr_gram(color); |
Geremia | 2:713844a55c4e | 179 | } |
Geremia | 5:b222a9461d6b | 180 | unsigned short TFT::pixelread(int x, int y) |
Geremia | 5:b222a9461d6b | 181 | { |
Geremia | 5:b222a9461d6b | 182 | unsigned short color; |
Geremia | 5:b222a9461d6b | 183 | window4read(x,y,1,1); |
Geremia | 5:b222a9461d6b | 184 | // proto->wr_gram(color); // 2C expects 16bit parameters |
Geremia | 5:b222a9461d6b | 185 | color = rd_gram(); |
Geremia | 5:b222a9461d6b | 186 | if(mipistd) color = BGR2RGB(color); // in case, convert BGR to RGB (should depend on cmd36 bit3) but maybe is device specific |
Geremia | 5:b222a9461d6b | 187 | return color; |
Geremia | 5:b222a9461d6b | 188 | } |
Geremia | 2:713844a55c4e | 189 | void TFT::cls (void) |
Geremia | 2:713844a55c4e | 190 | { |
Geremia | 2:713844a55c4e | 191 | WindowMax(); |
Geremia | 4:12ba0ecc2c1f | 192 | // proto->wr_gram(_background,LCDSIZE_X*LCDSIZE_Y); |
Geremia | 4:12ba0ecc2c1f | 193 | // proto->wr_gram(0,LCDSIZE_X*LCDSIZE_Y); |
Geremia | 4:12ba0ecc2c1f | 194 | wr_gram(_background,LCDSIZE_X*LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 195 | } |