If you create a lot of state machines, and especially if they are at all complex then one way to visualize the functionality is to create a SDL diagram out of it.
I recently made a simple gadget which needed some adjustable configuration parameters. These were edited using a few simple buttons. The state machine to handle the editing looks in SDL like the attached picture (in SDL behavioral presentation).
At first sight it looks just like a normal flowchart, and it almost is one. There are a couple of differences in the emphasis that make an SDL diagram very useful presentation for state madhines:
- The rounded rectangles are _states_ where the machine sits, until triggered by one of the _signals_ relevant to that particular state. The stipulation is that all other signals are ignored.
- All triggers initiate a sequence of actions that terminate in a state - either the same one or some other state. The machine stays in this new state until triggered by an event.
- SDL has specific notation for a number of things, such as external and internal events, and one especially useful one, the concept of "any state" or *. You can see it in the attached diagram, in this case handling the input timeout. The meaning of the presentation is that in any state the internal timeout signal triggers a transition to the idle state. How the timeout is implemented in the code is not the concern of this presentation. In an embedded system a simple counter attached to a hardware timer interrupt is the normal way to do these.
So you can follow what is happening, the state machine in this case works like this:
- Program start clears the state machine variables and puts the machine in idle state
- Menu key advances the system to item selection state. Any other key just returns the system to IDLE, i.e. they are ígnored. This is usually not shown explicitly, but i did it here once for clarity.
- Up and Down keys in item selection state increment/decrement the selected config item. Details like range limits are not usually shown explicitly, the sidebars in the action box mean that it is a formal procedure that can take care of itself. The state returns to item selection. Once "OK" is hit the state advances to editing or, if "Menu" is hit, the operation is canceled and state returns to idle.
- While editing the numeric value of the item can be incremented or decremented. To facilitate handling of large config values, the decade to edit can be selected to speed up things. "OK" stores the currently displayed value, and "Menu" again cancels everything.
In the top right corner are 2 special items.
-From any state the key timeout returns the state to idle. If the user just walks away, the editing is canceled after a time with this method.
-Any input will reset the key timeout timer to the starting value, again giving the user the set time to hit next button. This sequence does not have any impact to the state of the machine.
So, SDL is a bit different from just a regular flowchart. For a more formal presentation, see e.g.
http://www.sdl-forum.org/sdl2000present/sld001.htm