Skip to content

Commit 6f3b918

Browse files
committed
merged pr jonblack#34 Timed Transition Timer Reset
2 parents aafefbd + 5a551e1 commit 6f3b918

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

Fsm.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ void Fsm::add_timed_transition(State* state_from, State* state_to,
7979
m_num_timed_transitions++;
8080
}
8181

82-
8382
Fsm::Transition Fsm::create_transition(State* state_from, State* state_to,
8483
int event, void (*on_transition)(void * ctx))
8584
{
@@ -109,7 +108,11 @@ void Fsm::trigger(int event,void * ctx)
109108
}
110109
}
111110

112-
void Fsm::check_timed_transitions(void * ctx)
111+
State* Fsm::get_current_state() {
112+
return m_current_state;
113+
}
114+
115+
void Fsm::check_timed_transitions()
113116
{
114117
for (int i = 0; i < m_num_timed_transitions; ++i)
115118
{
@@ -132,7 +135,22 @@ void Fsm::check_timed_transitions(void * ctx)
132135
}
133136
}
134137

135-
void Fsm::run_machine(void * ctx)
138+
void Fsm::reset_timed_transition(State* state_to)
139+
{
140+
for (int i = 0; i < m_num_timed_transitions; ++i)
141+
{
142+
TimedTransition* transition = &m_timed_transitions[i];
143+
if (transition->transition.state_from == m_current_state)
144+
{
145+
if(state_to == NULL || (state_to != NULL && state_to == transition->transition.state_to) ) {
146+
transition->start = millis();
147+
}
148+
}
149+
}
150+
}
151+
152+
153+
void Fsm::run_machine()
136154
{
137155
// first run must exec first state "on_enter"
138156
if (!m_initialized)

Fsm.h

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,31 @@ 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(void * ctx);
49-
50-
void trigger(int event, void * ctx);
51-
void run_machine(void * ctx);
48+
/**
49+
* checks the timed transitions for the current state and if timeout occured
50+
* trigger appropriate transition. Timed transitions are checked and triggered in the same order as added
51+
*/
52+
void check_timed_transitions();
53+
54+
/**
55+
* looks for the current state's timed transitions to the target state and reset the timer
56+
* @param state_to target state to reset the timed transition for. If NULL reset all current state timers
57+
*/
58+
void reset_timed_transition(State* state_to);
59+
60+
/**
61+
* trigger transition with the event
62+
* @param event enum that defines the trigger
63+
*/
64+
void trigger(int event);
65+
66+
void run_machine();
67+
68+
/**
69+
* returns current state (helpful if the same handler is used to drive many similar states)
70+
* @return current state
71+
*/
72+
State* get_current_state();
5273

5374
private:
5475
struct Transition

0 commit comments

Comments
 (0)