7#include "src/system/hal/mutex.h"
25 ~Lock() { hal::mutex::hal_mutex_unlock(&_mutex); }
26 Lock(
const Lock&) =
delete;
27 Lock& operator=(
const Lock&) =
delete;
35 StateMachine(
const State s) : current(s), lastState(s), stateSetTime(hal::time_ms()), afterTimeoutState(s)
37 hal::mutex::hal_mutex_init(&mutex);
45 StateMachine(
const State s,
const uint32_t timeout,
const State stateOnTimeout) :
48 stateSetTime(hal::time_ms()),
51 hal::mutex::hal_mutex_init(&mutex);
53 set_state_with_timeout_locked(s, timeout, stateOnTimeout);
66 if (isTimeoutSet and (
hal::time_ms() - timeoutStart_ms) >= timeoutDuration_ms)
69 set_state_locked(afterTimeoutState,
false);
70 changedWithTimeout =
true;
80 bool set_state(
const State s,
bool forceUpdate =
false)
83 return set_state_locked(s, forceUpdate);
93 bool set_state(
const State s,
const uint32_t timeout,
const State stateOnTimeout)
96 return set_state_with_timeout_locked(s, timeout, stateOnTimeout);
104 bool set_state_unless(
const State blockingState,
const State s,
const uint32_t timeout,
const State stateOnTimeout)
107 if (current == blockingState)
109 set_state_with_timeout_locked(s, timeout, stateOnTimeout);
119 update_timeout_locked(timeout);
126 const bool temp = didStateJustChanged;
127 didStateJustChanged =
false;
135 return changedWithTimeout;
156 return afterTimeoutState;
166 return stateGeneration;
175 set_state_locked(afterTimeoutState,
false);
187 if (not isTimeoutSet or generation != stateGeneration)
189 set_state_locked(afterTimeoutState,
false);
209 bool set_state_locked(
const State s,
const bool forceUpdate)
211 if (not forceUpdate and s == current)
220 isTimeoutSet =
false;
222 timeoutDuration_ms = 0;
226 changedWithTimeout =
false;
227 didStateJustChanged =
true;
232 bool set_state_with_timeout_locked(
const State s,
const uint32_t timeout,
const State stateOnTimeout)
238 if (not isTimeoutSet or (s != current or afterTimeoutState != stateOnTimeout))
241 set_state_locked(s,
true);
243 afterTimeoutState = stateOnTimeout;
245 timeoutDuration_ms = timeout;
253 void update_timeout_locked(
const uint32_t timeout)
258 timeoutDuration_ms = timeout;
263 mutable hal::mutex::hal_mutex_t mutex;
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;
281 State afterTimeoutState;
284 bool didStateJustChanged =
false;
286 bool changedWithTimeout =
false;
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
Define all time related platform functions.