arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

ssd1306_128x32_spi.ino (11929B)


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