@@ -20,11 +20,26 @@ void State::exit() {
2020 }
2121}
2222
23+ /* *
24+ * This is a constructor for a StateMachine object that initializes the initial state and creates a
25+ * State object for it.
26+ *
27+ * @param initial_state The initial state parameter is an unsigned 8-bit integer that represents the
28+ * starting state of the state machine.
29+ */
2330StateMachine::StateMachine (uint8_t initial_state) :
2431 initial_state(initial_state), current_state(initial_state) {
2532 states[initial_state] = State ();
2633}
2734
35+ /* *
36+ * The function adds a state to a state machine and sets it as the initial and current state if it is
37+ * the first state added.
38+ *
39+ * @param state The state to be added to the state machine.
40+ *
41+ * @return The function does not return anything. It has a void return type.
42+ */
2843void StateMachine::add_state (uint8_t state) {
2944 if (states.contains (state)) {
3045 ErrorHandler (" The state %d is already added" , state);
@@ -38,6 +53,13 @@ void StateMachine::add_state(uint8_t state) {
3853 states[state];
3954}
4055
56+ /* *
57+ * This function adds an action to be executed when entering the current state of a state machine.
58+ *
59+ * @param action The parameter "action" is a function pointer to a function that takes no arguments and
60+ * returns nothing (void). It represents an action that should be executed when the current state of
61+ * the state machine is entered.
62+ */
4163void StateMachine::add_enter_action (function<void ()> action) {
4264 if (not current_state) {
4365 ErrorHandler (" The state machine does not have a current state" );
@@ -46,6 +68,15 @@ void StateMachine::add_enter_action(function<void()> action) {
4668 states[current_state].on_enter_actions .push_back (action);
4769}
4870
71+ /* *
72+ * This function adds an action to be executed when entering a specific state in a state machine.
73+ *
74+ * @param action The action parameter is a function pointer to a function that takes no arguments and
75+ * returns nothing. This function will be executed when the state machine transitions to the state
76+ * specified by the state parameter.
77+ * @param state The state parameter is an unsigned 8-bit integer that represents the state to which the
78+ * enter action is being added.
79+ */
4980void StateMachine::add_enter_action (function<void ()> action, uint8_t state) {
5081 if (not states.contains (state)) {
5182 ErrorHandler (" The state %d is not added to the state machine" , state);
@@ -54,13 +85,31 @@ void StateMachine::add_enter_action(function<void()> action, uint8_t state) {
5485 states[state].on_enter_actions .push_back (action);
5586}
5687
88+ /* *
89+ * This function adds an exit action to the current state of a state machine.
90+ *
91+ * @param action `action` is a function pointer to a function that takes no arguments and returns
92+ * nothing (`void`). It represents an action that should be executed when the state machine exits the
93+ * current state and transitions to a new state. The `add_exit_action` function adds this action to the
94+ * list of exit actions for
95+ */
5796void StateMachine::add_exit_action (function<void ()> action) {
5897 if (not current_state) {
5998 ErrorHandler (" The state machine does not have a current state" );
6099 return ;
61100 }
62101 states[current_state].on_exit_actions .push_back (action);
63102}
103+
104+ /* *
105+ * This function adds an exit action to a specific state in a state machine.
106+ *
107+ * @param action The action parameter is a function pointer to a function that takes no arguments and
108+ * returns nothing. This function represents the action that will be executed when the state machine
109+ * exits the specified state.
110+ * @param state The state parameter is an unsigned 8-bit integer that represents the state to which the
111+ * exit action is being added.
112+ */
64113void StateMachine::add_exit_action (function<void ()> action, uint8_t state) {
65114 if (not states.contains (state)) {
66115 ErrorHandler (" The state %d is not added to the state machine" );
@@ -69,6 +118,17 @@ void StateMachine::add_exit_action(function<void()> action, uint8_t state) {
69118 states[state].on_exit_actions .push_back (action);
70119}
71120
121+ /* *
122+ * This function adds a transition between two states in a state machine.
123+ *
124+ * @param old_state The current state of the state machine before the transition occurs. It is
125+ * represented as an unsigned 8-bit integer (uint8_t).
126+ * @param new_state The new state to transition to. It is of type uint8_t.
127+ * @param transition The parameter "transition" is a function pointer to a boolean function that takes
128+ * no arguments. This function represents the condition that must be met in order for the state machine
129+ * to transition from the old_state to the new_state. If the condition is true, the transition will
130+ * occur. If the condition is false
131+ */
72132void StateMachine::add_transition (uint8_t old_state, uint8_t new_state,
73133 function<bool ()> transition) {
74134 if (not states.contains (old_state)) {
@@ -83,6 +143,14 @@ void StateMachine::add_transition(uint8_t old_state, uint8_t new_state,
83143 transitions[old_state][new_state] = transition;
84144}
85145
146+ /* *
147+ * The function adds a nested state machine to the current state machine and clears any timed actions
148+ * associated with the nested state machine's current state if it is not the current state.
149+ *
150+ * @param state_machine A reference to a StateMachine object that is being added as a nested state
151+ * machine to the current StateMachine object.
152+ * @param state The state to which the nested state machine is being added.
153+ */
86154void StateMachine::add_state_machine (StateMachine& state_machine, uint8_t state) {
87155 nested_state_machine[state] = &state_machine;
88156
@@ -99,6 +167,10 @@ void StateMachine::add_state_machine(StateMachine& state_machine, uint8_t state)
99167 }
100168}
101169
170+ /* *
171+ * The function checks for state transitions and changes the current state if necessary, and also
172+ * checks for transitions in a nested state machine if applicable.
173+ */
102174void StateMachine::check_transitions () {
103175 for (auto const state_transition : transitions[current_state]) {
104176 if (state_transition.second ()) {
0 commit comments