1616#include " Fsm.h"
1717
1818
19- State::State (void (*on_enter)(), void (*on_state)(), void (*on_exit)())
19+ State::State (void (*on_enter)(void * ctx ), void (*on_state)(void * ctx ), void (*on_exit)(void * ctx ))
2020: on_enter(on_enter),
2121 on_state(on_state),
2222 on_exit(on_exit)
@@ -45,7 +45,7 @@ Fsm::~Fsm()
4545
4646
4747void Fsm::add_transition (State* state_from, State* state_to, int event,
48- void (*on_transition)())
48+ void (*on_transition)(void * ctx ))
4949{
5050 if (state_from == NULL || state_to == NULL )
5151 return ;
@@ -60,7 +60,7 @@ void Fsm::add_transition(State* state_from, State* state_to, int event,
6060
6161
6262void Fsm::add_timed_transition (State* state_from, State* state_to,
63- unsigned long interval, void (*on_transition)())
63+ unsigned long interval, void (*on_transition)(void * ctx ))
6464{
6565 if (state_from == NULL || state_to == NULL )
6666 return ;
@@ -81,7 +81,7 @@ void Fsm::add_timed_transition(State* state_from, State* state_to,
8181
8282
8383Fsm::Transition Fsm::create_transition (State* state_from, State* state_to,
84- int event, void (*on_transition)())
84+ int event, void (*on_transition)(void * ctx ))
8585{
8686 Transition t;
8787 t.state_from = state_from;
@@ -92,7 +92,7 @@ Fsm::Transition Fsm::create_transition(State* state_from, State* state_to,
9292 return t;
9393}
9494
95- void Fsm::trigger (int event)
95+ void Fsm::trigger (int event, void * ctx )
9696{
9797 if (m_initialized)
9898 {
@@ -102,14 +102,14 @@ void Fsm::trigger(int event)
102102 if (m_transitions[i].state_from == m_current_state &&
103103 m_transitions[i].event == event)
104104 {
105- Fsm::make_transition (&(m_transitions[i]));
105+ Fsm::make_transition (&(m_transitions[i]), ctx );
106106 return ;
107107 }
108108 }
109109 }
110110}
111111
112- void Fsm::check_timed_transitions ()
112+ void Fsm::check_timed_transitions (void * ctx )
113113{
114114 for (int i = 0 ; i < m_num_timed_transitions; ++i)
115115 {
@@ -124,42 +124,42 @@ void Fsm::check_timed_transitions()
124124 unsigned long now = millis ();
125125 if (now - transition->start >= transition->interval )
126126 {
127- Fsm::make_transition (&(transition->transition ));
127+ Fsm::make_transition (&(transition->transition ), ctx );
128128 transition->start = 0 ;
129129 }
130130 }
131131 }
132132 }
133133}
134134
135- void Fsm::run_machine ()
135+ void Fsm::run_machine (void * ctx )
136136{
137137 // first run must exec first state "on_enter"
138138 if (!m_initialized)
139139 {
140140 m_initialized = true ;
141141 if (m_current_state->on_enter != NULL )
142- m_current_state->on_enter ();
142+ m_current_state->on_enter (ctx );
143143 }
144144
145145 if (m_current_state->on_state != NULL )
146- m_current_state->on_state ();
146+ m_current_state->on_state (ctx );
147147
148- Fsm::check_timed_transitions ();
148+ Fsm::check_timed_transitions (ctx );
149149}
150150
151- void Fsm::make_transition (Transition* transition)
151+ void Fsm::make_transition (Transition* transition, void * ctx )
152152{
153153
154154 // Execute the handlers in the correct order.
155155 if (transition->state_from ->on_exit != NULL )
156- transition->state_from ->on_exit ();
156+ transition->state_from ->on_exit (ctx );
157157
158158 if (transition->on_transition != NULL )
159- transition->on_transition ();
159+ transition->on_transition (ctx );
160160
161161 if (transition->state_to ->on_enter != NULL )
162- transition->state_to ->on_enter ();
162+ transition->state_to ->on_enter (ctx );
163163
164164 m_current_state = transition->state_to ;
165165
0 commit comments