#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//1. Define the size of the screen
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
//2. Reset pin # (or -1 if sharing Arduino reset pin)
#define OLED_RESET -1
//3. Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
//4. Number of snowflakes in the animation example
#define NUMFLAKES 10
/* 5. Define a logo in binary/hex format so it can be displayed. Declare with its dimensions
using the PROGMEM directive */
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] = {
0x01, 0x80, 0x02, 0x40, 0x02, 0x40, 0x04, 0x20, 0x02, 0x40, 0x04, 0x20, 0x08, 0x10, 0x10, 0x08,
0x30, 0x0c, 0x0c, 0x30, 0x18, 0x18, 0x60, 0x06, 0xf0, 0x0f, 0x0f, 0xf0, 0x04, 0x20, 0x03, 0xc0
};
//6. Set up Function
void setup() {
//7. SSD1306_SWITCHCAPVCC = Generate display voltage from 3.3V internally
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
//8. If allocation failed, don't proceed and loop forever
for(;;);
}
//9. Clear the display
display.clearDisplay();
/*10. Show the display buffer on the screen. You MUST call display() after
drawing commands to make them visible on screen!*/
display.display();
//11. Animate the logo, using the testAnimate function
testAnimate(logo_bmp, LOGO_WIDTH, LOGO_HEIGHT);
}
void loop() {
}
#define XPOS 0 //12. Indexes into the 'icons' array in function below
#define YPOS 1
#define DELTAY 2
void testAnimate(const uint8_t *bitmap, uint8_t w, uint8_t h) {
int8_t f, icons[NUMFLAKES][3];
//13. Initialize 'logo' positions into a 2D array of (10 x 3)
for(f=0; f< NUMFLAKES; f++) {
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width()); // Horizontal position from -15 to 127
icons[f][YPOS] = -LOGO_HEIGHT; // Vertical position outside the display margins
icons[f][DELTAY] = random(1, 6); // Assign a random displacement to each logo
}
for(;;) { // 14. Loop forever...
display.clearDisplay(); // Clear the display buffer
// 15. Draw each logo:
for(f=0; f< NUMFLAKES; f++) {
display.drawBitmap(icons[f][XPOS], icons[f][YPOS], bitmap, w, h, SSD1306_WHITE);
}
display.display(); //16. Show the display buffer on the screen
delay(200); //17. Pause for 1/10 second
//18. Then update coordinates of each flake...
for(f=0; f< NUMFLAKES; f++) {
icons[f][YPOS] += icons[f][DELTAY];
//19. If snowflake is off the bottom of the screen...
if (icons[f][YPOS] >= display.height()) {
//20. Reinitialize to a random position, just off the top
icons[f][XPOS] = random(1 - LOGO_WIDTH, display.width());
icons[f][YPOS] = -LOGO_HEIGHT;
icons[f][DELTAY] = random(1, 6);
}
}
}
}