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 #include <mbed.h>
snatch59 0:918566a376fb 25 #include "MyInterruptHandler.h"
snatch59 0:918566a376fb 26 #include "FSMDefs.h"
snatch59 0:918566a376fb 27 #include "FSM.h"
snatch59 0:918566a376fb 28 #include "DebugTrace.h"
snatch59 0:918566a376fb 29
snatch59 0:918566a376fb 30 DebugTrace pc(ON, TO_SERIAL);
snatch59 0:918566a376fb 31
snatch59 0:918566a376fb 32 typedef void (MyInterruptHandler::* clientActionPtr)( );
snatch59 0:918566a376fb 33
snatch59 0:918566a376fb 34 // states
snatch59 0:918566a376fb 35 const char* S_START = "Start";
snatch59 0:918566a376fb 36 const char* S_LED_SEQ1 = "LedSeq1";
snatch59 0:918566a376fb 37 const char* S_LED_SEQ2 = "LedSeq2";
snatch59 0:918566a376fb 38
snatch59 0:918566a376fb 39 const StateDefinition<clientActionPtr> states[] =
snatch59 0:918566a376fb 40 {// state, entry action, exit action
snatch59 0:918566a376fb 41 S_START, NULL, NULL,
snatch59 0:918566a376fb 42 S_LED_SEQ1, &MyInterruptHandler::ledSequence1, NULL,
snatch59 0:918566a376fb 43 S_LED_SEQ2, &MyInterruptHandler::ledSequence2, NULL
snatch59 0:918566a376fb 44 };
snatch59 0:918566a376fb 45 const int total_states = sizeof(states)/sizeof(StateDefinition<clientActionPtr>);
snatch59 0:918566a376fb 46
snatch59 0:918566a376fb 47 // transitions
snatch59 0:918566a376fb 48 const TransitionDefinition<clientActionPtr> transitions[] =
snatch59 0:918566a376fb 49 {// start state, event, type transition action, end state
snatch59 0:918566a376fb 50 S_START, (int)intr1, actions, NULL, S_LED_SEQ1,
snatch59 0:918566a376fb 51 S_LED_SEQ1, (int)intr2, actions, NULL, S_LED_SEQ2,
snatch59 0:918566a376fb 52 S_LED_SEQ2, (int)intr1, actions, NULL, S_LED_SEQ1,
snatch59 0:918566a376fb 53 S_LED_SEQ1, (int)tickr1, actions, NULL, S_LED_SEQ1,
snatch59 0:918566a376fb 54 S_LED_SEQ2, (int)tickr1, actions, NULL, S_LED_SEQ2,
snatch59 0:918566a376fb 55 };
snatch59 0:918566a376fb 56 const int total_transitions = sizeof(transitions)/sizeof(TransitionDefinition<clientActionPtr>);
snatch59 0:918566a376fb 57
snatch59 0:918566a376fb 58 // declare a state machine
snatch59 0:918566a376fb 59 static FiniteStateMachine<MyInterruptHandler, clientActionPtr, total_states> itsStateMachine;
snatch59 0:918566a376fb 60
snatch59 0:918566a376fb 61 // put LEDs on a bus for convenience
snatch59 0:918566a376fb 62 BusOut leds(LED1, LED2, LED3, LED4);
snatch59 0:918566a376fb 63
snatch59 0:918566a376fb 64
snatch59 0:918566a376fb 65 MyInterruptHandler::MyInterruptHandler() : interrupt1(p5), interrupt2(p6)
snatch59 0:918566a376fb 66 {
snatch59 0:918566a376fb 67 // init state machine
snatch59 0:918566a376fb 68 itsStateMachine.initialize(this, states, total_states, transitions, total_transitions, S_START);
snatch59 0:918566a376fb 69
snatch59 0:918566a376fb 70 // attach callbacks for interrupts and timer
snatch59 0:918566a376fb 71 interrupt1.rise(this, &MyInterruptHandler::intrhandler1);
snatch59 0:918566a376fb 72 interrupt2.rise(this, &MyInterruptHandler::intrhandler2);
snatch59 0:918566a376fb 73 ticker1.attach(this, &MyInterruptHandler::tickerhandler1, 0.5);
snatch59 0:918566a376fb 74 }
snatch59 0:918566a376fb 75
snatch59 0:918566a376fb 76 void MyInterruptHandler::processEvent(clientEvent event_id)
snatch59 0:918566a376fb 77 {
snatch59 0:918566a376fb 78 itsStateMachine.traverse((int)event_id);
snatch59 0:918566a376fb 79 }
snatch59 0:918566a376fb 80
snatch59 0:918566a376fb 81 void MyInterruptHandler::intrhandler1()
snatch59 0:918566a376fb 82 {
snatch59 0:918566a376fb 83 processEvent(intr1);
snatch59 0:918566a376fb 84 }
snatch59 0:918566a376fb 85
snatch59 0:918566a376fb 86 void MyInterruptHandler::intrhandler2()
snatch59 0:918566a376fb 87 {
snatch59 0:918566a376fb 88 processEvent(intr2);
snatch59 0:918566a376fb 89 }
snatch59 0:918566a376fb 90
snatch59 0:918566a376fb 91 void MyInterruptHandler::tickerhandler1()
snatch59 0:918566a376fb 92 {
snatch59 0:918566a376fb 93 processEvent(tickr1);
snatch59 0:918566a376fb 94 }
snatch59 0:918566a376fb 95
snatch59 0:918566a376fb 96 void MyInterruptHandler::ledSequence1()
snatch59 0:918566a376fb 97 {
snatch59 0:918566a376fb 98 pc.traceOut("ledSequence1\r\n");
snatch59 0:918566a376fb 99 (0x00 == leds || 0x08 == leds) ? leds = 1 : leds = leds << 1;
snatch59 0:918566a376fb 100 }
snatch59 0:918566a376fb 101
snatch59 0:918566a376fb 102 void MyInterruptHandler::ledSequence2()
snatch59 0:918566a376fb 103 {
snatch59 0:918566a376fb 104 pc.traceOut("ledSequence2\r\n");
snatch59 0:918566a376fb 105 (0x00 == leds || 0x01 == leds) ? leds = 8 : leds = leds >> 1;
snatch59 0:918566a376fb 106 }