-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
301 lines (257 loc) · 6.01 KB
/
main.cpp
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* @Author: snow pang
* @Date: 2019-11-04 14:26:07
* @LastEditors: snow pang
* @LastEditTime: 2019-11-16 23:05:42
* @FilePath: \C++Embedded\main.cpp
* @Description:
*/
#include <iostream>
#include <string>
#include <list>
#include <map>
using namespace std;
class Transition;
class EventTransition;
class Fsm;
unsigned int times_count = 0;
unsigned int get_timestamp()
{
return times_count;
}
//抽象状态类
//包含两部分 状态和动作
class State
{
public:
State(int state_id):
state_id(state_id){}
virtual void enter() = 0;
virtual void update() = 0;
virtual void exit() = 0;
virtual bool check() = 0;
inline int get_to_state_id() const
{
return state_to_id;
}
inline void set_state_to_id(const int to_id)
{
state_to_id = to_id;
}
inline int get_state_id() const
{
return state_id;
}
private:
int state_id;
static int state_to_id;
};
int State::state_to_id = -1;
//注册状态表
class Fsm
{
public:
Fsm(State *state_init):
state_cur(state_init),
initialize(false)
{
state_map.clear();
}
void add_state(State *state)
{
map<int, State*>::iterator it = state_map.find(state->get_state_id());
if(it != state_map.end())
{
cout<<"状态已存在,请勿重复添加"<<endl;
}else{
state_map.insert(make_pair(state->get_state_id(), state));
}
}
void run()
{
if(!initialize)
{
initialize = true;
state_cur->enter();
}else{
state_cur->update();
if(state_cur->check())
{
state_cur->exit();
int to_state_id = state_cur->get_to_state_id();
state_cur = state_map[to_state_id];
state_cur->enter();
}
}
}
private:
map<int, State*> state_map;
State* state_cur;
bool initialize;
};
class TimedTransition
{
public:
TimedTransition(unsigned int interval):
interval(interval),
start(0){}
bool check_timed(const unsigned int timestamp)
{
if(start == 0)
{
start = timestamp;
}else{
if((timestamp - start) > interval)
{
start = 0;
return true;
}
}
return false;
}
private:
unsigned int interval; //ms
unsigned int start;
};
class EventTransition
{
public:
EventTransition(int id):event_id(id){}
inline int get_event_id() const
{
return event_id;
}
private:
int event_id;
};
class State_idle:public State
{
public:
State_idle():
State(0x01),
timed_t_1(1000000),
count(0)
{
timed_t = new TimedTransition(timed_t_1);
}
virtual void enter()
{
count = 0;
cout<<"idle state enter" << endl;
}
virtual void update()
{
count++;
}
virtual void exit()
{
count=0;
cout << "idle state exit"<< endl;
}
virtual bool check()
{
unsigned int timestamp = get_timestamp();
if(count > 100000000000)
{
set_state_to_id(0x02);
cout<< "计数完成" << endl;
return true;
}
if(timed_t->check_timed(timestamp))
{
cout<<"超时强制退出"<<endl;
set_state_to_id(0x03);
return true;
}
return false;
}
private:
TimedTransition* timed_t;
unsigned int timed_t_1;
unsigned int count;
};
class State_test:public State
{
public:
State_test():State(0x02),
count(0){}
virtual void enter()
{
count = 0;
cout<<"test state enter"<< endl;
cout<< "count = "<< count <<endl;
}
virtual void update()
{
count++;
}
virtual void exit()
{
count = 0;
cout << "test state exit" <<endl;
cout<< "count = "<< count <<endl;
}
bool check()
{
if(count > 1000000000)
{
set_state_to_id(0x01);
return true;
}else{
return false;
}
}
private:
int count;
};
class State_test1:public State
{
public:
State_test1():State(0x03),
count(0){}
virtual void enter()
{
count = 0;
cout<<"test1 state enter"<< endl;
cout<< "count = "<< count <<endl;
}
virtual void update()
{
count++;
}
virtual void exit()
{
count = 0;
cout << "test1 state exit" <<endl;
cout<< "count = "<< count <<endl;
}
bool check()
{
if(count > 1000000000)
{
set_state_to_id(0x02);
return true;
}else{
return false;
}
}
private:
int count;
};
int main()
{
times_count++;
State *state_idle = new State_idle();
State *state_test = new State_test();
State *state_test1 = new State_test1();
Fsm *task_fsm = new Fsm(state_idle);
task_fsm->add_state(state_idle);
task_fsm->add_state(state_test);
task_fsm->add_state(state_test1);
while(1)
{
task_fsm->run();
times_count++;
}
return 0;
}