From 11f07b9873fb9c45208735999bfc2218b2f7e989 Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 11:29:03 +0100 Subject: [PATCH 1/6] implemented reset of the timer --- Fsm.cpp | 22 +++++++++++++++++++++- Fsm.h | 8 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/Fsm.cpp b/Fsm.cpp index 9b932ca..230f379 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -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)()) { @@ -131,6 +130,27 @@ void Fsm::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 Fsm:void 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) ) { + unsigned long now = millis(); + transition->start = now; + } + } + } +} + + void Fsm::run_machine() { // first run must exec first state "on_enter" diff --git a/Fsm.h b/Fsm.h index afbb3dd..29750e3 100644 --- a/Fsm.h +++ b/Fsm.h @@ -47,7 +47,15 @@ class Fsm 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); + void trigger(int event); + void run_machine(); private: From c8f239fe480d166f75db70e785b9f155ed145caa Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 11:34:16 +0100 Subject: [PATCH 2/6] fixed small code issues --- Fsm.cpp | 7 +------ Fsm.h | 12 ++++++------ 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/Fsm.cpp b/Fsm.cpp index 230f379..ac6d151 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -130,12 +130,7 @@ void Fsm::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 Fsm:void reset_timed_transition(State* state_to) +void Fsm::reset_timed_transition(State* state_to) { for (int i = 0; i < m_num_timed_transitions; ++i) { diff --git a/Fsm.h b/Fsm.h index 29750e3..7a2af7b 100644 --- a/Fsm.h +++ b/Fsm.h @@ -47,12 +47,12 @@ class Fsm 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); + /** + * 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); void trigger(int event); From 46cd8dbfe2bebdd6e4f389bd5c322a30c140720c Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 17:09:28 +0100 Subject: [PATCH 3/6] reduced code size and debugged timers --- Fsm.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Fsm.cpp b/Fsm.cpp index ac6d151..5aa0cb8 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -138,8 +138,7 @@ void Fsm::reset_timed_transition(State* state_to) if (transition->transition.state_from == m_current_state) { if(state_to == NULL || (state_to != NULL && state_to == transition->transition.state_to) ) { - unsigned long now = millis(); - transition->start = now; + transition->start = millis(); } } } From 58d650494bd2c659c14db9b98554926e32eda9da Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 17:34:55 +0100 Subject: [PATCH 4/6] updated documentation --- Fsm.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Fsm.h b/Fsm.h index 7a2af7b..2b0ed57 100644 --- a/Fsm.h +++ b/Fsm.h @@ -45,15 +45,22 @@ 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 - * + * 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(); From bf5a4b6fa6397aefba7b5e446561421e9fe302c2 Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 18:11:01 +0100 Subject: [PATCH 5/6] added get_current_state method --- Fsm.cpp | 4 ++++ Fsm.h | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/Fsm.cpp b/Fsm.cpp index 5aa0cb8..b33ebfb 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -107,6 +107,10 @@ void Fsm::trigger(int event) } } +State* get_current_state() { + return m_current_state; +} + void Fsm::check_timed_transitions() { for (int i = 0; i < m_num_timed_transitions; ++i) diff --git a/Fsm.h b/Fsm.h index 2b0ed57..a9b827c 100644 --- a/Fsm.h +++ b/Fsm.h @@ -65,6 +65,12 @@ class Fsm 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 { From 5a551e13cdf035285e13d46e7bcd2429282ccbdd Mon Sep 17 00:00:00 2001 From: stellarshenson Date: Thu, 26 Mar 2020 18:19:26 +0100 Subject: [PATCH 6/6] added get current state method and tested --- Fsm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Fsm.cpp b/Fsm.cpp index b33ebfb..af2b115 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -107,7 +107,7 @@ void Fsm::trigger(int event) } } -State* get_current_state() { +State* Fsm::get_current_state() { return m_current_state; }