-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFiniteStateMachine.h
72 lines (59 loc) · 1.62 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
/* $Id: FiniteStateMachine.h 1198 2011-06-14 21:08:27Z bhagman $
||
|| @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.
||
*/
#ifndef FINITESTATEMACHINE_H
#define FINITESTATEMACHINE_H
#include <Arduino.h>
#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(void (*enterFunction)(), void (*updateFunction)(), void (*exitFunction)());
//State( byte newId, void (*enterFunction)(), void (*updateFunction)(), void (*exitFunction)() );
//void getId();
void enter();
void update();
void exit();
private:
//byte id;
void (*userEnter)();
void (*userUpdate)();
void (*userExit)();
};
//define the finite state machine functionality
class FiniteStateMachine
{
public:
FiniteStateMachine(State& current);
FiniteStateMachine& update();
FiniteStateMachine& transitionTo(State& state);
FiniteStateMachine& immediateTransitionTo(State& state);
State& getCurrentState() const;
bool isInState(State &state) const;
unsigned long timeInCurrentState() const;
private:
bool needToTriggerEnter;
State* currentState;
State* nextState;
unsigned long stateChangeTime;
};
#endif
// FINITESTATEMACHINE_H