My lib class is recognised but not working

I am working on an ESP32-WROOM-32 build as a lolin32.

I am refactoring an application that was previously built entirely in the main.cpp. Now, I am creating a class to manage the OLED display, but it does not work (it is not a hardware issue since developing everything in the main.cpp, it works).

There is no error. It is just that the oled display is not working with this new implementation

The current structure is:
| - lib
| | - DisplayOled
| | | - DisplayOled.h
| | | - DisplayOled.cpp
| - src
| | - main.cpp

Implementations in each file:
DisplayOled.h

#ifndef __DISPLAYOLED_H__
#define __DISPLAYOLED_H__


class DisplayOled 
{
    public:
        DisplayOled(); // constructor

        void setup();
        void configureDisplayToWrite();
        void clearDisplay();
        void displayWifiIcon();
        void displayIpAddress(char ipAddress[]);
        void displayDateAndTime(char dateStamp[], char timeStamp[]);
        void displayTest();

    private:
    protected:
};

#endif // __DISPLAYOLED_H__

DisplayOled.cpp

#include <DisplayOled.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <WiFi.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)


Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

// WiFi icon graphics for animation (sm, md, lg)
    unsigned int wifi_icon_wh = 16;

    static const unsigned char PROGMEM wifi_icon_lg[] = {
        0x1f, 0xe0, 0xe0, 0x1c, 0x0, 0x0, 0x7, 0x80, 0x18, 0x60, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
        };

    static const unsigned char PROGMEM wifi_icon_md[] = {
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x7, 0x80, 0x18, 0x60, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
        };
    static const unsigned char PROGMEM wifi_icon_sm[] = {
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x3, 0x0, 0x0, 0x0,
        0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
        };


DisplayOled::DisplayOled(){
    DisplayOled::setup();
    DisplayOled::configureDisplayToWrite();
}


void DisplayOled::setup()
{
    // Start I2C Communication SDA = 5 and SCL = 4 on Wemos Lolin32 ESP32 with built-in SSD1306 OLED
    Wire.begin(5, 4);
    

    // Initialize the OLED display
    display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Address 0x3D for 128x64
}

void DisplayOled::configureDisplayToWrite()
{
    display.clearDisplay();
    display.setTextColor(WHITE);
}

void DisplayOled::clearDisplay()
{
    display.clearDisplay();
}

void DisplayOled::displayWifiIcon()
{
    display.drawBitmap(0, 0,  wifi_icon_sm, wifi_icon_wh, wifi_icon_wh, 1);
    display.drawBitmap(0, 0,  wifi_icon_md, wifi_icon_wh, wifi_icon_wh, 1);
    display.drawBitmap(0, 0,  wifi_icon_lg, wifi_icon_wh, wifi_icon_wh, 1);
    display.display();
}

void DisplayOled::displayIpAddress(char ipAddress[])
{
    display.setCursor(20,0);
    display.println(ipAddress);
    display.display();
}

void DisplayOled::displayDateAndTime(char dateStamp[], char timeStamp[])
{
    display.setTextSize(2);
    display.setCursor(0,20);
    display.println(dateStamp);
    display.display();

    display.setTextSize(2);
    display.println(String(timeStamp));
    display.display();
    display.setTextSize(1);
}

void DisplayOled::displayTest()
{
    display.clearDisplay();
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println("Hello, world!");
    display.display();
}

main.cpp

#include <Arduino.h>
#include <DisplayOled.h>
#include <WiFi.h>
#include <WiFiManager.h>

DisplayOled displayOled;



void setup() {
    displayOled.setup();
    displayOled.configureDisplayToWrite();
}

void loop() {
    displayOled.displayWifiIcon();
    delay(1000);
}

Same as for Lis3dh library not working - #2 by maxgerhardt.

Do not call external libraries / object methods in your constructor, you don’t know whether they’ve been initialized before your constructor was called.

Have an empty constructor and the original code from the constructor in a DisplayOled::begin() method. Call this method in your setup() function.

1 Like

It’s working now.

Thank you so much.