// Project 10: ESP32 OLED Display (SSD1306, 128x64, I2C)
// Shows text on a small OLED over the I2C bus (SDA = GPIO 21, SCL = GPIO 22),
// then scrolls it left and right.
// Requires: Adafruit SSD1306 + Adafruit GFX libraries.

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>

#define SCREEN_WIDTH  128   // OLED width, in pixels
#define SCREEN_HEIGHT 64    // OLED height, in pixels

// I2C OLED, no reset pin (-1). Default I2C address 0x3C.
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

void setup() {
  Serial.begin(115200);
  delay(500);

  Serial.println();
  Serial.println("==============================================");
  Serial.println(" Project 10: ESP32 OLED Display (SSD1306)");
  Serial.println("==============================================");
  Serial.println("I2C: SDA = GPIO 21, SCL = GPIO 22, address 0x3C");

  // Try to start the display at I2C address 0x3C
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("[OLED] ERROR: display not found!");
    Serial.println("       Check SDA->21, SCL->22, VCC->3V3, GND->GND,");
    Serial.println("       and that the I2C address is 0x3C.");
    for(;;);   // stop here - nothing else to do without a display
  }
  Serial.println("[OLED] Display initialized OK.");

  delay(2000);
  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(WHITE);
  display.setCursor(0, 30);

  // Display static text
  display.println("LAFVIN");
  display.display();
  Serial.println("[OLED] Showing text: \"LAFVIN\" (will scroll).");
  delay(100);
}

void loop() {
  // Scroll right, pause, then scroll left, pause - forever.
  display.startscrollright(0x00, 0x0F);
  delay(7000);
  display.stopscroll();
  delay(1000);
  display.startscrollleft(0x00, 0x0F);
  delay(7000);
  display.stopscroll();
  delay(1000);
}
