Skip to content

Commit

Permalink
merged pr jonblack#34 Timed Transition Timer Reset
Browse files Browse the repository at this point in the history
  • Loading branch information
tig committed Jul 7, 2021
2 parents aafefbd + 5a551e1 commit 6f3b918
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
24 changes: 21 additions & 3 deletions Fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ void Fsm::add_timed_transition(State* state_from, State* state_to,
m_num_timed_transitions++;
}


Fsm::Transition Fsm::create_transition(State* state_from, State* state_to,
int event, void (*on_transition)(void * ctx))
{
Expand Down Expand Up @@ -109,7 +108,11 @@ void Fsm::trigger(int event,void * ctx)
}
}

void Fsm::check_timed_transitions(void * ctx)
State* Fsm::get_current_state() {
return m_current_state;
}

void Fsm::check_timed_transitions()
{
for (int i = 0; i < m_num_timed_transitions; ++i)
{
Expand All @@ -132,7 +135,22 @@ void Fsm::check_timed_transitions(void * ctx)
}
}

void Fsm::run_machine(void * ctx)
void Fsm::reset_timed_transition(State* state_to)
{
for (int i = 0; i < m_num_timed_transitions; ++i)
{
TimedTransition* transition = &m_timed_transitions[i];
if (transition->transition.state_from == m_current_state)
{
if(state_to == NULL || (state_to != NULL && state_to == transition->transition.state_to) ) {
transition->start = millis();
}
}
}
}


void Fsm::run_machine()
{
// first run must exec first state "on_enter"
if (!m_initialized)
Expand Down
29 changes: 25 additions & 4 deletions Fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,31 @@ class Fsm
void add_timed_transition(State* state_from, State* state_to,
unsigned long interval, void (*on_transition)() = nullptr);

void check_timed_transitions(void * ctx);

void trigger(int event, void * ctx);
void run_machine(void * ctx);
/**
* checks the timed transitions for the current state and if timeout occured
* trigger appropriate transition. Timed transitions are checked and triggered in the same order as added
*/
void check_timed_transitions();

/**
* looks for the current state's timed transitions to the target state and reset the timer
* @param state_to target state to reset the timed transition for. If NULL reset all current state timers
*/
void reset_timed_transition(State* state_to);

/**
* trigger transition with the event
* @param event enum that defines the trigger
*/
void trigger(int event);

void run_machine();

/**
* returns current state (helpful if the same handler is used to drive many similar states)
* @return current state
*/
State* get_current_state();

private:
struct Transition
Expand Down

0 comments on commit 6f3b918

Please sign in to comment.