I am looking for some implementation (ideas) of the one-dimensional state table in C. The transition are kind of objects (struct) that contain:
- Pointer to the guard function
- Next state
- List of pointers to action functions.
I am looking at C state-machine design but in my case I have multiple conditions to form an event.
Is there any generic approach or simple FSM sw processor suitable for embedded system?
The usual approach is to store pointer to a conditional function.
You implement the set of conditions as separate functions, and attach a pointer to the right condition in the table. Each function tests for given set of conditions. You iterate through the list till one of pointed functions returns true. Of course “current state” can be used as part of conditions, which removes the need for 2d array.
This may be inefficient if the conditions repeat a lot in a chain like:
(most events tests for
aseparately, if testing for a is computationally expensive it will take much more CPU time than necessary).In this case you’d have to remap the
condition_set -> eventlist into a single decision tree – much harder to maintain but more CPU-efficient:This of course cannot be a simple 1-dimensional list any more, but requires a tree-like structure instead, say, branched linked list:
NULL on any of the pointers means “no such item” (except on “test” which can’t be null). And of course recursion required to traverse the list.