How can we use Particle.function from within a class?

Hi,

I want this:

MyClass::begin(){
      Particle.function("myfunction",doIt);
}
int MyClass::doIt(String str){}

I tried:

1.  Particle.function("myfunction",MyClass::doIt);
2.  Particle.function("myfunction",this->doIt);

Is it is possible at all, and if, how :flushed:

Compared to a regular function pointer (which is a single pointer) a class function is actually 2 pointers - the function itself and the ‘this’ pointer. The simplest way to encapsulate both of these so to use a lambda expression.

class MyClass
{
	void begin()
	{
		Particle.function("myfunc", [this](const String& s)->int{return doIt(s); });
	}
	int doIt(String arg)
	{
		return 0;
	}
};
2 Likes

Perfect, works, tnx