arduino-fsm: Trigger is not reentrant
Hi,
I would like to be able to call trigger from within a “enter” or “transition” function. Do you see a easy modification of your lib to be able to write something like this:( modify your light_switch.ino and change just de bellow functions)
void on_light_on_enter()
{
Serial.println("Entering LIGHT_ON");
delay(2000);
fsm.trigger(FLIP_LIGHT_SWITCH);
}
void on_light_off_enter()
{
Serial.println("Entering LIGHT_OFF");
delay(2000);
fsm.trigger(FLIP_LIGHT_SWITCH);
}
void loop()
{
fsm.trigger(FLIP_LIGHT_SWITCH);
}
My goal is to write some “routing state” that would automatically branch to another state depending on the value of some sensor. Something like:
void on_router_enter()
{
int a = readSensor()
if (a>5) {
fsm.trigger(GO_STATE_HIGH);
} else {
fsm.trigger(GO_STATE_LOW);
}
}
Regards, Redge
About this issue
- Original URL
- State: open
- Created 8 years ago
- Comments: 27 (14 by maintainers)
I’ve been hoping for a light weight state machine like this, and there it is! I’ve used a stateless state machine in c# in some other project, and transitions/triggers are implemented a bit differently (it’s stateless).
I’m stepping in this conversation just to suggest yet another possible approach to the reentry problem: it might be possible to add an asynchronous trigger that decouples setting the new state from the trigger event itself. State transition could then be executed outside these methods in a loop “process” method, etc.
I’m going to give the branch a try… State machines generally make the code much easier to implement and maintain and I really need something like that right now. So thanks a bunch to @jonblack !
Edit: looks like someone implemented something similar in a fork https://github.com/machadolab/arduino-fsm/commit/a4e02930cc278823dd122509ba42aa897e74a50c