Skip to content

Commit a9719b2

Browse files
committed
Update the timed transitions start time on every state change
1 parent 0248127 commit a9719b2

File tree

3 files changed

+18
-11
lines changed

3 files changed

+18
-11
lines changed

Fsm.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ void Fsm::add_timed_transition(State* state_from, State* state_to,
6666

6767
TimedTransition timed_transition;
6868
timed_transition.transition = transition;
69-
timed_transition.start = 0;
69+
timed_transition.start = millis();
7070
timed_transition.interval = interval;
7171

7272
m_timed_transitions = (TimedTransition*) realloc(
@@ -97,6 +97,7 @@ void Fsm::trigger(int event)
9797
m_transitions[i].event == event)
9898
{
9999
m_current_state = m_transitions[i].make_transition();
100+
update_timed_transitions_starts();
100101
return;
101102
}
102103
}
@@ -110,17 +111,10 @@ void Fsm::check_timer()
110111
TimedTransition* transition = &m_timed_transitions[i];
111112
if (transition->transition.state_from == m_current_state)
112113
{
113-
if (transition->start == 0)
114+
if (millis() - transition->start >= transition->interval)
114115
{
115-
transition->start = millis();
116-
}
117-
else
118-
{
119-
if (millis() - transition->start >= transition->interval)
120-
{
121-
m_current_state = transition->transition.make_transition();
122-
transition->start = 0;
123-
}
116+
m_current_state = transition->transition.make_transition();
117+
update_timed_transitions_starts();
124118
}
125119
}
126120
}
@@ -141,3 +135,14 @@ State* Fsm::Transition::make_transition()
141135

142136
return state_to;
143137
}
138+
139+
void Fsm::update_timed_transitions_starts()
140+
{
141+
for (int i = 0; i < m_num_timed_transitions; ++i)
142+
{
143+
TimedTransition* transition = &m_timed_transitions[i];
144+
145+
if (transition->transition.state_from == m_current_state)
146+
transition->start = millis();
147+
}
148+
}

Fsm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class Fsm
6767

6868
static Transition create_transition(State* state_from, State* state_to,
6969
int event, void (*on_transition)());
70+
void update_timed_transitions_starts();
7071

7172
private:
7273
State* m_current_state;

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
The MIT License (MIT)
22

3+
Copyright (c) 2016 Nicola Corna <nicola@corna.info>
34
Copyright (c) 2015 Jon Black
45

56
Permission is hereby granted, free of charge, to any person obtaining a copy

0 commit comments

Comments
 (0)