Petras Saduikis / Mbed 2 deprecated FiniteStateMachine

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MyInterruptHandler.cpp Source File

MyInterruptHandler.cpp

00001 /*
00002 * FiniteStateMachine. Table driven Finite State Machine library 
00003 * based on theHarel state machine, supporting actions on transitions, state
00004 * entry and state exit.
00005 *
00006 * Copyright (C) <2009> Petras Saduikis <petras@petras.co.uk>
00007 *
00008 * This file is part of FiniteStateMachine.
00009 *
00010 * FiniteStateMachine is free software: you can redistribute it and/or modify
00011 * it under the terms of the GNU General Public License as published by
00012 * the Free Software Foundation, either version 3 of the License, or
00013 * (at your option) any later version.
00014 * 
00015 * FiniteStateMachine is distributed in the hope that it will be useful,
00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018 * GNU General Public License for more details.
00019 *
00020 * You should have received a copy of the GNU General Public License
00021 * along with DebugTrace.  If not, see <http://www.gnu.org/licenses/>.
00022 */
00023 
00024 #include <mbed.h>
00025 #include "MyInterruptHandler.h"
00026 #include "FSMDefs.h"
00027 #include "FSM.h"
00028 #include "DebugTrace.h"
00029 
00030 DebugTrace pc(ON, TO_SERIAL);
00031 
00032 typedef void (MyInterruptHandler::* clientActionPtr)( );
00033 
00034 // states
00035 const char* S_START     = "Start";
00036 const char* S_LED_SEQ1  = "LedSeq1";
00037 const char* S_LED_SEQ2  = "LedSeq2";
00038 
00039 const StateDefinition<clientActionPtr> states[] = 
00040    {// state,        entry action,                         exit action
00041        S_START,       NULL,                                NULL,
00042        S_LED_SEQ1,    &MyInterruptHandler::ledSequence1,   NULL,
00043        S_LED_SEQ2,    &MyInterruptHandler::ledSequence2,   NULL
00044    };
00045 const int total_states = sizeof(states)/sizeof(StateDefinition<clientActionPtr>);
00046 
00047 // transitions
00048 const TransitionDefinition<clientActionPtr> transitions[] = 
00049    {// start state,     event,          type        transition action,    end state
00050        S_START,         (int)intr1,     actions,    NULL,                 S_LED_SEQ1,
00051        S_LED_SEQ1,      (int)intr2,     actions,    NULL,                 S_LED_SEQ2,
00052        S_LED_SEQ2,      (int)intr1,     actions,    NULL,                 S_LED_SEQ1,
00053        S_LED_SEQ1,      (int)tickr1,    actions,    NULL,                 S_LED_SEQ1,
00054        S_LED_SEQ2,      (int)tickr1,    actions,    NULL,                 S_LED_SEQ2,
00055    };
00056 const int total_transitions = sizeof(transitions)/sizeof(TransitionDefinition<clientActionPtr>);
00057 
00058 // declare a state machine
00059 static FiniteStateMachine<MyInterruptHandler, clientActionPtr, total_states> itsStateMachine;
00060 
00061 // put LEDs on a bus for convenience
00062 BusOut leds(LED1, LED2, LED3, LED4);
00063 
00064 
00065 MyInterruptHandler::MyInterruptHandler() : interrupt1(p5), interrupt2(p6)
00066 {
00067     // init state machine
00068     itsStateMachine.initialize(this, states, total_states, transitions, total_transitions, S_START);
00069     
00070     // attach callbacks for interrupts and timer
00071     interrupt1.rise(this, &MyInterruptHandler::intrhandler1);
00072     interrupt2.rise(this, &MyInterruptHandler::intrhandler2);
00073     ticker1.attach(this, &MyInterruptHandler::tickerhandler1, 0.5);
00074 }
00075 
00076 void MyInterruptHandler::processEvent(clientEvent event_id)
00077 {
00078     itsStateMachine.traverse((int)event_id);
00079 }
00080 
00081 void MyInterruptHandler::intrhandler1()
00082 {
00083     processEvent(intr1);
00084 }
00085 
00086 void MyInterruptHandler::intrhandler2()
00087 {
00088     processEvent(intr2);
00089 }
00090 
00091 void MyInterruptHandler::tickerhandler1()
00092 {
00093     processEvent(tickr1);
00094 }
00095 
00096 void MyInterruptHandler::ledSequence1()
00097 {
00098     pc.traceOut("ledSequence1\r\n");
00099     (0x00 == leds || 0x08 == leds) ? leds = 1 : leds = leds << 1;
00100 }
00101 
00102 void MyInterruptHandler::ledSequence2()
00103 {
00104     pc.traceOut("ledSequence2\r\n");
00105     (0x00 == leds || 0x01 == leds) ? leds = 8 : leds = leds >> 1;
00106 }