Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

State update loop #22

Open
joshysav opened this issue Jan 29, 2020 · 4 comments
Open

State update loop #22

joshysav opened this issue Jan 29, 2020 · 4 comments
Labels

Comments

@joshysav
Copy link

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?

@digint
Copy link
Owner

digint commented Jan 31, 2020

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.

@MoJo2600
Copy link

MoJo2600 commented Feb 5, 2021

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-
Copy link

txf- commented Mar 8, 2021

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.

@outlookhazy
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants