-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfsm_switch_case_entry_exit_origin.c
105 lines (93 loc) · 2.02 KB
/
fsm_switch_case_entry_exit_origin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
/*
* gcc fsm_switch_case_entry_exit_origin.c -o fsm
*/
typedef enum
{
_NO_STATE,
TICK,
TACK
} STATE_T;
struct FSM_T
{
STATE_T newState;
STATE_T currentState;
void* pPrivateData;
};
void executeFsm( struct FSM_T* pFsm )
{
int* pMyCounter = pFsm->pPrivateData;
if( pFsm->newState != pFsm->currentState )
{
switch( pFsm->newState ) /* Entry actions */
{
case TICK:
printf( "entry TICK\n" );
*pMyCounter = 5;
break;
case TACK:
printf( "entry TACK\n" );
*pMyCounter = 10;
break;
default: assert( 0 ); break;
}
pFsm->currentState = pFsm->newState;
}
switch( pFsm->currentState ) /* Do actions */
{
case TICK:
printf( "do TICK counter: %d\n", *pMyCounter );
if( *pMyCounter == 0 )
{
pFsm->newState = TACK;
break;
}
(*pMyCounter)--;
break;
case TACK:
printf( "do TACK counter: %d\n", *pMyCounter );
if( *pMyCounter == 0 )
{
pFsm->newState = TICK;
break;
}
(*pMyCounter)--;
break;
default: assert( 0 ); break;
}
if( pFsm->newState != pFsm->currentState )
{
switch( pFsm->currentState ) /* Exit actions */
{
case TICK:
printf( "exit TICK\n" );
break;
case TACK:
printf( "exit TACK\n" );
break;
default: assert( 0 ); break;
}
}
}
void initFsm( struct FSM_T* pFsm, void* pPrivate, STATE_T startState )
{
pFsm->newState = startState;
pFsm->currentState = _NO_STATE;
pFsm->pPrivateData = pPrivate;
}
int main( void )
{
int i;
int myCounter = 10;
struct FSM_T myFsm;
printf( "Test of %s\n", __FILE__ );
initFsm( &myFsm, &myCounter, TICK );
for( i = 0; i < 100; i++ )
{
executeFsm( &myFsm );
sleep( 1 );
}
return 0;
}