Skip to content

Commit f4910a2

Browse files
committed
Added optional context
1 parent 338624a commit f4910a2

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

Fsm.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
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

4646
void 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

6161
void 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

8282
Fsm::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

Fsm.h

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
struct State
2828
{
29-
State(void (*on_enter)(), void (*on_state)(), void (*on_exit)());
30-
void (*on_enter)();
31-
void (*on_state)();
32-
void (*on_exit)();
29+
State(void (*on_enter)(void *), void (*on_state)(void *), void (*on_exit)(void *));
30+
void (*on_enter)(void * );
31+
void (*on_state)(void * );
32+
void (*on_exit)(void * );
3333
};
3434

3535

@@ -40,23 +40,23 @@ class Fsm
4040
~Fsm();
4141

4242
void add_transition(State* state_from, State* state_to, int event,
43-
void (*on_transition)());
43+
void (*on_transition)(void * ));
4444

4545
void add_timed_transition(State* state_from, State* state_to,
46-
unsigned long interval, void (*on_transition)());
46+
unsigned long interval, void (*on_transition)(void * ));
4747

48-
void check_timed_transitions();
48+
void check_timed_transitions(void * ctx);
4949

50-
void trigger(int event);
51-
void run_machine();
50+
void trigger(int event, void * ctx);
51+
void run_machine(void * ctx);
5252

5353
private:
5454
struct Transition
5555
{
5656
State* state_from;
5757
State* state_to;
5858
int event;
59-
void (*on_transition)();
59+
void (*on_transition)(void * ctx);
6060

6161
};
6262
struct TimedTransition
@@ -67,9 +67,9 @@ class Fsm
6767
};
6868

6969
static Transition create_transition(State* state_from, State* state_to,
70-
int event, void (*on_transition)());
70+
int event, void (*on_transition)(void * ctx));
7171

72-
void make_transition(Transition* transition);
72+
void make_transition(Transition* transition,void * ctx);
7373

7474
private:
7575
State* m_current_state;

0 commit comments

Comments
 (0)