.
Display/LCD.cpp@1:ff019d22b275, 2015-02-13 (annotated)
- Committer:
- Geremia
- Date:
- Fri Feb 13 15:25:10 2015 +0000
- Revision:
- 1:ff019d22b275
- Parent:
- 0:75ec1b3cde17
- Child:
- 2:713844a55c4e
Added SPI_16, LCD done
Who changed what in which revision?
User | Revision | Line number | New 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 | 1:ff019d22b275 | 8 | #define NOPCMD 0xE300 |
Geremia | 0:75ec1b3cde17 | 9 | //#define FRAMEBUFSIZE (LCDSIZE_X*LCDPAGES) |
Geremia | 0:75ec1b3cde17 | 10 | Protocols* proto; |
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 | 0:75ec1b3cde17 | 25 | // cls(); |
Geremia | 0:75ec1b3cde17 | 26 | // locate(0,0); |
Geremia | 0:75ec1b3cde17 | 27 | } |
Geremia | 1:ff019d22b275 | 28 | 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 | 29 | : 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 | 30 | { |
Geremia | 0:75ec1b3cde17 | 31 | // LCDPAGES = LCDSIZE_Y>>3; |
Geremia | 0:75ec1b3cde17 | 32 | // IC_PAGES = IC_Y_COMS>>3; |
Geremia | 0:75ec1b3cde17 | 33 | // buffer = new unsigned char [LCDSIZE_X*LCDPAGES]; |
Geremia | 0:75ec1b3cde17 | 34 | // PAR8 par8proto(port, CS, reset, DC, WR, RD); |
Geremia | 1:ff019d22b275 | 35 | if(displayproto==SPI_8) |
Geremia | 1:ff019d22b275 | 36 | { |
Geremia | 1:ff019d22b275 | 37 | proto = new SPI8(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 1:ff019d22b275 | 38 | useNOP=false; |
Geremia | 1:ff019d22b275 | 39 | } |
Geremia | 1:ff019d22b275 | 40 | else if(displayproto==SPI_16) |
Geremia | 1:ff019d22b275 | 41 | { |
Geremia | 1:ff019d22b275 | 42 | proto = new SPI16(Hz, mosi, miso, sclk, CS, reset, DC); |
Geremia | 1:ff019d22b275 | 43 | useNOP=true; |
Geremia | 1:ff019d22b275 | 44 | } |
Geremia | 0:75ec1b3cde17 | 45 | buffer = (unsigned char*) malloc (LCDSIZE_X*LCDPAGES); |
Geremia | 1:ff019d22b275 | 46 | buffer16 = (unsigned short*)buffer; |
Geremia | 0:75ec1b3cde17 | 47 | draw_mode = NORMAL; |
Geremia | 0:75ec1b3cde17 | 48 | // cls(); |
Geremia | 0:75ec1b3cde17 | 49 | set_orientation(1); |
Geremia | 0:75ec1b3cde17 | 50 | // locate(0,0); |
Geremia | 1:ff019d22b275 | 51 | |
Geremia | 0:75ec1b3cde17 | 52 | } |
Geremia | 0:75ec1b3cde17 | 53 | LCD::~LCD() |
Geremia | 0:75ec1b3cde17 | 54 | { |
Geremia | 0:75ec1b3cde17 | 55 | free(buffer); |
Geremia | 0:75ec1b3cde17 | 56 | } |
Geremia | 0:75ec1b3cde17 | 57 | |
Geremia | 1:ff019d22b275 | 58 | void LCD::wr_cmd8(unsigned char cmd) |
Geremia | 0:75ec1b3cde17 | 59 | { |
Geremia | 1:ff019d22b275 | 60 | if(useNOP) proto->wr_cmd16(NOPCMD|cmd); |
Geremia | 1:ff019d22b275 | 61 | else proto->wr_cmd8(cmd); |
Geremia | 0:75ec1b3cde17 | 62 | } |
Geremia | 1:ff019d22b275 | 63 | void LCD::wr_data8(unsigned char data) |
Geremia | 0:75ec1b3cde17 | 64 | { |
Geremia | 1:ff019d22b275 | 65 | proto->wr_data8(data); |
Geremia | 1:ff019d22b275 | 66 | } |
Geremia | 1:ff019d22b275 | 67 | void LCD::wr_data8(unsigned char data, unsigned int count) |
Geremia | 1:ff019d22b275 | 68 | { |
Geremia | 1:ff019d22b275 | 69 | proto->wr_data8(data, count); |
Geremia | 0:75ec1b3cde17 | 70 | } |
Geremia | 1:ff019d22b275 | 71 | void LCD::wr_data8buf(unsigned char* data, unsigned int lenght) |
Geremia | 0:75ec1b3cde17 | 72 | { |
Geremia | 1:ff019d22b275 | 73 | proto->wr_data8buf(data, lenght); |
Geremia | 1:ff019d22b275 | 74 | } |
Geremia | 1:ff019d22b275 | 75 | void LCD::wr_cmd16(unsigned short cmd) |
Geremia | 1:ff019d22b275 | 76 | { |
Geremia | 1:ff019d22b275 | 77 | proto->wr_cmd16(cmd); |
Geremia | 0:75ec1b3cde17 | 78 | } |
Geremia | 1:ff019d22b275 | 79 | void LCD::wr_data16(unsigned short data, unsigned int count) |
Geremia | 0:75ec1b3cde17 | 80 | { |
Geremia | 1:ff019d22b275 | 81 | proto->wr_data16(data, count); |
Geremia | 1:ff019d22b275 | 82 | } |
Geremia | 1:ff019d22b275 | 83 | void LCD::wr_data16buf(unsigned short* data, unsigned int lenght) |
Geremia | 1:ff019d22b275 | 84 | { |
Geremia | 1:ff019d22b275 | 85 | proto->wr_data16buf(data, lenght); |
Geremia | 0:75ec1b3cde17 | 86 | } |
Geremia | 0:75ec1b3cde17 | 87 | void LCD::hw_reset() |
Geremia | 0:75ec1b3cde17 | 88 | { |
Geremia | 0:75ec1b3cde17 | 89 | proto->hw_reset(); |
Geremia | 0:75ec1b3cde17 | 90 | } |
Geremia | 0:75ec1b3cde17 | 91 | void LCD::BusEnable(bool enable) |
Geremia | 0:75ec1b3cde17 | 92 | { |
Geremia | 0:75ec1b3cde17 | 93 | proto->BusEnable(enable); |
Geremia | 0:75ec1b3cde17 | 94 | } |
Geremia | 0:75ec1b3cde17 | 95 | |
Geremia | 0:75ec1b3cde17 | 96 | |
Geremia | 0:75ec1b3cde17 | 97 | |
Geremia | 0:75ec1b3cde17 | 98 | // monochrome LCD driver ICs does not have ram rotate in hw (swap raw<->columns) like TFT displays |
Geremia | 0:75ec1b3cde17 | 99 | // for portrait views, XY swap will be done in sw in pixel() function |
Geremia | 0:75ec1b3cde17 | 100 | void LCD::set_orientation(int o) |
Geremia | 0:75ec1b3cde17 | 101 | { |
Geremia | 0:75ec1b3cde17 | 102 | orientation = o; |
Geremia | 0:75ec1b3cde17 | 103 | switch (o) { |
Geremia | 0:75ec1b3cde17 | 104 | case (0):// portrait view -90° |
Geremia | 0:75ec1b3cde17 | 105 | mirrorXY(Y); |
Geremia | 0:75ec1b3cde17 | 106 | col_offset = 0; |
Geremia | 0:75ec1b3cde17 | 107 | page_offset = IC_PAGES-LCDPAGES; |
Geremia | 0:75ec1b3cde17 | 108 | set_width(LCDSIZE_Y); |
Geremia | 0:75ec1b3cde17 | 109 | set_height(LCDSIZE_X); |
Geremia | 0:75ec1b3cde17 | 110 | // portrait = true; |
Geremia | 0:75ec1b3cde17 | 111 | break; |
Geremia | 0:75ec1b3cde17 | 112 | case (1): // default, landscape view 0° |
Geremia | 0:75ec1b3cde17 | 113 | mirrorXY(NONE); |
Geremia | 0:75ec1b3cde17 | 114 | col_offset = 0; |
Geremia | 0:75ec1b3cde17 | 115 | page_offset = 0; |
Geremia | 0:75ec1b3cde17 | 116 | set_width(LCDSIZE_X); |
Geremia | 0:75ec1b3cde17 | 117 | set_height(LCDSIZE_Y); |
Geremia | 0:75ec1b3cde17 | 118 | // portrait = false; |
Geremia | 0:75ec1b3cde17 | 119 | break; |
Geremia | 0:75ec1b3cde17 | 120 | case (2):// portrait view +90° |
Geremia | 0:75ec1b3cde17 | 121 | mirrorXY(X); |
Geremia | 0:75ec1b3cde17 | 122 | col_offset = IC_X_SEGS-LCDSIZE_X; // some displays have less pixels than IC ram |
Geremia | 0:75ec1b3cde17 | 123 | page_offset = 0; |
Geremia | 0:75ec1b3cde17 | 124 | set_width(LCDSIZE_Y); |
Geremia | 0:75ec1b3cde17 | 125 | set_height(LCDSIZE_X); |
Geremia | 0:75ec1b3cde17 | 126 | // portrait = true; |
Geremia | 0:75ec1b3cde17 | 127 | break; |
Geremia | 0:75ec1b3cde17 | 128 | case (3):// landscape view +180° |
Geremia | 0:75ec1b3cde17 | 129 | mirrorXY(XY); |
Geremia | 0:75ec1b3cde17 | 130 | col_offset = IC_X_SEGS-LCDSIZE_X; |
Geremia | 0:75ec1b3cde17 | 131 | page_offset = IC_PAGES-LCDPAGES; |
Geremia | 0:75ec1b3cde17 | 132 | set_width(LCDSIZE_X); |
Geremia | 0:75ec1b3cde17 | 133 | set_height(LCDSIZE_Y); |
Geremia | 0:75ec1b3cde17 | 134 | // portrait = false; |
Geremia | 0:75ec1b3cde17 | 135 | break; |
Geremia | 0:75ec1b3cde17 | 136 | } |
Geremia | 0:75ec1b3cde17 | 137 | } |
Geremia | 0:75ec1b3cde17 | 138 | void LCD::mirrorXY(mirror_t mode) |
Geremia | 0:75ec1b3cde17 | 139 | { |
Geremia | 0:75ec1b3cde17 | 140 | switch (mode) |
Geremia | 0:75ec1b3cde17 | 141 | { |
Geremia | 0:75ec1b3cde17 | 142 | case(NONE): |
Geremia | 1:ff019d22b275 | 143 | // wr_cmd8(0xA0); |
Geremia | 1:ff019d22b275 | 144 | 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 | 145 | break; |
Geremia | 0:75ec1b3cde17 | 146 | case(X): |
Geremia | 1:ff019d22b275 | 147 | // wr_cmd8(0xA1); |
Geremia | 1:ff019d22b275 | 148 | wr_cmd16(0xA1C8); |
Geremia | 0:75ec1b3cde17 | 149 | break; |
Geremia | 0:75ec1b3cde17 | 150 | case(Y): |
Geremia | 1:ff019d22b275 | 151 | // wr_cmd8(0xA0); |
Geremia | 1:ff019d22b275 | 152 | wr_cmd16(0xA0C0); |
Geremia | 0:75ec1b3cde17 | 153 | break; |
Geremia | 0:75ec1b3cde17 | 154 | case(XY): |
Geremia | 1:ff019d22b275 | 155 | // wr_cmd8(0xA1); |
Geremia | 1:ff019d22b275 | 156 | wr_cmd16(0xA1C0); |
Geremia | 0:75ec1b3cde17 | 157 | break; |
Geremia | 0:75ec1b3cde17 | 158 | } |
Geremia | 0:75ec1b3cde17 | 159 | } |
Geremia | 0:75ec1b3cde17 | 160 | void LCD::invert(unsigned char o) |
Geremia | 0:75ec1b3cde17 | 161 | { |
Geremia | 1:ff019d22b275 | 162 | if(o == 0) wr_cmd8(0xA6); |
Geremia | 1:ff019d22b275 | 163 | else wr_cmd8(0xA7); |
Geremia | 0:75ec1b3cde17 | 164 | } |
Geremia | 0:75ec1b3cde17 | 165 | |
Geremia | 0:75ec1b3cde17 | 166 | void LCD::set_contrast(int o) |
Geremia | 0:75ec1b3cde17 | 167 | { |
Geremia | 0:75ec1b3cde17 | 168 | contrast = o; |
Geremia | 1:ff019d22b275 | 169 | // wr_cmd8(0x81); // set volume |
Geremia | 1:ff019d22b275 | 170 | wr_cmd16(0x8100|(o&0x3F)); |
Geremia | 0:75ec1b3cde17 | 171 | } |
Geremia | 0:75ec1b3cde17 | 172 | void LCD::set_auto_up(bool up) |
Geremia | 0:75ec1b3cde17 | 173 | { |
Geremia | 0:75ec1b3cde17 | 174 | if(up) auto_up = true; |
Geremia | 0:75ec1b3cde17 | 175 | else auto_up = false; |
Geremia | 0:75ec1b3cde17 | 176 | } |
Geremia | 0:75ec1b3cde17 | 177 | bool LCD::get_auto_up(void) |
Geremia | 0:75ec1b3cde17 | 178 | { |
Geremia | 0:75ec1b3cde17 | 179 | return (auto_up); |
Geremia | 0:75ec1b3cde17 | 180 | } |
Geremia | 0:75ec1b3cde17 | 181 | int LCD::get_contrast(void) |
Geremia | 0:75ec1b3cde17 | 182 | { |
Geremia | 0:75ec1b3cde17 | 183 | return(contrast); |
Geremia | 0:75ec1b3cde17 | 184 | } |
Geremia | 0:75ec1b3cde17 | 185 | void LCD::window(int x, int y, int w, int h) { |
Geremia | 0:75ec1b3cde17 | 186 | // current pixel location |
Geremia | 0:75ec1b3cde17 | 187 | cur_x = x; |
Geremia | 0:75ec1b3cde17 | 188 | cur_y = y; |
Geremia | 0:75ec1b3cde17 | 189 | // window settings |
Geremia | 0:75ec1b3cde17 | 190 | win_x1 = x; |
Geremia | 0:75ec1b3cde17 | 191 | win_x2 = x + w - 1; |
Geremia | 0:75ec1b3cde17 | 192 | win_y1 = y; |
Geremia | 0:75ec1b3cde17 | 193 | win_y2 = y + h - 1; |
Geremia | 0:75ec1b3cde17 | 194 | } |
Geremia | 0:75ec1b3cde17 | 195 | void LCD::window_pushpixel(unsigned short color) { |
Geremia | 0:75ec1b3cde17 | 196 | pixel(cur_x, cur_y, color); |
Geremia | 0:75ec1b3cde17 | 197 | cur_x++; |
Geremia | 0:75ec1b3cde17 | 198 | if(cur_x > win_x2) { |
Geremia | 0:75ec1b3cde17 | 199 | cur_x = win_x1; |
Geremia | 0:75ec1b3cde17 | 200 | cur_y++; |
Geremia | 0:75ec1b3cde17 | 201 | if(cur_y > win_y2) { |
Geremia | 0:75ec1b3cde17 | 202 | cur_y = win_y1; |
Geremia | 0:75ec1b3cde17 | 203 | } |
Geremia | 0:75ec1b3cde17 | 204 | } |
Geremia | 0:75ec1b3cde17 | 205 | } |
Geremia | 0:75ec1b3cde17 | 206 | void LCD::pixel(int x, int y, unsigned short color) |
Geremia | 0:75ec1b3cde17 | 207 | { |
Geremia | 0:75ec1b3cde17 | 208 | if(!(orientation&1)) SWAP(x,y); |
Geremia | 0:75ec1b3cde17 | 209 | // first check parameter |
Geremia | 0:75ec1b3cde17 | 210 | if((x >= LCDSIZE_X) || (y >= LCDSIZE_Y)) return; |
Geremia | 0:75ec1b3cde17 | 211 | |
Geremia | 0:75ec1b3cde17 | 212 | // if(draw_mode == NORMAL) |
Geremia | 0:75ec1b3cde17 | 213 | // { |
Geremia | 1:ff019d22b275 | 214 | if(color == 0) buffer[(x + ((y>>3)*LCDSIZE_X))^1] &= ~(1 << (y&7)); // erase pixel |
Geremia | 1:ff019d22b275 | 215 | else buffer[(x + ((y>>3)*LCDSIZE_X))^1] |= (1 << (y&7)); // set pixel |
Geremia | 0:75ec1b3cde17 | 216 | // } |
Geremia | 0:75ec1b3cde17 | 217 | // else |
Geremia | 0:75ec1b3cde17 | 218 | // { // XOR mode |
Geremia | 0:75ec1b3cde17 | 219 | // if(color == 1) buffer[x + ((y>>3) * LCDSIZE_X)] ^= (1 << (y&7)); // xor pixel |
Geremia | 0:75ec1b3cde17 | 220 | // } |
Geremia | 0:75ec1b3cde17 | 221 | } |
Geremia | 0:75ec1b3cde17 | 222 | void LCD::copy_to_lcd(void) |
Geremia | 0:75ec1b3cde17 | 223 | { |
Geremia | 0:75ec1b3cde17 | 224 | unsigned short i=0; |
Geremia | 1:ff019d22b275 | 225 | unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4); |
Geremia | 0:75ec1b3cde17 | 226 | for(int page=0; page<LCDPAGES; page++) |
Geremia | 0:75ec1b3cde17 | 227 | { |
Geremia | 1:ff019d22b275 | 228 | // wr_cmd8(col_offset&0xF); // set column low nibble |
Geremia | 1:ff019d22b275 | 229 | // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble |
Geremia | 1:ff019d22b275 | 230 | wr_cmd16(setcolcmd); |
Geremia | 1:ff019d22b275 | 231 | wr_cmd8(0xB0|(page+page_offset)); // set page |
Geremia | 1:ff019d22b275 | 232 | wr_data16buf(buffer16+i, LCDSIZE_X>>1); // send whole page pixels |
Geremia | 1:ff019d22b275 | 233 | i+=LCDSIZE_X>>1; |
Geremia | 0:75ec1b3cde17 | 234 | } |
Geremia | 0:75ec1b3cde17 | 235 | } |
Geremia | 0:75ec1b3cde17 | 236 | void LCD::cls(void) |
Geremia | 0:75ec1b3cde17 | 237 | { |
Geremia | 0:75ec1b3cde17 | 238 | memset(buffer,0x00,LCDSIZE_X*LCDPAGES); // clear display buffer |
Geremia | 1:ff019d22b275 | 239 | unsigned short setcolcmd = 0x0010 | ((col_offset&0xF)<<8) | (col_offset>>4); |
Geremia | 0:75ec1b3cde17 | 240 | for(int page=0; page<LCDPAGES; page++) |
Geremia | 0:75ec1b3cde17 | 241 | { |
Geremia | 1:ff019d22b275 | 242 | // wr_cmd8((unsigned char)col_offset&0xF); // set column low nibble |
Geremia | 1:ff019d22b275 | 243 | // wr_cmd8(0x10|(col_offset>>4)); // set column hi nibble |
Geremia | 1:ff019d22b275 | 244 | wr_cmd16(setcolcmd); |
Geremia | 1:ff019d22b275 | 245 | wr_cmd8(0xB0|(page+page_offset)); // set page |
Geremia | 1:ff019d22b275 | 246 | wr_data16(0, LCDSIZE_X>>1); // send whole page pixels =0 |
Geremia | 0:75ec1b3cde17 | 247 | } |
Geremia | 0:75ec1b3cde17 | 248 | } |