Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
state_machine.h
Go to the documentation of this file.
1
5#pragma once
6
8#include <cstdint>
9
10namespace lampda {
11namespace utils {
12
16template<typename State> class StateMachine
17{
18public:
20 StateMachine(const State s) :
21 current(s),
22 lastState(s),
23 stateSetTime(platform::time_ms()),
24 timeout_ms(0),
25 afterTimeoutState(s),
26 changedWithTimeout(false)
27 {
28 }
35 StateMachine(const State s, const uint32_t timeout, const State stateOnTimeout) :
36 current(s),
37 lastState(s),
38 stateSetTime(platform::time_ms()),
39 timeout_ms(0),
40 afterTimeoutState(s),
41 changedWithTimeout(false)
42 {
43 set_state(s, timeout, stateOnTimeout);
44 }
45
49 void run()
50 {
51 // there is a timeout set for this state
52 if (isTimeoutSet and platform::time_ms() >= timeout_ms)
53 {
54 // timeout reached
55 set_state(afterTimeoutState);
56 changedWithTimeout = true;
57 }
58 }
59
66 bool set_state(const State s, bool forceUpdate = false)
67 {
68 if (not forceUpdate and s == current)
69 {
70 // ignore a set state with same value
71 return false;
72 }
73 // update the state
74 lastState = current;
75 current = s;
76 // reset the timeout time
77 isTimeoutSet = false;
78 timeout_ms = 0;
79 stateSetTime = platform::time_ms();
80
81 changedWithTimeout = false;
82 didStateJustChanged = true;
83 return true;
84 }
85
93 bool set_state(const State s, const uint32_t timeout, const State stateOnTimeout)
94 {
95 // set the new state if
96 // - no timeout is active
97 // - new state changes
98 // - timeout state changes
99 if (not isTimeoutSet or (s != current or afterTimeoutState != stateOnTimeout))
100 {
101 // force update state
102 set_state(s, true);
103 // set a timeout
104 isTimeoutSet = true;
105 // set after timeout state & timeout
106 update_timeout(timeout);
107 afterTimeoutState = stateOnTimeout;
108 return true;
109 }
110 return false;
111 }
112
116 void update_timeout(const uint32_t timeout)
117 {
118 if (isTimeoutSet)
119 {
120 // potential clock overflow, be careful
121 timeout_ms = platform::time_ms() + timeout;
122 }
123 }
124
127 {
128 const bool temp = didStateJustChanged;
129 didStateJustChanged = false;
130 return temp;
131 }
132
134 bool state_changed_with_timeout() { return changedWithTimeout; }
135
137 State get_state() const { return current; }
138
140 State get_last_state() const { return lastState; }
141
144 {
145 if (isTimeoutSet)
146 {
147 set_state(afterTimeoutState);
148 }
149 }
150
152 uint32_t get_state_raised_time() const { return stateSetTime; }
153
154private:
156 State current;
158 State lastState;
159
160 // The time this state was raised
161 uint32_t stateSetTime;
162 // prevent some errors with clock value wrap
163 bool isTimeoutSet;
165 uint32_t timeout_ms;
166 // The state to go after a timout
167 State afterTimeoutState;
168
170 bool didStateJustChanged;
171 // indicates that this state was reched with a timeout
172 bool changedWithTimeout;
173};
174
175} // namespace utils
176} // namespace lampda
Define a state machine.
Definition: state_machine.h:17
State get_state() const
Return the actual state.
Definition: state_machine.h:137
bool set_state(const State s, bool forceUpdate=false)
Set the machine state.
Definition: state_machine.h:66
void skip_timeout()
use this in a state with a timeout set to skip to the next state directly
Definition: state_machine.h:143
bool state_changed_with_timeout()
return true if this state was reached with a timeout
Definition: state_machine.h:134
bool set_state(const State s, const uint32_t timeout, const State stateOnTimeout)
set the new current state, with a timeout
Definition: state_machine.h:93
StateMachine(const State s, const uint32_t timeout, const State stateOnTimeout)
Construct a state machine from the first given state, with a timeout to another.
Definition: state_machine.h:35
void update_timeout(const uint32_t timeout)
Update the current state timeout if a timeout is active.
Definition: state_machine.h:116
StateMachine(const State s)
Construct a state machine from the first given state.
Definition: state_machine.h:20
State get_last_state() const
Return the state before this one.
Definition: state_machine.h:140
uint32_t get_state_raised_time() const
Return the time at which this state was raised.
Definition: state_machine.h:152
bool state_just_changed()
Return true if the current state just changed. Resetted on read.
Definition: state_machine.h:126
void run()
timeout check, and such, call it often
Definition: state_machine.h:49
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
Definition: time.cpp:16
Program scope.
Definition: control_fixed_modes.hpp:12
Define all time related platform functions.