-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDurationFsm.cpp
60 lines (49 loc) · 1.72 KB
/
DurationFsm.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
57
58
59
60
#include "DurationFsm.h"
//DURATION FSM
#define LIBCALL_DEEP_SLEEP_SCHEDULER
#include <DeepSleepScheduler.h> // https://github.com/PRosenb/DeepSleepScheduler
DurationFsm::DurationFsm(DurationState& current, const String name): FiniteStateMachine(current, name) {
if (current.minDurationMs > 0 && current.nextState != NULL) {
scheduler.scheduleDelayed(this, current.minDurationMs);
}
}
DurationState& DurationFsm::immediatelyChangeToNextState() {
DurationState ¤tState = getCurrentState();
if (currentState.nextState != NULL) {
DurationState *nextState = currentState.nextState;
changeState(*nextState);
return *nextState;
} else {
// we do not call changeState() so we need to ensure the callback is cancelled.
scheduler.removeCallbacks(this);
return currentState;
}
}
DurationFsm& DurationFsm::changeState(DurationState& state) {
scheduler.removeCallbacks(this);
if (state.minDurationMs > 0 && state.nextState != NULL) {
scheduler.scheduleDelayed(this, state.minDurationMs);
}
DurationState& previousState = getCurrentState();
FiniteStateMachine::changeState(state);
if (&previousState != &state) {
stateChangeTime = scheduler.getMillis();
}
return *this;
}
DurationState& DurationFsm::getCurrentState() {
return (DurationState&) FiniteStateMachine::getCurrentState();
}
boolean DurationFsm::isInState(DurationState& state) const {
return FiniteStateMachine::isInState(state);
}
boolean DurationFsm::isInState(SuperState& superState) const {
return FiniteStateMachine::isInState(superState);
}
unsigned long DurationFsm::timeInCurrentState() {
return scheduler.getMillis() - stateChangeTime;
}
void DurationFsm::run() {
immediatelyChangeToNextState();
}
//END DURATION FSM