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
7#include "src/system/hal/mutex.h"
9#include <cstdint>
10
11namespace lampda {
12namespace common {
13
19template<typename State> class StateMachine
20{
22 struct Lock
23 {
24 explicit Lock(hal::mutex::hal_mutex_t& mutex) : _mutex(mutex) { hal::mutex::hal_mutex_lock(&_mutex); }
25 ~Lock() { hal::mutex::hal_mutex_unlock(&_mutex); }
26 Lock(const Lock&) = delete;
27 Lock& operator=(const Lock&) = delete;
28
29 private:
31 };
32
33public:
35 StateMachine(const State s) : current(s), lastState(s), stateSetTime(hal::time_ms()), afterTimeoutState(s)
36 {
37 hal::mutex::hal_mutex_init(&mutex);
38 }
45 StateMachine(const State s, const uint32_t timeout, const State stateOnTimeout) :
46 current(s),
47 lastState(s),
48 stateSetTime(hal::time_ms()),
49 afterTimeoutState(s)
50 {
51 hal::mutex::hal_mutex_init(&mutex);
52 Lock lock(mutex);
53 set_state_with_timeout_locked(s, timeout, stateOnTimeout);
54 }
55
56 StateMachine(const StateMachine&) = delete;
57 StateMachine& operator=(StateMachine const&) = delete;
58
62 void run()
63 {
64 Lock lock(mutex);
65 // there is a timeout set for this state (elapsed time comparison is safe across a time_ms wrap)
66 if (isTimeoutSet and (hal::time_ms() - timeoutStart_ms) >= timeoutDuration_ms)
67 {
68 // timeout reached
69 set_state_locked(afterTimeoutState, false);
70 changedWithTimeout = true;
71 }
72 }
73
80 bool set_state(const State s, bool forceUpdate = false)
81 {
82 Lock lock(mutex);
83 return set_state_locked(s, forceUpdate);
84 }
85
93 bool set_state(const State s, const uint32_t timeout, const State stateOnTimeout)
94 {
95 Lock lock(mutex);
96 return set_state_with_timeout_locked(s, timeout, stateOnTimeout);
97 }
98
104 bool set_state_unless(const State blockingState, const State s, const uint32_t timeout, const State stateOnTimeout)
105 {
106 Lock lock(mutex);
107 if (current == blockingState)
108 return false;
109 set_state_with_timeout_locked(s, timeout, stateOnTimeout);
110 return true;
111 }
112
116 void update_timeout(const uint32_t timeout)
117 {
118 Lock lock(mutex);
119 update_timeout_locked(timeout);
120 }
121
124 {
125 Lock lock(mutex);
126 const bool temp = didStateJustChanged;
127 didStateJustChanged = false;
128 return temp;
129 }
130
133 {
134 Lock lock(mutex);
135 return changedWithTimeout;
136 }
137
139 State get_state() const
140 {
141 Lock lock(mutex);
142 return current;
143 }
144
146 State get_last_state() const
147 {
148 Lock lock(mutex);
149 return lastState;
150 }
151
154 {
155 Lock lock(mutex);
156 return afterTimeoutState;
157 }
158
163 uint32_t get_state_generation() const
164 {
165 Lock lock(mutex);
166 return stateGeneration;
167 }
168
171 {
172 Lock lock(mutex);
173 if (isTimeoutSet)
174 {
175 set_state_locked(afterTimeoutState, false);
176 }
177 }
178
184 bool skip_timeout(const uint32_t generation)
185 {
186 Lock lock(mutex);
187 if (not isTimeoutSet or generation != stateGeneration)
188 return false;
189 set_state_locked(afterTimeoutState, false);
190 return true;
191 }
192
194 uint32_t get_state_raised_time() const
195 {
196 Lock lock(mutex);
197 return stateSetTime;
198 }
199
202 {
203 Lock lock(mutex);
204 stateSetTime = hal::time_ms();
205 }
206
207private:
209 bool set_state_locked(const State s, const bool forceUpdate)
210 {
211 if (not forceUpdate and s == current)
212 {
213 // ignore a set state with same value
214 return false;
215 }
216 // update the state
217 lastState = current;
218 current = s;
219 // reset the timeout time
220 isTimeoutSet = false;
221 timeoutStart_ms = 0;
222 timeoutDuration_ms = 0;
223 stateSetTime = hal::time_ms();
224 ++stateGeneration;
225
226 changedWithTimeout = false;
227 didStateJustChanged = true;
228 return true;
229 }
230
232 bool set_state_with_timeout_locked(const State s, const uint32_t timeout, const State stateOnTimeout)
233 {
234 // set the new state if
235 // - no timeout is active
236 // - new state changes
237 // - timeout state changes
238 if (not isTimeoutSet or (s != current or afterTimeoutState != stateOnTimeout))
239 {
240 // force update state
241 set_state_locked(s, true);
242 // timeout target and delay first, the timeout is active once all are set
243 afterTimeoutState = stateOnTimeout;
244 timeoutStart_ms = hal::time_ms();
245 timeoutDuration_ms = timeout;
246 isTimeoutSet = true;
247 return true;
248 }
249 return false;
250 }
251
253 void update_timeout_locked(const uint32_t timeout)
254 {
255 if (isTimeoutSet)
256 {
257 timeoutStart_ms = hal::time_ms();
258 timeoutDuration_ms = timeout;
259 }
260 }
261
263 mutable hal::mutex::hal_mutex_t mutex;
264
266 State current;
268 State lastState;
269
270 // The time this state was raised
271 uint32_t stateSetTime;
273 uint32_t stateGeneration = 0;
275 bool isTimeoutSet = false;
277 uint32_t timeoutStart_ms = 0;
279 uint32_t timeoutDuration_ms = 0;
280 // The state to go after a timout
281 State afterTimeoutState;
282
284 bool didStateJustChanged = false;
285 // indicates that this state was reched with a timeout
286 bool changedWithTimeout = false;
287};
288
289} // namespace common
290} // namespace lampda
Define a state machine. Thread safe: all the methods are synchronized, a state change and its timeout...
Definition: state_machine.h:20
State get_state() const
Return the actual state.
Definition: state_machine.h:139
void update_timeout(const uint32_t timeout)
Update the current state timeout if a timeout is active.
Definition: state_machine.h:116
bool set_state_unless(const State blockingState, const State s, const uint32_t timeout, const State stateOnTimeout)
Same as set_state(s, timeout, stateOnTimeout), unless the machine is in blockingState....
Definition: state_machine.h:104
bool set_state(const State s, bool forceUpdate=false)
Set the machine state.
Definition: state_machine.h:80
bool state_changed_with_timeout() const
return true if this state was reached with a timeout
Definition: state_machine.h:132
StateMachine(const State s)
Construct a state machine from the first given state.
Definition: state_machine.h:35
uint32_t get_state_generation() const
Return a value changed on every state set (timeout target included), to detect a concurrent update....
Definition: state_machine.h:163
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:45
void run()
timeout check, and such, call it often
Definition: state_machine.h:62
bool skip_timeout(const uint32_t generation)
Skip the timeout, only if the state was not set since generation was read (see get_state_generation)....
Definition: state_machine.h:184
void skip_timeout()
use this in a state with a timeout set to skip to the next state directly
Definition: state_machine.h:170
bool state_just_changed()
Return true if the current state just changed. Resetted on read.
Definition: state_machine.h:123
void reset_state_raised_time()
Restart the time returned by get_state_raised_time, without changing the state or its timeout.
Definition: state_machine.h:201
uint32_t get_state_raised_time() const
Return the time at which this state was raised.
Definition: state_machine.h:194
State after_timeout_state() const
Return the state after the timeout. May be undefined.
Definition: state_machine.h:153
State get_last_state() const
Return the state before this one.
Definition: state_machine.h:146
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
uint32_t time_ms(void)
Returns the number of milliseconds since the Arduino board began running the current program....
Program scope.
Definition: control_fixed_modes.hpp:14
Definition: mutex.h:49
Define all time related platform functions.