I have a statemachine in a real-time system with very few (3) states.
typedef enum {
STATE1,
STATE2,
STATE3
} state_t;
However, the transitions between these states require considerable time and have their own units. Thus, I have two options: either I am expanding the main statemachine so that all intermediate states are represented:
typedef enum {
STATE1,
STATE1_PREPARE_TRANSITION_TO_STATE2,
STATE1_DO_TRANSITION_TO_STATE2,
STATE1_PREPARE_TRANSITION_TO_STATE3,
STATE1_DO_TRANSITION_TO_STATE3,
STATE2,
...
} state_t;
or I create a nested statemachine for the corresponding ground states:
typedef enum {
STATE1_NOT_ACTIVE,
STATE1_NORMAL,
STATE1_PREPARE_TRANSITION_TO_STATE2,
STATE1_DO_TRANSITION_TO_STATE2,
STATE1_PREPARE_TRANSITION_TO_STATE3,
STATE1_DO_TRANSITION_TO_STATE3
} sub_state1_t;
...
Both features have advantages and disadvantages. A large static machine becomes messy and complex very easily. However, if all states agreed in the second case are not trivial, and many functions need information about both the global state and substations.
, , :
if ((global_state == STATE1) &&
(sub_state_1 == STATE1_DO_TRANSITION_TO_STATE2))
{
...
if (transition_xy_done(...))
{
global_state = STATE2;
sub_state_1 = STATE1_NOT_ACTIVE;
sub_state_2 = STATE2_NORMAL;
}
}
: statemachines ( ), statemachine - ?