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

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