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:
Sun Feb 15 20:06:07 2015 +0000
Revision:
4:12ba0ecc2c1f
Parent:
3:48f3282c2be8
Child:
7:bb0383b91104
Added PAR16, separated 16bit writes for cmd parameters and pixeldata

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Geremia 4:12ba0ecc2c1f 1 /* mbed UniGraphic library - universal LCD 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 the mbed Lab Board 128*32 pixel LCD
Geremia 3:48f3282c2be8 8 * use C12832 controller
Geremia 3:48f3282c2be8 9 * Copyright (c) 2012 Peter Drescher - DC2PD
Geremia 3:48f3282c2be8 10 * Released under the MIT License: http://mbed.org/license/mit
Geremia 3:48f3282c2be8 11 *
Geremia 3:48f3282c2be8 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Geremia 3:48f3282c2be8 13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Geremia 3:48f3282c2be8 14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Geremia 3:48f3282c2be8 15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Geremia 3:48f3282c2be8 16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Geremia 3:48f3282c2be8 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Geremia 3:48f3282c2be8 18 * THE SOFTWARE.
Geremia 3:48f3282c2be8 19 */
Geremia 3:48f3282c2be8 20
Geremia 0:75ec1b3cde17 21 #include "LCD.h"
Geremia 0:75ec1b3cde17 22
Geremia 0:75ec1b3cde17 23 //#include "mbed_debug.h"
Geremia 0:75ec1b3cde17 24 //#define LCDPAGES (LCDSIZE_Y>>3) // 8raws per page
Geremia 0:75ec1b3cde17 25 //#define IC_PAGES (IC_Y_COMS>>3) // max pages in IC ddram, 8raws per page
Geremia 0:75ec1b3cde17 26 #define SWAP(a, b) { a ^= b; b ^= a; a ^= b; }
Geremia 1:ff019d22b275 27 //#define USEFRAMEBUFFER
Geremia 2:713844a55c4e 28
Geremia 0:75ec1b3cde17 29 //#define FRAMEBUFSIZE (LCDSIZE_X*LCDPAGES)
Geremia 2:713844a55c4e 30
Geremia 0:75ec1b3cde17 31
Geremia 0:75ec1b3cde17 32 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 33 : /*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 34 {
Geremia 0:75ec1b3cde17 35 // LCDPAGES = LCDSIZE_Y>>3;
Geremia 0:75ec1b3cde17 36 // IC_PAGES = IC_Y_COMS>>3;
Geremia 0:75ec1b3cde17 37 // buffer = new unsigned char [LCDSIZE_X*LCDPAGES];
Geremia 0:75ec1b3cde17 38 // PAR8 par8proto(port, CS, reset, DC, WR, RD);
Geremia 0:75ec1b3cde17 39 if(displayproto==PAR_8) proto = new PAR8(port, CS, reset, DC, WR, RD);
Geremia 1:ff019d22b275 40 useNOP=false;
Geremia 0:75ec1b3cde17 41 buffer = (unsigned char*) malloc (LCDSIZE_X*LCDPAGES);
Geremia 1:ff019d22b275 42 buffer16 = (unsigned short*)buffer;
Geremia 0:75ec1b3cde17 43 draw_mode = NORMAL;
Geremia 0:75ec1b3cde17 44 set_orientation(1);
Geremia 4:12ba0ecc2c1f 45 foreground(White);
Geremia 4:12ba0ecc2c1f 46 background(Black);
Geremia 2:713844a55c4e 47 set_auto_up(true);
Geremia 0:75ec1b3cde17 48 // cls();
Geremia 0:75ec1b3cde17 49 // locate(0,0);
Geremia 0:75ec1b3cde17 50 }
Geremia 1:ff019d22b275 51 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 52 : 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 53 {
Geremia 0:75ec1b3cde17 54 // LCDPAGES = LCDSIZE_Y>>3;
Geremia 0:75ec1b3cde17 55 // IC_PAGES = IC_Y_COMS>>3;
Geremia 0:75ec1b3cde17 56 // buffer = new unsigned char [LCDSIZE_X*LCDPAGES];
Geremia 0:75ec1b3cde17 57 // PAR8 par8proto(port, CS, reset, DC, WR, RD);
Geremia 1:ff019d22b275 58 if(displayproto==SPI_8)
Geremia 1:ff019d22b275 59 {
Geremia 1:ff019d22b275 60 proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 1:ff019d22b275 61 useNOP=false;
Geremia 1:ff019d22b275 62 }
Geremia 1:ff019d22b275 63 else if(displayproto==SPI_16)
Geremia 1:ff019d22b275 64 {
Geremia 1:ff019d22b275 65 proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC);
Geremia 1:ff019d22b275 66 useNOP=true;
Geremia 1:ff019d22b275 67 }
Geremia 0:75ec1b3cde17 68 buffer = (unsigned char*) malloc (LCDSIZE_X*LCDPAGES);
Geremia 1:ff019d22b275 69 buffer16 = (unsigned short*)buffer;
Geremia 0:75ec1b3cde17 70 draw_mode = NORMAL;
Geremia 0:75ec1b3cde17 71 // cls();
Geremia 0:75ec1b3cde17 72 set_orientation(1);
Geremia 4:12ba0ecc2c1f 73 foreground(White);
Geremia 4:12ba0ecc2c1f 74 background(Black);
Geremia 2:713844a55c4e 75 set_auto_up(true);
Geremia 0:75ec1b3cde17 76 // locate(0,0);
Geremia 1:ff019d22b275 77
Geremia 0:75ec1b3cde17 78 }
Geremia 0:75ec1b3cde17 79 LCD::~LCD()
Geremia 0:75ec1b3cde17 80 {
Geremia 0:75ec1b3cde17 81 free(buffer);
Geremia 0:75ec1b3cde17 82 }
Geremia 0:75ec1b3cde17 83
Geremia 1:ff019d22b275 84 void LCD::wr_cmd8(unsigned char cmd)
Geremia 0:75ec1b3cde17 85 {
Geremia 2:713844a55c4e 86 if(useNOP) proto->wr_cmd16(0xE300|cmd); // E3 is NOP cmd for LCD
Geremia 1:ff019d22b275 87 else proto->wr_cmd8(cmd);
Geremia 0:75ec1b3cde17 88 }
Geremia 1:ff019d22b275 89 void LCD::wr_data8(unsigned char data)
Geremia 0:75ec1b3cde17 90 {
Geremia 1:ff019d22b275 91 proto->wr_data8(data);
Geremia 1:ff019d22b275 92 }
Geremia 1:ff019d22b275 93 void LCD::wr_cmd16(unsigned short cmd)
Geremia 1:ff019d22b275 94 {
Geremia 1:ff019d22b275 95 proto->wr_cmd16(cmd);
Geremia 0:75ec1b3cde17 96 }
Geremia 4:12ba0ecc2c1f 97 void LCD::wr_gram(unsigned short data, unsigned int count)
Geremia 0:75ec1b3cde17 98 {
Geremia 4:12ba0ecc2c1f 99 proto->wr_gram(data, count);
Geremia 1:ff019d22b275 100 }
Geremia 4:12ba0ecc2c1f 101 void LCD::wr_grambuf(unsigned short* data, unsigned int lenght)
Geremia 1:ff019d22b275 102 {
Geremia 4:12ba0ecc2c1f 103 proto->wr_grambuf(data, lenght);
Geremia 0:75ec1b3cde17 104 }
Geremia 0:75ec1b3cde17 105 void LCD::hw_reset()
Geremia 0:75ec1b3cde17 106 {
Geremia 0:75ec1b3cde17 107 proto->hw_reset();
Geremia 0:75ec1b3cde17 108 }
Geremia 0:75ec1b3cde17 109 void LCD::BusEnable(bool enable)
Geremia 0:75ec1b3cde17 110 {
Geremia 0:75ec1b3cde17 111 proto->BusEnable(enable);
Geremia 0:75ec1b3cde17 112 }
Geremia 0:75ec1b3cde17 113
Geremia 0:75ec1b3cde17 114
Geremia 0:75ec1b3cde17 115
Geremia 0:75ec1b3cde17 116 // monochrome LCD driver ICs does not have ram rotate in hw (swap raw<->columns) like TFT displays
Geremia 0:75ec1b3cde17 117 // for portrait views, XY swap will be done in sw in pixel() function
Geremia 0:75ec1b3cde17 118 void LCD::set_orientation(int o)
Geremia 0:75ec1b3cde17 119 {
Geremia 0:75ec1b3cde17 120 orientation = o;
Geremia 0:75ec1b3cde17 121 switch (o) {
Geremia 0:75ec1b3cde17 122 case (0):// portrait view -90°
Geremia 0:75ec1b3cde17 123 mirrorXY(Y);
Geremia 0:75ec1b3cde17 124 col_offset = 0;
Geremia 0:75ec1b3cde17 125 page_offset = IC_PAGES-LCDPAGES;
Geremia 0:75ec1b3cde17 126 set_width(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 127 set_height(LCDSIZE_X);
Geremia 0:75ec1b3cde17 128 // portrait = true;
Geremia 0:75ec1b3cde17 129 break;
Geremia 0:75ec1b3cde17 130 case (1): // default, landscape view 0°
Geremia 0:75ec1b3cde17 131 mirrorXY(NONE);
Geremia 0:75ec1b3cde17 132 col_offset = 0;
Geremia 0:75ec1b3cde17 133 page_offset = 0;
Geremia 0:75ec1b3cde17 134 set_width(LCDSIZE_X);
Geremia 0:75ec1b3cde17 135 set_height(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 136 // portrait = false;
Geremia 0:75ec1b3cde17 137 break;
Geremia 0:75ec1b3cde17 138 case (2):// portrait view +90°
Geremia 0:75ec1b3cde17 139 mirrorXY(X);
Geremia 0:75ec1b3cde17 140 col_offset = IC_X_SEGS-LCDSIZE_X; // some displays have less pixels than IC ram
Geremia 0:75ec1b3cde17 141 page_offset = 0;
Geremia 0:75ec1b3cde17 142 set_width(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 143 set_height(LCDSIZE_X);
Geremia 0:75ec1b3cde17 144 // portrait = true;
Geremia 0:75ec1b3cde17 145 break;
Geremia 0:75ec1b3cde17 146 case (3):// landscape view +180°
Geremia 0:75ec1b3cde17 147 mirrorXY(XY);
Geremia 0:75ec1b3cde17 148 col_offset = IC_X_SEGS-LCDSIZE_X;
Geremia 0:75ec1b3cde17 149 page_offset = IC_PAGES-LCDPAGES;
Geremia 0:75ec1b3cde17 150 set_width(LCDSIZE_X);
Geremia 0:75ec1b3cde17 151 set_height(LCDSIZE_Y);
Geremia 0:75ec1b3cde17 152 // portrait = false;
Geremia 0:75ec1b3cde17 153 break;
Geremia 0:75ec1b3cde17 154 }
Geremia 0:75ec1b3cde17 155 }
Geremia 0:75ec1b3cde17 156 void LCD::mirrorXY(mirror_t mode)
Geremia 0:75ec1b3cde17 157 {
Geremia 0:75ec1b3cde17 158 switch (mode)
Geremia 0:75ec1b3cde17 159 {
Geremia 0:75ec1b3cde17 160 case(NONE):
Geremia 1:ff019d22b275 161 // wr_cmd8(0xA0);
Geremia 1:ff019d22b275 162 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 163 break;
Geremia 0:75ec1b3cde17 164 case(X):
Geremia 1:ff019d22b275 165 // wr_cmd8(0xA1);
Geremia 1:ff019d22b275 166 wr_cmd16(0xA1C8);
Geremia 0:75ec1b3cde17 167 break;
Geremia 0:75ec1b3cde17 168 case(Y):
Geremia 1:ff019d22b275 169 // wr_cmd8(0xA0);
Geremia 1:ff019d22b275 170 wr_cmd16(0xA0C0);
Geremia 0:75ec1b3cde17 171 break;
Geremia 0:75ec1b3cde17 172 case(XY):
Geremia 1:ff019d22b275 173 // wr_cmd8(0xA1);
Geremia 1:ff019d22b275 174 wr_cmd16(0xA1C0);
Geremia 0:75ec1b3cde17 175 break;
Geremia 0:75ec1b3cde17 176 }
Geremia 0:75ec1b3cde17 177 }
Geremia 0:75ec1b3cde17 178 void LCD::invert(unsigned char o)
Geremia 0:75ec1b3cde17 179 {
Geremia 1:ff019d22b275 180 if(o == 0) wr_cmd8(0xA6);
Geremia 1:ff019d22b275 181 else wr_cmd8(0xA7);
Geremia 0:75ec1b3cde17 182 }
Geremia 0:75ec1b3cde17 183
Geremia 0:75ec1b3cde17 184 void LCD::set_contrast(int o)
Geremia 0:75ec1b3cde17 185 {
Geremia 0:75ec1b3cde17 186 contrast = o;
Geremia 1:ff019d22b275 187 // wr_cmd8(0x81); // set volume
Geremia 1:ff019d22b275 188 wr_cmd16(0x8100|(o&0x3F));
Geremia 0:75ec1b3cde17 189 }
Geremia 2:713844a55c4e 190
Geremia 0:75ec1b3cde17 191 int LCD::get_contrast(void)
Geremia 0:75ec1b3cde17 192 {
Geremia 0:75ec1b3cde17 193 return(contrast);
Geremia 0:75ec1b3cde17 194 }
Geremia 0:75ec1b3cde17 195 void LCD::window(int x, int y, int w, int h) {
Geremia 0:75ec1b3cde17 196 // current pixel location
Geremia 0:75ec1b3cde17 197 cur_x = x;
Geremia 0:75ec1b3cde17 198 cur_y = y;
Geremia 0:75ec1b3cde17 199 // window settings
Geremia 0:75ec1b3cde17 200 win_x1 = x;
Geremia 0:75ec1b3cde17 201 win_x2 = x + w - 1;
Geremia 0:75ec1b3cde17 202 win_y1 = y;
Geremia 0:75ec1b3cde17 203 win_y2 = y + h - 1;
Geremia 0:75ec1b3cde17 204 }
Geremia 0:75ec1b3cde17 205 void LCD::window_pushpixel(unsigned short color) {
Geremia 0:75ec1b3cde17 206 pixel(cur_x, cur_y, color);
Geremia 0:75ec1b3cde17 207 cur_x++;
Geremia 0:75ec1b3cde17 208 if(cur_x > win_x2) {
Geremia 0:75ec1b3cde17 209 cur_x = win_x1;
Geremia 0:75ec1b3cde17 210 cur_y++;
Geremia 0:75ec1b3cde17 211 if(cur_y > win_y2) {
Geremia 0:75ec1b3cde17 212 cur_y = win_y1;
Geremia 0:75ec1b3cde17 213 }
Geremia 0:75ec1b3cde17 214 }
Geremia 0:75ec1b3cde17 215 }
Geremia 2:713844a55c4e 216 void LCD::window_pushpixel(unsigned short color, unsigned int count) {
Geremia 2:713844a55c4e 217 while(count)
Geremia 2:713844a55c4e 218 {
Geremia 2:713844a55c4e 219 pixel(cur_x, cur_y, color);
Geremia 2:713844a55c4e 220 cur_x++;
Geremia 2:713844a55c4e 221 if(cur_x > win_x2)
Geremia 2:713844a55c4e 222 {
Geremia 2:713844a55c4e 223 cur_x = win_x1;
Geremia 2:713844a55c4e 224 cur_y++;
Geremia 2:713844a55c4e 225 if(cur_y > win_y2)
Geremia 2:713844a55c4e 226 {
Geremia 2:713844a55c4e 227 cur_y = win_y1;
Geremia 2:713844a55c4e 228 }
Geremia 2:713844a55c4e 229 }
Geremia 2:713844a55c4e 230 count--;
Geremia 2:713844a55c4e 231 }
Geremia 2:713844a55c4e 232 }
Geremia 2:713844a55c4e 233 void LCD::window_pushpixelbuf(unsigned short* color, unsigned int lenght) {
Geremia 2:713844a55c4e 234 while(lenght)
Geremia 2:713844a55c4e 235 {
Geremia 2:713844a55c4e 236 pixel(cur_x, cur_y, *color++);
Geremia 2:713844a55c4e 237 cur_x++;
Geremia 2:713844a55c4e 238 if(cur_x > win_x2)
Geremia 2:713844a55c4e 239 {
Geremia 2:713844a55c4e 240 cur_x = win_x1;
Geremia 2:713844a55c4e 241 cur_y++;
Geremia 2:713844a55c4e 242 if(cur_y > win_y2)
Geremia 2:713844a55c4e 243 {
Geremia 2:713844a55c4e 244 cur_y = win_y1;
Geremia 2:713844a55c4e 245 }
Geremia 2:713844a55c4e 246 }
Geremia 2:713844a55c4e 247 lenght--;
Geremia 2:713844a55c4e 248 }
Geremia 2:713844a55c4e 249 }
Geremia 0:75ec1b3cde17 250 void LCD::pixel(int x, int y, unsigned short color)
Geremia 0:75ec1b3cde17 251 {
Geremia 0:75ec1b3cde17 252 if(!(orientation&1)) SWAP(x,y);
Geremia 0:75ec1b3cde17 253 // first check parameter
Geremia 0:75ec1b3cde17 254 if((x >= LCDSIZE_X) || (y >= LCDSIZE_Y)) return;
Geremia 0:75ec1b3cde17 255
Geremia 0:75ec1b3cde17 256 // if(draw_mode == NORMAL)
Geremia 0:75ec1b3cde17 257 // {
Geremia 4:12ba0ecc2c1f 258 if(color) buffer[(x + ((y>>3)*LCDSIZE_X))^1] &= ~(1 << (y&7)); // erase pixel
Geremia 4:12ba0ecc2c1f 259 else buffer[(x + ((y>>3)*LCDSIZE_X))^1] |= (1 << (y&7)); //Black=0000, set pixel
Geremia 0:75ec1b3cde17 260 // }
Geremia 0:75ec1b3cde17 261 // else
Geremia 0:75ec1b3cde17 262 // { // XOR mode
Geremia 0:75ec1b3cde17 263 // if(color == 1) buffer[x + ((y>>3) * LCDSIZE_X)] ^= (1 << (y&7)); // xor pixel
Geremia 0:75ec1b3cde17 264 // }
Geremia 0:75ec1b3cde17 265 }
Geremia 0:75ec1b3cde17 266 void LCD::copy_to_lcd(void)
Geremia 0:75ec1b3cde17 267 {
Geremia 0:75ec1b3cde17 268 unsigned short i=0;
Geremia 1:ff019d22b275 269 unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4);
Geremia 0:75ec1b3cde17 270 for(int page=0; page<LCDPAGES; page++)
Geremia 0:75ec1b3cde17 271 {
Geremia 1:ff019d22b275 272 // wr_cmd8(col_offset&0xF); // set column low nibble
Geremia 1:ff019d22b275 273 // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble
Geremia 1:ff019d22b275 274 wr_cmd16(setcolcmd);
Geremia 1:ff019d22b275 275 wr_cmd8(0xB0|(page+page_offset)); // set page
Geremia 4:12ba0ecc2c1f 276 wr_grambuf(buffer16+i, LCDSIZE_X>>1); // send whole page pixels
Geremia 1:ff019d22b275 277 i+=LCDSIZE_X>>1;
Geremia 0:75ec1b3cde17 278 }
Geremia 0:75ec1b3cde17 279 }
Geremia 0:75ec1b3cde17 280 void LCD::cls(void)
Geremia 0:75ec1b3cde17 281 {
Geremia 4:12ba0ecc2c1f 282 unsigned short tmp = _background^0xFFFF;
Geremia 4:12ba0ecc2c1f 283 memset(buffer,tmp,LCDSIZE_X*LCDPAGES); // clear display buffer
Geremia 1:ff019d22b275 284 unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4);
Geremia 0:75ec1b3cde17 285 for(int page=0; page<LCDPAGES; page++)
Geremia 0:75ec1b3cde17 286 {
Geremia 1:ff019d22b275 287 // wr_cmd8((unsigned char)col_offset&0xF); // set column low nibble
Geremia 1:ff019d22b275 288 // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble
Geremia 1:ff019d22b275 289 wr_cmd16(setcolcmd);
Geremia 1:ff019d22b275 290 wr_cmd8(0xB0|(page+page_offset)); // set page
Geremia 4:12ba0ecc2c1f 291 wr_gram(tmp, LCDSIZE_X>>1); // send whole page pixels =0
Geremia 0:75ec1b3cde17 292 }
Geremia 0:75ec1b3cde17 293 }