LPC General Purpose Shield (OM13082) LCD module library

Dependents:   OM13082-LCD OM13082-test LPCXpresso4337_OM13082_test lpc4337_acc_demo ... more

Fork of KT7567 by Toyomasa Watarai

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ST7567.cpp Source File

ST7567.cpp

00001 /* mbed library for the mbed Lab Board  128*32 pixel LCD
00002  * use C12832 controller
00003  * Copyright (c) 2012 Peter Drescher - DC2PD
00004  * Released under the MIT License: http://mbed.org/license/mit
00005  *
00006  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00007  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00008  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00009  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00010  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00011  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00012  * THE SOFTWARE.
00013  */
00014  
00015 /* 
00016  * Modified for the LPC General Purpose Shield 128*64 pixel LCD
00017  * use ST7567 controller
00018  *
00019  * Copyright (c) 2015 Toyomasa Watarai
00020  */
00021 
00022 
00023 // 13.10.12    initial design
00024 // 25.10.12    add autorefresh of screen
00025 // 25.10.12    add standart font
00026 // 20.12.12    add bitmap graphics
00027 // 27.07.15    modified to 128x64 ST7567 contoller
00028 
00029 #include "ST7567.h"
00030 #include "mbed.h"
00031 #include "Small_7.h"
00032 
00033 
00034 ST7567::ST7567(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name)
00035     : GraphicsDisplay(name), _spi(mosi, NC, sck), _reset(reset), _A0(a0), _CS(ncs)
00036 {
00037     orientation = 1;
00038     draw_mode = LCD_NORMAL;
00039     char_x = 0;
00040     lcd_reset();
00041 }
00042 
00043 
00044 int ST7567::width()
00045 {
00046     if (orientation == 0 || orientation == 2)
00047         return MAX_PIXEL_Y;
00048     else
00049         return MAX_PIXEL_X;
00050 }
00051 
00052 int ST7567::height()
00053 {
00054     if (orientation == 0 || orientation == 2)
00055         return MAX_PIXEL_X;
00056     else
00057         return MAX_PIXEL_Y;
00058 }
00059 
00060 
00061 void ST7567::invert(unsigned int o)
00062 {
00063     if(o == 0)
00064         wr_cmd(0xA6);
00065     else
00066         wr_cmd(0xA7);
00067 }
00068 
00069 
00070 void ST7567::set_contrast(unsigned int o)
00071 {
00072     contrast = o;
00073     wr_cmd(0x81);      //  set volume
00074     wr_cmd(o & 0x3F);
00075 }
00076 
00077 unsigned int ST7567::get_contrast(void)
00078 {
00079     return(contrast);
00080 }
00081 
00082 
00083 // write command to lcd controller
00084 
00085 void ST7567::wr_cmd(unsigned char cmd)
00086 {
00087     _A0 = 0;
00088     _CS = 0;
00089     _spi.write(cmd);
00090     _CS = 1;
00091 }
00092 
00093 // write data to lcd controller
00094 
00095 void ST7567::wr_dat(unsigned char dat)
00096 {
00097     _A0 = 1;
00098     _CS = 0;
00099     _spi.write(dat);
00100     _CS = 1;
00101 }
00102 
00103 // reset and init the lcd controller
00104 
00105 void ST7567::lcd_reset()
00106 {
00107 
00108     _spi.format(8,3);                 // 8 bit spi mode 3
00109     _spi.frequency(10000000);         // 10 Mhz SPI clock
00110     _A0 = 0;
00111     _CS = 1;
00112     _reset = 0;                       // display reset
00113     wait_us(50);
00114     _reset = 1;                       // end reset
00115     wait_ms(5);
00116 
00117     /* Start Initial Sequence ----------------------------------------------------*/
00118 
00119     wr_cmd(0xE2);            // S/W RESWT
00120     wr_cmd(0xA3);            // LCD bias
00121     wr_cmd(0xAF);            // Display ON
00122     wr_cmd(0xA0);            // segment direction.
00123     wr_cmd(0xC8);            // Common Direction.
00124     wr_cmd(0x22);            // Regultion resistor select  //25
00125 
00126     wr_cmd(0x81);            // EV Select.
00127     wr_cmd(0x3f);            // Select EV value.
00128 
00129     wr_cmd(0x2f);            // Power control
00130 
00131     wr_cmd(0x40);            // Initial display line 40
00132     wr_cmd(0xB0);            // Set page address
00133     wr_cmd(0x10);            // Set coloumn addr  MSB
00134     wr_cmd(0x00);            // Set coloumn addr LSB
00135     wr_cmd(0xAF);            // Display ON
00136     wr_cmd(0xA4);            // A5 .Normal display, all pixels OFF.
00137     wr_cmd(0xA6);            // A7 .Normal display (Inverse Pixel)
00138 
00139     // clear and update LCD
00140     memset(buffer, 0x00, LCD_FB_SIZE);  // clear display buffer
00141     copy_to_lcd();
00142     auto_up = 1;              // switch on auto update
00143 
00144     locate(0,0);
00145     set_font((unsigned char*)Small_7);  // standart font
00146 }
00147 
00148 // set one pixel in buffer
00149 
00150 void ST7567::pixel(int x, int y, int color)
00151 {
00152     // first check parameter
00153     if(x > MAX_PIXEL_X || y > MAX_PIXEL_Y || x < 0 || y < 0) return;
00154 
00155     if(draw_mode == LCD_NORMAL) {
00156         if(color == 0)
00157             buffer[x + ((y/8) * 128)] &= ~(1 << (y%8));  // erase pixel
00158         else
00159             buffer[x + ((y/8) * 128)] |= (1 << (y%8));   // set pixel
00160     } else { // XOR mode
00161         if(color == 1)
00162             buffer[x + ((y/8) * 128)] ^= (1 << (y%8));   // xor pixel
00163     }
00164 }
00165 
00166 // update lcd
00167 
00168 void ST7567::copy_to_lcd(void)
00169 {
00170     for (int page = 0; page < 8; page++) {
00171         wr_cmd(0x00);        // set column low nibble 0
00172         wr_cmd(0x10);        // set column hi  nibble 0
00173         wr_cmd(0xB0|page);   // set page address  0
00174         _A0 = 1;
00175 
00176         for(int i=page*128; i<(page*128+128); i++) {
00177             wr_dat(buffer[i]);
00178         }
00179     }
00180 }
00181 
00182 void ST7567::cls(void)
00183 {
00184     memset(buffer, 0x00, LCD_FB_SIZE);  // clear display buffer
00185     copy_to_lcd();
00186 }
00187 
00188 
00189 void ST7567::line(int x0, int y0, int x1, int y1, int color)
00190 {
00191     int   dx = 0, dy = 0;
00192     int   dx_sym = 0, dy_sym = 0;
00193     int   dx_x2 = 0, dy_x2 = 0;
00194     int   di = 0;
00195 
00196     dx = x1-x0;
00197     dy = y1-y0;
00198 
00199     //  if (dx == 0) {        /* vertical line */
00200     //      if (y1 > y0) vline(x0,y0,y1,color);
00201     //      else vline(x0,y1,y0,color);
00202     //      return;
00203     //  }
00204 
00205     if (dx > 0) {
00206         dx_sym = 1;
00207     } else {
00208         dx_sym = -1;
00209     }
00210     //  if (dy == 0) {        /* horizontal line */
00211     //      if (x1 > x0) hline(x0,x1,y0,color);
00212     //      else  hline(x1,x0,y0,color);
00213     //      return;
00214     //  }
00215 
00216     if (dy > 0) {
00217         dy_sym = 1;
00218     } else {
00219         dy_sym = -1;
00220     }
00221 
00222     dx = dx_sym*dx;
00223     dy = dy_sym*dy;
00224 
00225     dx_x2 = dx*2;
00226     dy_x2 = dy*2;
00227 
00228     if (dx >= dy) {
00229         di = dy_x2 - dx;
00230         while (x0 != x1) {
00231 
00232             pixel(x0, y0, color);
00233             x0 += dx_sym;
00234             if (di<0) {
00235                 di += dy_x2;
00236             } else {
00237                 di += dy_x2 - dx_x2;
00238                 y0 += dy_sym;
00239             }
00240         }
00241         pixel(x0, y0, color);
00242     } else {
00243         di = dx_x2 - dy;
00244         while (y0 != y1) {
00245             pixel(x0, y0, color);
00246             y0 += dy_sym;
00247             if (di < 0) {
00248                 di += dx_x2;
00249             } else {
00250                 di += dx_x2 - dy_x2;
00251                 x0 += dx_sym;
00252             }
00253         }
00254         pixel(x0, y0, color);
00255     }
00256     if(auto_up) copy_to_lcd();
00257 }
00258 
00259 void ST7567::rect(int x0, int y0, int x1, int y1, int color)
00260 {
00261 
00262     if (x1 > x0) line(x0,y0,x1,y0,color);
00263     else  line(x1,y0,x0,y0,color);
00264 
00265     if (y1 > y0) line(x0,y0,x0,y1,color);
00266     else line(x0,y1,x0,y0,color);
00267 
00268     if (x1 > x0) line(x0,y1,x1,y1,color);
00269     else  line(x1,y1,x0,y1,color);
00270 
00271     if (y1 > y0) line(x1,y0,x1,y1,color);
00272     else line(x1,y1,x1,y0,color);
00273 
00274     if(auto_up) copy_to_lcd();
00275 }
00276 
00277 void ST7567::fillrect(int x0, int y0, int x1, int y1, int color)
00278 {
00279     int l,c,i;
00280     if(x0 > x1) {
00281         i = x0;
00282         x0 = x1;
00283         x1 = i;
00284     }
00285 
00286     if(y0 > y1) {
00287         i = y0;
00288         y0 = y1;
00289         y1 = i;
00290     }
00291 
00292     for(l = x0; l<= x1; l ++) {
00293         for(c = y0; c<= y1; c++) {
00294             pixel(l,c,color);
00295         }
00296     }
00297     if(auto_up) copy_to_lcd();
00298 }
00299 
00300 
00301 
00302 void ST7567::circle(int x0, int y0, int r, int color)
00303 {
00304 
00305     int draw_x0, draw_y0;
00306     int draw_x1, draw_y1;
00307     int draw_x2, draw_y2;
00308     int draw_x3, draw_y3;
00309     int draw_x4, draw_y4;
00310     int draw_x5, draw_y5;
00311     int draw_x6, draw_y6;
00312     int draw_x7, draw_y7;
00313     int xx, yy;
00314     int di;
00315     //WindowMax();
00316     if (r == 0) {       /* no radius */
00317         return;
00318     }
00319 
00320     draw_x0 = draw_x1 = x0;
00321     draw_y0 = draw_y1 = y0 + r;
00322     if (draw_y0 < height()) {
00323         pixel(draw_x0, draw_y0, color);     /* 90 degree */
00324     }
00325 
00326     draw_x2 = draw_x3 = x0;
00327     draw_y2 = draw_y3 = y0 - r;
00328     if (draw_y2 >= 0) {
00329         pixel(draw_x2, draw_y2, color);    /* 270 degree */
00330     }
00331 
00332     draw_x4 = draw_x6 = x0 + r;
00333     draw_y4 = draw_y6 = y0;
00334     if (draw_x4 < width()) {
00335         pixel(draw_x4, draw_y4, color);     /* 0 degree */
00336     }
00337 
00338     draw_x5 = draw_x7 = x0 - r;
00339     draw_y5 = draw_y7 = y0;
00340     if (draw_x5>=0) {
00341         pixel(draw_x5, draw_y5, color);     /* 180 degree */
00342     }
00343 
00344     if (r == 1) {
00345         return;
00346     }
00347 
00348     di = 3 - 2*r;
00349     xx = 0;
00350     yy = r;
00351     while (xx < yy) {
00352 
00353         if (di < 0) {
00354             di += 4*xx + 6;
00355         } else {
00356             di += 4*(xx - yy) + 10;
00357             yy--;
00358             draw_y0--;
00359             draw_y1--;
00360             draw_y2++;
00361             draw_y3++;
00362             draw_x4--;
00363             draw_x5++;
00364             draw_x6--;
00365             draw_x7++;
00366         }
00367         xx++;
00368         draw_x0++;
00369         draw_x1--;
00370         draw_x2++;
00371         draw_x3--;
00372         draw_y4++;
00373         draw_y5++;
00374         draw_y6--;
00375         draw_y7--;
00376 
00377         if ( (draw_x0 <= width()) && (draw_y0>=0) ) {
00378             pixel(draw_x0, draw_y0, color);
00379         }
00380 
00381         if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) {
00382             pixel(draw_x1, draw_y1, color);
00383         }
00384 
00385         if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) {
00386             pixel(draw_x2, draw_y2, color);
00387         }
00388 
00389         if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) {
00390             pixel(draw_x3, draw_y3, color);
00391         }
00392 
00393         if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) {
00394             pixel(draw_x4, draw_y4, color);
00395         }
00396 
00397         if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) {
00398             pixel(draw_x5, draw_y5, color);
00399         }
00400         if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) {
00401             pixel(draw_x6, draw_y6, color);
00402         }
00403         if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) {
00404             pixel(draw_x7, draw_y7, color);
00405         }
00406     }
00407     if(auto_up) copy_to_lcd();
00408 }
00409 
00410 void ST7567::fillcircle(int x, int y, int r, int color)
00411 {
00412     int i,up;
00413     up = auto_up;
00414     auto_up = 0;   // off
00415     for (i = 0; i <= r; i++)
00416         circle(x,y,i,color);
00417     auto_up = up;
00418     if(auto_up) copy_to_lcd();
00419 }
00420 
00421 void ST7567::setmode(int mode)
00422 {
00423     draw_mode = mode;
00424 }
00425 
00426 void ST7567::locate(int x, int y)
00427 {
00428     char_x = x;
00429     char_y = y;
00430 }
00431 
00432 
00433 
00434 int ST7567::columns()
00435 {
00436     return width() / font[1];
00437 }
00438 
00439 
00440 
00441 int ST7567::rows()
00442 {
00443     return height() / font[2];
00444 }
00445 
00446 
00447 
00448 int ST7567::_putc(int value)
00449 {
00450     if (value == '\n') {    // new line
00451         char_x = 0;
00452         char_y = char_y + font[2];
00453         if (char_y >= height() - font[2]) {
00454             char_y = 0;
00455         }
00456     } else {
00457         character(char_x, char_y, value);
00458         if(auto_up) copy_to_lcd();
00459     }
00460     return value;
00461 }
00462 
00463 void ST7567::character(int x, int y, int c)
00464 {
00465     unsigned int hor,vert,offset,bpl,j,i,b;
00466     unsigned char* zeichen;
00467     unsigned char z,w;
00468 
00469     if ((c < 31) || (c > 127)) return;   // test char range
00470 
00471     // read font parameter from start of array
00472     offset = font[0];                    // bytes / char
00473     hor = font[1];                       // get hor size of font
00474     vert = font[2];                      // get vert size of font
00475     bpl = font[3];                       // bytes per line
00476 
00477     if (char_x + hor > width()) {
00478         char_x = 0;
00479         char_y = char_y + vert;
00480         if (char_y >= height() - font[2]) {
00481             char_y = 0;
00482         }
00483     }
00484 
00485     zeichen = &font[((c - ' ') * offset) + 4]; // start of char bitmap
00486     w = zeichen[0];                            // width of actual char
00487     // construct the char into the buffer
00488     for (j=0; j<vert; j++) {  //  vert line
00489         for (i=0; i<hor; i++) {   //  horz line
00490             z =  zeichen[bpl * i + ((j & 0xF8) >> 3)+1];
00491             b = 1 << (j & 0x07);
00492             if (( z & b ) == 0x00) {
00493                 pixel(x+i,y+j,0);
00494             } else {
00495                 pixel(x+i,y+j,1);
00496             }
00497 
00498         }
00499     }
00500 
00501     char_x += w;
00502 }
00503 
00504 
00505 void ST7567::set_font(unsigned char* f)
00506 {
00507     font = f;
00508 }
00509 
00510 void ST7567::set_auto_up(unsigned int up)
00511 {
00512     if (up)
00513         auto_up = 1;
00514     else
00515         auto_up = 0;
00516 }
00517 
00518 unsigned int ST7567::get_auto_up(void)
00519 {
00520     return (auto_up);
00521 }
00522 
00523 void ST7567::print_bm(Bitmap bm, int x, int y)
00524 {
00525     int h,v,b;
00526     char d;
00527 
00528     for(v=0; v < bm.ySize; v++) {   // lines
00529         for(h=0; h < bm.xSize; h++) { // pixel
00530             if(h + x > (MAX_PIXEL_X-1)) break;
00531             if(v + y > (MAX_PIXEL_Y-1)) break;
00532             d = bm.data[bm.Byte_in_Line * v + ((h & 0xF8) >> 3)];
00533             b = 0x80 >> (h & 0x07);
00534             if((d & b) == 0) {
00535                 pixel(x+h,y+v,0);
00536             } else {
00537                 pixel(x+h,y+v,1);
00538             }
00539         }
00540     }
00541 
00542 }