ssd1306_128x32_i2c.ino (11873B)
1 /************************************************************************** 2 This is an example for our Monochrome OLEDs based on SSD1306 drivers 3 4 Pick one up today in the adafruit shop! 5 ------> http://www.adafruit.com/category/63_98 6 7 This example is for a 128x32 pixel display using I2C to communicate 8 3 pins are required to interface (two I2C and one reset). 9 10 Adafruit invests time and resources providing this open 11 source code, please support Adafruit and open-source 12 hardware by purchasing products from Adafruit! 13 14 Written by Limor Fried/Ladyada for Adafruit Industries, 15 with contributions from the open source community. 16 BSD license, check license.txt for more information 17 All text above, and the splash screen below must be 18 included in any redistribution. 19 **************************************************************************/ 20 21 #include <SPI.h> 22 #include <Wire.h> 23 #include <Adafruit_GFX.h> 24 #include <Adafruit_SSD1306.h> 25 26 #define SCREEN_WIDTH 128 // OLED display width, in pixels 27 #define SCREEN_HEIGHT 32 // OLED display height, in pixels 28 29 // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) 30 // The pins for I2C are defined by the Wire-library. 31 // On an arduino UNO: A4(SDA), A5(SCL) 32 // On an arduino MEGA 2560: 20(SDA), 21(SCL) 33 // On an arduino LEONARDO: 2(SDA), 3(SCL), ... 34 #define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) 35 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); 36 37 #define NUMFLAKES 10 // Number of snowflakes in the animation example 38 39 #define LOGO_HEIGHT 16 40 #define LOGO_WIDTH 16 41 static const unsigned char PROGMEM logo_bmp[] = 42 { B00000000, B11000000, 43 B00000001, B11000000, 44 B00000001, B11000000, 45 B00000011, B11100000, 46 B11110011, B11100000, 47 B11111110, B11111000, 48 B01111110, B11111111, 49 B00110011, B10011111, 50 B00011111, B11111100, 51 B00001101, B01110000, 52 B00011011, B10100000, 53 B00111111, B11100000, 54 B00111111, B11110000, 55 B01111100, B11110000, 56 B01110000, B01110000, 57 B00000000, B00110000 }; 58 59 void setup() { 60 Serial.begin(9600); 61 62 // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally 63 if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 64 Serial.println(F("SSD1306 allocation failed")); 65 for(;;); // Don't proceed, loop forever 66 } 67 68 // Show initial display buffer contents on the screen -- 69 // the library initializes this with an Adafruit splash screen. 70 display.display(); 71 delay(2000); // Pause for 2 seconds 72 73 // Clear the buffer 74 display.clearDisplay(); 75 76 // Draw a single pixel in white 77 display.drawPixel(10, 10, SSD1306_WHITE); 78 79 // Show the display buffer on the screen. You MUST call display() after 80 // drawing commands to make them visible on screen! 81 display.display(); 82 delay(2000); 83 // display.display() is NOT necessary after every single drawing command, 84 // unless that's what you want...rather, you can batch up a bunch of 85 // drawing operations and then update the screen all at once by calling 86 // display.display(). These examples demonstrate both approaches... 87 88 testdrawline(); // Draw many lines 89 90 testdrawrect(); // Draw rectangles (outlines) 91 92 testfillrect(); // Draw rectangles (filled) 93 94 testdrawcircle(); // Draw circles (outlines) 95 96 testfillcircle(); // Draw circles (filled) 97 98 testdrawroundrect(); // Draw rounded rectangles (outlines) 99 100 testfillroundrect(); // Draw rounded rectangles (filled) 101 102 testdrawtriangle(); // Draw triangles (outlines) 103 104 testfilltriangle(); // Draw triangles (filled) 105 106 testdrawchar(); // Draw characters of the default font 107 108 testdrawstyles(); // Draw 'stylized' characters 109 110 testscrolltext(); // Draw scrolling text 111 112 testdrawbitmap(); // Draw a small bitmap image 113 114 // Invert and restore display, pausing in-between 115 display.invertDisplay(true); 116 delay(1000); 117 display.invertDisplay(false); 118 delay(1000); 119 120 testanimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT); // Animate bitmaps 121 } 122 123 void loop() { 124 } 125 126 void testdrawline() { 127 int16_t i; 128 129 display.clearDisplay(); // Clear display buffer 130 131 for(i=0; i<display.width(); i+=4) { 132 display.drawLine(0, 0, i, display.height()-1, SSD1306_WHITE); 133 display.display(); // Update screen with each newly-drawn line 134 delay(1); 135 } 136 for(i=0; i<display.height(); i+=4) { 137 display.drawLine(0, 0, display.width()-1, i, SSD1306_WHITE); 138 display.display(); 139 delay(1); 140 } 141 delay(250); 142 143 display.clearDisplay(); 144 145 for(i=0; i<display.width(); i+=4) { 146 display.drawLine(0, display.height()-1, i, 0, SSD1306_WHITE); 147 display.display(); 148 delay(1); 149 } 150 for(i=display.height()-1; i>=0; i-=4) { 151 display.drawLine(0, display.height()-1, display.width()-1, i, SSD1306_WHITE); 152 display.display(); 153 delay(1); 154 } 155 delay(250); 156 157 display.clearDisplay(); 158 159 for(i=display.width()-1; i>=0; i-=4) { 160 display.drawLine(display.width()-1, display.height()-1, i, 0, SSD1306_WHITE); 161 display.display(); 162 delay(1); 163 } 164 for(i=display.height()-1; i>=0; i-=4) { 165 display.drawLine(display.width()-1, display.height()-1, 0, i, SSD1306_WHITE); 166 display.display(); 167 delay(1); 168 } 169 delay(250); 170 171 display.clearDisplay(); 172 173 for(i=0; i<display.height(); i+=4) { 174 display.drawLine(display.width()-1, 0, 0, i, SSD1306_WHITE); 175 display.display(); 176 delay(1); 177 } 178 for(i=0; i<display.width(); i+=4) { 179 display.drawLine(display.width()-1, 0, i, display.height()-1, SSD1306_WHITE); 180 display.display(); 181 delay(1); 182 } 183 184 delay(2000); // Pause for 2 seconds 185 } 186 187 void testdrawrect(void) { 188 display.clearDisplay(); 189 190 for(int16_t i=0; i<display.height()/2; i+=2) { 191 display.drawRect(i, i, display.width()-2*i, display.height()-2*i, SSD1306_WHITE); 192 display.display(); // Update screen with each newly-drawn rectangle 193 delay(1); 194 } 195 196 delay(2000); 197 } 198 199 void testfillrect(void) { 200 display.clearDisplay(); 201 202 for(int16_t i=0; i<display.height()/2; i+=3) { 203 // The INVERSE color is used so rectangles alternate white/black 204 display.fillRect(i, i, display.width()-i*2, display.height()-i*2, SSD1306_INVERSE); 205 display.display(); // Update screen with each newly-drawn rectangle 206 delay(1); 207 } 208 209 delay(2000); 210 } 211 212 void testdrawcircle(void) { 213 display.clearDisplay(); 214 215 for(int16_t i=0; i<max(display.width(),display.height())/2; i+=2) { 216 display.drawCircle(display.width()/2, display.height()/2, i, SSD1306_WHITE); 217 display.display(); 218 delay(1); 219 } 220 221 delay(2000); 222 } 223 224 void testfillcircle(void) { 225 display.clearDisplay(); 226 227 for(int16_t i=max(display.width(),display.height())/2; i>0; i-=3) { 228 // The INVERSE color is used so circles alternate white/black 229 display.fillCircle(display.width() / 2, display.height() / 2, i, SSD1306_INVERSE); 230 display.display(); // Update screen with each newly-drawn circle 231 delay(1); 232 } 233 234 delay(2000); 235 } 236 237 void testdrawroundrect(void) { 238 display.clearDisplay(); 239 240 for(int16_t i=0; i<display.height()/2-2; i+=2) { 241 display.drawRoundRect(i, i, display.width()-2*i, display.height()-2*i, 242 display.height()/4, SSD1306_WHITE); 243 display.display(); 244 delay(1); 245 } 246 247 delay(2000); 248 } 249 250 void testfillroundrect(void) { 251 display.clearDisplay(); 252 253 for(int16_t i=0; i<display.height()/2-2; i+=2) { 254 // The INVERSE color is used so round-rects alternate white/black 255 display.fillRoundRect(i, i, display.width()-2*i, display.height()-2*i, 256 display.height()/4, SSD1306_INVERSE); 257 display.display(); 258 delay(1); 259 } 260 261 delay(2000); 262 } 263 264 void testdrawtriangle(void) { 265 display.clearDisplay(); 266 267 for(int16_t i=0; i<max(display.width(),display.height())/2; i+=5) { 268 display.drawTriangle( 269 display.width()/2 , display.height()/2-i, 270 display.width()/2-i, display.height()/2+i, 271 display.width()/2+i, display.height()/2+i, SSD1306_WHITE); 272 display.display(); 273 delay(1); 274 } 275 276 delay(2000); 277 } 278 279 void testfilltriangle(void) { 280 display.clearDisplay(); 281 282 for(int16_t i=max(display.width(),display.height())/2; i>0; i-=5) { 283 // The INVERSE color is used so triangles alternate white/black 284 display.fillTriangle( 285 display.width()/2 , display.height()/2-i, 286 display.width()/2-i, display.height()/2+i, 287 display.width()/2+i, display.height()/2+i, SSD1306_INVERSE); 288 display.display(); 289 delay(1); 290 } 291 292 delay(2000); 293 } 294 295 void testdrawchar(void) { 296 display.clearDisplay(); 297 298 display.setTextSize(1); // Normal 1:1 pixel scale 299 display.setTextColor(SSD1306_WHITE); // Draw white text 300 display.setCursor(0, 0); // Start at top-left corner 301 display.cp437(true); // Use full 256 char 'Code Page 437' font 302 303 // Not all the characters will fit on the display. This is normal. 304 // Library will draw what it can and the rest will be clipped. 305 for(int16_t i=0; i<256; i++) { 306 if(i == '\n') display.write(' '); 307 else display.write(i); 308 } 309 310 display.display(); 311 delay(2000); 312 } 313 314 void testdrawstyles(void) { 315 display.clearDisplay(); 316 317 display.setTextSize(1); // Normal 1:1 pixel scale 318 display.setTextColor(SSD1306_WHITE); // Draw white text 319 display.setCursor(0,0); // Start at top-left corner 320 display.println(F("Hello, world!")); 321 322 display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text 323 display.println(3.141592); 324 325 display.setTextSize(2); // Draw 2X-scale text 326 display.setTextColor(SSD1306_WHITE); 327 display.print(F("0x")); display.println(0xDEADBEEF, HEX); 328 329 display.display(); 330 delay(2000); 331 } 332 333 void testscrolltext(void) { 334 display.clearDisplay(); 335 336 display.setTextSize(2); // Draw 2X-scale text 337 display.setTextColor(SSD1306_WHITE); 338 display.setCursor(10, 0); 339 display.println(F("scroll")); 340 display.display(); // Show initial text 341 delay(100); 342 343 // Scroll in various directions, pausing in-between: 344 display.startscrollright(0x00, 0x0F); 345 delay(2000); 346 display.stopscroll(); 347 delay(1000); 348 display.startscrollleft(0x00, 0x0F); 349 delay(2000); 350 display.stopscroll(); 351 delay(1000); 352 display.startscrolldiagright(0x00, 0x07); 353 delay(2000); 354 display.startscrolldiagleft(0x00, 0x07); 355 delay(2000); 356 display.stopscroll(); 357 delay(1000); 358 } 359 360 void testdrawbitmap(void) { 361 display.clearDisplay(); 362 363 display.drawBitmap( 364 (display.width() - LOGO_WIDTH ) / 2, 365 (display.height() - LOGO_HEIGHT) / 2, 366 logo_bmp, LOGO_WIDTH, LOGO_HEIGHT, 1); 367 display.display(); 368 delay(1000); 369 } 370 371 #define XPOS 0 // Indexes into the 'icons' array in function below 372 #define YPOS 1 373 #define DELTAY 2 374 375 void testanimate(const uint8_t *bitmap, uint8_t w, uint8_t h) { 376 int8_t f, icons[NUMFLAKES][3]; 377 378 // Initialize 'snowflake' positions 379 for(f=0; f< NUMFLAKES; f++) { 380 icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); 381 icons[f][YPOS] = -LOGO_HEIGHT; 382 icons[f][DELTAY] = random(1, 6); 383 Serial.print(F("x: ")); 384 Serial.print(icons[f][XPOS], DEC); 385 Serial.print(F(" y: ")); 386 Serial.print(icons[f][YPOS], DEC); 387 Serial.print(F(" dy: ")); 388 Serial.println(icons[f][DELTAY], DEC); 389 } 390 391 for(;;) { // Loop forever... 392 display.clearDisplay(); // Clear the display buffer 393 394 // Draw each snowflake: 395 for(f=0; f< NUMFLAKES; f++) { 396 display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE); 397 } 398 399 display.display(); // Show the display buffer on the screen 400 delay(200); // Pause for 1/10 second 401 402 // Then update coordinates of each flake... 403 for(f=0; f< NUMFLAKES; f++) { 404 icons[f][YPOS] += icons[f][DELTAY]; 405 // If snowflake is off the bottom of the screen... 406 if (icons[f][YPOS] >= display.height()) { 407 // Reinitialize to a random position, just off the top 408 icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); 409 icons[f][YPOS] = -LOGO_HEIGHT; 410 icons[f][DELTAY] = random(1, 6); 411 } 412 } 413 } 414 }