liu baohu / Mbed 2 deprecated OLED_SSD1306

Dependencies:   mbed

Dependents:   053_SPI_OLED

Fork of L152RE_OLED_SSD1306 by Charles Fhda

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  Demonstrate 0.96 in. OLED module (128x64/SPI). Revised for use with ST Nucleo L152RE
00003  
00004  Note:  On 16 pin OLED module from Taobao, MOSI connects to module D1
00005         and CLK (also called SCK) connects to module D0. Module RW and RD lines
00006         are connected to GND.
00007 */
00008 
00009 #include "mbed.h"
00010 
00011 #include "ssd1306.h"
00012 #include "standard_font.h"
00013 #include "bold_font.h"
00014 
00015 DigitalOut myled(LED3);
00016 
00017 
00018 Serial pc(SERIAL_TX, SERIAL_RX);
00019 
00020 Timer timer1;
00021 Ticker tick;
00022 
00023 unsigned char data[256];
00024 unsigned char RX_Cout=0;
00025 
00026 int last_Rec_Time=0,cur_Time=0;
00027 
00028 
00029 void echouart()
00030 {
00031     if(RX_Cout < 255){
00032         data[RX_Cout++] = pc.getc();
00033     }
00034     last_Rec_Time = timer1.read_ms();
00035 }
00036 
00037 //tick中断执行函数:
00038 void tipInt(void)
00039 {
00040     myled =! myled;
00041 }
00042     
00043 /* definitions for ST Nucleo L152RE  */
00044 #define CSEL  PB_6  // CS  D10
00045 #define RST   PC_7  //     D9
00046 #define DCMD  PA_9  // DC  D8
00047 #define CLK   PA_5  // CLK D13
00048 #define DATA  PA_7  // MOSI D11
00049 
00050 // SSD1306 oled(p8 /* cs */, p9 /* reset */, p14 /* dc */, p13 /* clock */, p11 /* data */); // LPC1768
00051 // SSD1306 oled(PTA13 /* cs */, PTD5 /* reset */, PTD0 /* dc */, PTD1 /* clock */, PTD2 /* data */); // KL25Z
00052 // SSD1306 oled(D10 /* cs */, D9 /* reset */, D8 /* dc */, D13 /* clock */, D11 /* data */); // KL05Z or Arduino styles
00053 SSD1306 oled(CSEL, RST, DCMD, CLK, DATA);   // STM32 Nucleo
00054 
00055 #define NUMFLAKES 10
00056 #define XPOS 0
00057 #define YPOS 1
00058 #define DELTAY 2
00059 
00060 #define LOGO16_GLCD_HEIGHT 16 
00061 #define LOGO16_GLCD_WIDTH  16 
00062 
00063 static const unsigned char  logo16_glcd_bmp[] =
00064 { 0x00, 0xc0, // B00000000, B11000000,
00065   0x01, 0xc0, // B00000001, B11000000,
00066   0x01, 0xc0, // B00000001, B11000000,
00067   0x03, 0xe0, // B00000011, B11100000,
00068   0xf3, 0xe0, // B11110011, B11100000,
00069   0xfe, 0xf8, // B11111110, B11111000,
00070   0x7e, 0xff, // B01111110, B11111111,
00071   0x33, 0x9f, // B00110011, B10011111,
00072   0x1f, 0xfc, // B00011111, B11111100,
00073   0x0d, 0x70, // B00001101, B01110000,
00074   0x1b, 0xa0, // B00011011, B10100000,
00075   0x3f, 0xe0, // B00111111, B11100000,
00076   0x3f, 0xf0, // B00111111, B11110000,
00077   0x7c, 0xf0, // B01111100, B11110000,
00078   0x70, 0x70, // B01110000, B01110000,
00079   0x00, 0x30 }; // B00000000, B00110000 };
00080 
00081 void testdrawbitmap(const unsigned char *bitmap, int w, int h) {
00082   uint8_t icons[NUMFLAKES][3];
00083   srand((unsigned int)time(NULL)); // srandom(123);     // whatever seed
00084   int i;
00085  
00086   // initialize
00087   for (uint8_t f=0; f< NUMFLAKES; f++) {
00088     icons[f][XPOS] = rand() % SSD1306_LCDWIDTH; // display.width();
00089     icons[f][YPOS] = 0;
00090     icons[f][DELTAY] = rand() % 5 + 1;
00091 #if 0
00092     Serial.print("x: ");
00093     Serial.print(icons[f][XPOS], DEC);
00094     Serial.print(" y: ");
00095     Serial.print(icons[f][YPOS], DEC);
00096     Serial.print(" dy: ");
00097     Serial.println(icons[f][DELTAY], DEC);
00098 #endif
00099   }
00100 
00101   i = 0;
00102   while (1) {
00103     // draw each icon
00104     for (uint8_t f=0; f< NUMFLAKES; f++) {
00105       oled.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, 1); // WHITE);
00106     }
00107     oled.update();
00108     wait(0.2); // delay(200);
00109     
00110     // then erase it + move it
00111     for (uint8_t f=0; f< NUMFLAKES; f++) {
00112       oled.drawBitmap(icons[f][XPOS], icons[f][YPOS],  logo16_glcd_bmp, w, h, 0); // BLACK);
00113       // move it
00114       icons[f][YPOS] += icons[f][DELTAY];
00115       // if its gone, reinit
00116       if (icons[f][YPOS] > SSD1306_LCDHEIGHT) { // display.height()) {
00117         icons[f][XPOS] = rand() % SSD1306_LCDWIDTH; // display.width();
00118         icons[f][YPOS] = 0;
00119         icons[f][DELTAY] = rand() % 5 + 1;
00120       }
00121     }
00122     if (i++ > 100) break;
00123    }
00124 
00125 }
00126 
00127 void testdrawline() {  
00128   for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) {
00129     oled.line(0, 0, i, SSD1306_LCDHEIGHT-1);
00130     oled.update();
00131   }
00132   for (int16_t i=0; i<SSD1306_LCDHEIGHT; i+=4) {
00133     oled.line(0, 0, SSD1306_LCDWIDTH-1, i);
00134     oled.update();
00135   }
00136   wait(0.25); // delay(250);
00137   
00138   oled.clear();
00139   for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) {
00140     oled.line(0, SSD1306_LCDHEIGHT-1, i, 0);
00141     oled.update();
00142   }
00143   for (int16_t i=SSD1306_LCDHEIGHT-1; i>=0; i-=4) {
00144     oled.line(0, SSD1306_LCDHEIGHT-1, SSD1306_LCDWIDTH-1, i);
00145     oled.update();
00146   }
00147   wait(0.25); // delay(250);
00148   
00149   oled.clear();
00150   for (int16_t i=SSD1306_LCDWIDTH-1; i>=0; i-=4) {
00151     oled.line(SSD1306_LCDWIDTH-1, SSD1306_LCDHEIGHT-1, i, 0);
00152     oled.update();
00153   }
00154   for (int16_t i=SSD1306_LCDHEIGHT-1; i>=0; i-=4) {
00155     oled.line(SSD1306_LCDWIDTH-1, SSD1306_LCDHEIGHT-1, 0, i);
00156     oled.update();
00157   }
00158   wait(0.25); // delay(250);
00159 
00160   oled.clear();
00161   for (int16_t i=0; i<SSD1306_LCDHEIGHT; i+=4) {
00162     oled.line(SSD1306_LCDWIDTH-1, 0, 0, i);
00163     oled.update();
00164   }
00165   for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) {
00166     oled.line(SSD1306_LCDWIDTH-1, 0, i, SSD1306_LCDHEIGHT-1); 
00167     oled.update();
00168   }
00169   wait(0.25);
00170 }
00171 
00172 int main()
00173 {
00174     oled.initialise();
00175     oled.clear();
00176     oled.set_contrast(255); // max contrast
00177     
00178     tick.attach(&tipInt,0.200f);        //0.5秒的定时中断闪灯
00179     timer1.start();
00180 
00181     pc.baud(230400);
00182     pc.attach(&echouart,SerialBase::RxIrq);
00183     pc.printf("\r\nHello World !\n");
00184     
00185     while(1)
00186     {
00187         cur_Time = timer1.read_ms();
00188         if(RX_Cout>0){
00189             if (abs(cur_Time - last_Rec_Time)>=5){
00190                 pc.printf("%s",data);
00191                 RX_Cout=0;
00192             }
00193         }
00194         
00195         oled.drawBitmap(30, 16,  logo16_glcd_bmp, 16, 16);
00196         oled.update();
00197         wait(0.3);
00198         
00199         testdrawline();
00200         
00201         oled.clear();
00202         testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH);
00203     
00204         oled.set_font(bold_font, 8);
00205         oled.printf("Heading\r\n");
00206     
00207         oled.set_font(standard_font, 6);
00208         oled.printf("Hello World!\r\n");
00209         oled.printf("Some more text here...\r\n\r\n\r\n\r\n");
00210         // oled.set_font(bold_font, 8);
00211         oled.line(127, 0, 0, 63);
00212     
00213         oled.update();
00214         wait(0.5);
00215     
00216         int i = 10;
00217         while (i > 0)
00218         {
00219             wait(1);
00220             oled.printf("%d\r\n", i--);
00221             oled.update();
00222             oled.scroll_up();
00223         }
00224         oled.clear();
00225 
00226 //        wait(0.5); // 200 ms
00227         
00228     } // end outside loop for OLED demo
00229 }
00230 
00231 
00232 // EOF