Using a class method for mesh.subscribe callback

I want to register a class method as the callback function for Mesh.subscribe, like you can using Particle.subscribe. I have tried using this syntax:

Mesh.subscribe("some_event", &Subscriber::handler, this);

But it gives an error in the compiler. If I change it to Particle.subscribe it compiles fine.

Why isn’t the class method callback available for Mesh.subscribe? Or more importantly, is there a workaround I could try?

Because it wasn’t implemented yet, I’d say.

You could use a static method.

Can you help with the syntax? I have tried declaring in the header:

 static void handleInput(const char *event, const char *data);

And the method:

void PCTrigger::handleInput(const char *event, const char *data)
    // My method

Calling Mesh.subscribe from the constructor:

Mesh.subscribe("trigger/" + _hexid, &PCTrigger::handleInput, this);

Gives the error:
no matching function for call to 'spark::MeshClass::subscribe(StringSumHelper&, void (*)(const char*, const char*), PCTrigger*)'

Never mind. I realize that I would use the callback without the class reference, like this:

Mesh.subscribe("trigger/" + _hexid, handleInput);

Compiles fine.

1 Like