greiman / SSD1306Ascii

Text only Arduino Library for SSD1306 OLED displays
MIT License
490 stars 120 forks source link

Begin() return error if OLED not connected/found. #95

Closed QuidneIT closed 1 year ago

QuidneIT commented 1 year ago

The Adafruit OLED library returns an error if the OLED is not present. e.g. if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))

Is there a way this can be done with this library?

greiman commented 1 year ago

begin() is void so no error is returned.

You could simply check to see if a device responds to the I2C address after the Wire.begin() call.

This how a typical I2C scanned checks for devices.

    Wire.beginTransmission(address);
    byte error = Wire.endTransmission();

    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.print(address, HEX);
      Serial.println("  !");

Several other users do this when a display may not be present.

QuidneIT commented 1 year ago

Thank you. Was thinking of this solution as well if the library did not have the build in functionality.