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