ban4jp - / sharp_mlcd

Dependents:   Sharp_Memory_LCD_Test hello_GT20L16J1Y_FONT_mlcd

Fork of sharp_mlcd by Masato YAMANISHI

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sharp_mlcd.cpp Source File

sharp_mlcd.cpp

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