-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiniteStateMachine.h
executable file
·110 lines (96 loc) · 3.06 KB
/
FiniteStateMachine.h
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
/*
||
|| @file FiniteStateMachine.h
|| @version 1.7
|| @author Alexander Brevig
|| @contact [email protected]
||
|| @description
|| | Provide an easy way of making finite state machines
|| #
||
|| @license
|| | This library is free software; you can redistribute it and/or
|| | modify it under the terms of the GNU Lesser General Public
|| | License as published by the Free Software Foundation; version
|| | 2.1 of the License.
|| |
|| | This library is distributed in the hope that it will be useful,
|| | but WITHOUT ANY WARRANTY; without even the implied warranty of
|| | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|| | Lesser General Public License for more details.
|| |
|| | You should have received a copy of the GNU Lesser General Public
|| | License along with this library; if not, write to the Free Software
|| | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|| #
||
*/
#ifndef FINITESTATEMACHINE_H
#define FINITESTATEMACHINE_H
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif
#define NO_ENTER (0)
#define NO_UPDATE (0)
#define NO_EXIT (0)
#define FSM FiniteStateMachine
// define the functionality of the states
class State {
public:
State(void (*updateFunction)());
State(int id, void (*updateFunction)());
State(int id, void (*enterFunction)(), void (*updateFunction)(),
void (*exitFunction)());
void init(int id, void (*enterFunction)(), void (*updateFunction)(),
void (*exitFunction)());
// State( byte newId, void (*enterFunction)(), void (*updateFunction)(), void
// (*exitFunction)() );
// void getId();
void enter();
void update();
void exit();
int id();
private:
// byte id;
void (*userEnter)();
void (*userUpdate)();
void (*userExit)();
int _id;
};
// define the finite state machine functionality
class FiniteStateMachine {
public:
FiniteStateMachine(State ¤t);
FiniteStateMachine &update();
FiniteStateMachine &transitionTo(State &state);
FiniteStateMachine &immediateTransitionTo(State &state);
State &getCurrentState();
boolean isInState(State &state) const;
unsigned long timeInCurrentState();
private:
State *currentState;
State *nextState;
unsigned long stateChangeTime;
};
#endif
/*
|| @changelog
|| | 1.7 2010-03-08- Alexander Brevig : Fixed a bug, constructor ran update,
thanks to René Pressé
|| | 1.6 2010-03-08- Alexander Brevig : Added timeInCurrentState() , requested
by sendhb
|| | 1.5 2009-11-29- Alexander Brevig : Fixed a bug, introduced by the below
fix, thanks to Jon Hylands again...
|| | 1.4 2009-11-29- Alexander Brevig : Fixed a bug, enter gets triggered on the
first state. Big thanks to Jon Hylands who pointed this out.
|| | 1.3 2009-11-01 - Alexander Brevig : Added getCurrentState : &State
|| | 1.3 2009-11-01 - Alexander Brevig : Added isInState : boolean, requested by
Henry Herman
|| | 1.2 2009-05-18 - Alexander Brevig : enter and exit bug fix
|| | 1.1 2009-05-18 - Alexander Brevig : Added support for cascaded calls
|| | 1.0 2009-04-13 - Alexander Brevig : Initial Release
|| #
*/