Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
C12832.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 // 13.10.12 initial design 00016 // 25.10.12 add autorefresh of screen 00017 // 25.10.12 add standart font 00018 // 20.12.12 add bitmap graphics 00019 00020 // optional defines : 00021 // #define debug_lcd 1 00022 00023 #include "C12832.h" 00024 #include "mbed.h" 00025 #include "stdio.h" 00026 #include "Small_7.h" 00027 00028 #define BPP 1 // Bits per pixel 00029 00030 00031 C12832::C12832(PinName mosi, PinName sck, PinName reset, PinName a0, PinName ncs, const char* name) 00032 : GraphicsDisplay(name),_spi(mosi,NC,sck),_reset(reset),_A0(a0),_CS(ncs) 00033 { 00034 orientation = 1; 00035 draw_mode = NORMAL; 00036 char_x = 0; 00037 lcd_reset(); 00038 } 00039 00040 00041 int C12832::width() 00042 { 00043 if (orientation == 0 || orientation == 2) return 32; 00044 else return 128; 00045 } 00046 00047 int C12832::height() 00048 { 00049 if (orientation == 0 || orientation == 2) return 128; 00050 else return 32; 00051 } 00052 00053 00054 void C12832::invert(unsigned int o) 00055 { 00056 if(o == 0) wr_cmd(0xA6); 00057 else wr_cmd(0xA7); 00058 } 00059 00060 00061 void C12832::set_contrast(unsigned int o) 00062 { 00063 contrast = o; 00064 wr_cmd(0x81); // set volume 00065 wr_cmd(o & 0x3F); 00066 } 00067 00068 unsigned int C12832::get_contrast(void) 00069 { 00070 return(contrast); 00071 } 00072 00073 00074 // write command to lcd controller 00075 00076 void C12832::wr_cmd(unsigned char cmd) 00077 { 00078 _A0 = 0; 00079 _CS = 0; 00080 _spi.write(cmd); 00081 _CS = 1; 00082 } 00083 00084 // write data to lcd controller 00085 00086 void C12832::wr_dat(unsigned char dat) 00087 { 00088 _A0 = 1; 00089 _CS = 0; 00090 _spi.write(dat); 00091 _CS = 1; 00092 } 00093 00094 // reset and init the lcd controller 00095 00096 void C12832::lcd_reset() 00097 { 00098 00099 _spi.format(8,3); // 8 bit spi mode 3 00100 _spi.frequency(20000000); // 19,2 Mhz SPI clock 00101 _A0 = 0; 00102 _CS = 1; 00103 _reset = 0; // display reset 00104 wait_us(50); 00105 _reset = 1; // end reset 00106 wait_ms(5); 00107 00108 /* Start Initial Sequence ----------------------------------------------------*/ 00109 00110 wr_cmd(0xAE); // display off 00111 wr_cmd(0xA2); // bias voltage 00112 00113 wr_cmd(0xA0); 00114 wr_cmd(0xC8); // colum normal 00115 00116 wr_cmd(0x22); // voltage resistor ratio 00117 wr_cmd(0x2F); // power on 00118 //wr_cmd(0xA4); // LCD display ram 00119 wr_cmd(0x40); // start line = 0 00120 wr_cmd(0xAF); // display ON 00121 00122 wr_cmd(0x81); // set contrast 00123 wr_cmd(0x17); // set contrast 00124 00125 wr_cmd(0xA6); // display normal 00126 00127 00128 // clear and update LCD 00129 memset(buffer,0x00,512); // clear display buffer 00130 copy_to_lcd(); 00131 auto_up = 1; // switch on auto update 00132 // dont do this by default. Make the user call 00133 //claim(stdout); // redirekt printf to lcd 00134 locate(0,0); 00135 set_font((unsigned char*)Small_7); // standart font 00136 } 00137 00138 // set one pixel in buffer 00139 00140 void C12832::pixel(int x, int y, int color) 00141 { 00142 // first check parameter 00143 if(x > 128 || y > 32 || x < 0 || y < 0) return; 00144 00145 if(draw_mode == NORMAL) { 00146 if(color == 0) 00147 buffer[x + ((y/8) * 128)] &= ~(1 << (y%8)); // erase pixel 00148 else 00149 buffer[x + ((y/8) * 128)] |= (1 << (y%8)); // set pixel 00150 } else { // XOR mode 00151 if(color == 1) 00152 buffer[x + ((y/8) * 128)] ^= (1 << (y%8)); // xor pixel 00153 } 00154 } 00155 00156 // update lcd 00157 00158 void C12832::copy_to_lcd(void) 00159 { 00160 00161 int i=0; 00162 00163 //page 0 00164 wr_cmd(0x00); // set column low nibble 0 00165 wr_cmd(0x10); // set column hi nibble 0 00166 wr_cmd(0xB0); // set page address 0 00167 _A0 = 1; 00168 for(i=0; i<128; i++) { 00169 wr_dat(buffer[i]); 00170 } 00171 00172 // page 1 00173 wr_cmd(0x00); // set column low nibble 0 00174 wr_cmd(0x10); // set column hi nibble 0 00175 wr_cmd(0xB1); // set page address 1 00176 _A0 = 1; 00177 for(i=128; i<256; i++) { 00178 wr_dat(buffer[i]); 00179 } 00180 00181 //page 2 00182 wr_cmd(0x00); // set column low nibble 0 00183 wr_cmd(0x10); // set column hi nibble 0 00184 wr_cmd(0xB2); // set page address 2 00185 _A0 = 1; 00186 for(i=256; i<384; i++) { 00187 wr_dat(buffer[i]); 00188 } 00189 00190 //page 3 00191 wr_cmd(0x00); // set column low nibble 0 00192 wr_cmd(0x10); // set column hi nibble 0 00193 wr_cmd(0xB3); // set page address 3 00194 _A0 = 1; 00195 00196 _CS = 0; 00197 00198 for(i=384; i<512; i++) { 00199 wr_dat(buffer[i]); 00200 } 00201 00202 } 00203 00204 void C12832::cls(void) 00205 { 00206 memset(buffer,0x00,512); // clear display buffer 00207 copy_to_lcd(); 00208 } 00209 00210 00211 void C12832::line(int x0, int y0, int x1, int y1, int color) 00212 { 00213 int dx = 0, dy = 0; 00214 int dx_sym = 0, dy_sym = 0; 00215 int dx_x2 = 0, dy_x2 = 0; 00216 int di = 0; 00217 00218 dx = x1-x0; 00219 dy = y1-y0; 00220 00221 // if (dx == 0) { /* vertical line */ 00222 // if (y1 > y0) vline(x0,y0,y1,color); 00223 // else vline(x0,y1,y0,color); 00224 // return; 00225 // } 00226 00227 if (dx > 0) { 00228 dx_sym = 1; 00229 } else { 00230 dx_sym = -1; 00231 } 00232 // if (dy == 0) { /* horizontal line */ 00233 // if (x1 > x0) hline(x0,x1,y0,color); 00234 // else hline(x1,x0,y0,color); 00235 // return; 00236 // } 00237 00238 if (dy > 0) { 00239 dy_sym = 1; 00240 } else { 00241 dy_sym = -1; 00242 } 00243 00244 dx = dx_sym*dx; 00245 dy = dy_sym*dy; 00246 00247 dx_x2 = dx*2; 00248 dy_x2 = dy*2; 00249 00250 if (dx >= dy) { 00251 di = dy_x2 - dx; 00252 while (x0 != x1) { 00253 00254 pixel(x0, y0, color); 00255 x0 += dx_sym; 00256 if (di<0) { 00257 di += dy_x2; 00258 } else { 00259 di += dy_x2 - dx_x2; 00260 y0 += dy_sym; 00261 } 00262 } 00263 pixel(x0, y0, color); 00264 } else { 00265 di = dx_x2 - dy; 00266 while (y0 != y1) { 00267 pixel(x0, y0, color); 00268 y0 += dy_sym; 00269 if (di < 0) { 00270 di += dx_x2; 00271 } else { 00272 di += dx_x2 - dy_x2; 00273 x0 += dx_sym; 00274 } 00275 } 00276 pixel(x0, y0, color); 00277 } 00278 if(auto_up) copy_to_lcd(); 00279 } 00280 00281 void C12832::rect(int x0, int y0, int x1, int y1, int color) 00282 { 00283 00284 if (x1 > x0) line(x0,y0,x1,y0,color); 00285 else line(x1,y0,x0,y0,color); 00286 00287 if (y1 > y0) line(x0,y0,x0,y1,color); 00288 else line(x0,y1,x0,y0,color); 00289 00290 if (x1 > x0) line(x0,y1,x1,y1,color); 00291 else line(x1,y1,x0,y1,color); 00292 00293 if (y1 > y0) line(x1,y0,x1,y1,color); 00294 else line(x1,y1,x1,y0,color); 00295 00296 if(auto_up) copy_to_lcd(); 00297 } 00298 00299 void C12832::fillrect(int x0, int y0, int x1, int y1, int color) 00300 { 00301 int l,c,i; 00302 if(x0 > x1) { 00303 i = x0; 00304 x0 = x1; 00305 x1 = i; 00306 } 00307 00308 if(y0 > y1) { 00309 i = y0; 00310 y0 = y1; 00311 y1 = i; 00312 } 00313 00314 for(l = x0; l<= x1; l ++) { 00315 for(c = y0; c<= y1; c++) { 00316 pixel(l,c,color); 00317 } 00318 } 00319 if(auto_up) copy_to_lcd(); 00320 } 00321 00322 00323 00324 void C12832::circle(int x0, int y0, int r, int color) 00325 { 00326 00327 int draw_x0, draw_y0; 00328 int draw_x1, draw_y1; 00329 int draw_x2, draw_y2; 00330 int draw_x3, draw_y3; 00331 int draw_x4, draw_y4; 00332 int draw_x5, draw_y5; 00333 int draw_x6, draw_y6; 00334 int draw_x7, draw_y7; 00335 int xx, yy; 00336 int di; 00337 //WindowMax(); 00338 if (r == 0) { /* no radius */ 00339 return; 00340 } 00341 00342 draw_x0 = draw_x1 = x0; 00343 draw_y0 = draw_y1 = y0 + r; 00344 if (draw_y0 < height()) { 00345 pixel(draw_x0, draw_y0, color); /* 90 degree */ 00346 } 00347 00348 draw_x2 = draw_x3 = x0; 00349 draw_y2 = draw_y3 = y0 - r; 00350 if (draw_y2 >= 0) { 00351 pixel(draw_x2, draw_y2, color); /* 270 degree */ 00352 } 00353 00354 draw_x4 = draw_x6 = x0 + r; 00355 draw_y4 = draw_y6 = y0; 00356 if (draw_x4 < width()) { 00357 pixel(draw_x4, draw_y4, color); /* 0 degree */ 00358 } 00359 00360 draw_x5 = draw_x7 = x0 - r; 00361 draw_y5 = draw_y7 = y0; 00362 if (draw_x5>=0) { 00363 pixel(draw_x5, draw_y5, color); /* 180 degree */ 00364 } 00365 00366 if (r == 1) { 00367 return; 00368 } 00369 00370 di = 3 - 2*r; 00371 xx = 0; 00372 yy = r; 00373 while (xx < yy) { 00374 00375 if (di < 0) { 00376 di += 4*xx + 6; 00377 } else { 00378 di += 4*(xx - yy) + 10; 00379 yy--; 00380 draw_y0--; 00381 draw_y1--; 00382 draw_y2++; 00383 draw_y3++; 00384 draw_x4--; 00385 draw_x5++; 00386 draw_x6--; 00387 draw_x7++; 00388 } 00389 xx++; 00390 draw_x0++; 00391 draw_x1--; 00392 draw_x2++; 00393 draw_x3--; 00394 draw_y4++; 00395 draw_y5++; 00396 draw_y6--; 00397 draw_y7--; 00398 00399 if ( (draw_x0 <= width()) && (draw_y0>=0) ) { 00400 pixel(draw_x0, draw_y0, color); 00401 } 00402 00403 if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { 00404 pixel(draw_x1, draw_y1, color); 00405 } 00406 00407 if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { 00408 pixel(draw_x2, draw_y2, color); 00409 } 00410 00411 if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) { 00412 pixel(draw_x3, draw_y3, color); 00413 } 00414 00415 if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { 00416 pixel(draw_x4, draw_y4, color); 00417 } 00418 00419 if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { 00420 pixel(draw_x5, draw_y5, color); 00421 } 00422 if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) { 00423 pixel(draw_x6, draw_y6, color); 00424 } 00425 if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { 00426 pixel(draw_x7, draw_y7, color); 00427 } 00428 } 00429 if(auto_up) copy_to_lcd(); 00430 } 00431 00432 void C12832::fillcircle(int x, int y, int r, int color) 00433 { 00434 int i,up; 00435 up = auto_up; 00436 auto_up = 0; // off 00437 for (i = 0; i <= r; i++) 00438 circle(x,y,i,color); 00439 auto_up = up; 00440 if(auto_up) copy_to_lcd(); 00441 } 00442 00443 void C12832::setmode(int mode) 00444 { 00445 draw_mode = mode; 00446 } 00447 00448 void C12832::locate(int x, int y) 00449 { 00450 char_x = x; 00451 char_y = y; 00452 } 00453 00454 00455 00456 int C12832::columns() 00457 { 00458 return width() / font[1]; 00459 } 00460 00461 00462 00463 int C12832::rows() 00464 { 00465 return height() / font[2]; 00466 } 00467 00468 00469 00470 int C12832::_putc(int value) 00471 { 00472 if (value == '\n') { // new line 00473 char_x = 0; 00474 char_y = char_y + font[2]; 00475 if (char_y >= height() - font[2]) { 00476 char_y = 0; 00477 } 00478 } else { 00479 character(char_x, char_y, value); 00480 if(auto_up) copy_to_lcd(); 00481 } 00482 return value; 00483 } 00484 00485 void C12832::character(int x, int y, int c) 00486 { 00487 unsigned int hor,vert,offset,bpl,j,i,b; 00488 unsigned char* zeichen; 00489 unsigned char z,w; 00490 00491 if ((c < 31) || (c > 127)) return; // test char range 00492 00493 // read font parameter from start of array 00494 offset = font[0]; // bytes / char 00495 hor = font[1]; // get hor size of font 00496 vert = font[2]; // get vert size of font 00497 bpl = font[3]; // bytes per line 00498 00499 if (char_x + hor > width()) { 00500 char_x = 0; 00501 char_y = char_y + vert; 00502 if (char_y >= height() - font[2]) { 00503 char_y = 0; 00504 } 00505 } 00506 00507 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap 00508 w = zeichen[0]; // width of actual char 00509 // construct the char into the buffer 00510 for (j=0; j<vert; j++) { // vert line 00511 for (i=0; i<hor; i++) { // horz line 00512 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; 00513 b = 1 << (j & 0x07); 00514 if (( z & b ) == 0x00) { 00515 pixel(x+i,y+j,0); 00516 } else { 00517 pixel(x+i,y+j,1); 00518 } 00519 00520 } 00521 } 00522 00523 char_x += w; 00524 } 00525 00526 00527 void C12832::set_font(unsigned char* f) 00528 { 00529 font = f; 00530 } 00531 00532 void C12832::set_auto_up(unsigned int up) 00533 { 00534 if(up ) auto_up = 1; 00535 else auto_up = 0; 00536 } 00537 00538 unsigned int C12832::get_auto_up(void) 00539 { 00540 return (auto_up); 00541 } 00542 00543 void C12832::print_bm(Bitmap bm, int x, int y) 00544 { 00545 int h,v,b; 00546 char d; 00547 00548 for(v=0; v < bm.ySize; v++) { // lines 00549 for(h=0; h < bm.xSize; h++) { // pixel 00550 if(h + x > 127) break; 00551 if(v + y > 31) break; 00552 d = bm.data[bm.Byte_in_Line * v + ((h & 0xF8) >> 3)]; 00553 b = 0x80 >> (h & 0x07); 00554 if((d & b) == 0) { 00555 pixel(x+h,y+v,0); 00556 } else { 00557 pixel(x+h,y+v,1); 00558 } 00559 } 00560 } 00561 00562 } 00563 00564
Generated on Tue Jul 12 2022 14:23:27 by
