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)
@@ -44,7 +44,7 @@ Fsm::~Fsm()
4444
4545
4646void Fsm::add_transition (State* state_from, State* state_to, int event,
47- void (*on_transition)())
47+ void (*on_transition)(void * ctx ))
4848{
4949 if (state_from == NULL || state_to == NULL )
5050 return ;
@@ -59,7 +59,7 @@ void Fsm::add_transition(State* state_from, State* state_to, int event,
5959
6060
6161void Fsm::add_timed_transition (State* state_from, State* state_to,
62- unsigned long interval, void (*on_transition)())
62+ unsigned long interval, void (*on_transition)(void * ctx ))
6363{
6464 if (state_from == NULL || state_to == NULL )
6565 return ;
@@ -80,7 +80,7 @@ void Fsm::add_timed_transition(State* state_from, State* state_to,
8080
8181
8282Fsm::Transition Fsm::create_transition (State* state_from, State* state_to,
83- int event, void (*on_transition)())
83+ int event, void (*on_transition)(void * ctx ))
8484{
8585 Transition t;
8686 t.state_from = state_from;
@@ -91,7 +91,7 @@ Fsm::Transition Fsm::create_transition(State* state_from, State* state_to,
9191 return t;
9292}
9393
94- void Fsm::trigger (int event)
94+ void Fsm::trigger (int event, void * ctx )
9595{
9696 if (m_initialized)
9797 {
@@ -101,14 +101,14 @@ void Fsm::trigger(int event)
101101 if (m_transitions[i].state_from == m_current_state &&
102102 m_transitions[i].event == event)
103103 {
104- Fsm::make_transition (&(m_transitions[i]));
104+ Fsm::make_transition (&(m_transitions[i]), ctx );
105105 return ;
106106 }
107107 }
108108 }
109109}
110110
111- void Fsm::check_timed_transitions ()
111+ void Fsm::check_timed_transitions (void * ctx )
112112{
113113 for (int i = 0 ; i < m_num_timed_transitions; ++i)
114114 {
@@ -123,42 +123,42 @@ void Fsm::check_timed_transitions()
123123 unsigned long now = millis ();
124124 if (now - transition->start >= transition->interval )
125125 {
126- Fsm::make_transition (&(transition->transition ));
126+ Fsm::make_transition (&(transition->transition ), ctx );
127127 transition->start = 0 ;
128128 }
129129 }
130130 }
131131 }
132132}
133133
134- void Fsm::run_machine ()
134+ void Fsm::run_machine (void * ctx )
135135{
136136 // first run must exec first state "on_enter"
137137 if (!m_initialized)
138138 {
139139 m_initialized = true ;
140140 if (m_current_state->on_enter != NULL )
141- m_current_state->on_enter ();
141+ m_current_state->on_enter (ctx );
142142 }
143143
144144 if (m_current_state->on_state != NULL )
145- m_current_state->on_state ();
145+ m_current_state->on_state (ctx );
146146
147- Fsm::check_timed_transitions ();
147+ Fsm::check_timed_transitions (ctx );
148148}
149149
150- void Fsm::make_transition (Transition* transition)
150+ void Fsm::make_transition (Transition* transition, void * ctx )
151151{
152152
153153 // Execute the handlers in the correct order.
154154 if (transition->state_from ->on_exit != NULL )
155- transition->state_from ->on_exit ();
155+ transition->state_from ->on_exit (ctx );
156156
157157 if (transition->on_transition != NULL )
158- transition->on_transition ();
158+ transition->on_transition (ctx );
159159
160160 if (transition->state_to ->on_enter != NULL )
161- transition->state_to ->on_enter ();
161+ transition->state_to ->on_enter (ctx );
162162
163163 m_current_state = transition->state_to ;
164164
0 commit comments