A ST7565R display driver written with backbuffering capabilities. Model used and tested: NHD-C12832A1Z-FSW-FBW-3V3
ST7565R.cpp
00001 00002 #include "ST7565R.h" 00003 #include "mbed.h" 00004 00005 ST7565R::ST7565R(PinName cs, PinName a0, PinName scl, PinName si, PinName rst, bool BackBuffer) : _cs(cs), _a0(a0), _spi(si, NC, scl), _rst(rst) 00006 { 00007 _spi.format(8,3); 00008 _spi.frequency(20000000); //datasheet 50 ns sclk 00009 _rst=1; 00010 _a0=0; 00011 _cs=1; 00012 scr=1; 00013 reset(); 00014 UsingBackBuffer = BackBuffer; 00015 } 00016 00017 void ST7565R::reset() 00018 { 00019 _rst = 0; 00020 wait_ms(1); 00021 _rst = 1; 00022 wait_ms(1); 00023 command(0xA0); 00024 command(0xAF); 00025 command(0xC0); 00026 command(0xA2); 00027 command(0x2F); 00028 command(0x24); 00029 command(0x81); 00030 command(0x00); 00031 command(0xA7); 00032 00033 00034 clearBuffer(); 00035 00036 } 00037 #define FONT_SIZE_X 6 00038 #define FONT_SIZE_Y 8 00039 void ST7565R::moveto(int col, int row) 00040 { 00041 _column = col; 00042 _row = row; 00043 } 00044 void ST7565R::character(int column, int row, char c) 00045 { 00046 char v = c - 0x20; 00047 if (c >= 0x20 && c < 0x80) 00048 { 00049 c-=0x20; 00050 bitblt(column*FONT_SIZE_X,row*FONT_SIZE_Y, 00051 FONT_SIZE_X, FONT_SIZE_Y, (char*)TEXT, 00052 TEXT_WIDTH, TEXT_HEIGHT, 00053 (v%0x10) * FONT_SIZE_X, (v/0x10)*FONT_SIZE_Y,BITBLT_SRCCOPY); 00054 } 00055 } 00056 int ST7565R::_putc(int value) { 00057 if (value == '\n') { 00058 _column = 0; 00059 _row++; 00060 if (_row >= rows()) { 00061 _row = 0; 00062 } 00063 } else { 00064 character(_column, _row, value); 00065 _column++; 00066 if (_column >= columns()) { 00067 _column = 0; 00068 _row++; 00069 if (_row >= rows()) { 00070 _row = 0; 00071 } 00072 } 00073 } 00074 return value; 00075 } 00076 00077 int ST7565R::rows() { return 4; } 00078 int ST7565R::columns() { return 21; } 00079 int ST7565R::_getc() { 00080 return -1; 00081 } 00082 00083 00084 void ST7565R::clearBuffer() 00085 { 00086 00087 char * screen =getDrawingScreen(); 00088 int i=512; 00089 while(i--) 00090 screen[i]=0xff; 00091 00092 00093 if (!UsingBackBuffer) 00094 swapBuffers(); //should just commit the white to memory 00095 } 00096 void ST7565R::setpixel(int x, int y) 00097 { 00098 if (x < 0 || x > 127) return; 00099 if (y < 0 || y > 31) return; 00100 char * screen = getDrawingScreen(); 00101 loc_t byte = { x , (y / 8) * 128}; 00102 screen[byte.y + byte.x] |= 0x80 >> (y%8); 00103 00104 //if theres no backbuffer,then we'll do our writes immediately 00105 if (!UsingBackBuffer) 00106 { 00107 command(0xb0 | ( (3-(y/8))+scr*4)); 00108 command(0x10 | (x >> 4)); //column upper 00109 command(0x00 | (x & 0x0f)); //column lower 00110 data(screen[byte.y +byte.x]); 00111 } 00112 } 00113 00114 void ST7565R::clearpixel(int x, int y) 00115 { 00116 if (x < 0 || x > 127) return; 00117 if (y < 0 || y > 31) return; 00118 char * screen = getDrawingScreen(); 00119 loc_t byte = { x , (y / 8) * 128}; 00120 screen[byte.y + byte.x] &= ~(0x80 >> (y%8)); 00121 00122 //if theres no backbuffer,then we'll do our writes immediately 00123 if (!UsingBackBuffer) 00124 { 00125 command(0xb0 | ( (3-(y/8))+scr*4)); 00126 command(0x10 | (x >> 4)); //column upper 00127 command(0x00 | (x & 0x0f)); //column lower 00128 data(screen[byte.y +byte.x]); 00129 } 00130 } 00131 00132 void ST7565R::data(int value) 00133 { 00134 _cs = 0; 00135 _a0 = 1; 00136 _spi.write(value); 00137 _cs = 1; 00138 00139 } 00140 void ST7565R::command(int value) 00141 { 00142 _cs = 0; 00143 _a0 = 0; 00144 _spi.write(value); 00145 _cs = 1; 00146 } 00147 00148 void ST7565R::swapBuffers() 00149 { 00150 char * screen = getDrawingScreen(); 00151 for (int i=0; i<4; i++) 00152 { 00153 00154 command(0xb0 | ( (3-i)+scr*4)); 00155 command(0x10); 00156 command(0x00); 00157 for (int j=0; j<128; j++) 00158 { 00159 data(screen[i*128+j]); 00160 } 00161 } 00162 command(0x40 | scr * 32); 00163 if (UsingBackBuffer) scr^=1; 00164 } 00165 #define SCREEN_1 0 00166 #define SCREEN_2 1 00167 00168 00169 char * ST7565R::getDrawingScreen() 00170 { 00171 if (scr) 00172 return screen.screen1; 00173 else 00174 return screen.screen2; 00175 } 00176 void ST7565R::bitblt(int dstx, int dsty, 00177 int blit_width, int blit_height, 00178 char* img, 00179 int img_width, int img_height, 00180 int srcx, int srcy, 00181 int format) 00182 { 00183 char * current_draw_buffer = getDrawingScreen(); 00184 bool pixel = 0; 00185 int byte_size_wide = img_width / 8; 00186 for (int i=0; i < blit_height; i++) 00187 { 00188 for (int j=0; j < blit_width; j++) 00189 { 00190 pixel = img[ byte_size_wide*(srcy+i) + (srcx+j)/8] & (0x80 >> ((srcx+j)%8)); 00191 00192 switch(format) 00193 { 00194 case BITBLT_SRCCOPY: if (pixel) setpixel(dstx+j,dsty+i); else 00195 clearpixel(dstx+j,dsty+i); break; 00196 case BITBLT_SRCOR: if (pixel) setpixel(dstx+j,dsty+i); break; 00197 case BITBLT_SRCAND: if (!pixel) clearpixel(dstx+j,dsty+i); break; 00198 } 00199 } 00200 } 00201 00202 }
Generated on Wed Jul 13 2022 07:17:09 by 1.7.2