digint/tinyfsm

State update loop

joshysav opened this issue · 4 comments

Might be a dumb question. But my understanding is that the react method of each state is used to update the current state.

Is there some way of including an update method that is called on each loop of the main program? Making it possible to perform an operation for the duration of the state?

Might be a dumb question. But my understanding is that the react method of each state is used to update the current state.

The state is updated in transit:
https://github.com/digint/tinyfsm/blob/master/include/tinyfsm.hpp#L137

Is there some way of including an update method that is called on each loop of the main program? Making it possible to perform an operation for the duration of the state?

You can e.g. call such a methon in exit/entry functions, these are always called before / after transit.

Hello @digint could you maybe elaborate a little bit further on this? I'm a novice C programmer so maybe you could give me some hint?

I'd love to implement this state machine on an ESP8266. The ESP is currently connected to a LED matrix and I implemented two featrues: display a clock and display animated Gifs. Now i want to combine both and be able to switch between the two states clock and gif. Both implementations need to have a method loop() called in the EPS loop method to update the time / draw the next frame of the gif.

The state machine itself is working, but I'm not sure how to update the state in the ESP loop method. So the state transition would set up the clock or the gif decoder. But I'm not sure how to implement a method loop that is called on the current active state and will call for example clock->loop() or gifdecoder->loop().

I tried to create a Loop event and continue to call this in the loop method. But I think this is a little bit overkill. A direct call would be easier? After looking into the code and documentation, maybe a event is the way to go?

txf- commented

You can e.g. call such a methon in exit/entry functions, these are always called before / after transit.

These methods don't really call on for the duration of the state. Presumably one could loop inside the entry and exit methods, but ideally those methods would execute once and return.

My use case is that I'd like to poll a device in order to get the conditions to transit to other states. I'm thinking something like
fsmList::Run(), that would could call the run methods of each fsm in the list.

Is this something of interest, or am I thinking of a use case that is ill suited for this library?

edit:
Also another way would be to constantly transit between two states, which would then evaluate the conditions for breaking the loop.

Old topic but I'll add this for anyone passing through:
The simple solution to the original question appears to be to just define an 'Update' event with a corresponding react handler just like any other event.
Dispatch the 'Update' event every iteration of the main loop, and it will call the respective react for 'Update' on the current state if defined. I was able to use this for simple timeout functionality when playing around with the library.