Craig Evans / Mbed 2 deprecated ELEC2645_FSM_Counter

Dependencies:   mbed

main.cpp

Committer:
eencae
Date:
2020-01-24
Revision:
3:aac8dcbe6e18
Parent:
2:bdd8b01141d1
Child:
4:c6d3c2dea4b0

File content as of revision 3:aac8dcbe6e18:

/* 

2645_FSM_Counter

Sample code from ELEC2645

Demonstrates how to implement a simple FSM counter

(c) Craig A. Evans, University of Leeds, Jan 2016
Updated January 2020

*/ 

#include "mbed.h"

// K64F on-board LEDs 
BusOut k64f_leds(LED_RED, LED_GREEN, LED_BLUE);
// Gamepad switches
InterruptIn buttonA(PTC7);
InterruptIn buttonB(PTC9);
// LEDs on Gamepad (1 to 3) - active-low 0 = on and 1 = off
// BusOut arguments are LSB first
BusOut output(PTA2,PTC2,PTC3);

// array of states in the FSM, each element is the output of the counter
// set the output in binary to make it easier, 0 is LED on, 1 is LED off
int fsm[3] = {0b011,0b101,0b110};


int main()
{
    k64f_leds = 0b111;  // turn off K64F LEDs
    
    // set inital state 
    int state = 0;
    
    while(1) {  // loop forever

        output = fsm[state];  // output current state

        // check which state we are in and see which the next state should be
        switch(state) {
            case 0:
                state = 1;
                break;
            case 1:
                state = 2;
                break;
            case 2:
                state = 0;
                break;
            default:
                error("Invalid state");  //invalid state - call Mbed error routine
                // or could jump to starting state i.e. state = 0
                break;  
        }

        wait(0.5);  // small delay

    }
}