-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathState.cpp
51 lines (44 loc) · 971 Bytes
/
State.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
#ifdef LINUX
#include <cstddef>
#include <cstdint>
#include <cstring>
#else
#include <Arduino.h>
#endif
#include "State.h"
#include "Event.h"
State::State(StateMachine& owner)
: _owner(owner)
{
_evtListIdx = 0;
_duringLambda = nullptr;
for (int i = 0; i < MAX_ARR_SIZE_STS_EVT; i++) {
_evtList[i] = nullptr;
_evtProcLambda[i] = nullptr;
_nextState[i] = nullptr;
}
}
State::~State()
{
}
void State::registerEvent(Event* evt, State* nextState, void (*evtProcLambda)(StateMachine& stateMachine))
{
if (_evtListIdx < MAX_ARR_SIZE_STS_EVT) {
_evtList[_evtListIdx] = evt;
_evtProcLambda[_evtListIdx] = evtProcLambda;
_nextState[_evtListIdx] = nextState;
_evtListIdx++;
} else {
return; // TODO: Error handling.
}
}
State* State::processEvent(Event& evt)
{
for (int i = 0; i < _evtListIdx; i++) {
if ((*_evtList[i]) == evt) {
(_evtProcLambda[i])(_owner);
return _nextState[i];
}
}
return nullptr;
}