Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timed Transition Timer Reset #34

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion Fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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)())
{
Expand Down Expand Up @@ -108,6 +107,10 @@ void Fsm::trigger(int event)
}
}

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 @@ -131,6 +134,21 @@ void Fsm::check_timed_transitions()
}
}

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"
Expand Down
21 changes: 21 additions & 0 deletions Fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,32 @@ class Fsm
void add_timed_transition(State* state_from, State* state_to,
unsigned long interval, void (*on_transition)());

/**
* 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