Basically i glued Peter Drescher and Simon Ford libs in a GraphicsDisplay class, then derived TFT or LCD class (which inherits Protocols class), then the most derived ones (Inits), which are per-display and are the only part needed to be adapted to diff hw.

Fork of UniGraphic by GraphicsDisplay

Committer:
Geremia
Date:
Sat Feb 14 17:42:21 2015 +0000
Revision:
3:48f3282c2be8
Parent:
2:713844a55c4e
Child:
4:12ba0ecc2c1f
small things

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 3:48f3282c2be8 1 /* mbed library for 240*320 pixel display TFT based on ILI9341 LCD Controller
Geremia 3:48f3282c2be8 2 * Copyright (c) 2013 Peter Drescher - DC2PD
Geremia 3:48f3282c2be8 3 *
Geremia 3:48f3282c2be8 4 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Geremia 3:48f3282c2be8 5 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Geremia 3:48f3282c2be8 6 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Geremia 3:48f3282c2be8 7 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Geremia 3:48f3282c2be8 8 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Geremia 3:48f3282c2be8 9 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Geremia 3:48f3282c2be8 10 * THE SOFTWARE.
Geremia 3:48f3282c2be8 11 */
Geremia 3:48f3282c2be8 12
Geremia 2:713844a55c4e 13 #include "TFT.h"
Geremia 2:713844a55c4e 14
Geremia 2:713844a55c4e 15 //#include "mbed_debug.h"
Geremia 2:713844a55c4e 16
Geremia 2:713844a55c4e 17 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
Geremia 2:713844a55c4e 18
Geremia 2:713844a55c4e 19 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 20 : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y)
Geremia 2:713844a55c4e 21 {
Geremia 2:713844a55c4e 22 if(displayproto==PAR_8) proto = new PAR8(port, CS, reset, DC, WR, RD);
Geremia 2:713844a55c4e 23 useNOP=false;
Geremia 2:713844a55c4e 24 set_orientation(0);
Geremia 2:713844a55c4e 25 foreground(White);
Geremia 2:713844a55c4e 26 background(Black);
Geremia 2:713844a55c4e 27 set_auto_up(false); //we don't have framebuffer
Geremia 2:713844a55c4e 28 mipistd=false;
Geremia 2:713844a55c4e 29 // cls();
Geremia 2:713844a55c4e 30 // locate(0,0);
Geremia 2:713844a55c4e 31 }
Geremia 2:713844a55c4e 32 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 33 : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y)
Geremia 2:713844a55c4e 34 {
Geremia 2:713844a55c4e 35 if(displayproto==SPI_8)
Geremia 2:713844a55c4e 36 {
Geremia 2:713844a55c4e 37 proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 2:713844a55c4e 38 useNOP=false;
Geremia 2:713844a55c4e 39 }
Geremia 2:713844a55c4e 40 else if(displayproto==SPI_16)
Geremia 2:713844a55c4e 41 {
Geremia 2:713844a55c4e 42 proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 2:713844a55c4e 43 useNOP=true;
Geremia 2:713844a55c4e 44 }
Geremia 2:713844a55c4e 45 // cls();
Geremia 2:713844a55c4e 46 set_orientation(0);
Geremia 2:713844a55c4e 47 foreground(White);
Geremia 2:713844a55c4e 48 background(Black);
Geremia 2:713844a55c4e 49 set_auto_up(false);
Geremia 2:713844a55c4e 50 mipistd=false;
Geremia 2:713844a55c4e 51 // locate(0,0);
Geremia 2:713844a55c4e 52 }
Geremia 2:713844a55c4e 53 void TFT::wr_cmd8(unsigned char cmd)
Geremia 2:713844a55c4e 54 {
Geremia 2:713844a55c4e 55 if(useNOP) proto->wr_cmd16(cmd); // 0x0000|cmd, 00 is NOP cmd for TFT
Geremia 2:713844a55c4e 56 else proto->wr_cmd8(cmd);
Geremia 2:713844a55c4e 57 }
Geremia 2:713844a55c4e 58 void TFT::wr_data8(unsigned char data)
Geremia 2:713844a55c4e 59 {
Geremia 2:713844a55c4e 60 proto->wr_data8(data);
Geremia 2:713844a55c4e 61 }
Geremia 2:713844a55c4e 62 void TFT::wr_data8(unsigned char data, unsigned int count)
Geremia 2:713844a55c4e 63 {
Geremia 2:713844a55c4e 64 proto->wr_data8(data, count);
Geremia 2:713844a55c4e 65 }
Geremia 2:713844a55c4e 66 void TFT::wr_data8buf(unsigned char* data, unsigned int lenght)
Geremia 2:713844a55c4e 67 {
Geremia 2:713844a55c4e 68 proto->wr_data8buf(data, lenght);
Geremia 2:713844a55c4e 69 }
Geremia 2:713844a55c4e 70 void TFT::wr_cmd16(unsigned short cmd)
Geremia 2:713844a55c4e 71 {
Geremia 2:713844a55c4e 72 proto->wr_cmd16(cmd);
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 2:713844a55c4e 78 void TFT::wr_data16(unsigned short data, unsigned int count)
Geremia 2:713844a55c4e 79 {
Geremia 2:713844a55c4e 80 proto->wr_data16(data, count);
Geremia 2:713844a55c4e 81 }
Geremia 2:713844a55c4e 82 void TFT::wr_data16buf(unsigned short* data, unsigned int lenght)
Geremia 2:713844a55c4e 83 {
Geremia 2:713844a55c4e 84 proto->wr_data16buf(data, lenght);
Geremia 2:713844a55c4e 85 }
Geremia 2:713844a55c4e 86 void TFT::hw_reset()
Geremia 2:713844a55c4e 87 {
Geremia 2:713844a55c4e 88 proto->hw_reset();
Geremia 2:713844a55c4e 89 }
Geremia 2:713844a55c4e 90 void TFT::BusEnable(bool enable)
Geremia 2:713844a55c4e 91 {
Geremia 2:713844a55c4e 92 proto->BusEnable(enable);
Geremia 2:713844a55c4e 93 }
Geremia 2:713844a55c4e 94 // color TFT can rotate in hw (swap raw<->columns) for landscape views
Geremia 2:713844a55c4e 95 void TFT::set_orientation(int o)
Geremia 2:713844a55c4e 96 {
Geremia 2:713844a55c4e 97 orientation = o;
Geremia 2:713844a55c4e 98 wr_cmd8(0x36);
Geremia 2:713844a55c4e 99 switch (orientation) {
Geremia 2:713844a55c4e 100 case 0:// default, portrait view 0°
Geremia 2:713844a55c4e 101 if(mipistd) wr_data8(0x0A); // this is in real a vertical flip enabled, seems most displays are vertical flipped
Geremia 3:48f3282c2be8 102 else wr_data8(0x48); //for some other ILIxxxx
Geremia 2:713844a55c4e 103 set_width(LCDSIZE_X);
Geremia 2:713844a55c4e 104 set_height(LCDSIZE_Y);
Geremia 2:713844a55c4e 105 break;
Geremia 2:713844a55c4e 106 case 1:// landscape view +90°
Geremia 2:713844a55c4e 107 if(mipistd) wr_data8(0x28);
Geremia 3:48f3282c2be8 108 else wr_data8(0x29);//for some other ILIxxxx
Geremia 2:713844a55c4e 109 set_width(LCDSIZE_Y);
Geremia 2:713844a55c4e 110 set_height(LCDSIZE_X);
Geremia 2:713844a55c4e 111 break;
Geremia 2:713844a55c4e 112 case 2:// portrait view +180°
Geremia 2:713844a55c4e 113 if(mipistd) wr_data8(0x09);
Geremia 3:48f3282c2be8 114 else wr_data8(0x99);//for some other ILIxxxx
Geremia 2:713844a55c4e 115 set_width(LCDSIZE_X);
Geremia 2:713844a55c4e 116 set_height(LCDSIZE_Y);
Geremia 2:713844a55c4e 117 break;
Geremia 2:713844a55c4e 118 case 3:// landscape view -90°
Geremia 2:713844a55c4e 119 if(mipistd) wr_data8(0x2B);
Geremia 3:48f3282c2be8 120 else wr_data8(0xF8);//for some other ILIxxxx
Geremia 2:713844a55c4e 121 set_width(LCDSIZE_Y);
Geremia 2:713844a55c4e 122 set_height(LCDSIZE_X);
Geremia 2:713844a55c4e 123 break;
Geremia 2:713844a55c4e 124 }
Geremia 2:713844a55c4e 125 }
Geremia 2:713844a55c4e 126 // TFT have both column and raw autoincrement inside a window, with internal counters
Geremia 2:713844a55c4e 127 void TFT::window(int x, int y, int w, int h)
Geremia 2:713844a55c4e 128 {
Geremia 2:713844a55c4e 129 //ili9486 does not like truncated 2A/2B cmds, at least in par mode
Geremia 2:713844a55c4e 130 //setting only start column/page would speedup, but needs a windowmax() before, maybe implement later
Geremia 3:48f3282c2be8 131 //fixme for PAR_16: // cmd 2A/2B expects 8bit parameters
Geremia 2:713844a55c4e 132 wr_cmd8(0x2A);
Geremia 2:713844a55c4e 133 wr_data16(x); //start column
Geremia 2:713844a55c4e 134 wr_data16(x+w-1);//end column
Geremia 2:713844a55c4e 135
Geremia 2:713844a55c4e 136 wr_cmd8(0x2B);
Geremia 2:713844a55c4e 137 wr_data16(y); //start page
Geremia 2:713844a55c4e 138 wr_data16(y+h-1);//end page
Geremia 2:713844a55c4e 139
Geremia 2:713844a55c4e 140 wr_cmd8(0x2C); //write mem, just send pixels color next
Geremia 2:713844a55c4e 141 }
Geremia 2:713844a55c4e 142 //for TFT, just send data, position counters are in hw
Geremia 2:713844a55c4e 143 void TFT::window_pushpixel(unsigned short color)
Geremia 2:713844a55c4e 144 {
Geremia 2:713844a55c4e 145 proto->wr_data16(color);
Geremia 2:713844a55c4e 146 }
Geremia 2:713844a55c4e 147 void TFT::window_pushpixel(unsigned short color, unsigned int count)
Geremia 2:713844a55c4e 148 {
Geremia 2:713844a55c4e 149 proto->wr_data16(color, count);
Geremia 2:713844a55c4e 150 }
Geremia 2:713844a55c4e 151 void TFT::window_pushpixelbuf(unsigned short* color, unsigned int lenght)
Geremia 2:713844a55c4e 152 {
Geremia 2:713844a55c4e 153 proto->wr_data16buf(color, lenght);
Geremia 2:713844a55c4e 154 }
Geremia 2:713844a55c4e 155 void TFT::pixel(int x, int y, unsigned short color)
Geremia 2:713844a55c4e 156 {
Geremia 2:713844a55c4e 157 window(x,y,1,1);
Geremia 2:713844a55c4e 158 wr_data16(color); // 2C expects 16bit parameters
Geremia 2:713844a55c4e 159 //proto->wr_data16(color);
Geremia 2:713844a55c4e 160 }
Geremia 2:713844a55c4e 161 void TFT::cls (void)
Geremia 2:713844a55c4e 162 {
Geremia 2:713844a55c4e 163 WindowMax();
Geremia 2:713844a55c4e 164 // wr_data16(_background,pixels);
Geremia 2:713844a55c4e 165 wr_data16(0,LCDSIZE_X*LCDSIZE_Y);
Geremia 2:713844a55c4e 166 //proto->wr_data16(_background,pixels);
Geremia 2:713844a55c4e 167 }