-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiniteStateMachine.cpp
202 lines (188 loc) · 3.71 KB
/
FiniteStateMachine.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
/* $Id: FiniteStateMachine.cpp 1235 2011-09-11 19:49:14Z abrevig $
||
|| @author Alexander Brevig <[email protected]>
|| @url http://wiring.org.co/
|| @url http://alexanderbrevig.com/
|| @contribution Brett Hagman <[email protected]>
||
|| @description
|| | Provides an easy way of making finite state machines.
|| |
|| | Wiring Cross-platform Library
|| #
||
|| @license Please see cores/Common/License.txt.
||
*/
#include "FiniteStateMachine.h"
/*
|| @constructor
|| | Update only state
|| | Set the initial state of this State
|| #
||
|| @parameter updateFunction the function to call when this state updates
*/
State::State(void (*updateFunction)())
{
userEnter = 0;
userUpdate = updateFunction;
userExit = 0;
}
/*
|| @constructor
|| | Set the initial state of this State
|| #
||
|| @parameter enterFunction the function to call when this state enters
|| @parameter updateFunction the function to call when this state updates
|| @parameter exitFunction the function to call when this state exits
*/
State::State(void (*enterFunction)(), void (*updateFunction)(), void (*exitFunction)())
{
userEnter = enterFunction;
userUpdate = updateFunction;
userExit = exitFunction;
}
/*
|| @description
|| | Enter this state
|| | Call the userEnter function callback
|| #
*/
void State::enter()
{
if (userEnter)
{
userEnter();
}
}
/*
|| @description
|| | Update this state
|| | Call the userUpdate function callback
|| #
*/
void State::update()
{
if (userUpdate)
{
userUpdate();
}
}
/*
|| @description
|| | Exit this state
|| | Call the userExit function callback
|| #
*/
void State::exit()
{
if (userExit)
{
userExit();
}
}
/*
|| @constructor
|| | Set the initial state of this Finite State Machine
|| #
||
|| @parameter current The initial state of the FSM
*/
FiniteStateMachine::FiniteStateMachine(State& current)
{
needToTriggerEnter = true;
currentState = nextState = ¤t;
stateChangeTime = 0;
}
/*
|| @description
|| | Update this FSM
|| | It will call enter/update/exit on the current at the correct time
|| #
*/
FiniteStateMachine& FiniteStateMachine::update()
{
//simulate a transition to the first state
//this only happens the first time update is called
if (needToTriggerEnter)
{
currentState->enter();
needToTriggerEnter = false;
}
else
{
if (currentState != nextState)
{
immediateTransitionTo(*nextState);
}
currentState->update();
}
return *this;
}
/*
|| @description
|| | Have the FSM schedule a transition for the next update()
|| #
||
|| @parameter state The state to transit to
*/
FiniteStateMachine& FiniteStateMachine::transitionTo(State& state)
{
nextState = &state;
stateChangeTime = millis();
return *this;
}
/*
|| @description
|| | Have the FSM transit to the next state immediatly
|| #
||
|| @parameter state The state to transit to
*/
FiniteStateMachine& FiniteStateMachine::immediateTransitionTo(State& state)
{
currentState->exit();
currentState = nextState = &state;
currentState->enter();
stateChangeTime = millis();
return *this;
}
/*
|| @description
|| | Get the current state of this FSM
|| #
*/
State& FiniteStateMachine::getCurrentState() const
{
return *currentState;
}
/*
|| @description
|| | Check if this FSM is in the State state
|| #
||
|| @parameter state The state to check against
||
|| @return true if the FSM is in State state
*/
bool FiniteStateMachine::isInState(State &state) const
{
return (&state == currentState);
}
/*
|| @description
|| | Get the current state of this FSM
|| #
||
|| @return The time this state has been active
*/
unsigned long FiniteStateMachine::timeInCurrentState() const
{
if (stateChangeTime)
{
return millis() - stateChangeTime;
}
return 0;
}