A ST7565R display driver written with backbuffering capabilities. Model used and tested: NHD-C12832A1Z-FSW-FBW-3V3
Revision 0:59cb20947fd6, committed 2011-08-11
- Comitter:
- celeritous
- Date:
- Thu Aug 11 16:00:05 2011 +0000
- Commit message:
- Added doxygen tags.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ST7565R.cpp Thu Aug 11 16:00:05 2011 +0000 @@ -0,0 +1,202 @@ + +#include "ST7565R.h" +#include "mbed.h" + +ST7565R::ST7565R(PinName cs, PinName a0, PinName scl, PinName si, PinName rst, bool BackBuffer) : _cs(cs), _a0(a0), _spi(si, NC, scl), _rst(rst) +{ + _spi.format(8,3); + _spi.frequency(20000000); //datasheet 50 ns sclk + _rst=1; + _a0=0; + _cs=1; + scr=1; + reset(); + UsingBackBuffer = BackBuffer; +} + +void ST7565R::reset() +{ + _rst = 0; + wait_ms(1); + _rst = 1; + wait_ms(1); + command(0xA0); + command(0xAF); + command(0xC0); + command(0xA2); + command(0x2F); + command(0x24); + command(0x81); + command(0x00); + command(0xA7); + + + clearBuffer(); + +} +#define FONT_SIZE_X 6 +#define FONT_SIZE_Y 8 +void ST7565R::moveto(int col, int row) +{ + _column = col; + _row = row; +} +void ST7565R::character(int column, int row, char c) +{ + char v = c - 0x20; + if (c >= 0x20 && c < 0x80) + { + c-=0x20; + bitblt(column*FONT_SIZE_X,row*FONT_SIZE_Y, + FONT_SIZE_X, FONT_SIZE_Y, (char*)TEXT, + TEXT_WIDTH, TEXT_HEIGHT, + (v%0x10) * FONT_SIZE_X, (v/0x10)*FONT_SIZE_Y,BITBLT_SRCCOPY); + } +} +int ST7565R::_putc(int value) { + if (value == '\n') { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } else { + character(_column, _row, value); + _column++; + if (_column >= columns()) { + _column = 0; + _row++; + if (_row >= rows()) { + _row = 0; + } + } + } + return value; +} + +int ST7565R::rows() { return 4; } +int ST7565R::columns() { return 21; } +int ST7565R::_getc() { + return -1; +} + + +void ST7565R::clearBuffer() +{ + + char * screen =getDrawingScreen(); + int i=512; + while(i--) + screen[i]=0xff; + + + if (!UsingBackBuffer) + swapBuffers(); //should just commit the white to memory +} +void ST7565R::setpixel(int x, int y) +{ + if (x < 0 || x > 127) return; + if (y < 0 || y > 31) return; + char * screen = getDrawingScreen(); + loc_t byte = { x , (y / 8) * 128}; + screen[byte.y + byte.x] |= 0x80 >> (y%8); + + //if theres no backbuffer,then we'll do our writes immediately + if (!UsingBackBuffer) + { + command(0xb0 | ( (3-(y/8))+scr*4)); + command(0x10 | (x >> 4)); //column upper + command(0x00 | (x & 0x0f)); //column lower + data(screen[byte.y +byte.x]); + } +} + +void ST7565R::clearpixel(int x, int y) +{ + if (x < 0 || x > 127) return; + if (y < 0 || y > 31) return; + char * screen = getDrawingScreen(); + loc_t byte = { x , (y / 8) * 128}; + screen[byte.y + byte.x] &= ~(0x80 >> (y%8)); + + //if theres no backbuffer,then we'll do our writes immediately + if (!UsingBackBuffer) + { + command(0xb0 | ( (3-(y/8))+scr*4)); + command(0x10 | (x >> 4)); //column upper + command(0x00 | (x & 0x0f)); //column lower + data(screen[byte.y +byte.x]); + } +} + +void ST7565R::data(int value) +{ + _cs = 0; + _a0 = 1; + _spi.write(value); + _cs = 1; + +} +void ST7565R::command(int value) +{ + _cs = 0; + _a0 = 0; + _spi.write(value); + _cs = 1; +} + +void ST7565R::swapBuffers() +{ + char * screen = getDrawingScreen(); + for (int i=0; i<4; i++) + { + + command(0xb0 | ( (3-i)+scr*4)); + command(0x10); + command(0x00); + for (int j=0; j<128; j++) + { + data(screen[i*128+j]); + } + } + command(0x40 | scr * 32); + if (UsingBackBuffer) scr^=1; +} +#define SCREEN_1 0 +#define SCREEN_2 1 + + +char * ST7565R::getDrawingScreen() +{ + if (scr) + return screen.screen1; + else + return screen.screen2; +} +void ST7565R::bitblt(int dstx, int dsty, + int blit_width, int blit_height, + char* img, + int img_width, int img_height, + int srcx, int srcy, + int format) +{ + char * current_draw_buffer = getDrawingScreen(); + bool pixel = 0; + int byte_size_wide = img_width / 8; + for (int i=0; i < blit_height; i++) + { + for (int j=0; j < blit_width; j++) + { + pixel = img[ byte_size_wide*(srcy+i) + (srcx+j)/8] & (0x80 >> ((srcx+j)%8)); + + switch(format) + { + case BITBLT_SRCCOPY: if (pixel) setpixel(dstx+j,dsty+i); else + clearpixel(dstx+j,dsty+i); break; + case BITBLT_SRCOR: if (pixel) setpixel(dstx+j,dsty+i); break; + case BITBLT_SRCAND: if (!pixel) clearpixel(dstx+j,dsty+i); break; + } + } + } + +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ST7565R.h Thu Aug 11 16:00:05 2011 +0000 @@ -0,0 +1,180 @@ +/* + * mbed ST7565R Display Driver + * Copyright (c) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef ST7565R_H +#define ST7565R_H + +#include "Text.h" +#include "mbed.h" + +typedef struct +{ + char screen1[512]; + char screen2[512]; +} screen_memory_t; + +typedef struct +{ + short x; + short y; +} loc_t; +/** ST7565R Display Driver; uses some code taken from the TextLCD driver for displaying text. + * + * Example: + * @code + * #include "ST7565R.h" + * #include "mbed.h" + * + * ST7565R lcd(p12,p29,p7,p5, p30 , false); + * + * int main() { + * lcd.printf("Hello world!"); + * } + * @endcode + */ +class ST7565R : public Stream +{ +public: + enum { + BITBLT_SRCCOPY, + BITBLT_SRCAND, + BITBLT_SRCOR + }; + /** Create a ST7565R display object on the pins specified. + * + * @param cs The chip select pin for the display. + * @param a0 The data/read select line for the display; specified as A0 in the datasheet. + * @param scl The clock pin for SPI. + * @param rst The reset pin on the display. + * @param BackBuffer The display I have has a memory area thats can be used for backbuffering. This turns this option on. When using this option you must call swapBuffers() in order to display your changes to the screen. This is to help prevent flickering on the display. If not using a backbuffer all writes using bitblt or setpixel are immediate and visible to the screen. + */ + ST7565R(PinName cs,PinName a0, PinName scl, PinName si, PinName rst, bool BackBuffer); + + /** Function to move the text cursor to where specified; + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + * + * @param col The column number. With the font in Text.h this number is 21 and can be retrieved from columns() + * @param row The chip select pin for the display. + */ + void moveto(int col, int row); + + /** Function to send a command byte to the display. + * + * @param value The command byte to send over spi. + */ + void command(int value); + + /** Function to send display data to the display. + * + * @param value The data byte to send over spi. Memory is arranged in pages. A data write will cover 8 pixels vertically (the page) and only one column. A data write will increment the column by 1 but will never increment the page. + */ + void data(int value); + + /** Function that uses the reset line and then reinitializes the display as well as clearing the current buffer. + */ + void reset(); + + /** Function to set a specific pixel to black. + * @param x Measured from the top left of the display; If the display is in backbuffering mode the pixel change is completely within the offscreen buffer and won't display until swapBuffer() is called. + * @param y Measured from the top left of the display; If the display is in backbuffering mode the pixel change is completely within the offscreen buffer and won't display until swapBuffer() is called. + */ + void setpixel(int x, int y); + + /** Function to set a specific pixel to white. + * @param x Measured from the top left of the display; If the display is in backbuffering mode the pixel change is completely within the offscreen buffer and won't display until swapBuffer() is called. + * @param y Measured from the top left of the display; If the display is in backbuffering mode the pixel change is completely within the offscreen buffer and won't display until swapBuffer() is called. + */ + void clearpixel(int x, int y); + /** Function to get the buffer being used inside the object to do bit writes, required because of the lack of reading screen data back over spi for the ST7565R. The buffer returned is the one that you can draw to without immediately updating the screen. + */ + char * getDrawingScreen(); + /** Function to get the buffer being used inside the object to do bit writes, required because of the lack of reading screen data back over spi for the ST7565R. The buffer returned is the one that has the current screen data on it. Without backbuffering mode this returns the current screen. + */ + char * getCurrentScreen(); + /** Function to clear the current drawing buffer. This clears the current screen when not using a backbuffer. + */ + void clearBuffer(); + /** Function to commit all the data to the drawing buffer, then calls the display command to change buffers so as to not promote a flicker in the display. Not needed if not using a backbuffer. + */ + void swapBuffers(); + /** Function to blit an image to the screen. Used internally for copying the font to the screen, but also is powerful for displaying any monochrome image. + * @param dstx The x coordinate destination on the screen you want to blit to. + * @param dsty The y coordinate destination on the screen you want to blit to. + * @param blit_width The width of the image copy you want to perform. + * @param blit_height The height of the image copy you want to perform. + * @param img The pointer to the data of the image. + * @param img_width The width of the image. Required for calculating the address to start the next line in the img buffer. + * @param img_height The height of the image. + * @param srcx The x coordinate where to start copying image data from within the source image. + * @param srcy The y coordinate where to start copying image data from within the source image + * @param format What kind of copy to perform. BITBLT_SRCCOPY is a standard image copy. + */ + void bitblt(int dstx, int dsty, + int blit_width, int blit_height, + char* img, + int img_width, int img_height, + int srcx, int srcy, + int format); + + /** Function to place a character on the screen. + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + * + * @param column The column number. With the font in Text.h this number is 21 and can be retrieved from columns() + * @param row The row number. With the font in Text.h this number is 4 and can be retrieved from rows() + */ + void character(int column, int row, char c); + /** Function to return the number of columns; for now all it does is returns 21. Reserved for possible later code manipulation with multiple fonts. + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + */ + int columns(); + /** Function to return the number of rows; for now all it does is returns 4. Reserved for possible later code manipulation with multiple fonts. + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + */ + int rows(); +protected: + /** Overwritten function from the Stream object. + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + */ + virtual int _putc(int value); + /** Overwritten function from the Stream object. + * DISCLAIMER: All text routines are based on code from TextLCD, who's link is in the cookbook. + */ + virtual int _getc(); +private: + int _column; + int _row; + + SPI _spi; + DigitalOut _cs; + DigitalOut _a0; + DigitalOut _rst; + + bool scr; + + char * current_buffer; + loc_t pos; + screen_memory_t screen; + bool UsingBackBuffer; +}; + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Text.h Thu Aug 11 16:00:05 2011 +0000 @@ -0,0 +1,63 @@ +//Created by bmp2h; compiled: __DATE__ __TIME__ +//file used: font.bmp +//Celeritous Technical Services + +#ifndef TEXT_H +#define TEXT_H + +#define TEXT_WIDTH 103 +#define TEXT_HEIGHT 48 +static const unsigned char TEXT[576] = { + + 0xFF, 0x72, 0x7F, 0xBC, 0xDB, 0xE7, 0xDE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFE, 0x32, 0x6B, 0x8C, 0xD5, 0xE7, 0xBF, 0x7A, 0xF7, 0xFF, 0xFF, 0xFD, + 0xFE, 0x36, 0xC1, 0x7F, 0xB5, 0xEF, 0xBF, 0x78, 0xF7, 0xFF, 0xFF, 0xFB, + 0xFF, 0x7F, 0xEB, 0x9F, 0x7B, 0xFF, 0xBF, 0x70, 0x41, 0xFC, 0x1F, 0xF7, + 0xFF, 0x7F, 0xEB, 0xEE, 0xF5, 0x7F, 0xBF, 0x78, 0xF7, 0xFF, 0xFF, 0xEF, + 0xFF, 0xFF, 0xC1, 0x1D, 0x96, 0xFF, 0xBF, 0x7A, 0xF7, 0x9F, 0xF9, 0xDF, + 0xFF, 0x7F, 0xEB, 0xDD, 0x99, 0x7F, 0xDE, 0xFF, 0xFF, 0x9F, 0xF9, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0x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