#include <Wire.h>
#include <Arduino.h>
#include <U8g2lib.h>

#define SDA_2 25
#define SCL_2 26

TwoWire Wire2 = TwoWire(1);

U8G2_SSD1306_64X32_1F_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
U8G2_SSD1306_64X32_1F_F_HW_I2C u8g2_2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, SDA_2, SCL_2);

void setup() {
  delay(5000);  // Delay to allow time for serial monitoring

  Serial.begin(115200);
  Serial.println("\nI2C Scanner");

  Wire.begin();
  Wire2.begin(25, 26);  // Renamed Wire1 to Wire2

  u8g2.begin();
  u8g2.setPowerSave(0);

  u8g2_2.setI2CAddress(0x3C);
  u8g2_2.begin();
  u8g2_2.setPowerSave(0);
}

void scanI2C(TwoWire &wire, const char *busName) {
  byte error, address;
  int nDevices = 0;

  Serial.print("Scanning ");
  Serial.print(busName);
  Serial.println("...");

  for (address = 1; address < 127; address++) {
    wire.beginTransmission(address);
    error = wire.endTransmission();

    if (error == 0) {
      Serial.print("  - Device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.print(address, HEX);
      Serial.println(" !");
      nDevices++;
    } else if (error == 4) {
      Serial.print("  - Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }

  if (nDevices == 0)
    Serial.println("  - No devices found\n");
  else
    Serial.println("  - Done\n");
}

void loop() {
  scanI2C(Wire, "default bus");
  scanI2C(Wire2, "second bus");

  u8g2.clearBuffer();
  u8g2.setFontMode(1);
  u8g2.setFont(u8g2_font_cu12_tr);
  u8g2.setCursor(0, 15);
  u8g2.print(F("DIS"));
  u8g2.sendBuffer();

  u8g2_2.clearBuffer();
  u8g2_2.setFontMode(1);
  u8g2_2.setFont(u8g2_font_cu12_tr);
  u8g2_2.setCursor(0, 15);
  u8g2_2.print(F("DICK"));
  u8g2_2.sendBuffer();

  delay(5000);  // wait 5 seconds for the next I2C scan
}