-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStateMachine.cpp
57 lines (47 loc) · 993 Bytes
/
StateMachine.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
#ifdef LINUX
#include <cstddef>
#include <cstdint>
#include <cstring>
#else
#include <Arduino.h>
#endif
#include "StateMachine.h"
#include "Data.h"
StateMachine::StateMachine(State* initState)
: _currState(initState)
{
this->subscribe(this);
}
StateMachine::~StateMachine()
{
}
void StateMachine::pushEvent(Event* evt)
{
Data* data = new Data(sizeof(uint32_t)); // TODO: Data& data = CircularQueue<Data>::alloc();
evt->serialize(*data);
this->push(data);
}
void StateMachine::generateData()
{
uint16_t evtCnt = 0;
for (int i = 0; i < this->getDataListLen(); i++) {
Data& data = this->getData(i);
if (data.type() == Event::_evtDataType) {
evtCnt++;
break;
}
}
if (evtCnt == 0) {
_currState->during();
}
}
void StateMachine::notify(Data& data)
{
if (data.type() == Event::_evtDataType) {
Event evt(data);
State* nextState = _currState->processEvent(evt);
if (nextState != nullptr) {
_currState = nextState;
}
}
}