Arduino code refactoring in a C++ Class

I am trying to refactor this WIFI ESP32 Arduino example into a self-contained C++ class. My implementation contains two files: WifiManager.h and WifiManager.cpp (Please see the attachment). In a nuttshell, the header file defines the API of the library and the cpp file provide its implementation. However, when I try to run it, it does not work at all (I get no output). After testing, I know the problem resides in the even callback void WiFiEvent(WiFiEvent_t event but I do not know how to fix the problem. Any suggestions?

WifiManager.cpp (4.62 KB)

WifiManager.h (347 Bytes)

main.cpp (171 Bytes)

Please post the code here rather than attaching it and use code tags when you post it.

Also posted at:

If you're going to do that then please be considerate enough to add links to the other places you cross posted. This will let us avoid wasting time due to duplicate effort and also help others who have the same questions and find your post to discover all the relevant information. When you post links please always use the chain links icon on the toolbar to make them clickable.

After testing, I know the problem resides in the even callback void WiFiEvent(WiFiEvent_t event but I do not know how to fix the problem.

You don't even (seem to) know WHAT the problem is, so how did you arrive at that conclusion?

If you DO know what the problem is, you forgot to share that.

At a guess,

The WiFi manager instance needs to create a callback object and pass that to the API. The callback object needs to contain some sort of reference to the manager instance that it belongs to.

Now, I don't know how nested classes work in C++. As far as I can see, they don't automatically hold a reference to the enclosing instance, so that means you will need to pass them in in the constructor.

It would be something vaguely approximately something like this, kinda:

// THIS IS THE WIFI LIBRARY STUFF

namespace WiFiAPIExample {
class Event {
  public:
    int code;
} myEvent;

class EventHandler {
  public:
    virtual void handle(Event& e) = 0;
};

EventHandler *handler = NULL;

void setHandler(EventHandler *h) {
  handler = h;
}

void foo(int code) {
  myEvent.code = code;
  if (handler) {
    handler->handle(myEvent);
  }
  else {
    Serial.println("API - no event handler set");
  }
}
}

// this is the gear in my sketch that manages the callbacks

class Manager {
    void handle(WiFiAPIExample::Event& e) {
      Serial.print("GOT AN EVENT ");
      Serial.print(e.code);
      Serial.println();
    }

    class Callback : public WiFiAPIExample::EventHandler {
      public:
        Manager& mgr;
        Callback(Manager& m) : mgr(m) {
        }
        void handle(WiFiAPIExample::Event& e) {
          mgr.handle(e);
        }
    };


    Callback callBack = Callback(*this);

  public:
    Manager() {
    }


    void setup() {
      WiFiAPIExample::setHandler(&callBack);
    }
} manager;

void setup() {
  Serial.begin(9600);
  Serial.println("starting sketch");
  WiFiAPIExample::foo(1);
  manager.setup();
  WiFiAPIExample::foo(2);
}

void loop() {
}