#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(128, 64, &Wire, OLED_RESET);
#define NUMFLAKES 10 // Number of snowflakes in the animation example
#define LOGO_HEIGHT 16
#define LOGO_WIDTH 16
static const unsigned char PROGMEM logo_bmp[] = {
0b00000000, 0b11000000, 0b00000001, 0b11000000, 0b00000001, 0b11000000,
0b00000011, 0b11100000, 0b11110011, 0b11100000, 0b11111110, 0b11111000,
0b01111110, 0b11111111, 0b00110011, 0b10011111, 0b00011111, 0b11111100,
0b00001101, 0b01110000, 0b00011011, 0b10100000, 0b00111111, 0b11100000,
0b00111111, 0b11110000, 0b01111100, 0b11110000, 0b01110000, 0b01110000,
0b00000000, 0b00110000
};
void setup() {
Serial.begin(9600);
display.begin(SSD1306_SWITCHCAPVCC, 0X3C);
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.drawPixel(10, 10, WHITE);
display.display();
delay(2000);
testdrawline(); // Draw many lines
testdrawrect(); // Draw rectangles (outlines)
testfillrect(); // Draw rectangles (filled)
testdrawcircle(); // Draw circles (outlines)
testfillcircle(); // Draw circles (filled)
testdrawroundrect(); // Draw rounded rectangles (outlines)
testfillroundrect(); // Draw rounded rectangles (filled)
testdrawtriangle(); // Draw triangles (outlines)
testfilltriangle(); // Draw triangles (filled)
testdrawchar();
testdrawstyles();
testscrolltext();
}
void loop() {}
void testdrawline() {
int16_t i;
display.clearDisplay(); // Clear display buffer
for (i = 0; i < 128; i += 4) {
display.drawLine(0, 0, i, 63, WHITE);
display.display(); // Update screen with each newly-drawn line
delay(1);
}
for (i = 0; i < 64; i += 4) {
display.drawLine(0, 0, 127, i, WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for (i = 0; i < 128; i += 4) {
display.drawLine(0, 63, i, 0, WHITE);
display.display();
delay(1);
}
for (i = 63; i >= 0; i -= 4) {
display.drawLine(0, 63, 127, i,
WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for (i = 127; i >= 0; i -= 4) {
display.drawLine(127, 63, i, 0, WHITE);
display.display();
delay(1);
}
for (i = 63; i >= 0; i -= 4) {
display.drawLine(127, 63, 0, i, WHITE);
display.display();
delay(1);
}
delay(250);
display.clearDisplay();
for (i = 0; i < 63; i += 4) {
display.drawLine(127, 0, 0, i, WHITE);
display.display();
delay(1);
}
for (i = 0; i < 127; i += 4) {
display.drawLine(127, 0, i, 63, WHITE);
display.display();
delay(1);
}
delay(500); // Pause for 2 seconds
}
void testdrawrect() {
display.clearDisplay();
for (int16_t i = 0; i < 64 / 2; i += 2) {
display.drawRect(i, i, 128 - 2 * i, 64 - 2 * i, WHITE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(500);
}
void testfillrect(void) {
display.clearDisplay();
for (int16_t i = 0; i < 64 / 2; i += 3) {
// The INVERSE color is used so rectangles alternate white/black
display.fillRect(i, i, 128 - i * 2, 64 - i * 2,
INVERSE);
display.display(); // Update screen with each newly-drawn rectangle
delay(1);
}
delay(500);
}
void testdrawcircle(void) {
display.clearDisplay();
for (int16_t i = 0; i < max(128, 64) / 2; i += 2) {
display.drawCircle(128 / 2, 64 / 2, i, WHITE);
display.display();
delay(1);
}
delay(500);
}
void testfillcircle(void) {
display.clearDisplay();
for (int16_t i = max(128, 64) / 2; i > 0; i -= 3) {
// The INVERSE color is used so circles alternate white/black
display.fillCircle(128 / 2, 64 / 2, i, INVERSE);
display.display(); // Update screen with each newly-drawn circle
delay(1);
}
delay(500);
}
void testdrawroundrect(void) {
display.clearDisplay();
for (int16_t i = 0; i < 64 / 2 - 2; i += 2) {
display.drawRoundRect(i, i, 128 - 2 * i, 64 - 2 * i, 64 / 4, WHITE);
display.display();
delay(1);
}
delay(500);
}
void testfillroundrect(void) {
display.clearDisplay();
for (int16_t i = 0; i < 64 / 2 - 2; i += 2) {
// The INVERSE color is used so round-rects alternate white/black
display.fillRoundRect(i, i, 128 - 2 * i, 64 - 2 * i, 64 / 4, INVERSE);
display.display();
delay(1);
}
delay(500);
}
void testdrawtriangle(void) {
display.clearDisplay();
for (int16_t i = 0; i < max(128, 64) / 2; i += 5) {
display.drawTriangle(128 / 2, 64 / 2 - i, 128 / 2 - i, 64 / 2 + i, 128 / 2 + i, 64 / 2 + i, WHITE);
display.display();
delay(1);
}
delay(500);
}
void testfilltriangle(void) {
display.clearDisplay();
for (int16_t i = max(128, 64) / 2; i > 0; i -= 5) {
// The INVERSE color is used so triangles alternate white/black
display.fillTriangle(128 / 2, 64 / 2 - i, 128 / 2 - i, 64 / 2 + i, 128 / 2 + i, 64 / 2 + i, INVERSE);
display.display();
delay(1);
}
delay(500);
}
void testdrawchar(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.cp437(true); // Use full 256 char 'Code Page 437' font
for (int16_t i = 0; i < 256; i++) {
if (i == '\n')
display.write(' ');
else
display.write(i);
}
display.display();
delay(2);
}
void testdrawstyles(void) {
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0, 0); // Start at top-left corner
display.println(F("Hello, world!"));
display.setCursor(0, 12);
display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text
display.println(3.141592);
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.print(F("0x"));
display.println(0xDEADBEEF, HEX);
display.display();
delay(500);
}
void testscrolltext(void) {
display.clearDisplay();
display.setTextSize(2); // Draw 2X-scale text
display.setTextColor(SSD1306_WHITE);
display.setCursor(10, 0);
display.println(F("scroll"));
display.display(); // Show initial text
delay(100);
// Scroll in various directions, pausing in-between:
display.startscrollright(0x00, 0x0F);
delay(500);
display.stopscroll();
delay(500);
display.startscrollleft(0x00, 0x0F);
delay(500);
display.stopscroll();
delay(500);
display.startscrolldiagright(0x00, 0x07);
delay(500);
display.startscrolldiagleft(0x00, 0x07);
delay(500);
display.stopscroll();
delay(500);
}