Eric Johnson / Mbed 2 deprecated SSD1306-I2C

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "SSD1306-Library.h"
00004 #include "FreeSans9pt7b.h"
00005 
00006 
00007 Serial pc(USBTX, USBRX);
00008 
00009 DigitalOut gpo(D0);
00010 DigitalOut led(LED_RED);
00011 
00012 /**************************************************
00013  * graphics_simple_demo
00014  **************************************************/
00015 void graphics_simple_demo(SSD1306 display) {
00016     display.setRotation(0);
00017     display.clearDisplay();
00018     display.drawFastVLine(SSD1306_LCDWIDTH/2, 0, SSD1306_LCDHEIGHT, WHITE);
00019     display.display();
00020 
00021 //    wait(1);
00022     display.clearDisplay();
00023     display.drawFastHLine(0, SSD1306_LCDHEIGHT/2, SSD1306_LCDWIDTH, WHITE);
00024     display.display();
00025 }
00026 
00027 /**************************************************
00028  * graphics_speed_demo
00029  **************************************************/
00030 void graphics_speed_demo(SSD1306 display, bool trail) {
00031     display.setRotation(0);
00032     //display.clearDisplay();
00033 
00034     int32_t y = 0;
00035     int32_t delta_y = 1;
00036 
00037     for (int32_t x=0; x<display.width(); x++) {
00038     display.drawFastVLine(x, 0, y, WHITE);
00039     display.display();
00040     if (!trail)
00041         display.drawFastVLine(x, 0, y, BLACK);
00042     y += delta_y;
00043     if ((y<0) || (y >= display.height()))
00044         delta_y = -delta_y;
00045     }
00046 }
00047 /**************************************************
00048  * graphics_line_demo
00049  **************************************************/
00050 void graphics_line_demo(SSD1306 display, int32_t count) {
00051     display.setRotation(0);
00052     display.clearDisplay();
00053 
00054     //int32_t y = 0;
00055     int32_t delta_y = 3;
00056     int32_t delta_x = 5;
00057 
00058     switch (count % 4) {
00059     //////////////////////////////////////////////////////////////////
00060     case 0:
00061         for (int32_t x=0; x<display.width(); x+=delta_x)
00062             display.drawFastVLine(x, 0, display.height(), WHITE);
00063 
00064         for (int32_t y=0; y<display.height(); y+=delta_y)
00065             display.drawFastHLine(0, y, display.width(), WHITE);
00066         break;
00067     //////////////////////////////////////////////////////////////////
00068     case 1:
00069         delta_y = 2;
00070         delta_x = 2;
00071         for (int32_t x=0; x<display.width(); x+=delta_x)
00072             display.drawFastVLine(x, 0, display.height(), WHITE);
00073 
00074         for (int32_t y=0; y<display.height(); y+=delta_y)
00075             display.drawFastHLine(0, y, display.width(), WHITE);
00076         break;
00077     //////////////////////////////////////////////////////////////////
00078     case 2:
00079         {
00080         int32_t w = display.width();
00081         int32_t h = display.height();
00082         delta_y = 4;
00083         delta_x = 4;
00084         for (int32_t x=0; x<w/2; x+=delta_x) {
00085             display.drawLine(w/2, h-x, w-x, h, WHITE);
00086             display.drawLine(w/2, h-x, x, h, WHITE);
00087         }
00088         display.drawLine(w/2, 0, w/2, h, WHITE);
00089         break;
00090         }
00091     //////////////////////////////////////////////////////////////////
00092     case 3:
00093         {
00094         int32_t w = display.width();
00095         int32_t h = display.height();
00096         delta_x = 4;
00097         display.drawLine(0, 0, w, 0, WHITE);
00098         for (int32_t x=0; x<w/2; x+=delta_x) {
00099             display.drawLine(w/2+x, 0, w, 2*(w-2*x)*x/h, WHITE);
00100             display.drawLine(w/2-x, 0, 0, 2*(w-2*x)*x/h, WHITE);
00101         }
00102         break;
00103         }
00104     }
00105     //////////////////////////////////////////////////////////////////
00106 
00107     display.display();
00108 }
00109 
00110 /**************************************************
00111  * graphics_demo
00112  **************************************************/
00113 #define GDLOGO_W 111
00114 #define GDLOGO_H  48
00115 
00116  const uint8_t gdLogo[GDLOGO_H/8 * GDLOGO_W] = { 
00117     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xE0, 0xE0, 0xF0, 0xF0, 0xF8, 0xF8, 0xF8, 0xFC, 0xFC, 0xFC, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0xFC, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0x7C, 0xFC, 0xFC, 0xFC, 0xF8, 0xF8, 0xF8, 0xF0, 0xF0, 0xE0, 0xE0, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  
00118     0x00, 0x00, 0xC0, 0xF0, 0xFC, 0xFE, 0xFF, 0xFF, 0x3F, 0x1F, 0x0F, 0x07, 0x03, 0x03, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x03, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0xFF, 0xFF, 0xFE, 0xF8, 0xF0, 0xC0, 0x00, 0x00,  
00119     0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0x3F, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x3F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8,  
00120     0x1F, 0xFF, 0xFF, 0xFF, 0xFF, 0xF8, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0xF8, 0xFF, 0xFF, 0xFF, 0xFF, 0x1F,  
00121     0x00, 0x01, 0x07, 0x0F, 0x3F, 0x7F, 0xFF, 0xFF, 0xFC, 0xF8, 0xF0, 0xE0, 0xC0, 0xC0, 0x80, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x80, 0xC0, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFF, 0xFF, 0x7F, 0x3F, 0x0F, 0x07, 0x01, 0x00,  
00122     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07, 0x0F, 0x0F, 0x1F, 0x1F, 0x1F, 0x3F, 0x3F, 0x3F, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3F, 0x3F, 0x3F, 0x3F, 0x3F, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3E, 0x3F, 0x3F, 0x3F, 0x1F, 0x1F, 0x1F, 0x0F, 0x0F, 0x07, 0x03, 0x03, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
00123 };
00124 
00125 void drawBitmap(SSD1306 display, int16_t x, int16_t y, const uint8_t *bitmap, int16_t w, int16_t h, uint16_t color) {
00126 
00127   int16_t i, j = (w + 7) / 8;
00128   uint8_t byte;
00129 
00130   for(i=0; i<w; i++ ) {
00131     for(j=0; j<h; j++) {
00132       if(j & 7)
00133         byte >>= 1;
00134       else
00135         byte = bitmap[w*j/8  + i];
00136       if(byte & 1)
00137     display.drawPixel(x+i, y+j, color);
00138     }
00139   }
00140 }
00141 
00142 
00143 void testdrawchar(SSD1306 display) {
00144   display.setTextSize(1);
00145   display.setTextColor(WHITE);
00146   display.setCursor(0,0);
00147 
00148   for (uint8_t i=0; i < 168; i++) {
00149     if (i % 21 == 20)
00150         display.write('\n');
00151     if (i != '\n')
00152         display.write(i);
00153   }
00154   display.display();
00155 }
00156 
00157 void doText(SSD1306 display, bool both=true) {
00158     display.setTextSize(1);
00159     display.setTextColor(WHITE);
00160     display.setCursor(0,both ? 0 : 12);
00161     display.println("Hello, world!");
00162     if (both)
00163     display.setTextColor(BLACK, WHITE); // 'inverted' text
00164     display.println("3.141592");
00165     if (both) {
00166         display.setTextSize(2);
00167         display.setTextColor(WHITE);
00168         display.print("0x"); display.println("DEADBEEF");
00169     }
00170     display.display();
00171 }
00172  
00173  
00174 void graphics_demo(SSD1306 display) {
00175     display.setRotation(0);
00176 
00177     display.clearDisplay();
00178     drawBitmap(display, 8, 16, gdLogo, GDLOGO_W, GDLOGO_H, 2);
00179     //display.startscrolldiagleft(3, 0x0F);
00180     display.display();
00181     wait(8);
00182     display.stopscroll();
00183 
00184     display.setFont(NULL);
00185     display.clearDisplay();
00186     display.fillCircle(display.width()/2, display.height()/2, display.height()/2-1, WHITE);
00187     display.display();
00188     wait(2);
00189 
00190     display.clearDisplay();
00191     display.drawFastVLine(0, 16, 32, WHITE);
00192     display.drawFastHLine(0, 16, 32, WHITE);
00193     display.display();
00194     wait(2);
00195     
00196     display.clearDisplay();
00197     testdrawchar(display);
00198     wait(4);
00199     
00200     display.clearDisplay();
00201     doText(display);
00202     wait(4);
00203     
00204     display.clearDisplay();
00205     display.setFont(&FreeSans9pt7b);
00206     doText(display, false);
00207     wait(2);
00208     
00209     
00210     display.startscrollleft(8, 0x0F);
00211     wait(4);
00212 }
00213 
00214 
00215 
00216 /**************************************************
00217  * main
00218  **************************************************/
00219 int main()
00220 {
00221     int32_t count = 0;
00222     pc.baud(115200);
00223     pc.printf("SSD1306 I2C Demo\r\n");
00224     
00225     SSD1306 display = SSD1306();
00226     display.begin(true);
00227     
00228     while (true) {
00229         gpo = !gpo; // toggle pin
00230         led = !led; // toggle led
00231         wait(1);
00232         graphics_line_demo(display, count);
00233         //graphics_simple_demo(display);
00234         //graphics_speed_demo(display, (count%3)==0);
00235         //graphics_demo(display);
00236 
00237         pc.printf("Count: %d\r\n", count);
00238 
00239         count++;
00240     }
00241 }