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.
Display/TFT.cpp@2:713844a55c4e, 2015-02-13 (annotated)
- Committer:
- Geremia
- Date:
- Fri Feb 13 23:17:55 2015 +0000
- Revision:
- 2:713844a55c4e
- Child:
- 3:48f3282c2be8
Initial TFT implementation, needs to add read cmds
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Geremia | 2:713844a55c4e | 1 | #include "TFT.h" |
Geremia | 2:713844a55c4e | 2 | |
Geremia | 2:713844a55c4e | 3 | //#include "mbed_debug.h" |
Geremia | 2:713844a55c4e | 4 | |
Geremia | 2:713844a55c4e | 5 | #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; } |
Geremia | 2:713844a55c4e | 6 | |
Geremia | 2:713844a55c4e | 7 | 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 | 8 | : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y) |
Geremia | 2:713844a55c4e | 9 | { |
Geremia | 2:713844a55c4e | 10 | if(displayproto==PAR_8) proto = new PAR8(port, CS, reset, DC, WR, RD); |
Geremia | 2:713844a55c4e | 11 | useNOP=false; |
Geremia | 2:713844a55c4e | 12 | set_orientation(0); |
Geremia | 2:713844a55c4e | 13 | foreground(White); |
Geremia | 2:713844a55c4e | 14 | background(Black); |
Geremia | 2:713844a55c4e | 15 | set_auto_up(false); //we don't have framebuffer |
Geremia | 2:713844a55c4e | 16 | mipistd=false; |
Geremia | 2:713844a55c4e | 17 | // cls(); |
Geremia | 2:713844a55c4e | 18 | // locate(0,0); |
Geremia | 2:713844a55c4e | 19 | } |
Geremia | 2:713844a55c4e | 20 | 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 | 21 | : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y) |
Geremia | 2:713844a55c4e | 22 | { |
Geremia | 2:713844a55c4e | 23 | if(displayproto==SPI_8) |
Geremia | 2:713844a55c4e | 24 | { |
Geremia | 2:713844a55c4e | 25 | proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 26 | useNOP=false; |
Geremia | 2:713844a55c4e | 27 | } |
Geremia | 2:713844a55c4e | 28 | else if(displayproto==SPI_16) |
Geremia | 2:713844a55c4e | 29 | { |
Geremia | 2:713844a55c4e | 30 | proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 2:713844a55c4e | 31 | useNOP=true; |
Geremia | 2:713844a55c4e | 32 | } |
Geremia | 2:713844a55c4e | 33 | // cls(); |
Geremia | 2:713844a55c4e | 34 | set_orientation(0); |
Geremia | 2:713844a55c4e | 35 | foreground(White); |
Geremia | 2:713844a55c4e | 36 | background(Black); |
Geremia | 2:713844a55c4e | 37 | set_auto_up(false); |
Geremia | 2:713844a55c4e | 38 | mipistd=false; |
Geremia | 2:713844a55c4e | 39 | // locate(0,0); |
Geremia | 2:713844a55c4e | 40 | } |
Geremia | 2:713844a55c4e | 41 | void TFT::wr_cmd8(unsigned char cmd) |
Geremia | 2:713844a55c4e | 42 | { |
Geremia | 2:713844a55c4e | 43 | if(useNOP) proto->wr_cmd16(cmd); // 0x0000|cmd, 00 is NOP cmd for TFT |
Geremia | 2:713844a55c4e | 44 | else proto->wr_cmd8(cmd); |
Geremia | 2:713844a55c4e | 45 | } |
Geremia | 2:713844a55c4e | 46 | void TFT::wr_data8(unsigned char data) |
Geremia | 2:713844a55c4e | 47 | { |
Geremia | 2:713844a55c4e | 48 | proto->wr_data8(data); |
Geremia | 2:713844a55c4e | 49 | } |
Geremia | 2:713844a55c4e | 50 | void TFT::wr_data8(unsigned char data, unsigned int count) |
Geremia | 2:713844a55c4e | 51 | { |
Geremia | 2:713844a55c4e | 52 | proto->wr_data8(data, count); |
Geremia | 2:713844a55c4e | 53 | } |
Geremia | 2:713844a55c4e | 54 | void TFT::wr_data8buf(unsigned char* data, unsigned int lenght) |
Geremia | 2:713844a55c4e | 55 | { |
Geremia | 2:713844a55c4e | 56 | proto->wr_data8buf(data, lenght); |
Geremia | 2:713844a55c4e | 57 | } |
Geremia | 2:713844a55c4e | 58 | void TFT::wr_cmd16(unsigned short cmd) |
Geremia | 2:713844a55c4e | 59 | { |
Geremia | 2:713844a55c4e | 60 | proto->wr_cmd16(cmd); |
Geremia | 2:713844a55c4e | 61 | } |
Geremia | 2:713844a55c4e | 62 | void TFT::wr_data16(unsigned short data) |
Geremia | 2:713844a55c4e | 63 | { |
Geremia | 2:713844a55c4e | 64 | proto->wr_data16(data); |
Geremia | 2:713844a55c4e | 65 | } |
Geremia | 2:713844a55c4e | 66 | void TFT::wr_data16(unsigned short data, unsigned int count) |
Geremia | 2:713844a55c4e | 67 | { |
Geremia | 2:713844a55c4e | 68 | proto->wr_data16(data, count); |
Geremia | 2:713844a55c4e | 69 | } |
Geremia | 2:713844a55c4e | 70 | void TFT::wr_data16buf(unsigned short* data, unsigned int lenght) |
Geremia | 2:713844a55c4e | 71 | { |
Geremia | 2:713844a55c4e | 72 | proto->wr_data16buf(data, lenght); |
Geremia | 2:713844a55c4e | 73 | } |
Geremia | 2:713844a55c4e | 74 | void TFT::hw_reset() |
Geremia | 2:713844a55c4e | 75 | { |
Geremia | 2:713844a55c4e | 76 | proto->hw_reset(); |
Geremia | 2:713844a55c4e | 77 | } |
Geremia | 2:713844a55c4e | 78 | void TFT::BusEnable(bool enable) |
Geremia | 2:713844a55c4e | 79 | { |
Geremia | 2:713844a55c4e | 80 | proto->BusEnable(enable); |
Geremia | 2:713844a55c4e | 81 | } |
Geremia | 2:713844a55c4e | 82 | // color TFT can rotate in hw (swap raw<->columns) for landscape views |
Geremia | 2:713844a55c4e | 83 | void TFT::set_orientation(int o) |
Geremia | 2:713844a55c4e | 84 | { |
Geremia | 2:713844a55c4e | 85 | orientation = o; |
Geremia | 2:713844a55c4e | 86 | wr_cmd8(0x36); |
Geremia | 2:713844a55c4e | 87 | switch (orientation) { |
Geremia | 2:713844a55c4e | 88 | case 0:// default, portrait view 0° |
Geremia | 2:713844a55c4e | 89 | if(mipistd) wr_data8(0x0A); // this is in real a vertical flip enabled, seems most displays are vertical flipped |
Geremia | 2:713844a55c4e | 90 | else wr_data8(0x48); //48 for 9341 |
Geremia | 2:713844a55c4e | 91 | set_width(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 92 | set_height(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 93 | break; |
Geremia | 2:713844a55c4e | 94 | case 1:// landscape view +90° |
Geremia | 2:713844a55c4e | 95 | if(mipistd) wr_data8(0x28); |
Geremia | 2:713844a55c4e | 96 | else wr_data8(0x29);//28 for 9341 |
Geremia | 2:713844a55c4e | 97 | set_width(LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 98 | set_height(LCDSIZE_X); |
Geremia | 2:713844a55c4e | 99 | break; |
Geremia | 2:713844a55c4e | 100 | case 2:// portrait view +180° |
Geremia | 2:713844a55c4e | 101 | if(mipistd) wr_data8(0x09); |
Geremia | 2:713844a55c4e | 102 | else wr_data8(0x99);//88 for 9341 |
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 3:// landscape view -90° |
Geremia | 2:713844a55c4e | 107 | if(mipistd) wr_data8(0x2B); |
Geremia | 2:713844a55c4e | 108 | else wr_data8(0xF8);//E8 for 9341 |
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 | } |
Geremia | 2:713844a55c4e | 113 | } |
Geremia | 2:713844a55c4e | 114 | // TFT have both column and raw autoincrement inside a window, with internal counters |
Geremia | 2:713844a55c4e | 115 | void TFT::window(int x, int y, int w, int h) |
Geremia | 2:713844a55c4e | 116 | { |
Geremia | 2:713844a55c4e | 117 | //ili9486 does not like truncated 2A/2B cmds, at least in par mode |
Geremia | 2:713844a55c4e | 118 | //setting only start column/page would speedup, but needs a windowmax() before, maybe implement later |
Geremia | 2:713844a55c4e | 119 | wr_cmd8(0x2A); |
Geremia | 2:713844a55c4e | 120 | wr_data16(x); //start column |
Geremia | 2:713844a55c4e | 121 | wr_data16(x+w-1);//end column |
Geremia | 2:713844a55c4e | 122 | |
Geremia | 2:713844a55c4e | 123 | wr_cmd8(0x2B); |
Geremia | 2:713844a55c4e | 124 | wr_data16(y); //start page |
Geremia | 2:713844a55c4e | 125 | wr_data16(y+h-1);//end page |
Geremia | 2:713844a55c4e | 126 | |
Geremia | 2:713844a55c4e | 127 | wr_cmd8(0x2C); //write mem, just send pixels color next |
Geremia | 2:713844a55c4e | 128 | } |
Geremia | 2:713844a55c4e | 129 | //for TFT, just send data, position counters are in hw |
Geremia | 2:713844a55c4e | 130 | void TFT::window_pushpixel(unsigned short color) |
Geremia | 2:713844a55c4e | 131 | { |
Geremia | 2:713844a55c4e | 132 | proto->wr_data16(color); |
Geremia | 2:713844a55c4e | 133 | } |
Geremia | 2:713844a55c4e | 134 | void TFT::window_pushpixel(unsigned short color, unsigned int count) |
Geremia | 2:713844a55c4e | 135 | { |
Geremia | 2:713844a55c4e | 136 | proto->wr_data16(color, count); |
Geremia | 2:713844a55c4e | 137 | } |
Geremia | 2:713844a55c4e | 138 | void TFT::window_pushpixelbuf(unsigned short* color, unsigned int lenght) |
Geremia | 2:713844a55c4e | 139 | { |
Geremia | 2:713844a55c4e | 140 | proto->wr_data16buf(color, lenght); |
Geremia | 2:713844a55c4e | 141 | } |
Geremia | 2:713844a55c4e | 142 | void TFT::pixel(int x, int y, unsigned short color) |
Geremia | 2:713844a55c4e | 143 | { |
Geremia | 2:713844a55c4e | 144 | window(x,y,1,1); |
Geremia | 2:713844a55c4e | 145 | wr_data16(color); // 2C expects 16bit parameters |
Geremia | 2:713844a55c4e | 146 | //proto->wr_data16(color); |
Geremia | 2:713844a55c4e | 147 | } |
Geremia | 2:713844a55c4e | 148 | void TFT::cls (void) |
Geremia | 2:713844a55c4e | 149 | { |
Geremia | 2:713844a55c4e | 150 | WindowMax(); |
Geremia | 2:713844a55c4e | 151 | // wr_data16(_background,pixels); |
Geremia | 2:713844a55c4e | 152 | wr_data16(0,LCDSIZE_X*LCDSIZE_Y); |
Geremia | 2:713844a55c4e | 153 | //proto->wr_data16(_background,pixels); |
Geremia | 2:713844a55c4e | 154 | } |