/*
 * Project 10: ESP32 OLED Display (SSD1306)
 * Display text and graphics on an I2C OLED screen.
 *
 * Pin map:
 *   GPIO 21 -> SDA (OLED)
 *   GPIO 22 -> SCL (OLED)
 *   OLED address: 0x3C
 *
 * Libraries: Wire, Adafruit_SSD1306, Adafruit_GFX
 * Serial: 115200 baud
 */

#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);
}
