Clock Sample |
The clock sample replicates the behavior of a LCD wrist watch with 3 buttons Mode, Set and Up. It is implemented as a Windows Forms application. Like its original, it needs some fumbling to set the alarm or use the stopwatch.
The bottom text in the application window shows the state machine active state as provided through the StateConfiguration.ToString method.
The central points of the sample are:
The button pressed events of the Mode, Set and Up buttons trigger different transitions dependent on the state machine active state.
3 orthogonal regions represent the display unit, the stopwatch unit and the alarm unit of the watch. They provide a state for the stopwatch and alarm while the display shows something different. When the alarm is due, the display is switched to the AlarmDisplay and reverts back to the previous mode inside NormalDisplay through the history function.
The code for setting up the state machine structure and string constants for the states and transitions are generated from Microsoft Visio.
Usage of state guarded transitions: If the display unit of the state machine is in the state StopwatchMode, the outgoing transitions with priority 3 and 4 both react on the Up button. These transitions are active dependent on the state of the stopwatch unit. When the stopwatch unit is in state StopwatchStopped, the transition with priority 3 is active through its source state reference to StopwatchStopped. When the stopwatch unit is in state StopwatchRunning, the transition with priority 4 is active through its source state reference to StopwatchRunning.
The clock sample uses a 10ms Windows Forms timer to update the time display in the UI (what a waste!) and an additional array of timers each with 15sec for timeouts of the modes.
There are several aspects of Windows Forms usage that should be improved, still the sample shows quite well the StaMa functionality.