Close

Life changing events

A project log for Old Roomba, new tricks

Add mapping & MQTT-interfacing with ESP8266 and an IMU

Simon JansenSimon Jansen 08/11/2022 at 20:060 Comments

Important events deserve to be noticed

To give the library some actual reason to be among us, it needs some practical benefits. I think all tinkerers and hackers know the "So what does it actually do?" question... 

At the moment it can only clean on schedule. It would be nice to know when it is done cleaning for example.

For this, I wanted to use some type of event handling. The code in the library should decide when this event comes to pass, but the action should come from the sketch.

This meant using function callbacks in the library so it'll be independent of the sketch. Yaaaay, function pointers!

This is all new to me. It took me quite some googling... so for your benefit

Added in header file:

class Roomba632 {
public:
    void setCallbackDoneCleaning(void (*callbackFuncDoneCleaning)());
private:
    void (*_callbackFuncDoneCleaning)();
    void EventDoneCleaning();
};

Added in source file:

void Roomba632::setCallbackDoneCleaning(void (*callbackFuncDoneCleaning)()){
    _callbackFuncDoneCleaning = callbackFuncDoneCleaning;
}
void Roomba632::EventDoneCleaning(){
    if (_callbackFuncDoneCleaning){
        _callbackFuncDoneCleaning();
    }
}

Whenever the roomba library decides it's done cleaning, it will call EventDoneCleaning() Wich in turn will call the function from the sketch that's been set as a callback.

In my sketch something like this:

void onDoneCleaning(){
  mqttClient.publish(MQTT_PUB_TOPIC, 0, false, "done cleaning");
}
void setup(){
  roomba.setCallbackDoneCleaning(onDoneCleaning);
}

Are we learning yet 

I think this will come in handy later. Now I can set up Home Assistant to be triggered on these events and send me a notification with the results of the cleaning cycle for instance. Also, adding new features postpones the difficult maths :) So next up: Home Assistant MQTT-autoconfig setup.

Discussions