From 6fa9d60878de7eda005b69af206bd752a90a873f Mon Sep 17 00:00:00 2001 From: mtangy Date: Wed, 10 Apr 2019 15:03:57 -0500 Subject: [PATCH 1/2] Added capability to call the entry/exit action when transitioning to and from a group state --- src/stateMachine.c | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/stateMachine.c b/src/stateMachine.c index 25a8fc8..a1b8302 100644 --- a/src/stateMachine.c +++ b/src/stateMachine.c @@ -82,20 +82,40 @@ int stateM_handleEvent( struct stateMachine *fsm, while ( nextState->entryState ) nextState = nextState->entryState; - /* Run exit action only if the current state is left (only if it does + /* Run exit actions only if the current state is left (only if it does * not return to itself): */ - if ( nextState != fsm->currentState && fsm->currentState->exitAction ) - fsm->currentState->exitAction( fsm->currentState->data, event ); + if(nextState != fsm->currentState) + { + if ( fsm->currentState->exitAction ) + fsm->currentState->exitAction( fsm->currentState->data, event ); + + /* Call the current state's parent state exit action if it has one + * and if new parent state is different than the current states parent + * state */ + if ( fsm->currentState->parentState && fsm->currentState->parentState->entryAction && + (nextState->parentState != fsm->currentState->parentState) ) + fsm->currentState->parentState->exitAction( nextState->data, event ); + } /* Run transition action (if any): */ if ( transition->action ) transition->action( fsm->currentState->data, event, nextState-> data ); - /* Call the new state's entry action if it has any (only if state does - * not return to itself): */ - if ( nextState != fsm->currentState && nextState->entryAction ) - nextState->entryAction( nextState->data, event ); + /* Call the new state's entry actions if it has any (only if state does + * not return to itself): */ + if(nextState != fsm->currentState) + { + /* Call the new state's parent state entry action if it has one + * and if its parent state is different than the current states parent + * state */ + if ( nextState->parentState && nextState->parentState->entryAction && + (nextState->parentState != fsm->currentState->parentState) ) + nextState->parentState->entryAction( nextState->data, event ); + + if ( nextState->entryAction ) + nextState->entryAction( nextState->data, event ); + } fsm->previousState = fsm->currentState; fsm->currentState = nextState; From 08428d50095d61366902b8347d4cbd979bc3ceee Mon Sep 17 00:00:00 2001 From: mtangy Date: Thu, 11 Apr 2019 13:16:01 -0500 Subject: [PATCH 2/2] Fixed wrong data member being passed to parent entry/exit actions. Fixed white space. --- src/stateMachine.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/stateMachine.c b/src/stateMachine.c index a1b8302..44d09e2 100644 --- a/src/stateMachine.c +++ b/src/stateMachine.c @@ -82,7 +82,7 @@ int stateM_handleEvent( struct stateMachine *fsm, while ( nextState->entryState ) nextState = nextState->entryState; - /* Run exit actions only if the current state is left (only if it does + /* Run exit actions only if the current state is left (only if it does * not return to itself): */ if(nextState != fsm->currentState) { @@ -94,7 +94,7 @@ int stateM_handleEvent( struct stateMachine *fsm, * state */ if ( fsm->currentState->parentState && fsm->currentState->parentState->entryAction && (nextState->parentState != fsm->currentState->parentState) ) - fsm->currentState->parentState->exitAction( nextState->data, event ); + fsm->currentState->parentState->exitAction( fsm->currentState->parentState->data, event ); } /* Run transition action (if any): */ @@ -102,8 +102,8 @@ int stateM_handleEvent( struct stateMachine *fsm, transition->action( fsm->currentState->data, event, nextState-> data ); - /* Call the new state's entry actions if it has any (only if state does - * not return to itself): */ + /* Call the new state's entry actions if it has any (only if state does + * not return to itself): */ if(nextState != fsm->currentState) { /* Call the new state's parent state entry action if it has one @@ -111,7 +111,7 @@ int stateM_handleEvent( struct stateMachine *fsm, * state */ if ( nextState->parentState && nextState->parentState->entryAction && (nextState->parentState != fsm->currentState->parentState) ) - nextState->parentState->entryAction( nextState->data, event ); + nextState->parentState->entryAction( nextState->parentState->data, event ); if ( nextState->entryAction ) nextState->entryAction( nextState->data, event );