Hey folks,
I’ve got an interesting problem with my architecture in a little electron project. The way I’ve been working on my project so-far is to create a driver layer for all my hardware interfaces (like debounced buttons, decoded analog inputs, SPI outputs to a matrix display, etc…), and construct these driver objects as static global singletons at the top of my .ino sketch. It’s a bit of a hack, but I’ve been having scoping issues because of the whole loop(){…} structure that goes on not giving me anywhere else to put stuff that needs to exist for the entire run-time of the application, particuarly things like DMX control that need a background timer running the whole time.
Tonight I wrote a driver that’s supposed to handle all my particle cloud transactions, including subscribing to a couple of external event feeds. A minimal test case for my CloudDriver looks something like this:
/* this is at the top of main.ino */
SYSTEM_THREAD(ENABLED);
SYSTEM_MODE(SEMI_AUTOMATIC);
MyCloudDriver CloudDrv;
/* In CloudDriver.cpp */
MyCloudDriver::MyCloudDriver(void):
{
Particle.variable("GameStat", StateParams); // Works Fine
Particle.function("SetMode", &MyCloudDriver::SetMode, this); // Works Fine
Particle.subscribe("MyEvent", &MyCloudDriver::EventHandler, this); // Doesn't ever call the event handler
Particle.connect();
}
void MyCloudDriver::EventHandler(const char* name, const char* data)
{
digitalWrite(D7, HIGH); // Should turn on the LED when the event handler gets called
}
I’ve currently got a work-around by adding a RegisterEventHooks function that I call from startup() back in main.ino:
MyCloudDriver::RegisterEventHook(void)
{
Particle.subscribe("MyEvent", &MyCloudDriver::EventHandler, this); // This one actually works!
}
If I do that, everything works as expected. According to the docs I should be subscribing before I call connect to minimise the data use on the Electron that’s running this project. I’ve also done a test case where I try and subscribe immediately after executing connect in the constructor, but again that doesn’t work, so it seems to be specific to putting the subscribe call in the constructor for my driver class.
Am I missing something obvious, or have I stumbled across a known (or unknown) issue?