Statechart Implementation : Statechart Implementation: Generated Functions

Statechart Implementation: Generated Functions

Statechart Code Frame

Consider the following example of code generated from a Statechart (Note: line numbers are included in this code sample for discussion purposes):

1 void
2 cgDo_A11_CTRLcnt1(void)
3 {
4 StateInfo_A11_CTRLcnt1 nextState_A11_CTRLcnt1 = 0;
5 if (currentState_A11_CTRLcnt1 == 0) {
6 nextState_A11_CTRLcnt1 = FS_A11_CTRLst2;
7 }
8
9 else
10 {
11 … The rest of the Statechart logic
12 }
13 if (nextState_A11_CTRLcnt1 != 0) {
14 if (currentState_A11_CTRLcnt1 !=
nextState_A11_CTRLcnt1)
15 cgGlobalFlags |= BITSUPERSTEP_TASK1;
16 currentState_A11_CTRLcnt1 =
nextState_A11_CTRLcnt1;
17 }
18 }
 

In general, the overall code frame of a Statechart looks like the cgDo_A11_CTRLcnt1 function shown above. However, you will discover in the following discussions that code frame is not fixed.

Line 4 resets the nextState variable. This variable will be set only if a transition has been made, and will hold the new state configuration of the Statechart.

Lines 13 and 14 check the nextState variable, to determine if a transition was taken and whether to enforce another step in the TASK holding the Statechart...

Line 15: cgGlobalFlags | = BITSUPERSTEP_TASK1
Line 16: currentState_A11_CTRLcnt1 = nextState_A11_CTRLcnt1

advances the Statechart configuration a step, to hold the configuration of the next step.

Lines 5 to 12 will be replaced with specific code resulting from the specified Statechart logic. For example, two additional functions might be commonly generated here: entry actions and exit actions. If the Statechart logic requires entering/exiting reactions, the functions will resemble the following:

void
cgEnterActions_A11_CTRLcnt1(void)
{
… entering reactions code
}
void
cgExitActions_A11_CTRLcnt1(void)
{
… exiting reactions code
}
 

When either of these function are needed, the following changes to cgDo_will also be made:

void
cgDo_A11_CTRLcnt1(void)
{
StateInfo_A11_CTRLcnt1 nextState_A11_CTRLcnt1 = 0;
staySame_A11_CTRLcnt1 = 0;
if (currentState_A11_CTRLcnt1 == 0) {
nextState_A11_CTRLcnt1 =
FS_DefaultOf_Chart_A11_CTRL;
}
else
{
… The rest of the Statechart logic
}
if (nextState_A11_CTRLcnt1 != 0) {
cgExitActions_A11_CTRLcnt1();
cgEnterActions_A11_CTRLcnt1();
if (currentState_A11_CTRLcnt1 !=
nextState_A11_CTRLcnt1)
cgGlobalFlags |= BITSUPERSTEP_TASK1;
currentState_A11_CTRLcnt1 = nextState_A11_CTRLcnt1;
}
}
 

Of course, the function calls to cgExitActions_A11_CTRLcnt1() required. See Optimization Algorithms for information on the MicroC algorithms that create more efficient code.