Table driven Finite State Machine library based on the Harel state machine, supporting actions on transitions, state entry and state exit. Comes with example illustrating use with interrupts and timers. 03/01/2010 - fixed potential memory leak in DebugTrace.

Dependencies:   mbed

Committer:
snatch59
Date:
Sun Jan 03 11:56:03 2010 +0000
Revision:
0:918566a376fb

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
snatch59 0:918566a376fb 1 /*
snatch59 0:918566a376fb 2 * FiniteStateMachine. Table driven Finite State Machine library
snatch59 0:918566a376fb 3 * based on theHarel state machine, supporting actions on transitions, state
snatch59 0:918566a376fb 4 * entry and state exit.
snatch59 0:918566a376fb 5 *
snatch59 0:918566a376fb 6 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
snatch59 0:918566a376fb 7 *
snatch59 0:918566a376fb 8 * This file is part of FiniteStateMachine.
snatch59 0:918566a376fb 9 *
snatch59 0:918566a376fb 10 * FiniteStateMachine is free software: you can redistribute it and/or modify
snatch59 0:918566a376fb 11 * it under the terms of the GNU General Public License as published by
snatch59 0:918566a376fb 12 * the Free Software Foundation, either version 3 of the License, or
snatch59 0:918566a376fb 13 * (at your option) any later version.
snatch59 0:918566a376fb 14 *
snatch59 0:918566a376fb 15 * FiniteStateMachine is distributed in the hope that it will be useful,
snatch59 0:918566a376fb 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
snatch59 0:918566a376fb 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
snatch59 0:918566a376fb 18 * GNU General Public License for more details.
snatch59 0:918566a376fb 19 *
snatch59 0:918566a376fb 20 * You should have received a copy of the GNU General Public License
snatch59 0:918566a376fb 21 * along with DebugTrace. If not, see <http://www.gnu.org/licenses/>.
snatch59 0:918566a376fb 22 */
snatch59 0:918566a376fb 23
snatch59 0:918566a376fb 24 #ifndef SNATCH59_STATETRANSITION_H
snatch59 0:918566a376fb 25 #define SNATCH59_STATETRANSITION_H
snatch59 0:918566a376fb 26
snatch59 0:918566a376fb 27 #include <mbed.h>
snatch59 0:918566a376fb 28 #include "FSMDefs.h"
snatch59 0:918566a376fb 29
snatch59 0:918566a376fb 30 STATE_TEMPLATE_ class State;
snatch59 0:918566a376fb 31
snatch59 0:918566a376fb 32 #include "State.h"
snatch59 0:918566a376fb 33
snatch59 0:918566a376fb 34 STATE_TEMPLATE_
snatch59 0:918566a376fb 35 class StateTransition
snatch59 0:918566a376fb 36 {
snatch59 0:918566a376fb 37
snatch59 0:918566a376fb 38 public:
snatch59 0:918566a376fb 39 StateTransition(int event_id, ActionPtrType action, STATE_* state_ptr, StateBehaviour behaviour);
snatch59 0:918566a376fb 40
snatch59 0:918566a376fb 41 STATE_* match(const int event_id) const;
snatch59 0:918566a376fb 42 void doAction(StateObjectType* &its_state_object) const;
snatch59 0:918566a376fb 43 StateBehaviour getStateBehaviour() const;
snatch59 0:918566a376fb 44
snatch59 0:918566a376fb 45 private:
snatch59 0:918566a376fb 46 int eventId;
snatch59 0:918566a376fb 47 ActionPtrType action;
snatch59 0:918566a376fb 48 STATE_* newState;
snatch59 0:918566a376fb 49 StateBehaviour mode;
snatch59 0:918566a376fb 50 };
snatch59 0:918566a376fb 51
snatch59 0:918566a376fb 52 STATE_TEMPLATE_
snatch59 0:918566a376fb 53 inline STATE_* STATE_TRANSITION_::match(const int event_id) const
snatch59 0:918566a376fb 54 {
snatch59 0:918566a376fb 55 if (eventId == event_id) return newState;
snatch59 0:918566a376fb 56
snatch59 0:918566a376fb 57 return NULL;
snatch59 0:918566a376fb 58 }
snatch59 0:918566a376fb 59
snatch59 0:918566a376fb 60 STATE_TEMPLATE_
snatch59 0:918566a376fb 61 inline StateBehaviour STATE_TRANSITION_::getStateBehaviour() const
snatch59 0:918566a376fb 62 {
snatch59 0:918566a376fb 63 return mode;
snatch59 0:918566a376fb 64 }
snatch59 0:918566a376fb 65
snatch59 0:918566a376fb 66 STATE_TEMPLATE_
snatch59 0:918566a376fb 67 STATE_TRANSITION_::StateTransition(int event_id, ActionPtrType action_ptr, STATE_* state_ptr, StateBehaviour behaviour) :
snatch59 0:918566a376fb 68 eventId(event_id), action(action_ptr), newState(state_ptr), mode(behaviour)
snatch59 0:918566a376fb 69 {
snatch59 0:918566a376fb 70 }
snatch59 0:918566a376fb 71
snatch59 0:918566a376fb 72 STATE_TEMPLATE_
snatch59 0:918566a376fb 73 void STATE_TRANSITION_::doAction(StateObjectType* &its_state_object) const
snatch59 0:918566a376fb 74 {
snatch59 0:918566a376fb 75 if (action != NULL)
snatch59 0:918566a376fb 76 {
snatch59 0:918566a376fb 77 (its_state_object->*action)( );
snatch59 0:918566a376fb 78 }
snatch59 0:918566a376fb 79 }
snatch59 0:918566a376fb 80
snatch59 0:918566a376fb 81 #endif