Skip to content

Commit aafefbd

Browse files
committed
now includes fix to /jonblack/arduino-fsm/ PR jonblack#29
2 parents 234de69 + f4910a2 commit aafefbd

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
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)
@@ -45,7 +45,7 @@ Fsm::~Fsm()
4545

4646

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

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

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

Fsm.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ class Fsm
4545
void add_timed_transition(State* state_from, State* state_to,
4646
unsigned long interval, void (*on_transition)() = nullptr);
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;

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ An arduino library for implementing a finite state machine.
55
Other than the examples included in the library, the following pages might be
66
useful to you:
77

8-
* [Humble Coder: Arduino finite state machine library][1]
9-
* [Humble Coder: Arduino multitasking using a finite state machine][2]
8+
* [Arduino finite state machine library][1]
9+
* [Arduino multitasking using a finite state machine][2]
1010

11-
[1]: http://www.humblecoder.com/arduino-finite-state-machine-library/
12-
[2]: http://www.humblecoder.com/arduino-multitasking-using-finite-state-machines/
11+
[1]: https://jonblack.me/arduino-finite-state-machine-library/
12+
[2]: https://jonblack.me/arduino-multitasking-using-finite-state-machines/
1313

1414
# Contribution
1515

0 commit comments

Comments
 (0)