Simple State Machine in C 

Over the past few years I have been reading about UML Statecharts and state machines in general, but never found a construct that I liked for small embedded systems. Most designs have some events being fired from outside processes, but in most of the stuff I do the event processing belongs inside of the state machine itself!

I created some macros in C that make it very easy to create a state machine that supports entry actions, transaction testing, and exit actions, all living inside the same case statement, making it very simple to understand and maintain.

It will be very clear what is going by viewing this example . However, I should explain the meaning of the "_EXCLUSIVE" macros...

With the entry action, transition test, and exit actions all inside the same case statement, the default is to execute them in order, potentially finishing all three in a single call. This might be good in some cases, but causes problems when expecting predictable timing through the state machine.

The "Exclusive" versions of the macros only allow one of the three parts of a state to be performed each time in. This means it will always take at least three calls to the state machine to pass through a state.

Try it out, and have fun...
[ 208 comments ] ( 3283 views ) ( 3 / 370 )

| 1 | 2 | 3 | 4 |