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.

Dependents:   Brew

Committer:
Geremia
Date:
Fri Feb 13 23:17:55 2015 +0000
Revision:
2:713844a55c4e
Parent:
1:ff019d22b275
Child:
3:48f3282c2be8
Initial TFT implementation, needs to add read cmds

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 0:75ec1b3cde17 1 #include "LCD.h"
Geremia 0:75ec1b3cde17 2
Geremia 0:75ec1b3cde17 3 //#include "mbed_debug.h"
Geremia 0:75ec1b3cde17 4 //#define LCDPAGES (LCDSIZE_Y>>3) // 8raws per page
Geremia 0:75ec1b3cde17 5 //#define IC_PAGES (IC_Y_COMS>>3) // max pages in IC ddram, 8raws per page
Geremia 0:75ec1b3cde17 6 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
Geremia 1:ff019d22b275 7 //#define USEFRAMEBUFFER
Geremia 2:713844a55c4e 8
Geremia 0:75ec1b3cde17 9 //#define FRAMEBUFSIZE (LCDSIZE_X*LCDPAGES)
Geremia 2:713844a55c4e 10
Geremia 0:75ec1b3cde17 11
Geremia 0:75ec1b3cde17 12 LCD::LCD(proto_t displayproto, PortName port, PinName CS, PinName reset, PinName DC, PinName WR, PinName RD, const int lcdsize_x, const int lcdsize_y, const int ic_x_segs, const int ic_y_coms, const char *name)
Geremia 0:75ec1b3cde17 13 : /*PAR8(port, CS, reset, DC, WR, RD),*/ GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y), LCDPAGES(lcdsize_y>>3), IC_X_SEGS(ic_x_segs), IC_Y_COMS(ic_y_coms), IC_PAGES(ic_y_coms>>3)
Geremia 0:75ec1b3cde17 14 {
Geremia 0:75ec1b3cde17 15 // LCDPAGES = LCDSIZE_Y>>3;
Geremia 0:75ec1b3cde17 16 // IC_PAGES = IC_Y_COMS>>3;
Geremia 0:75ec1b3cde17 17 // buffer = new unsigned char [LCDSIZE_X*LCDPAGES];
Geremia 0:75ec1b3cde17 18 // PAR8 par8proto(port, CS, reset, DC, WR, RD);
Geremia 0:75ec1b3cde17 19 if(displayproto==PAR_8) proto = new PAR8(port, CS, reset, DC, WR, RD);
Geremia 1:ff019d22b275 20 useNOP=false;
Geremia 0:75ec1b3cde17 21 buffer = (unsigned char*) malloc (LCDSIZE_X*LCDPAGES);
Geremia 1:ff019d22b275 22 buffer16 = (unsigned short*)buffer;
Geremia 0:75ec1b3cde17 23 draw_mode = NORMAL;
Geremia 0:75ec1b3cde17 24 set_orientation(1);
Geremia 2:713844a55c4e 25 foreground(Black);
Geremia 2:713844a55c4e 26 background(White);
Geremia 2:713844a55c4e 27 set_auto_up(true);
Geremia 0:75ec1b3cde17 28 // cls();
Geremia 0:75ec1b3cde17 29 // locate(0,0);
Geremia 0:75ec1b3cde17 30 }
Geremia 1:ff019d22b275 31 LCD::LCD(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 int ic_x_segs, const int ic_y_coms, const char *name)
Geremia 0:75ec1b3cde17 32 : GraphicsDisplay(name), LCDSIZE_X(lcdsize_x), LCDSIZE_Y(lcdsize_y), LCDPAGES(lcdsize_y>>3), IC_X_SEGS(ic_x_segs), IC_Y_COMS(ic_y_coms), IC_PAGES(ic_y_coms>>3)
Geremia 0:75ec1b3cde17 33 {
Geremia 0:75ec1b3cde17 34 // LCDPAGES = LCDSIZE_Y>>3;
Geremia 0:75ec1b3cde17 35 // IC_PAGES = IC_Y_COMS>>3;
Geremia 0:75ec1b3cde17 36 // buffer = new unsigned char [LCDSIZE_X*LCDPAGES];
Geremia 0:75ec1b3cde17 37 // PAR8 par8proto(port, CS, reset, DC, WR, RD);
Geremia 1:ff019d22b275 38 if(displayproto==SPI_8)
Geremia 1:ff019d22b275 39 {
Geremia 1:ff019d22b275 40 proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 1:ff019d22b275 41 useNOP=false;
Geremia 1:ff019d22b275 42 }
Geremia 1:ff019d22b275 43 else if(displayproto==SPI_16)
Geremia 1:ff019d22b275 44 {
Geremia 1:ff019d22b275 45 proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 1:ff019d22b275 46 useNOP=true;
Geremia 1:ff019d22b275 47 }
Geremia 0:75ec1b3cde17 48 buffer = (unsigned char*) malloc (LCDSIZE_X*LCDPAGES);
Geremia 1:ff019d22b275 49 buffer16 = (unsigned short*)buffer;
Geremia 0:75ec1b3cde17 50 draw_mode = NORMAL;
Geremia 0:75ec1b3cde17 51 // cls();
Geremia 0:75ec1b3cde17 52 set_orientation(1);
Geremia 2:713844a55c4e 53 foreground(Black);
Geremia 2:713844a55c4e 54 background(White);
Geremia 2:713844a55c4e 55 set_auto_up(true);
Geremia 0:75ec1b3cde17 56 // locate(0,0);
Geremia 1:ff019d22b275 57
Geremia 0:75ec1b3cde17 58 }
Geremia 0:75ec1b3cde17 59 LCD::~LCD()
Geremia 0:75ec1b3cde17 60 {
Geremia 0:75ec1b3cde17 61 free(buffer);
Geremia 0:75ec1b3cde17 62 }
Geremia 0:75ec1b3cde17 63
Geremia 1:ff019d22b275 64 void LCD::wr_cmd8(unsigned char cmd)
Geremia 0:75ec1b3cde17 65 {
Geremia 2:713844a55c4e 66 if(useNOP) proto->wr_cmd16(0xE300|cmd); // E3 is NOP cmd for LCD
Geremia 1:ff019d22b275 67 else proto->wr_cmd8(cmd);
Geremia 0:75ec1b3cde17 68 }
Geremia 1:ff019d22b275 69 void LCD::wr_data8(unsigned char data)
Geremia 0:75ec1b3cde17 70 {
Geremia 1:ff019d22b275 71 proto->wr_data8(data);
Geremia 1:ff019d22b275 72 }
Geremia 1:ff019d22b275 73 void LCD::wr_data8(unsigned char data, unsigned int count)
Geremia 1:ff019d22b275 74 {
Geremia 1:ff019d22b275 75 proto->wr_data8(data, count);
Geremia 0:75ec1b3cde17 76 }
Geremia 1:ff019d22b275 77 void LCD::wr_data8buf(unsigned char* data, unsigned int lenght)
Geremia 0:75ec1b3cde17 78 {
Geremia 1:ff019d22b275 79 proto->wr_data8buf(data, lenght);
Geremia 1:ff019d22b275 80 }
Geremia 1:ff019d22b275 81 void LCD::wr_cmd16(unsigned short cmd)
Geremia 1:ff019d22b275 82 {
Geremia 1:ff019d22b275 83 proto->wr_cmd16(cmd);
Geremia 0:75ec1b3cde17 84 }
Geremia 1:ff019d22b275 85 void LCD::wr_data16(unsigned short data, unsigned int count)
Geremia 0:75ec1b3cde17 86 {
Geremia 1:ff019d22b275 87 proto->wr_data16(data, count);
Geremia 1:ff019d22b275 88 }
Geremia 1:ff019d22b275 89 void LCD::wr_data16buf(unsigned short* data, unsigned int lenght)
Geremia 1:ff019d22b275 90 {
Geremia 1:ff019d22b275 91 proto->wr_data16buf(data, lenght);
Geremia 0:75ec1b3cde17 92 }
Geremia 0:75ec1b3cde17 93 void LCD::hw_reset()
Geremia 0:75ec1b3cde17 94 {
Geremia 0:75ec1b3cde17 95 proto->hw_reset();
Geremia 0:75ec1b3cde17 96 }
Geremia 0:75ec1b3cde17 97 void LCD::BusEnable(bool enable)
Geremia 0:75ec1b3cde17 98 {
Geremia 0:75ec1b3cde17 99 proto->BusEnable(enable);
Geremia 0:75ec1b3cde17 100 }
Geremia 0:75ec1b3cde17 101
Geremia 0:75ec1b3cde17 102
Geremia 0:75ec1b3cde17 103
Geremia 0:75ec1b3cde17 104 // monochrome LCD driver ICs does not have ram rotate in hw (swap raw<->columns) like TFT displays
Geremia 0:75ec1b3cde17 105 // for portrait views, XY swap will be done in sw in pixel() function
Geremia 0:75ec1b3cde17 106 void LCD::set_orientation(int o)
Geremia 0:75ec1b3cde17 107 {
Geremia 0:75ec1b3cde17 108 orientation = o;
Geremia 0:75ec1b3cde17 109 switch (o) {
Geremia 0:75ec1b3cde17 110 case (0):// portrait view -90°
Geremia 0:75ec1b3cde17 111 mirrorXY(Y);
Geremia 0:75ec1b3cde17 112 col_offset = 0;
Geremia 0:75ec1b3cde17 113 page_offset = IC_PAGES-LCDPAGES;
Geremia 0:75ec1b3cde17 114 set_width(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 115 set_height(LCDSIZE_X);
Geremia 0:75ec1b3cde17 116 // portrait = true;
Geremia 0:75ec1b3cde17 117 break;
Geremia 0:75ec1b3cde17 118 case (1): // default, landscape view 0°
Geremia 0:75ec1b3cde17 119 mirrorXY(NONE);
Geremia 0:75ec1b3cde17 120 col_offset = 0;
Geremia 0:75ec1b3cde17 121 page_offset = 0;
Geremia 0:75ec1b3cde17 122 set_width(LCDSIZE_X);
Geremia 0:75ec1b3cde17 123 set_height(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 124 // portrait = false;
Geremia 0:75ec1b3cde17 125 break;
Geremia 0:75ec1b3cde17 126 case (2):// portrait view +90°
Geremia 0:75ec1b3cde17 127 mirrorXY(X);
Geremia 0:75ec1b3cde17 128 col_offset = IC_X_SEGS-LCDSIZE_X; // some displays have less pixels than IC ram
Geremia 0:75ec1b3cde17 129 page_offset = 0;
Geremia 0:75ec1b3cde17 130 set_width(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 131 set_height(LCDSIZE_X);
Geremia 0:75ec1b3cde17 132 // portrait = true;
Geremia 0:75ec1b3cde17 133 break;
Geremia 0:75ec1b3cde17 134 case (3):// landscape view +180°
Geremia 0:75ec1b3cde17 135 mirrorXY(XY);
Geremia 0:75ec1b3cde17 136 col_offset = IC_X_SEGS-LCDSIZE_X;
Geremia 0:75ec1b3cde17 137 page_offset = IC_PAGES-LCDPAGES;
Geremia 0:75ec1b3cde17 138 set_width(LCDSIZE_X);
Geremia 0:75ec1b3cde17 139 set_height(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 140 // portrait = false;
Geremia 0:75ec1b3cde17 141 break;
Geremia 0:75ec1b3cde17 142 }
Geremia 0:75ec1b3cde17 143 }
Geremia 0:75ec1b3cde17 144 void LCD::mirrorXY(mirror_t mode)
Geremia 0:75ec1b3cde17 145 {
Geremia 0:75ec1b3cde17 146 switch (mode)
Geremia 0:75ec1b3cde17 147 {
Geremia 0:75ec1b3cde17 148 case(NONE):
Geremia 1:ff019d22b275 149 // wr_cmd8(0xA0);
Geremia 1:ff019d22b275 150 wr_cmd16(0xA0C8); // this is in real Y mirror command, but seems most displays have COMs wired inverted, so assume this is the default no-y-mirror
Geremia 0:75ec1b3cde17 151 break;
Geremia 0:75ec1b3cde17 152 case(X):
Geremia 1:ff019d22b275 153 // wr_cmd8(0xA1);
Geremia 1:ff019d22b275 154 wr_cmd16(0xA1C8);
Geremia 0:75ec1b3cde17 155 break;
Geremia 0:75ec1b3cde17 156 case(Y):
Geremia 1:ff019d22b275 157 // wr_cmd8(0xA0);
Geremia 1:ff019d22b275 158 wr_cmd16(0xA0C0);
Geremia 0:75ec1b3cde17 159 break;
Geremia 0:75ec1b3cde17 160 case(XY):
Geremia 1:ff019d22b275 161 // wr_cmd8(0xA1);
Geremia 1:ff019d22b275 162 wr_cmd16(0xA1C0);
Geremia 0:75ec1b3cde17 163 break;
Geremia 0:75ec1b3cde17 164 }
Geremia 0:75ec1b3cde17 165 }
Geremia 0:75ec1b3cde17 166 void LCD::invert(unsigned char o)
Geremia 0:75ec1b3cde17 167 {
Geremia 1:ff019d22b275 168 if(o == 0) wr_cmd8(0xA6);
Geremia 1:ff019d22b275 169 else wr_cmd8(0xA7);
Geremia 0:75ec1b3cde17 170 }
Geremia 0:75ec1b3cde17 171
Geremia 0:75ec1b3cde17 172 void LCD::set_contrast(int o)
Geremia 0:75ec1b3cde17 173 {
Geremia 0:75ec1b3cde17 174 contrast = o;
Geremia 1:ff019d22b275 175 // wr_cmd8(0x81); // set volume
Geremia 1:ff019d22b275 176 wr_cmd16(0x8100|(o&0x3F));
Geremia 0:75ec1b3cde17 177 }
Geremia 2:713844a55c4e 178
Geremia 0:75ec1b3cde17 179 int LCD::get_contrast(void)
Geremia 0:75ec1b3cde17 180 {
Geremia 0:75ec1b3cde17 181 return(contrast);
Geremia 0:75ec1b3cde17 182 }
Geremia 0:75ec1b3cde17 183 void LCD::window(int x, int y, int w, int h) {
Geremia 0:75ec1b3cde17 184 // current pixel location
Geremia 0:75ec1b3cde17 185 cur_x = x;
Geremia 0:75ec1b3cde17 186 cur_y = y;
Geremia 0:75ec1b3cde17 187 // window settings
Geremia 0:75ec1b3cde17 188 win_x1 = x;
Geremia 0:75ec1b3cde17 189 win_x2 = x + w - 1;
Geremia 0:75ec1b3cde17 190 win_y1 = y;
Geremia 0:75ec1b3cde17 191 win_y2 = y + h - 1;
Geremia 0:75ec1b3cde17 192 }
Geremia 0:75ec1b3cde17 193 void LCD::window_pushpixel(unsigned short color) {
Geremia 0:75ec1b3cde17 194 pixel(cur_x, cur_y, color);
Geremia 0:75ec1b3cde17 195 cur_x++;
Geremia 0:75ec1b3cde17 196 if(cur_x > win_x2) {
Geremia 0:75ec1b3cde17 197 cur_x = win_x1;
Geremia 0:75ec1b3cde17 198 cur_y++;
Geremia 0:75ec1b3cde17 199 if(cur_y > win_y2) {
Geremia 0:75ec1b3cde17 200 cur_y = win_y1;
Geremia 0:75ec1b3cde17 201 }
Geremia 0:75ec1b3cde17 202 }
Geremia 0:75ec1b3cde17 203 }
Geremia 2:713844a55c4e 204 void LCD::window_pushpixel(unsigned short color, unsigned int count) {
Geremia 2:713844a55c4e 205 while(count)
Geremia 2:713844a55c4e 206 {
Geremia 2:713844a55c4e 207 pixel(cur_x, cur_y, color);
Geremia 2:713844a55c4e 208 cur_x++;
Geremia 2:713844a55c4e 209 if(cur_x > win_x2)
Geremia 2:713844a55c4e 210 {
Geremia 2:713844a55c4e 211 cur_x = win_x1;
Geremia 2:713844a55c4e 212 cur_y++;
Geremia 2:713844a55c4e 213 if(cur_y > win_y2)
Geremia 2:713844a55c4e 214 {
Geremia 2:713844a55c4e 215 cur_y = win_y1;
Geremia 2:713844a55c4e 216 }
Geremia 2:713844a55c4e 217 }
Geremia 2:713844a55c4e 218 count--;
Geremia 2:713844a55c4e 219 }
Geremia 2:713844a55c4e 220 }
Geremia 2:713844a55c4e 221 void LCD::window_pushpixelbuf(unsigned short* color, unsigned int lenght) {
Geremia 2:713844a55c4e 222 while(lenght)
Geremia 2:713844a55c4e 223 {
Geremia 2:713844a55c4e 224 pixel(cur_x, cur_y, *color++);
Geremia 2:713844a55c4e 225 cur_x++;
Geremia 2:713844a55c4e 226 if(cur_x > win_x2)
Geremia 2:713844a55c4e 227 {
Geremia 2:713844a55c4e 228 cur_x = win_x1;
Geremia 2:713844a55c4e 229 cur_y++;
Geremia 2:713844a55c4e 230 if(cur_y > win_y2)
Geremia 2:713844a55c4e 231 {
Geremia 2:713844a55c4e 232 cur_y = win_y1;
Geremia 2:713844a55c4e 233 }
Geremia 2:713844a55c4e 234 }
Geremia 2:713844a55c4e 235 lenght--;
Geremia 2:713844a55c4e 236 }
Geremia 2:713844a55c4e 237 }
Geremia 0:75ec1b3cde17 238 void LCD::pixel(int x, int y, unsigned short color)
Geremia 0:75ec1b3cde17 239 {
Geremia 0:75ec1b3cde17 240 if(!(orientation&1)) SWAP(x,y);
Geremia 0:75ec1b3cde17 241 // first check parameter
Geremia 0:75ec1b3cde17 242 if((x >= LCDSIZE_X) || (y >= LCDSIZE_Y)) return;
Geremia 0:75ec1b3cde17 243
Geremia 0:75ec1b3cde17 244 // if(draw_mode == NORMAL)
Geremia 0:75ec1b3cde17 245 // {
Geremia 2:713844a55c4e 246 if(color == Black) buffer[(x + ((y>>3)*LCDSIZE_X))^1] &= ~(1 << (y&7)); // erase pixel
Geremia 1:ff019d22b275 247 else buffer[(x + ((y>>3)*LCDSIZE_X))^1] |= (1 << (y&7)); // set pixel
Geremia 0:75ec1b3cde17 248 // }
Geremia 0:75ec1b3cde17 249 // else
Geremia 0:75ec1b3cde17 250 // { // XOR mode
Geremia 0:75ec1b3cde17 251 // if(color == 1) buffer[x + ((y>>3) * LCDSIZE_X)] ^= (1 << (y&7)); // xor pixel
Geremia 0:75ec1b3cde17 252 // }
Geremia 0:75ec1b3cde17 253 }
Geremia 0:75ec1b3cde17 254 void LCD::copy_to_lcd(void)
Geremia 0:75ec1b3cde17 255 {
Geremia 0:75ec1b3cde17 256 unsigned short i=0;
Geremia 1:ff019d22b275 257 unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4);
Geremia 0:75ec1b3cde17 258 for(int page=0; page<LCDPAGES; page++)
Geremia 0:75ec1b3cde17 259 {
Geremia 1:ff019d22b275 260 // wr_cmd8(col_offset&0xF); // set column low nibble
Geremia 1:ff019d22b275 261 // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble
Geremia 1:ff019d22b275 262 wr_cmd16(setcolcmd);
Geremia 1:ff019d22b275 263 wr_cmd8(0xB0|(page+page_offset)); // set page
Geremia 1:ff019d22b275 264 wr_data16buf(buffer16+i, LCDSIZE_X>>1); // send whole page pixels
Geremia 1:ff019d22b275 265 i+=LCDSIZE_X>>1;
Geremia 0:75ec1b3cde17 266 }
Geremia 0:75ec1b3cde17 267 }
Geremia 0:75ec1b3cde17 268 void LCD::cls(void)
Geremia 0:75ec1b3cde17 269 {
Geremia 0:75ec1b3cde17 270 memset(buffer,0x00,LCDSIZE_X*LCDPAGES); // clear display buffer
Geremia 1:ff019d22b275 271 unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4);
Geremia 0:75ec1b3cde17 272 for(int page=0; page<LCDPAGES; page++)
Geremia 0:75ec1b3cde17 273 {
Geremia 1:ff019d22b275 274 // wr_cmd8((unsigned char)col_offset&0xF); // set column low nibble
Geremia 1:ff019d22b275 275 // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble
Geremia 1:ff019d22b275 276 wr_cmd16(setcolcmd);
Geremia 1:ff019d22b275 277 wr_cmd8(0xB0|(page+page_offset)); // set page
Geremia 1:ff019d22b275 278 wr_data16(0, LCDSIZE_X>>1); // send whole page pixels =0
Geremia 0:75ec1b3cde17 279 }
Geremia 0:75ec1b3cde17 280 }