Team Alpha / Mbed 2 deprecated UserIntefaceLCD

Dependencies:   mbed mbed-rtos MLX90614

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers st7565LCD.cpp Source File

st7565LCD.cpp

00001 /*
00002 $Id:$
00003  
00004 ST7565 LCD library!
00005  
00006 Copyright (C) 2010 Limor Fried, Adafruit Industries
00007  
00008 This library is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU Lesser General Public
00010 License as published by the Free Software Foundation; either
00011 version 2.1 of the License, or (at your option) any later version.
00012  
00013 This library is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016 Lesser General Public License for more details.
00017  
00018 You should have received a copy of the GNU Lesser General Public
00019 License along with this library; if not, write to the Free Software
00020 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00021  
00022 // some of this code was written by <cstone@pobox.com> originally; it is in the public domain.
00023  
00024  
00025 ******the library is modified for mbed**********
00026     http://www.adafruit.com/products/250
00027     
00028     *!function drawbitmap(); is left undone!*
00029     
00030                     2014/03/18     by imachooon
00031                     mailto: imachooon@gmail.com
00032 ************************************************
00033 */
00034  
00035 #include "st7565LCD.h"
00036 #include "st7565LCDfont.h"
00037  
00038 #define ST7565_STARTBYTES 1
00039  
00040 uint8_t is_reversed = 0;
00041  
00042 // a handy reference to where the pages are on the screen
00043 const uint8_t pagemap[] = { 3, 2, 1, 0, 7, 6, 5, 4 };
00044  
00045 // a 5x7 font table
00046 extern const uint8_t font[];
00047  
00048 // the memory buffer for the LCD
00049 uint8_t st7565_buffer[1024] = {
00050     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00051     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
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x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x6, 0x6, 0x0, 0x0, 0x0, 0x3, 0x3,
00066     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00067  
00068     0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0x1F, 0xF, 0x7, 0x7,
00069     0x7, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x3E, 0x0, 0x0,
00070     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xF, 0x3F,
00071     0x70, 0x60, 0x60, 0x60, 0x60, 0x30, 0x7F, 0x3F, 0x0, 0x0, 0x1F, 0x3F, 0x70, 0x60, 0x60, 0x60,
00072     0x60, 0x39, 0xFF, 0xFF, 0x0, 0x6, 0x1F, 0x39, 0x60, 0x60, 0x60, 0x60, 0x30, 0x3F, 0x7F, 0x0,
00073     0x0, 0x60, 0xFF, 0xFF, 0x60, 0x60, 0x0, 0x7F, 0x7F, 0x70, 0x60, 0x60, 0x40, 0x0, 0x7F, 0x7F,
00074     0x0, 0x0, 0x0, 0x0, 0x7F, 0x7F, 0x0, 0x0, 0x0, 0x7F, 0x7F, 0x0, 0x0, 0x60, 0xFF, 0xFF,
00075     0x60, 0x60, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00076  
00077     0x80, 0xF8, 0xFC, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xEF, 0xE7, 0xE7, 0xE3,
00078     0xF3, 0xF9, 0xFF, 0xFF, 0xFF, 0xF7, 0x7, 0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF,
00079     0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x7F, 0x3F, 0x3F, 0x1F, 0xF, 0x7, 0x3, 0x0, 0x0, 0x0, 0xC0,
00080     0xE0, 0x60, 0x20, 0x20, 0x60, 0xE0, 0xE0, 0xE0, 0x0, 0x0, 0x80, 0xC0, 0xE0, 0x60, 0x20, 0x60,
00081     0x60, 0xE0, 0xE0, 0xE0, 0x0, 0x0, 0x80, 0xC0, 0x60, 0x60, 0x20, 0x60, 0x60, 0xE0, 0xE0, 0x0,
00082     0x0, 0x0, 0xE0, 0xE0, 0x0, 0x0, 0x0, 0xE0, 0xE0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x80, 0xE0,
00083     0x60, 0x60, 0x60, 0x60, 0xE0, 0x80, 0x0, 0x0, 0x0, 0xE0, 0xE0, 0x0, 0x0, 0x0, 0xE0, 0xE0,
00084     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00085  
00086     0x0, 0x0, 0x0, 0x3, 0x7, 0x1F, 0x9F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFD, 0xF1, 0xE3,
00087     0xE3, 0xCF, 0xFF, 0xFF, 0xFF, 0xFF, 0xF0, 0xFC, 0x7F, 0x3F, 0x3F, 0x3F, 0x3F, 0x7F, 0xFF, 0xFF,
00088     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFC, 0xF0, 0xE0, 0x80, 0x0, 0x0, 0x0, 0xC,
00089     0x1C, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00090     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7F, 0x7F, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 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, 0x0, 0x0,
00117     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00118     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00119     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00120     0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
00121 };
00122  
00123  
00124  
00125  
00126 // reduces how much is refreshed, which speeds it up!
00127 // originally derived from Steve Evans/JCW's mod but cleaned up and
00128 // optimized
00129 #define enablePartialUpdate
00130  
00131 #ifdef enablePartialUpdate
00132 static uint8_t xUpdateMin, xUpdateMax, yUpdateMin, yUpdateMax;
00133 #endif
00134  
00135  
00136  
00137 static void updateBoundingBox(uint8_t xmin, uint8_t ymin, uint8_t xmax, uint8_t ymax)
00138 {
00139 #ifdef enablePartialUpdate
00140     if (xmin < xUpdateMin) xUpdateMin = xmin;
00141     if (xmax > xUpdateMax) xUpdateMax = xmax;
00142     if (ymin < yUpdateMin) yUpdateMin = ymin;
00143     if (ymax > yUpdateMax) yUpdateMax = ymax;
00144 #endif
00145 }
00146  
00147 /*
00148 void ST7565::drawbitmap(uint8_t x, uint8_t y,
00149             const uint8_t *bitmap, uint8_t w, uint8_t h,
00150             uint8_t color) {
00151   for (uint8_t i=0; i<h; i++) {
00152     for (uint8_t j=0; j<w; j++ ) {
00153       if (pgm_read_byte(bitmap + j + (j/8)*w) & _BV(j%8)) {
00154     my_setpixel(x+j, y+i, color);
00155       }
00156     }
00157   }
00158  
00159   updateBoundingBox(x, y, x+w, y+h);
00160 }
00161 */
00162  
00163 void ST7565::drawstring(uint8_t x, uint8_t line, char *c)
00164 {
00165     while (*c != '\0') {
00166         drawchar(x, line, *c);
00167         *c++;
00168         x += 6; // 6 pixels wide
00169         if (x + 6 >= LCDWIDTH) {
00170             x = 0;    // ran out of this line
00171             line++;
00172         }
00173         if (line >= (LCDHEIGHT/8))
00174             return;        // ran out of space :(
00175     }
00176 }
00177  
00178  
00179 void  ST7565::drawchar(uint8_t x, uint8_t line, char c)
00180 {
00181     for (uint8_t i =0; i<5; i++ ) {
00182         *(st7565_buffer + x + line*128) = *(font + (c*5) + i);
00183         x++;
00184     }
00185     updateBoundingBox(x, line*8, x+5, line*8+8);    //only updates relevant area of LCD
00186 }
00187  
00188  
00189 // bresenham's algorithm - thx wikpedia
00190 void ST7565::drawline(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1,
00191                       uint8_t color)
00192 {
00193     uint8_t steep = abs(y1 - y0) > abs(x1 - x0);
00194     if (steep) {
00195         swap(x0, y0);
00196         swap(x1, y1);
00197     }
00198  
00199     if (x0 > x1) {
00200         swap(x0, x1);
00201         swap(y0, y1);
00202     }
00203  
00204     // much faster to put the test here, since we've already sorted the points
00205     updateBoundingBox(x0, y0, x1, y1);
00206  
00207     uint8_t dx, dy;
00208     dx = x1 - x0;
00209     dy = abs(y1 - y0);
00210  
00211     int8_t err = dx / 2;
00212     int8_t ystep;
00213  
00214     if (y0 < y1) {
00215         ystep = 1;
00216     } else {
00217         ystep = -1;
00218     }
00219  
00220     for (; x0<=x1; x0++) {
00221         if (steep) {
00222             my_setpixel(y0, x0, color);
00223         } else {
00224             my_setpixel(x0, y0, color);
00225         }
00226         err -= dy;
00227         if (err < 0) {
00228             y0 += ystep;
00229             err += dx;
00230         }
00231     }
00232 }
00233  
00234 // filled rectangle
00235 void ST7565::fillrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
00236                       uint8_t color)
00237 {
00238  
00239     // stupidest version - just pixels - but fast with internal buffer!
00240     for (uint8_t i=x; i<x+w; i++) {
00241         for (uint8_t j=y; j<y+h; j++) {
00242             my_setpixel(i, j, color);
00243         }
00244     }
00245  
00246     updateBoundingBox(x, y, x+w, y+h);
00247 }
00248  
00249 // draw a rectangle
00250 void ST7565::drawrect(uint8_t x, uint8_t y, uint8_t w, uint8_t h,
00251                       uint8_t color)
00252 {
00253     // stupidest version - just pixels - but fast with internal buffer!
00254     for (uint8_t i=x; i<x+w; i++) {
00255         my_setpixel(i, y, color);
00256         my_setpixel(i, y+h-1, color);
00257     }
00258     for (uint8_t i=y; i<y+h; i++) {
00259         my_setpixel(x, i, color);
00260         my_setpixel(x+w-1, i, color);
00261     }
00262  
00263     updateBoundingBox(x, y, x+w, y+h);
00264 }
00265  
00266 // draw a circle outline
00267 void ST7565::drawcircle(uint8_t x0, uint8_t y0, uint8_t r,
00268                         uint8_t color)
00269 {
00270     updateBoundingBox(x0-r, y0-r, x0+r, y0+r);
00271  
00272     int8_t f = 1 - r;
00273     int8_t ddF_x = 1;
00274     int8_t ddF_y = -2 * r;
00275     int8_t x = 0;
00276     int8_t y = r;
00277  
00278     my_setpixel(x0, y0+r, color);
00279     my_setpixel(x0, y0-r, color);
00280     my_setpixel(x0+r, y0, color);
00281     my_setpixel(x0-r, y0, color);
00282  
00283     while (x<y) {
00284         if (f >= 0) {
00285             y--;
00286             ddF_y += 2;
00287             f += ddF_y;
00288         }
00289         x++;
00290         ddF_x += 2;
00291         f += ddF_x;
00292  
00293         my_setpixel(x0 + x, y0 + y, color);
00294         my_setpixel(x0 - x, y0 + y, color);
00295         my_setpixel(x0 + x, y0 - y, color);
00296         my_setpixel(x0 - x, y0 - y, color);
00297  
00298         my_setpixel(x0 + y, y0 + x, color);
00299         my_setpixel(x0 - y, y0 + x, color);
00300         my_setpixel(x0 + y, y0 - x, color);
00301         my_setpixel(x0 - y, y0 - x, color);
00302  
00303     }
00304 }
00305  
00306 void ST7565::fillcircle(uint8_t x0, uint8_t y0, uint8_t r,
00307                         uint8_t color)
00308 {
00309     updateBoundingBox(x0-r, y0-r, x0+r, y0+r);
00310  
00311     int8_t f = 1 - r;
00312     int8_t ddF_x = 1;
00313     int8_t ddF_y = -2 * r;
00314     int8_t x = 0;
00315     int8_t y = r;
00316  
00317     for (uint8_t i=y0-r; i<=y0+r; i++) {
00318         my_setpixel(x0, i, color);
00319     }
00320  
00321     while (x<y) {
00322         if (f >= 0) {
00323             y--;
00324             ddF_y += 2;
00325             f += ddF_y;
00326         }
00327         x++;
00328         ddF_x += 2;
00329         f += ddF_x;
00330  
00331         for (uint8_t i=y0-y; i<=y0+y; i++) {
00332             my_setpixel(x0+x, i, color);
00333             my_setpixel(x0-x, i, color);
00334         }
00335         for (uint8_t i=y0-x; i<=y0+x; i++) {
00336             my_setpixel(x0+y, i, color);
00337             my_setpixel(x0-y, i, color);
00338         }
00339     }
00340 }
00341  
00342 void ST7565::my_setpixel(uint8_t x, uint8_t y, uint8_t color)
00343 {
00344     if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
00345         return;
00346  
00347     // x is which column
00348     if (color)
00349         st7565_buffer[x+ (y/8)*128] |= _BV(7-(y%8));
00350     else
00351         st7565_buffer[x+ (y/8)*128] &= _nBV(7-(y%8));
00352 }
00353  
00354 // the most basic function, set a single pixel
00355 void ST7565::setpixel(uint8_t x, uint8_t y, uint8_t color)
00356 {
00357     if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
00358         return;
00359     // x is which column
00360     if (color)
00361         st7565_buffer[x+ (y/8)*128] |= _BV(7-(y%8));
00362     else
00363         st7565_buffer[x+ (y/8)*128] &= _nBV(7-(y%8));
00364  
00365     updateBoundingBox(x,y,x,y);
00366 }
00367  
00368  
00369 // the most basic function, get a single pixel
00370 uint8_t ST7565::getpixel(uint8_t x, uint8_t y)
00371 {
00372     if ((x >= LCDWIDTH) || (y >= LCDHEIGHT))
00373         return 0;
00374  
00375     return (st7565_buffer[x+ (y/8)*128] >> (7-(y%8))) & 0x1;
00376 }
00377  
00378  
00379 ST7565::ST7565(PinName mosi, PinName sclk, PinName cs, PinName rst, PinName a0)
00380 // set pin directions
00381     : _spi(mosi, NC, sclk), _cs(cs), _rst(rst), _a0(a0)
00382  
00383 {
00384     _spi.format(8, 0);
00385     _spi.frequency(1000000);
00386 }
00387  
00388  
00389  
00390 void ST7565::st7565_init(void)
00391 {
00392     wait_ms(100); 
00393     _rst = 0;
00394     wait_ms(100);
00395     _rst = 1;
00396     
00397  
00398     // LCD bias select
00399     st7565_command(CMD_SET_BIAS_7);
00400     // ADC select
00401     st7565_command(CMD_SET_ADC_NORMAL);
00402     // SHL select
00403     st7565_command(CMD_SET_COM_NORMAL);
00404  
00405     // turn on voltage converter (VC=1, VR=0, VF=0)
00406     st7565_command(CMD_SET_POWER_CONTROL | 0x4);
00407     // wait for 50% rising
00408     wait_ms(50);
00409  
00410     // turn on voltage regulator (VC=1, VR=1, VF=0)
00411     st7565_command(CMD_SET_POWER_CONTROL | 0x6);
00412     // wait >=50ms
00413     wait_ms(20);
00414  
00415     // turn on voltage follower (VC=1, VR=1, VF=1)
00416     st7565_command(CMD_SET_POWER_CONTROL | 0x7);
00417     // wait
00418     wait_ms(10);
00419     
00420     // set lcd operating voltage (regulator resistor, ref voltage resistor)
00421     st7565_command(CMD_SET_RESISTOR_RATIO | 0x4);
00422     wait_ms(10);
00423  
00424  
00425 //    st7565_set_brightness(INITIAL_CONTRAST);
00426     // set up a bounding box for screen updates
00427     updateBoundingBox(0, 0, LCDWIDTH-1, LCDHEIGHT-1);
00428  
00429  
00430     st7565_command(CMD_DISPLAY_ON);
00431     st7565_command(CMD_SET_ALLPTS_NORMAL);
00432     
00433     
00434     // reference voltage resistor    
00435     st7565_command(CMD_SET_VOLUME_FIRST);
00436     wait_ms(10);
00437     st7565_command(CMD_SET_VOLUME_SECOND);
00438     wait_ms(10);
00439  
00440  
00441  
00442     // Initial display line ( = 0)
00443     st7565_command(CMD_SET_DISP_START_LINE);
00444     // Initial page address ( = 0)
00445     st7565_command(CMD_SET_PAGE);    
00446     st7565_command(CMD_SET_COLUMN_UPPER);
00447     st7565_command(CMD_SET_COLUMN_LOWER);
00448     
00449 }
00450  
00451 inline void ST7565::spiwrite(uint8_t c)
00452 {
00453     _spi.write(c);
00454 }
00455  
00456 void ST7565::st7565_command(uint8_t c)
00457 {
00458     _cs = 0;
00459     _a0 = 0;
00460     _spi.write(c);
00461     _cs = 1;
00462 }
00463  
00464 void ST7565::st7565_data(uint8_t c)
00465 {
00466     _cs = 0;
00467     _a0 = 1;
00468     _spi.write(c);
00469     _cs = 1;
00470 }
00471  
00472 void ST7565::st7565_set_brightness(uint8_t val)
00473 {
00474     st7565_command(CMD_SET_VOLUME_FIRST);
00475     st7565_command(CMD_SET_VOLUME_SECOND | (val & 0x3f));
00476 }
00477  
00478 void ST7565::begin(uint8_t contrast){
00479     st7565_init();
00480   st7565_command(CMD_DISPLAY_ON);
00481   st7565_command(CMD_SET_ALLPTS_NORMAL);
00482   st7565_set_brightness(contrast);
00483 }
00484     
00485  
00486 void ST7565::display(void)
00487 {
00488     uint8_t col, maxcol, p;
00489  
00490     for(p = 0; p < 8; p++) {
00491 #ifdef enablePartialUpdate
00492         // check if this page is part of update
00493         if ( yUpdateMin >= ((p+1)*8) ) {
00494             continue;   // nope, skip it!
00495         }
00496         if (yUpdateMax < p*8) {
00497             break;
00498         }
00499 #endif
00500         st7565_command(CMD_SET_PAGE | pagemap[p]);
00501  
00502 #ifdef enablePartialUpdate
00503         col = xUpdateMin;
00504         maxcol = xUpdateMax;
00505 #else
00506         // start at the beginning of the row
00507         col = 0;
00508         maxcol = LCDWIDTH-1;
00509 #endif
00510  
00511         st7565_command(CMD_SET_COLUMN_LOWER | ((col+ST7565_STARTBYTES) & 0xf));
00512         st7565_command(CMD_SET_COLUMN_UPPER | (((col+ST7565_STARTBYTES) >> 4) & 0x0F));
00513         st7565_command(CMD_RMW);
00514  
00515         for(; col <= maxcol; col++) {
00516             st7565_data(st7565_buffer[(128*p)+col]);
00517         }
00518     }
00519  
00520 #ifdef enablePartialUpdate
00521     xUpdateMin = LCDWIDTH - 1;
00522     xUpdateMax = 0;
00523     yUpdateMin = LCDHEIGHT-1;
00524     yUpdateMax = 0;
00525 #endif
00526 }
00527  
00528 // clear everything
00529 void ST7565::clear(void)
00530 {
00531     memset(st7565_buffer, 0, 1024);
00532     updateBoundingBox(0, 0, LCDWIDTH-1, LCDHEIGHT-1);
00533 }
00534  
00535  
00536 // this doesnt touch the buffer, just clears the display RAM - might be handy
00537 void ST7565::clear_display(void)
00538 {
00539     uint8_t p, c;
00540  
00541     for(p = 0; p < 8; p++) {
00542  
00543         st7565_command(CMD_SET_PAGE | p);
00544         for(c = 0; c < 129; c++) {
00545             st7565_command(CMD_SET_COLUMN_LOWER | (c & 0xf));
00546             st7565_command(CMD_SET_COLUMN_UPPER | ((c >> 4) & 0xf));
00547             st7565_data(0x0);
00548         }
00549     }
00550 }
00551  
00552 //additional function to set a bit
00553 uint8_t ST7565::_BV(uint8_t bit)
00554 {
00555     return 1 << bit;
00556 }
00557  
00558 uint8_t ST7565::_nBV(uint8_t bit)
00559 {
00560     return 0 << bit;
00561 }