I am attempting to write and implement my own waitFor function that is a public member of a class, and waits for another public member of the same class which is of type boolean to change to true. I am using this as an acknowledgment between two functions for one to let the other know it is complete. I will write the code to explain.
Files
MyClass.h
#ifndef MyClass_h
#define MyClass_h
namespace myProject
{
class MyClass
{
public:
bool publicFlagMember;
bool waitForFlag();
void simpleFunction();
}
}
MyClass.cpp
Function that I have previously tried
bool MyProject::MyClass::waitForFlag(uint16_t timeout)
{
uint16_t timer = millis();
while (millis() - timer <= timeout)
{
// call Particle.process() to ensure that when the variable changes we catch it
delay(200);
Particle.process();
if (publicFlagMember)
{
return true;
}
}
return false;
}
void MyProject::MyClass::simpleFunction(){
// do something
publicFlagMember = true;
}
Expectations vs. reality
I expected this function to work similarly to particle.io’s waitFor()
function, and it does, it works exactly as you would expect it to for the first 15-20 iterations, and at a certain point the function no longer returns true even if the member boolean is set to true elsewhere in the program.
I am hoping someone can provide some insight on this topic, thanks in advance.
P.S. If there is a way to use the default particle.io waitfor function from within a class to wait on a class member that would be ideal.