[Solved] How to register a C++ instance method with Spark.function?

Thanks for your help, I’ll replicate this with the rest of the cloud functions.

Great to hear it works now!

Can you rename your topic “[Solved] How to register a C++ instance method with Spark.function?”

I think it will be helpful for other people searching for this topic.

Good news and bad news, this works for the cloud functions, but not exactly for Spark.subscribe or attachInterrupt(). I’ve pushed another commit.

attachInterrupt expects a function with no arguments and return value so you need to remove the placeholder:

  auto leftInterruptHandler = std::bind(&sparkbot::switchLights, this);
  attachInterrupt(LEFTBUTTON, leftInterruptHandler, RISING);

As far as I can tell, Spark.subscribe only allows C callbacks, not C++ function wrappers at this time. This will need to be changed in the firmware before you can use subscribe from a C++ object.

You can go back to a C wrapper in main.ino until this is fixed.

void syncServosHandler(const char *event, const char *data) {
  sp.syncServosSlave(event, data);
}

void setup() {
  Spark.subscribe("syncServos", &syncServosHandler, MY_DEVICES);
}

Thanks

The missing function object support in Spark.subscribe() should probably be a github issue, right?

Yes, I opened an issue for this.

Yay! My code works compiles now without any warnings or errors, and it works Perfectly on my Photon.

I’m glad to hear that @nrobinson2000! Good luck with the rest of your project!