Avionics
Core avionics package for CURE flight computers
Loading...
Searching...
No Matches
BaseStateMachine.cpp
Go to the documentation of this file.
2
3BaseStateMachine::BaseStateMachine(FlightState initialState) : state_(initialState) {}
4
6 return static_cast<uint8_t>(state_);
7}
8
10 if (functionPtr == nullptr) {
11 return false;
12 }
13
14 // Searching for a duplicate registration
15 for (std::size_t i = 0; i < callbackCount_; i++) {
16 const StateCallbackRegistration& registration = onStateEntryCallbacks_[i];
17 if (registration.state == targetState && registration.functionPtr == functionPtr) {
18 return false;
19 }
20 }
21
22 // Checking if we have room for another callback
23 if (callbackCount_ >= kMaxStateEntryCallbacks) {
24 return false;
25 }
26
27 // Register the new callback
28 onStateEntryCallbacks_[callbackCount_] = {targetState, functionPtr};
29 callbackCount_++;
30 return true;
31}
32
34 if (state_ == newState) {
35 return false;
36 }
37
38 state_ = newState;
39
40 // Calling the registered callbacks for the new state
41 for (std::size_t i = 0; i < callbackCount_; i++) {
42 const StateCallbackRegistration& registration = onStateEntryCallbacks_[i];
43 if (registration.state == state_) {
44 registration.functionPtr();
45 }
46 }
47
48 return true;
49}
50
52 return state_;
53}
FlightState
Definition States.h:17
virtual uint8_t getState() const
Current discrete state identifier.
bool registerOnStateEntry(FlightState targetState, StateEntryCallback functionPtr)
Register a callback to invoke each time a target state is entered.
bool changeState(FlightState newState)
Transition to a new state and trigger registered on-entry callbacks.
void(*)() StateEntryCallback
static constexpr std::size_t kMaxStateEntryCallbacks
FlightState getFlightState() const
Current state as FlightState enum.
BaseStateMachine(FlightState initialState=STATE_UNARMED)