OLED
Dependents: nucled_L476RG_OLED_Display
Fork of L152RE_OLED_SSD1306 by
main.cpp@1:e03d94d28ce5, 2016-11-17 (annotated)
- Committer:
- garryzheng
- Date:
- Thu Nov 17 19:08:49 2016 +0000
- Revision:
- 1:e03d94d28ce5
- Parent:
- 0:761e771590e5
OLED SSD1306 SPI STM32 L476RG
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
charlesfhda | 0:761e771590e5 | 1 | /* |
charlesfhda | 0:761e771590e5 | 2 | Demonstrate 0.96 in. OLED module (128x64/SPI). Revised for use with ST Nucleo L152RE |
charlesfhda | 0:761e771590e5 | 3 | |
charlesfhda | 0:761e771590e5 | 4 | Note: On 16 pin OLED module from Taobao, MOSI connects to module D1 |
charlesfhda | 0:761e771590e5 | 5 | and CLK (also called SCK) connects to module D0. Module RW and RD lines |
charlesfhda | 0:761e771590e5 | 6 | are connected to GND. |
charlesfhda | 0:761e771590e5 | 7 | */ |
charlesfhda | 0:761e771590e5 | 8 | |
charlesfhda | 0:761e771590e5 | 9 | #include "mbed.h" |
charlesfhda | 0:761e771590e5 | 10 | |
charlesfhda | 0:761e771590e5 | 11 | #include "ssd1306.h" |
charlesfhda | 0:761e771590e5 | 12 | #include "standard_font.h" |
charlesfhda | 0:761e771590e5 | 13 | #include "bold_font.h" |
charlesfhda | 0:761e771590e5 | 14 | |
charlesfhda | 0:761e771590e5 | 15 | /* definitions for ST Nucleo L152RE */ |
charlesfhda | 0:761e771590e5 | 16 | #define CSEL PB_6 // CS D10 |
charlesfhda | 0:761e771590e5 | 17 | #define RST PC_7 // D9 |
charlesfhda | 0:761e771590e5 | 18 | #define DCMD PA_9 // DC D8 |
charlesfhda | 0:761e771590e5 | 19 | #define CLK PA_5 // CLK D13 |
charlesfhda | 0:761e771590e5 | 20 | #define DATA PA_7 // MOSI D11 |
charlesfhda | 0:761e771590e5 | 21 | |
charlesfhda | 0:761e771590e5 | 22 | // SSD1306 oled(p8 /* cs */, p9 /* reset */, p14 /* dc */, p13 /* clock */, p11 /* data */); // LPC1768 |
charlesfhda | 0:761e771590e5 | 23 | // SSD1306 oled(PTA13 /* cs */, PTD5 /* reset */, PTD0 /* dc */, PTD1 /* clock */, PTD2 /* data */); // KL25Z |
charlesfhda | 0:761e771590e5 | 24 | // SSD1306 oled(D10 /* cs */, D9 /* reset */, D8 /* dc */, D13 /* clock */, D11 /* data */); // KL05Z or Arduino styles |
charlesfhda | 0:761e771590e5 | 25 | SSD1306 oled(CSEL, RST, DCMD, CLK, DATA); // STM32 Nucleo |
charlesfhda | 0:761e771590e5 | 26 | |
charlesfhda | 0:761e771590e5 | 27 | #define NUMFLAKES 10 |
charlesfhda | 0:761e771590e5 | 28 | #define XPOS 0 |
charlesfhda | 0:761e771590e5 | 29 | #define YPOS 1 |
charlesfhda | 0:761e771590e5 | 30 | #define DELTAY 2 |
charlesfhda | 0:761e771590e5 | 31 | |
charlesfhda | 0:761e771590e5 | 32 | #define LOGO16_GLCD_HEIGHT 16 |
charlesfhda | 0:761e771590e5 | 33 | #define LOGO16_GLCD_WIDTH 16 |
charlesfhda | 0:761e771590e5 | 34 | |
charlesfhda | 0:761e771590e5 | 35 | static const unsigned char logo16_glcd_bmp[] = |
charlesfhda | 0:761e771590e5 | 36 | { 0x00, 0xc0, // B00000000, B11000000, |
charlesfhda | 0:761e771590e5 | 37 | 0x01, 0xc0, // B00000001, B11000000, |
charlesfhda | 0:761e771590e5 | 38 | 0x01, 0xc0, // B00000001, B11000000, |
charlesfhda | 0:761e771590e5 | 39 | 0x03, 0xe0, // B00000011, B11100000, |
charlesfhda | 0:761e771590e5 | 40 | 0xf3, 0xe0, // B11110011, B11100000, |
charlesfhda | 0:761e771590e5 | 41 | 0xfe, 0xf8, // B11111110, B11111000, |
charlesfhda | 0:761e771590e5 | 42 | 0x7e, 0xff, // B01111110, B11111111, |
charlesfhda | 0:761e771590e5 | 43 | 0x33, 0x9f, // B00110011, B10011111, |
charlesfhda | 0:761e771590e5 | 44 | 0x1f, 0xfc, // B00011111, B11111100, |
charlesfhda | 0:761e771590e5 | 45 | 0x0d, 0x70, // B00001101, B01110000, |
charlesfhda | 0:761e771590e5 | 46 | 0x1b, 0xa0, // B00011011, B10100000, |
charlesfhda | 0:761e771590e5 | 47 | 0x3f, 0xe0, // B00111111, B11100000, |
charlesfhda | 0:761e771590e5 | 48 | 0x3f, 0xf0, // B00111111, B11110000, |
charlesfhda | 0:761e771590e5 | 49 | 0x7c, 0xf0, // B01111100, B11110000, |
charlesfhda | 0:761e771590e5 | 50 | 0x70, 0x70, // B01110000, B01110000, |
charlesfhda | 0:761e771590e5 | 51 | 0x00, 0x30 }; // B00000000, B00110000 }; |
charlesfhda | 0:761e771590e5 | 52 | |
charlesfhda | 0:761e771590e5 | 53 | void testdrawbitmap(const unsigned char *bitmap, int w, int h) { |
charlesfhda | 0:761e771590e5 | 54 | uint8_t icons[NUMFLAKES][3]; |
charlesfhda | 0:761e771590e5 | 55 | srand((unsigned int)time(NULL)); // srandom(123); // whatever seed |
charlesfhda | 0:761e771590e5 | 56 | int i; |
charlesfhda | 0:761e771590e5 | 57 | |
charlesfhda | 0:761e771590e5 | 58 | // initialize |
charlesfhda | 0:761e771590e5 | 59 | for (uint8_t f=0; f< NUMFLAKES; f++) { |
charlesfhda | 0:761e771590e5 | 60 | icons[f][XPOS] = rand() % SSD1306_LCDWIDTH; // display.width(); |
charlesfhda | 0:761e771590e5 | 61 | icons[f][YPOS] = 0; |
charlesfhda | 0:761e771590e5 | 62 | icons[f][DELTAY] = rand() % 5 + 1; |
charlesfhda | 0:761e771590e5 | 63 | #if 0 |
charlesfhda | 0:761e771590e5 | 64 | Serial.print("x: "); |
charlesfhda | 0:761e771590e5 | 65 | Serial.print(icons[f][XPOS], DEC); |
charlesfhda | 0:761e771590e5 | 66 | Serial.print(" y: "); |
charlesfhda | 0:761e771590e5 | 67 | Serial.print(icons[f][YPOS], DEC); |
charlesfhda | 0:761e771590e5 | 68 | Serial.print(" dy: "); |
charlesfhda | 0:761e771590e5 | 69 | Serial.println(icons[f][DELTAY], DEC); |
charlesfhda | 0:761e771590e5 | 70 | #endif |
charlesfhda | 0:761e771590e5 | 71 | } |
charlesfhda | 0:761e771590e5 | 72 | |
charlesfhda | 0:761e771590e5 | 73 | i = 0; |
charlesfhda | 0:761e771590e5 | 74 | while (1) { |
charlesfhda | 0:761e771590e5 | 75 | // draw each icon |
charlesfhda | 0:761e771590e5 | 76 | for (uint8_t f=0; f< NUMFLAKES; f++) { |
charlesfhda | 0:761e771590e5 | 77 | oled.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, 1); // WHITE); |
charlesfhda | 0:761e771590e5 | 78 | } |
charlesfhda | 0:761e771590e5 | 79 | oled.update(); |
charlesfhda | 0:761e771590e5 | 80 | wait(0.2); // delay(200); |
charlesfhda | 0:761e771590e5 | 81 | |
charlesfhda | 0:761e771590e5 | 82 | // then erase it + move it |
charlesfhda | 0:761e771590e5 | 83 | for (uint8_t f=0; f< NUMFLAKES; f++) { |
charlesfhda | 0:761e771590e5 | 84 | oled.drawBitmap(icons[f][XPOS], icons[f][YPOS], logo16_glcd_bmp, w, h, 0); // BLACK); |
charlesfhda | 0:761e771590e5 | 85 | // move it |
charlesfhda | 0:761e771590e5 | 86 | icons[f][YPOS] += icons[f][DELTAY]; |
charlesfhda | 0:761e771590e5 | 87 | // if its gone, reinit |
charlesfhda | 0:761e771590e5 | 88 | if (icons[f][YPOS] > SSD1306_LCDHEIGHT) { // display.height()) { |
charlesfhda | 0:761e771590e5 | 89 | icons[f][XPOS] = rand() % SSD1306_LCDWIDTH; // display.width(); |
charlesfhda | 0:761e771590e5 | 90 | icons[f][YPOS] = 0; |
charlesfhda | 0:761e771590e5 | 91 | icons[f][DELTAY] = rand() % 5 + 1; |
charlesfhda | 0:761e771590e5 | 92 | } |
charlesfhda | 0:761e771590e5 | 93 | } |
charlesfhda | 0:761e771590e5 | 94 | if (i++ > 100) break; |
charlesfhda | 0:761e771590e5 | 95 | } |
charlesfhda | 0:761e771590e5 | 96 | |
charlesfhda | 0:761e771590e5 | 97 | } |
charlesfhda | 0:761e771590e5 | 98 | |
charlesfhda | 0:761e771590e5 | 99 | void testdrawline() { |
charlesfhda | 0:761e771590e5 | 100 | for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) { |
charlesfhda | 0:761e771590e5 | 101 | oled.line(0, 0, i, SSD1306_LCDHEIGHT-1); |
charlesfhda | 0:761e771590e5 | 102 | oled.update(); |
charlesfhda | 0:761e771590e5 | 103 | } |
charlesfhda | 0:761e771590e5 | 104 | for (int16_t i=0; i<SSD1306_LCDHEIGHT; i+=4) { |
charlesfhda | 0:761e771590e5 | 105 | oled.line(0, 0, SSD1306_LCDWIDTH-1, i); |
charlesfhda | 0:761e771590e5 | 106 | oled.update(); |
charlesfhda | 0:761e771590e5 | 107 | } |
charlesfhda | 0:761e771590e5 | 108 | wait(0.25); // delay(250); |
charlesfhda | 0:761e771590e5 | 109 | |
charlesfhda | 0:761e771590e5 | 110 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 111 | for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) { |
charlesfhda | 0:761e771590e5 | 112 | oled.line(0, SSD1306_LCDHEIGHT-1, i, 0); |
charlesfhda | 0:761e771590e5 | 113 | oled.update(); |
charlesfhda | 0:761e771590e5 | 114 | } |
charlesfhda | 0:761e771590e5 | 115 | for (int16_t i=SSD1306_LCDHEIGHT-1; i>=0; i-=4) { |
charlesfhda | 0:761e771590e5 | 116 | oled.line(0, SSD1306_LCDHEIGHT-1, SSD1306_LCDWIDTH-1, i); |
charlesfhda | 0:761e771590e5 | 117 | oled.update(); |
charlesfhda | 0:761e771590e5 | 118 | } |
charlesfhda | 0:761e771590e5 | 119 | wait(0.25); // delay(250); |
charlesfhda | 0:761e771590e5 | 120 | |
charlesfhda | 0:761e771590e5 | 121 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 122 | for (int16_t i=SSD1306_LCDWIDTH-1; i>=0; i-=4) { |
charlesfhda | 0:761e771590e5 | 123 | oled.line(SSD1306_LCDWIDTH-1, SSD1306_LCDHEIGHT-1, i, 0); |
charlesfhda | 0:761e771590e5 | 124 | oled.update(); |
charlesfhda | 0:761e771590e5 | 125 | } |
charlesfhda | 0:761e771590e5 | 126 | for (int16_t i=SSD1306_LCDHEIGHT-1; i>=0; i-=4) { |
charlesfhda | 0:761e771590e5 | 127 | oled.line(SSD1306_LCDWIDTH-1, SSD1306_LCDHEIGHT-1, 0, i); |
charlesfhda | 0:761e771590e5 | 128 | oled.update(); |
charlesfhda | 0:761e771590e5 | 129 | } |
charlesfhda | 0:761e771590e5 | 130 | wait(0.25); // delay(250); |
charlesfhda | 0:761e771590e5 | 131 | |
charlesfhda | 0:761e771590e5 | 132 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 133 | for (int16_t i=0; i<SSD1306_LCDHEIGHT; i+=4) { |
charlesfhda | 0:761e771590e5 | 134 | oled.line(SSD1306_LCDWIDTH-1, 0, 0, i); |
charlesfhda | 0:761e771590e5 | 135 | oled.update(); |
charlesfhda | 0:761e771590e5 | 136 | } |
charlesfhda | 0:761e771590e5 | 137 | for (int16_t i=0; i<SSD1306_LCDWIDTH; i+=4) { |
charlesfhda | 0:761e771590e5 | 138 | oled.line(SSD1306_LCDWIDTH-1, 0, i, SSD1306_LCDHEIGHT-1); |
charlesfhda | 0:761e771590e5 | 139 | oled.update(); |
charlesfhda | 0:761e771590e5 | 140 | } |
charlesfhda | 0:761e771590e5 | 141 | wait(0.25); |
charlesfhda | 0:761e771590e5 | 142 | } |
charlesfhda | 0:761e771590e5 | 143 | |
charlesfhda | 0:761e771590e5 | 144 | int main() |
charlesfhda | 0:761e771590e5 | 145 | { |
charlesfhda | 0:761e771590e5 | 146 | oled.initialise(); |
charlesfhda | 0:761e771590e5 | 147 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 148 | oled.set_contrast(255); // max contrast |
charlesfhda | 0:761e771590e5 | 149 | |
charlesfhda | 0:761e771590e5 | 150 | while(1) |
charlesfhda | 0:761e771590e5 | 151 | { |
charlesfhda | 0:761e771590e5 | 152 | |
charlesfhda | 0:761e771590e5 | 153 | oled.drawBitmap(30, 16, logo16_glcd_bmp, 16, 16); |
charlesfhda | 0:761e771590e5 | 154 | oled.update(); |
charlesfhda | 0:761e771590e5 | 155 | wait(3); |
charlesfhda | 0:761e771590e5 | 156 | |
charlesfhda | 0:761e771590e5 | 157 | testdrawline(); |
charlesfhda | 0:761e771590e5 | 158 | |
charlesfhda | 0:761e771590e5 | 159 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 160 | testdrawbitmap(logo16_glcd_bmp, LOGO16_GLCD_HEIGHT, LOGO16_GLCD_WIDTH); |
charlesfhda | 0:761e771590e5 | 161 | |
charlesfhda | 0:761e771590e5 | 162 | oled.set_font(bold_font, 8); |
charlesfhda | 0:761e771590e5 | 163 | oled.printf("Heading\r\n"); |
charlesfhda | 0:761e771590e5 | 164 | |
charlesfhda | 0:761e771590e5 | 165 | oled.set_font(standard_font, 6); |
charlesfhda | 0:761e771590e5 | 166 | oled.printf("Hello World!\r\n"); |
charlesfhda | 0:761e771590e5 | 167 | oled.printf("Some more text here...\r\n\r\n\r\n\r\n"); |
charlesfhda | 0:761e771590e5 | 168 | // oled.set_font(bold_font, 8); |
charlesfhda | 0:761e771590e5 | 169 | oled.line(127, 0, 0, 63); |
charlesfhda | 0:761e771590e5 | 170 | |
charlesfhda | 0:761e771590e5 | 171 | oled.update(); |
charlesfhda | 0:761e771590e5 | 172 | wait(1); |
charlesfhda | 0:761e771590e5 | 173 | |
charlesfhda | 0:761e771590e5 | 174 | int i = 10; |
charlesfhda | 0:761e771590e5 | 175 | while (i > 0) |
charlesfhda | 0:761e771590e5 | 176 | { |
charlesfhda | 0:761e771590e5 | 177 | wait(1); |
charlesfhda | 0:761e771590e5 | 178 | oled.printf("%d\r\n", i--); |
charlesfhda | 0:761e771590e5 | 179 | oled.update(); |
charlesfhda | 0:761e771590e5 | 180 | oled.scroll_up(); |
charlesfhda | 0:761e771590e5 | 181 | } |
charlesfhda | 0:761e771590e5 | 182 | |
charlesfhda | 0:761e771590e5 | 183 | oled.clear(); |
charlesfhda | 0:761e771590e5 | 184 | |
charlesfhda | 0:761e771590e5 | 185 | } // end outside loop for OLED demo |
charlesfhda | 0:761e771590e5 | 186 | } |
charlesfhda | 0:761e771590e5 | 187 | |
charlesfhda | 0:761e771590e5 | 188 | // EOF |