-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstateMachine.c
109 lines (91 loc) · 1.67 KB
/
stateMachine.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
106
107
/*
* =====================================================================================
*
* Filename: state.c
*
* Description:
*
* Version: 1.0
* Created: 2017年08月08日 08时48分35秒
* Last Modified: 2018-01-17 16:51:14
* Revision: none
* Compiler: gcc
*
* Author: zt (),
* Organization:
*
* =====================================================================================
*/
#include <stdio.h>
typedef unsigned char State;
typedef State ( *Procedure ) ( void* );
enum states
{
s_init,
s_count,
s_done,
s_default,
};
typedef struct
{
int cnt;
} SM_VAR;
State step_init ( void* arg )
{
SM_VAR* p = ( SM_VAR* ) arg;
p->cnt = 0;
printf ( "CS:init; cnt=%d; NS:count\n", p->cnt );
return s_count;
}
State step_count ( void* arg )
{
SM_VAR* p = ( SM_VAR* ) arg;
if ( p->cnt < 3 )
{
p->cnt += 1;
printf ( "CS:count; cnt=%d; NS:count\n", p->cnt );
return s_count;
}
else
{
printf ( "CS:count; cnt=%d; NS:done\n", p->cnt );
return s_done;
}
}
State step_done ( void* arg )
{
SM_VAR* p = ( SM_VAR* ) arg;
printf ( "CS:init; cnt=%d; NS:init\n", p->cnt );
return s_init;
}
State step_default ( void* arg )
{
( void ) arg;
//SM_VAR* p = ( SM_VAR* ) arg;
printf ( "Wrong State\n" );
return s_init;
}
Procedure Steps[] =
{
step_init,
step_count,
step_done,
step_default,
};
void BestStateMachine ( void* invar )
{
static State NS = s_init;
NS = Steps[NS] ( invar );
}
int main ( int argc, char* argv[] )
{
( void ) argc;
( void ) argv;
SM_VAR var;
int i;
for ( i = 0 ; i < 8; ++i )
{
BestStateMachine ( &var );
}
return 0;
}