This library provides a way to create a simple state machine
auto firstState = new State;
auto secondState = new StateTimed{10};
auto thirdState = new State;
auto firstStateId = firstState->id();
auto secondStateId = secondState->id();
auto thirdStateId = thirdState->id();
auto run1 = [secondStateId]() {
std::cerr << "firstState\n";
return secondStateId;
};
auto run2 = [thirdStateId]() {
std::cerr << "secondState\n";
return thirdStateId;
};
auto run3 = [thirdStateId]() {
std::cerr << "thirdState\n";
return thirdStateId;
};
firstState->setRunnable(run1);
secondState->setRunnable(run2);
thirdState->setRunnable(run3);
StateMachine<3> machine {{firstState, secondState, thirdState} };
machine.start();
// Should initially be at the first state
REQUIRE(machine.current(firstState->id()) == true);
// then second state
machine.handle();
REQUIRE(machine.current(secondState->id()) == true);
// Still at second state
machine.handle();
REQUIRE(machine.current(secondState->id()) == true);
// should have been advanced to third state
millisStubbed = 11;
machine.handle();
REQUIRE(machine.current(thirdState->id()) == true);
An example can be find here bbq-controller/src/main.cpp where it´s used to connect to wifi and Mosquitto. It will detected if the connection drops and re-connects
This code is released under the MIT License.
- Each state should return a state number instead of this
- Allow to reference state numbers instead of order added
- Allow to create the state prior to the runnable code