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