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.
OLED32028P1T.cpp
00001 // mbed library for 4DSystems uOLED-32028-P1T 00002 #include "mbed.h" 00003 #include "OLED32028P1T.h" 00004 00005 OLED32028P1T::OLED32028P1T(PinName serialTx, PinName serialRx, PinName resetPin) : s(serialTx, serialRx), reset(resetPin){ 00006 s.baud(BAUDRATE); 00007 while (s.readable()) { 00008 s.getc(); 00009 } } 00010 // Initialise OLED display and redifine baud faster baud rate. 00011 void OLED32028P1T::init(){ 00012 resetDisplay(); 00013 s.putc(0x55); // send byte for OLED to autodetect baudrate 00014 getResponse(); 00015 s.putc (0x51); // send code to display to change baud rate 00016 #if defined(TARGET_KL05Z) 00017 s.putc (0x0E); // send new display baud rate from list below, upto 128k 00018 #elif defined(TARGET_KL25Z) 00019 s.putc (0x0E); // send new display baud rate from list below, upto 128k 00020 #else 00021 s.putc (0x0F); // send new display baud rate from list below, maximun 282353 00022 #endif 00023 /* 00024 00hex : 110 Baud 00025 01hex : 300 Baud 00026 02hex : 600 Baud 00027 03hex : 1200 Baud 00028 04hex : 2400 Baud 00029 05hex : 4800 Baud 00030 06hex : 9600 Baud 00031 07hex : 14400 Baud 00032 08hex : 19200 Baud 00033 09hex : 31250 Baud 00034 0Ahex : 38400 Baud 00035 0Bhex : 56000 Baud 00036 0Chex : 57600 Baud 00037 0Dhex : 115200 Baud 00038 0Ehex : 128000 Baud (It is actually 129032Baud) 00039 0Fhex : 256000 Baud (It is actually 282353Baud) 00040 10hex : 128000 Baud 00041 11hex : 256000 Baud 00042 */ 00043 wait_ms(10); 00044 #undef BAUDRATE 00045 #if defined(TARGET_KL05Z) 00046 #define BAUDRATE 129032 // ***** set matching new display baud rate, upto 128k ***** 00047 #elif defined(TARGET_KL25Z) 00048 #define BAUDRATE 129032 // ***** set matching new display baud rate, upto 128k ***** 00049 # else 00050 #define BAUDRATE 282353 // ***** set matching new display baud rate, maximum 282353 ***** 00051 #endif 00052 s.baud(BAUDRATE); 00053 while (s.readable()) { 00054 s.getc(); 00055 } } 00056 void OLED32028P1T::resetDisplay(){ 00057 reset = 0; 00058 wait_ms(200); 00059 reset = 1; 00060 wait_ms(1000); 00061 } 00062 void OLED32028P1T::displayControl(int mode, int value){ 00063 s.putc(0x59); 00064 s.putc(mode); 00065 s.putc(value); 00066 getResponse(); 00067 } 00068 void OLED32028P1T::displayOff(){ 00069 s.putc(0x59); 00070 s.putc(0x01); 00071 s.putc(0x00); 00072 getResponse(); 00073 } 00074 void OLED32028P1T::displayOn(){ 00075 s.putc(0x59); 00076 s.putc(0x01); 00077 s.putc(0x01); 00078 getResponse(); 00079 } 00080 void OLED32028P1T::displaySleep(){ 00081 s.putc(0x5A); 00082 s.putc(0x02); 00083 s.putc(0xFF); 00084 while (!s.readable()){} 00085 00086 } 00087 void OLED32028P1T::getResponse(){ 00088 char response = 0x15; 00089 lastCount = 0; 00090 NAKCount = 0; 00091 while (!s.readable() || response == 0x15) { 00092 wait_ms(1); 00093 lastCount++; 00094 if (s.readable()) { 00095 response = s.getc(); // Read response 00096 if (response == 0x06) { 00097 return; 00098 } else if (response == 0x15) { 00099 NAKCount++; 00100 } } } } 00101 int OLED32028P1T::toRGB(int red, int green, int blue){ 00102 int outR = ((red * 31) / 255); 00103 int outG = ((green * 63) / 255); 00104 int outB = ((blue * 31) / 255); 00105 return (outR << 11) | (outG << 5) | outB; 00106 } 00107 void OLED32028P1T::clear(){ 00108 s.putc(0x45); 00109 getResponse(); 00110 _row = 0; 00111 _column = 0; 00112 } 00113 void OLED32028P1T::drawPixel(int x, int y, int color){ 00114 s.putc(0x50); 00115 s.putc(x >> 8); 00116 s.putc(x & 0xFF); 00117 s.putc(y >> 8); 00118 s.putc(y & 0xFF); 00119 s.putc(color >> 8); // MSB 00120 s.putc(color & 0xFF); // LSB 00121 getResponse(); 00122 } 00123 void OLED32028P1T::drawLine(int x1, int y1, int x2, int y2, int color){ 00124 s.putc(0x4C); // Line 00125 s.putc(x1 >> 8); 00126 s.putc(x1 & 0xFF); 00127 s.putc(y1 >> 8); 00128 s.putc(y1 & 0xFF); 00129 s.putc(x2 >> 8); 00130 s.putc(x2 & 0xFF); 00131 s.putc(y2 >> 8); 00132 s.putc(y2 & 0xFF); 00133 s.putc(color >> 8); // MSB 00134 s.putc(color & 0xFF); // LSB 00135 getResponse(); 00136 } 00137 void OLED32028P1T::drawRectangle(int x, int y, int width, int height, int color){ 00138 s.putc(0x72); 00139 s.putc(x >> 8); 00140 s.putc(x & 0xFF); 00141 s.putc(y >> 8); 00142 s.putc(y & 0xFF); 00143 s.putc(x+width >> 8); 00144 s.putc(x+width & 0xFF); 00145 s.putc(y+height >> 8); 00146 s.putc(y+height & 0xFF); 00147 s.putc(color >> 8); // MSB 00148 s.putc(color & 0xFF); // LSB 00149 getResponse(); 00150 } 00151 void OLED32028P1T::drawCircle(int x, int y, int radius, int color){ 00152 s.putc(0x43); 00153 s.putc(0); 00154 s.putc(x); 00155 s.putc(0); 00156 s.putc(y); 00157 s.putc(0); 00158 s.putc(radius); 00159 s.putc(color >> 8); // MSB 00160 s.putc(color & 0xFF); // LSB 00161 getResponse(); 00162 } 00163 void OLED32028P1T::setFontSize(int fontSize){ 00164 s.putc(0x46); 00165 s.putc(fontSize); 00166 _fontSize = fontSize; 00167 getResponse(); 00168 } 00169 void OLED32028P1T::setFontColor(int fontColor){ 00170 _fontColor = fontColor; 00171 } 00172 void OLED32028P1T::setPenSize(int penSize){ 00173 s.putc(0x70); 00174 s.putc(penSize); 00175 _penSize = penSize; 00176 getResponse(); 00177 } 00178 void OLED32028P1T::setTextBackgroundType(int textBackgroundType){ 00179 s.putc(0x4F); 00180 s.putc(textBackgroundType); 00181 getResponse(); 00182 } 00183 void OLED32028P1T::setBackgroundColor(int color){ 00184 s.putc(0x42); 00185 s.putc(color >> 8); // MSB 00186 s.putc(color & 0xFF); // LSB 00187 getResponse(); 00188 } 00189 void OLED32028P1T::drawText(int column, int row, int font_size, char *mytext, int color){ 00190 s.putc(0x73); 00191 // Adjust to center of the screen (26 Columns at font size 0) 00192 //int newCol = 13 - (strlen(mytext)/2); 00193 //printByte(newCol); // column 00194 s.putc(column); // column 00195 s.putc(row); // row 00196 s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font, 3 = 12x16) 00197 s.putc(color >> 8); // MSB 00198 s.putc(color & 0xFF); // LSB 00199 for (int i = 0; i < strlen(mytext); i++) { 00200 s.putc(mytext[i]); // character to write 00201 } 00202 s.putc(0x00); // string terminator (always 0x00) 00203 getResponse(); 00204 } 00205 void OLED32028P1T::drawTextGraphic(int x, int y, int font_size, char *mytext, int width, int height, int color){ 00206 s.putc(0x53); 00207 s.putc(x >> 8); //MSB 00208 s.putc(x & 0xFF); //LSB 00209 s.putc(y >> 8); // MSB 00210 s.putc(y & 0xFF); //LSB 00211 s.putc(font_size); // font size (0 = 5x7 font, 1 = 8x8 font, 2 = 8x12 font, 3 = 12x16) 00212 s.putc(color >> 8); // MSB 00213 s.putc(color & 0xFF); // LSB 00214 s.putc(width); // character width 00215 s.putc(height); // character height 00216 for (int i = 0; i < strlen(mytext); i++) { 00217 s.putc(mytext[i]); // character to write 00218 } 00219 s.putc(0x00); // string terminator (always 0x00) 00220 getResponse(); 00221 } 00222 void OLED32028P1T::drawSingleChar(int column, int row, int theChar, int color){ 00223 s.putc(0x54); 00224 s.putc(theChar); 00225 s.putc(column); 00226 s.putc(row); 00227 s.putc(color >> 8); // MSB 00228 s.putc(color & 0xFF); // LSB 00229 getResponse(); 00230 } 00231 char OLED32028P1T::getPenSize(){ 00232 return _penSize; 00233 } 00234 void OLED32028P1T::drawTextButton(int up_down, int x, int y, int button_colour, int font, int string_colour, int width, int height, char *mytext){ 00235 s.putc(0x62); 00236 s.putc(up_down); 00237 s.putc(x >> 8); 00238 s.putc(x & 0xFF); 00239 s.putc(y >> 8); 00240 s.putc(y & 0xFF); 00241 s.putc(button_colour >> 8); 00242 s.putc(button_colour & 0xFF); 00243 s.putc(font); 00244 s.putc(string_colour >> 8); 00245 s.putc(string_colour & 0xFF); 00246 s.putc(width); 00247 s.putc(height); 00248 for (int i = 0; i < strlen(mytext); i++) { 00249 s.putc(mytext[i]); // characters to write 00250 } 00251 s.putc(0x00); 00252 getResponse(); 00253 } 00254 void OLED32028P1T::enableTouch(){ 00255 s.putc(0x59); // display control command 00256 s.putc(0x05); // Touch Control 00257 s.putc(0x00); // enable Touch 00258 getResponse(); 00259 } 00260 void OLED32028P1T::disableTouch(){ 00261 s.putc(0x59); // display control command 00262 s.putc(0x05); // Touch Control 00263 s.putc(0x01); // disable Touch 00264 getResponse(); 00265 } 00266 unsigned char OLED32028P1T::getTouch(int *xbuffer, int *ybuffer){ //Touch Press on the screen, if return=1, touch coordinates are stored on the buffer[] 00267 00268 //xbuffer=0;ybuffer=0; 00269 s.putc(0x6F); 00270 s.putc(0x04); 00271 xbuffer[0]=s.getc(); 00272 xbuffer[0]=xbuffer[0]<<8; 00273 xbuffer[0]|=s.getc(); 00274 ybuffer[0]=s.getc(); 00275 ybuffer[0]=ybuffer[0]<<8; 00276 ybuffer[0]|=s.getc(); 00277 00278 if(xbuffer[0] != 0){ 00279 s.putc(0x6F); 00280 s.putc(0x05); 00281 xbuffer[0]=s.getc(); 00282 xbuffer[0]=xbuffer[0]<<8; 00283 xbuffer[0]|=s.getc(); 00284 ybuffer[0]=s.getc(); 00285 ybuffer[0]=ybuffer[0]<<8; 00286 ybuffer[0]|=s.getc(); 00287 return 1; 00288 }else{ 00289 return 0; 00290 } 00291 } 00292 unsigned char OLED32028P1T::getTouchPress(int *xbuffer, int *ybuffer){ //Touch Release on the screen, if return=1, touch coordinates are stored on the buffer[] 00293 s.putc(0x6F); 00294 s.putc(0x01); 00295 xbuffer[0]=s.getc(); 00296 xbuffer[0]=xbuffer[0]<<8; 00297 xbuffer[0]|=s.getc(); 00298 ybuffer[0]=s.getc(); 00299 ybuffer[0]=ybuffer[0]<<8; 00300 ybuffer[0]|=s.getc(); 00301 00302 if(xbuffer[0] != 0){ 00303 s.putc(0x6F); 00304 s.putc(0x05); 00305 xbuffer[0]=s.getc(); 00306 xbuffer[0]=xbuffer[0]<<8; 00307 xbuffer[0]|=s.getc(); 00308 ybuffer[0]=s.getc(); 00309 ybuffer[0]=ybuffer[0]<<8; 00310 ybuffer[0]|=s.getc(); 00311 return 1; 00312 }else{ 00313 return 0; 00314 } 00315 } 00316 unsigned char OLED32028P1T::getTouchRelease(int *xbuffer, int *ybuffer){ //Touch Release on the screen, if return=1, touch coordinates are stored on the buffer[] 00317 s.putc(0x6F); 00318 s.putc(0x02); 00319 xbuffer[0]=s.getc(); 00320 xbuffer[0]=xbuffer[0]<<8; 00321 xbuffer[0]|=s.getc(); 00322 ybuffer[0]=s.getc(); 00323 ybuffer[0]=ybuffer[0]<<8; 00324 ybuffer[0]|=s.getc(); 00325 00326 if(xbuffer[0] != 0){ 00327 s.putc(0x6F); 00328 s.putc(0x05); 00329 xbuffer[0]=s.getc(); 00330 xbuffer[0]=xbuffer[0]<<8; 00331 xbuffer[0]|=s.getc(); 00332 ybuffer[0]=s.getc(); 00333 ybuffer[0]=ybuffer[0]<<8; 00334 ybuffer[0]|=s.getc(); 00335 return 1; 00336 }else{ 00337 return 0; 00338 } 00339 } 00340 void OLED32028P1T::setTouchArea(int x1, int y1 , int x2, int y2) { // define touch area 00341 char command[9]= "";int i; 00342 command[0] = (0x75); 00343 command[1] = (x1 >> 8) & 0xFF; 00344 command[2] = x1 & 0xFF; 00345 command[3] = (y1 >> 8) & 0xFF; 00346 command[4] = y1 & 0xFF; 00347 command[5] = (x2 >> 8) & 0xFF; 00348 command[6] = x2 & 0xFF; 00349 command[7] = (y2 >> 8) & 0xFF; 00350 command[8] = y2 & 0xFF; 00351 for (i = 0; i < 9; i++) s.putc(command[i]); 00352 getResponse(); 00353 } 00354 void OLED32028P1T::resetTouchArea(){ 00355 s.putc(0x59); // display control command 00356 s.putc(0x05); // Touch Control 00357 s.putc(0x02); // reset Touch Area to whole screen 00358 getResponse(); 00359 } 00360 void OLED32028P1T::waitTouch(int x){ 00361 s.putc(0x77); // wait Touch command 00362 s.putc(x >> 8); // MSB 00363 s.putc(x & 0xFF); // LSB 00364 getResponse(); 00365 } 00366 unsigned char OLED32028P1T::stringSD(int x1, int y1, int x2, int y2, int colour, unsigned char font, unsigned char fill, int BS, int BR, char name[]){ 00367 //Draw a String from a text file contained on the micro SD card on the screen 00368 unsigned char counter=0; 00369 s.putc('S'); 00370 s.putc('S'); //from SD 00371 s.putc(x1>>8); 00372 s.putc(x1); 00373 s.putc(y1>>8); 00374 s.putc(y1); 00375 s.putc(x2>>8); 00376 s.putc(x2); 00377 s.putc(y2>>8); 00378 s.putc(y2); 00379 s.putc(colour>>8); 00380 s.putc(colour); 00381 s.putc(font); 00382 s.putc(fill); 00383 s.putc(BS>>8); 00384 s.putc(BS); 00385 s.putc(BR>>8); 00386 s.putc(BR); 00387 while(1){ 00388 s.putc(name[counter]); 00389 if(name[counter]==0x00){ 00390 break; 00391 } 00392 counter++; 00393 } 00394 return s.getc(); 00395 } 00396 unsigned char OLED32028P1T::imageSD(int x, int y, char name[]){ 00397 //Draw an Image contained on the micro SD card on the screen, top left corner coordinates 00398 unsigned char counter=0; 00399 s.putc('I'); 00400 s.putc('S'); //from SD 00401 s.putc(x>>8); 00402 s.putc(x); 00403 s.putc(y>>8); 00404 s.putc(y); 00405 while(1){ 00406 s.putc(name[counter]); 00407 if(name[counter]==0x00){ 00408 break; 00409 } 00410 counter++; 00411 } 00412 return s.getc(); 00413 } 00414 int OLED32028P1T::_putc(int value){ 00415 if (value == '\n') { 00416 _column = 0; 00417 _row++; 00418 if(_row >= rows()) { 00419 _row = 0; 00420 } 00421 } else { 00422 drawSingleChar(_column, _row, value, _fontColor); 00423 wait_ms(1); //TODO: why is this needed? 00424 _column++; 00425 if (_column >= columns()) { 00426 _column = 0; 00427 _row++; 00428 if(_row >= rows()) { 00429 _row = 0; 00430 } } } 00431 return value; 00432 } 00433 void OLED32028P1T::locate(int column, int row){ 00434 _column = column; 00435 _row = row; 00436 } 00437 int OLED32028P1T::rows(){ 00438 return 30; 00439 } 00440 int OLED32028P1T::columns(){ 00441 return 55; 00442 } 00443 int OLED32028P1T::_getc(){ 00444 return -1; 00445 }
Generated on Thu Jul 21 2022 17:24:33 by
1.7.2