-
Notifications
You must be signed in to change notification settings - Fork 0
/
OnixartsFSM.h
107 lines (96 loc) · 3.84 KB
/
OnixartsFSM.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
// Onixarts_FSM is a Finite State Machine implementation library for Arduino.
// FSM helps with state based system implementation.
//
// Define OA_NO_CALLBACKS preprocesor symbol to compile library without callback function support (reducing SRAM usage)
// when using as class member
//
// Github: https://github.com/Onixarts/Onixarts_FSM
// Author's site: http://onixarts.pl
// Contact: [email protected]
//
// Copyright (C) 2017 Bartosz Rosa (onixarts.pl)
//
// This program is free software : you can redistribute it and / or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.If not, see <http://www.gnu.org/licenses/>.
#ifndef _OnixartsFSM_h
#define _OnixartsFSM_h
#include "Arduino.h"
#define BEGIN_STATE(parentClass, stateClass, transitionsCount)\
class stateClass : public Onixarts::Tools::FSM::State{\
parentClass& m_parent;\
byte idx;\
Onixarts::Tools::FSM::Transition transitionsTable[transitionsCount];\
parentClass& GetParent() {return m_parent;}\
public:\
stateClass(parentClass* parent) : m_parent(*parent), idx(0) { size = transitionsCount; transitions = transitionsTable; memset(transitionsTable, 0, size*sizeof(Onixarts::Tools::FSM::Transition) ); \
#define TRANSITION(event, toState)\
if(idx < size) \
transitionsTable[idx++].Init(event, &(m_parent.toState));\
#define END_STATE(instanceName) } instanceName;
namespace Onixarts
{
namespace Tools
{
namespace FSM
{
#define InsertTransition(trans) trans, sizeof(trans)/sizeof(trans[0])
class State;
typedef void (*FSMEventDelegate)(byte eventID);
//-------------------------------------------------------------------------------------------------------------------
struct Transition
{
byte eventID;
State* state;
void Init(uint8_t eventID, State* nextState) { this->eventID = eventID; state = nextState; }
};
//-------------------------------------------------------------------------------------------------------------------
class Machine
{
protected:
State* currentState;
public:
Machine();
Machine(State& state);
void Notify(byte eventID );
State* GetCurrentState() { return this->currentState; }
void SetCurrentState(State& state);
};
//-------------------------------------------------------------------------------------------------------------------
class State
{
protected:
Transition* transitions;
uint8_t size;
#ifndef OA_NO_CALLBACKS
FSMEventDelegate m_enterDelegate; // pointer to enter state callback
FSMEventDelegate m_exitDelegate; // pointer to exit state callback
#endif
virtual void OnEnter(byte eventID) {};
virtual void OnExit(byte eventID) {};
public:
State();
#ifndef OA_NO_CALLBACKS
State(FSMEventDelegate enterDelegate);
State(FSMEventDelegate enterDelegate, FSMEventDelegate exitDelegate);
void SetEnterEventDelegate(FSMEventDelegate fsmDelegate) { m_enterDelegate = fsmDelegate; }
void SetExitEventDelegate(FSMEventDelegate fsmDelegate) { m_exitDelegate = fsmDelegate; }
#endif
bool Notify(byte eventID, State*& nextState ); // returns true if there was a transformation. nextState is set to state from transitions array, or to this if event not found.
void SetTransitions(Transition* transitions, byte size);
void Enter(byte eventID = 0);
void Exit(byte eventID = 0);
};
}
}
}
#endif