An example program of OLEDaccel library.

Dependencies:   OLEDaccel mbed

Fork of OLEDexample by Hideki Kozima

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "OLEDaccel.h"
00003 #include "image.h"  //  xkozima's image (96x64@24bpp)
00004 
00005 OLED display(p16, p14, p15, p11, p13);
00006 AnalogIn ain(p20);
00007 
00008 int main() {
00009     //  radomize
00010     srand(ain.read_u16());
00011 
00012     //  infinite loop
00013     while(1) {
00014         //
00015         //  caption
00016         display.text( 0, 12, "Accelerated OLED", display.color(128, 255, 128) );
00017         display.text( 9, 40, "xkozima (MYU)",    display.color(128, 255, 128));
00018         display.text(18, 50, "(Aug 2012)",       display.color(128, 255, 128));
00019         wait_ms(4000);
00020         display.clear(0);
00021         //
00022         //  random rectangles/filled
00023         for (int k = 0; k < 8; k++) {
00024             for (int c = 0; c < 32; c++) {
00025                 int cx, cy, x, y, w, h;
00026                 //  center
00027                 cx = rand() % 96;
00028                 cy = rand() % 64;
00029                 //  size
00030                 w = rand() % 30 + 6;
00031                 h = rand() % 20 + 4;
00032                 //  upper-left
00033                 x = cx - w / 2;
00034                 y = cy - h / 2;
00035                 if (x < 0) x = 0;
00036                 if (y < 0) y = 0;
00037                 //  adjust size
00038                 if (x + w > 96) w = 96 - x;
00039                 if (y + h > 64) h = 64 - y;
00040                 //  random color
00041                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00042                 //  fill rectangle
00043                 display.rect(x, y, w, h, display.color(c1, c2, c3), 1);
00044             }
00045             //  watch the result!
00046             wait_ms(500);
00047             display.clear(0);
00048         }
00049         //  random rectangles/outlined
00050         for (int k = 0; k < 8; k++) {
00051             for (int c = 0; c < 32; c++) {
00052                 int cx, cy, x, y, w, h;
00053                 //  center
00054                 cx = rand() % 96;
00055                 cy = rand() % 64;
00056                 //  size
00057                 w = rand() % 30 + 6;
00058                 h = rand() % 20 + 4;
00059                 //  upper-left
00060                 x = cx - w / 2;
00061                 y = cy - h / 2;
00062                 if (x < 0) x = 0;
00063                 if (y < 0) y = 0;
00064                 //  adjust size
00065                 if (x + w > 96) w = 96 - x;
00066                 if (y + h > 64) h = 64 - y;
00067                 //  random color
00068                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00069                 //  draw rectangle
00070                 display.rect(x, y, w, h, display.color(c1, c2, c3), 0);
00071             }
00072             //  watch the result!
00073             wait_ms(500);
00074             display.clear(0);
00075         }
00076         //  random circles/filled
00077         for (int k = 0; k < 8; k++) {
00078             for (int c = 0; c < 32; c++) {
00079                 //  coordinates
00080                 int x = rand() % 90 + 3, y = rand() % 60 + 2, r = rand() % 20 + 1;
00081                 if (x - r <  0) r = x;
00082                 if (x + r > 95) r = 95 - x;
00083                 if (y - r <  0) r = y;
00084                 if (y + r > 63) r = 63 - y;
00085                 //  random color
00086                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00087                 //  draw line
00088                 display.circle(x, y, r, display.color(c1, c2, c3), 1);
00089             }
00090             //  watch the result!
00091             wait_ms(500);
00092             display.clear(0);
00093         }
00094         //  random circles/outlined
00095         for (int k = 0; k < 8; k++) {
00096             for (int c = 0; c < 32; c++) {
00097                 //  coordinates
00098                 int x = rand() % 90 + 3, y = rand() % 60 + 2, r = rand() % 20 + 1;
00099                 if (x - r <  0) r = x;
00100                 if (x + r > 95) r = 95 - x;
00101                 if (y - r <  0) r = y;
00102                 if (y + r > 63) r = 63 - y;
00103                 //  random color
00104                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00105                 //  draw line
00106                 display.circle(x, y, r, display.color(c1, c2, c3), 0);
00107             }
00108             //  watch the result!
00109             wait_ms(500);
00110             display.clear(0);
00111         }
00112         //  random ellipses/filled
00113         for (int k = 0; k < 8; k++) {
00114             for (int c = 0; c < 32; c++) {
00115                 //  coordinates
00116                 int x = rand() % 90 + 3, y = rand() % 60 + 2, 
00117                     rx = rand() % 20 + 1, ry = rand() % 20 + 1;
00118                 if (x - rx <  0) rx = x;
00119                 if (x + rx > 95) rx = 95 - x;
00120                 if (y - ry <  0) ry = y;
00121                 if (y + ry > 63) ry = 63 - y;
00122                 //  random color
00123                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00124                 //  draw line
00125                 display.ellipse(x, y, rx, ry, display.color(c1, c2, c3), 1);
00126             }
00127             //  watch the result!
00128             wait_ms(500);
00129             display.clear(0);
00130         }
00131         //  random ellipses/outlined
00132         for (int k = 0; k < 8; k++) {
00133             for (int c = 0; c < 32; c++) {
00134                 //  coordinates
00135                 int x = rand() % 90 + 3, y = rand() % 60 + 2, 
00136                     rx = rand() % 20 + 1, ry = rand() % 20 + 1;
00137                 if (x - rx <  0) rx = x;
00138                 if (x + rx > 95) rx = 95 - x;
00139                 if (y - ry <  0) ry = y;
00140                 if (y + ry > 63) ry = 63 - y;
00141                 //  random color
00142                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00143                 //  draw line
00144                 display.ellipse(x, y, rx, ry, display.color(c1, c2, c3), 0);
00145             }
00146             //  watch the result!
00147             wait_ms(500);
00148             display.clear(0);
00149         }
00150         //  random lines
00151         for (int k = 0; k < 8; k++) {
00152             for (int c = 0; c < 32; c++) {
00153                 //  coordinates
00154                 int x1 = rand() % 96, y1 = rand() % 64, x2 = rand() % 96, y2 = rand() % 64;
00155                 //  random color
00156                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00157                 //  draw line
00158                 display.line(x1, y1, x2, y2, display.color(c1, c2, c3));
00159             }
00160             //  watch the result!
00161             wait_ms(500);
00162             display.clear(0);
00163         }
00164         //  random points
00165         for (int k = 0; k < 8; k++) {
00166             for (int c = 0; c < 1000; c++) {
00167                 //  coordinates
00168                 int x = rand() % 96, y = rand() % 64;
00169                 //  random color
00170                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00171                 //  draw line
00172                 display.point(x, y, display.color(c1, c2, c3));
00173             }
00174             //  watch the result!
00175             wait_ms(500);
00176             display.clear(0);
00177         }
00178         //  random text
00179         for (int k = 0; k < 8; k++) {
00180             for (int c = 0; c < 10; c++) {
00181                 //  coordinates
00182                 int x = rand() % (96 - 18), y = rand() % (64 - 8);
00183                 //  text
00184                 char string[4];
00185                 string[0] = 'A' + rand() % 26;
00186                 string[1] = 'a' + rand() % 26;
00187                 string[2] = '0' + rand() % 10;
00188                 string[3] = '\0';
00189                 //  random color
00190                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00191                 //  draw text
00192                 display.text(x, y, string, display.color(c1, c2, c3));
00193             }
00194             //  watch the result!
00195             wait_ms(500);
00196             display.clear(0);
00197         }
00198         //  random text (with background)
00199         for (int k = 0; k < 8; k++) {
00200             for (int c = 0; c < 10; c++) {
00201                 //  coordinates
00202                 int x = rand() % (96 - 18), y = rand() % (64 - 8);
00203                 //  text
00204                 char string[4];
00205                 string[0] = 'A' + rand() % 26;
00206                 string[1] = 'a' + rand() % 26;
00207                 string[2] = '0' + rand() % 10;
00208                 string[3] = '\0';
00209                 //  random color
00210                 int f1 = rand() % 256, f2 = rand() % 256, f3 = rand() % 256;
00211                 int b1 = rand() % 256, b2 = rand() % 256, b3 = rand() % 256;
00212                 //  draw text
00213                 display.text(x, y, string, display.color(f1, f2, f3), display.color(b1, b2, b3));
00214             }
00215             //  watch the result!
00216             wait_ms(500);
00217             display.clear(0);
00218         }
00219         //
00220         //  slow to fast (rectangles/filled)
00221         int usec = 1000000;
00222         for (int cc = 0; cc < 14; cc++) {
00223             usec /= 2;
00224             int count = 1000000 / usec;
00225             //  random rects
00226             for (int c = 0; c < count; c++) {
00227                 int cx, cy, x, y, w, h;
00228                 //  center
00229                 cx = rand() % 96;
00230                 cy = rand() % 64;
00231                 //  size
00232                 w = rand() % 60 + 3;
00233                 h = rand() % 40 + 2;
00234                 //  upper-left
00235                 x = cx - w / 2;
00236                 y = cy - h / 2;
00237                 if (x < 0) x = 0;
00238                 if (y < 0) y = 0;
00239                 //  adjust size
00240                 if (x + w > 96) w = 96 - x;
00241                 if (y + h > 64) h = 64 - y;
00242                 //  random color
00243                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00244                 //  fill rectangle
00245                 display.rect(x, y, w, h, display.color(c1, c2, c3), 1);
00246                 //  wait (slow to fast)
00247                 wait_us(usec);
00248             }
00249         }
00250         wait_ms(2000);
00251         display.clear(0);
00252         //  slow to fast (rectangles/outlined)       
00253         usec = 1000000;
00254         for (int cc = 0; cc < 14; cc++) {
00255             usec /= 2;
00256             int count = 1000000 / usec;
00257             //  random rects
00258             for (int c = 0; c < count; c++) {
00259                 int cx, cy, x, y, w, h;
00260                 //  center
00261                 cx = rand() % 96;
00262                 cy = rand() % 64;
00263                 //  size
00264                 w = rand() % 60 + 3;
00265                 h = rand() % 40 + 2;
00266                 //  upper-left
00267                 x = cx - w / 2;
00268                 y = cy - h / 2;
00269                 if (x < 0) x = 0;
00270                 if (y < 0) y = 0;
00271                 //  adjust size
00272                 if (x + w > 96) w = 96 - x;
00273                 if (y + h > 64) h = 64 - y;
00274                 //  random color
00275                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00276                 //  draw rectangle
00277                 display.rect(x, y, w, h, display.color(c1, c2, c3), 0);
00278                 //  wait (slow to fast)
00279                 wait_us(usec);
00280             }
00281         }
00282         wait_ms(2000);
00283         display.clear(0);
00284         //  slow to fast (circles/filled)    
00285         usec = 1000000;
00286         for (int cc = 0; cc < 12; cc++) {
00287             usec /= 2;
00288             int count = 1000000 / usec;
00289             //  random circles
00290             for (int c = 0; c < count; c++) {
00291                 //  coordinates
00292                 int x = rand() % 90 + 3, y = rand() % 60 + 2, r = rand() % 20 + 1;
00293                 if (x - r <  0) r = x;
00294                 if (x + r > 95) r = 95 - x;
00295                 if (y - r <  0) r = y;
00296                 if (y + r > 63) r = 63 - y;
00297                 //  random color
00298                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00299                 //  draw circle
00300                 display.circle(x, y, r, display.color(c1, c2, c3), 1);
00301                 //  wait (slow to fast)
00302                 wait_us(usec);
00303             }
00304         }
00305         wait_ms(2000);
00306         display.clear(0);
00307         //  slow to fast (circles/outlined)    
00308         usec = 1000000;
00309         for (int cc = 0; cc < 12; cc++) {
00310             usec /= 2;
00311             int count = 1000000 / usec;
00312             //  random circles
00313             for (int c = 0; c < count; c++) {
00314                 //  coordinates
00315                 int x = rand() % 90 + 3, y = rand() % 60 + 2, r = rand() % 20 + 1;
00316                 if (x - r <  0) r = x;
00317                 if (x + r > 95) r = 95 - x;
00318                 if (y - r <  0) r = y;
00319                 if (y + r > 63) r = 63 - y;
00320                 //  random color
00321                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00322                 //  draw cirlce
00323                 display.circle(x, y, r, display.color(c1, c2, c3), 0);
00324                 //  wait (slow to fast)
00325                 wait_us(usec);
00326             }
00327         }
00328         wait_ms(2000);
00329         display.clear(0);
00330         //  slow to fast (ellipses/filled)    
00331         usec = 1000000;
00332         for (int cc = 0; cc < 12; cc++) {
00333             usec /= 2;
00334             int count = 1000000 / usec;
00335             //  random circles
00336             for (int c = 0; c < count; c++) {
00337                 //  coordinates
00338                 int x = rand() % 90 + 3, y = rand() % 60 + 2, 
00339                     rx = rand() % 20 + 1, ry = rand() % 20 + 1;
00340                 if (x - rx <  0) rx = x;
00341                 if (x + rx > 95) rx = 95 - x;
00342                 if (y - ry <  0) ry = y;
00343                 if (y + ry > 63) ry = 63 - y;
00344                 //  random color
00345                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00346                 //  draw line
00347                 display.ellipse(x, y, rx, ry, display.color(c1, c2, c3), 1);
00348                 //  wait (slow to fast)
00349                 wait_us(usec);
00350             }
00351         }
00352         wait_ms(2000);
00353         display.clear(0);
00354         //  slow to fast (ellipses/outlined)    
00355         usec = 1000000;
00356         for (int cc = 0; cc < 12; cc++) {
00357             usec /= 2;
00358             int count = 1000000 / usec;
00359             //  random circles
00360             for (int c = 0; c < count; c++) {
00361                 //  coordinates
00362                 int x = rand() % 90 + 3, y = rand() % 60 + 2, 
00363                     rx = rand() % 20 + 1, ry = rand() % 20 + 1;
00364                 if (x - rx <  0) rx = x;
00365                 if (x + rx > 95) rx = 95 - x;
00366                 if (y - ry <  0) ry = y;
00367                 if (y + ry > 63) ry = 63 - y;
00368                 //  random color
00369                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00370                 //  draw line
00371                 display.ellipse(x, y, rx, ry, display.color(c1, c2, c3), 0);
00372                 //  wait (slow to fast)
00373                 wait_us(usec);
00374             }
00375         }
00376         wait_ms(2000);
00377         display.clear(0);
00378         //  slow to fast (lines)    
00379         usec = 1000000;
00380         for (int cc = 0; cc < 14; cc++) {
00381             usec /= 2;
00382             int count = 1000000 / usec;
00383             //  random lines
00384             for (int c = 0; c < count; c++) {
00385                 //  coordinates
00386                 int x1 = rand() % 96, y1 = rand() % 64, x2 = rand() % 96, y2 = rand() % 64;
00387                 //  random color
00388                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00389                 //  draw line
00390                 display.line(x1, y1, x2, y2, display.color(c1, c2, c3));
00391                 //  wait (slow to fast)
00392                 wait_us(usec);
00393             }
00394         }
00395         wait_ms(2000);
00396         display.clear(0);
00397         wait_ms(1000);
00398         //  slow to fast (points)    
00399         usec = 200000;
00400         for (int cc = 0; cc < 18; cc++) {
00401             usec /= 2;
00402             int count = 200000 / usec;
00403             //  random lines
00404             for (int c = 0; c < count; c++) {
00405                 //  coordinates
00406                 int x = rand() % 96, y = rand() % 64;
00407                 //  random color
00408                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00409                 //  draw line
00410                 display.point(x, y, display.color(c1, c2, c3));
00411                 //  wait (slow to fast)
00412                 wait_us(usec);
00413             }
00414         }
00415         wait_ms(2000);
00416         display.clear(0);
00417         wait_ms(1000);
00418         //
00419         //  slow to fast (text)    
00420         usec = 1000000;
00421         for (int cc = 0; cc < 12; cc++) {
00422             usec /= 2;
00423             int count = 1000000 / usec;
00424             //  random text
00425             for (int c = 0; c < count; c++) {
00426                 //  coordinates
00427                 int x = rand() % (96 - 18), y = rand() % (64 - 8);
00428                 //  text
00429                 char string[4];
00430                 string[0] = 'A' + rand() % 26;
00431                 string[1] = 'a' + rand() % 26;
00432                 string[2] = '0' + rand() % 10;
00433                 string[3] = '\0';
00434                 //  random color
00435                 int c1 = rand() % 256, c2 = rand() % 256, c3 = rand() % 256;
00436                 //  draw text
00437                 display.text(x, y, string, display.color(c1, c2, c3));
00438                 //  wait (slow to fast)
00439                 wait_us(usec);
00440             }
00441         }
00442         wait_ms(2000);
00443         display.clear(0);        
00444         //
00445         //  slow to fast (text with background)    
00446         usec = 1000000;
00447         for (int cc = 0; cc < 12; cc++) {
00448             usec /= 2;
00449             int count = 1000000 / usec;
00450             //  random text
00451             for (int c = 0; c < count; c++) {
00452                 //  coordinates
00453                 int x = rand() % (96 - 18), y = rand() % (64 - 8);
00454                 //  text
00455                 char string[4];
00456                 string[0] = 'A' + rand() % 26;
00457                 string[1] = 'a' + rand() % 26;
00458                 string[2] = '0' + rand() % 10;
00459                 string[3] = '\0';
00460                 //  random color
00461                 int f1 = rand() % 256, f2 = rand() % 256, f3 = rand() % 256;
00462                 int b1 = rand() % 256, b2 = rand() % 256, b3 = rand() % 256;
00463                 //  draw text
00464                 display.text(x, y, string, display.color(f1, f2, f3), display.color(b1, b2, b3));
00465                 //  wait (slow to fast)
00466                 wait_us(usec);
00467             }
00468         }
00469         wait_ms(2000);
00470         display.clear(0);        
00471         //
00472         //  checker 32x32
00473         for (int yy = 0; yy < 2; yy++) {
00474             int col = yy;
00475             for (int xx = 0; xx < 3; xx++) {
00476                 display.rect(xx * 32, yy * 32, 32, 32, 
00477                              (col++ % 2)? display.color(255, 0, 0): 
00478                                           display.color(255, 255, 255), 
00479                              1 );
00480                 //wait_us(32*32/10);
00481             }
00482         }
00483         wait_ms(1000);
00484         //  checker 16x16
00485         for (int yy = 0; yy < 4; yy++) {
00486             int col = yy;
00487             for (int xx = 0; xx < 6; xx++) {
00488                 display.rect(xx * 16, yy * 16, 16, 16, 
00489                              (col++ % 2)? display.color(255, 0, 0): 
00490                                           display.color(255, 255, 255), 
00491                              1 );
00492                 //wait_us(16*16/10);
00493             }
00494         }
00495         wait_ms(1000);
00496         //  checker 8x8
00497         for (int yy = 0; yy < 8; yy++) {
00498             int col = yy;
00499             for (int xx = 0; xx < 12; xx++) {
00500                 display.rect(xx * 8, yy * 8, 8, 8, 
00501                              (col++ % 2)? display.color(255, 0, 0): 
00502                                           display.color(255, 255, 255), 
00503                              1 );
00504                 //wait_us(8*8/10);
00505             }
00506         }
00507         wait_ms(1000);
00508         //  checker 4x4
00509         for (int yy = 0; yy < 16; yy++) {
00510             int col = yy;
00511             for (int xx = 0; xx < 24; xx++) {
00512                 display.rect(xx * 4, yy * 4, 4, 4, 
00513                              (col++ % 2)? display.color(255, 0, 0): 
00514                                           display.color(255, 255, 255), 
00515                              1 );
00516                 //wait_us(4*4/10);
00517             }
00518         }
00519         wait_ms(1000);
00520         //  checker 2x2
00521         for (int yy = 0; yy < 32; yy++) {
00522             int col = yy;
00523             for (int xx = 0; xx < 48; xx++) {
00524                 display.rect(xx * 2, yy * 2, 2, 2, 
00525                              (col++ % 2)? display.color(255, 0, 0): 
00526                                           display.color(255, 255, 255), 
00527                              1 );
00528                 //wait_us(1);
00529             }
00530         }
00531         wait_ms(1000);
00532         //  checker 1x1
00533         for (int yy = 0; yy < 64; yy++) {
00534             int col = yy;
00535             for (int xx = 0; xx < 96; xx++) {
00536                 display.rect(xx * 1, yy * 1, 1, 1, 
00537                              (col++ % 2)? display.color(255, 0, 0): 
00538                                           display.color(255, 255, 255), 
00539                              1 );
00540                 //wait_us(1);
00541             }
00542         }
00543         wait_ms(1000);
00544         display.clear(0);
00545         wait_ms(1000);
00546         //
00547         //  draw image
00548         display.image(0, 0, 96, 64, image8x3);
00549         wait_ms(2000);
00550         //  scroll image
00551         for (int yyy = 0; yyy < 64; yyy++) {
00552             display.image(0, 0, 96, yyy + 1, image8x3 + ((63 - yyy) * 96) * 3 );
00553             display.image(0, yyy + 1, 96, 63 - yyy, image8x3 );
00554         }
00555         wait_ms(2000);
00556         display.clear(0);
00557         wait_ms(1000);   
00558     }   
00559 }