Craig Evans / Mbed 2 deprecated ELEC2645_FSM_Puffin

Dependencies:   mbed

Revision:
0:32827ba075c9
Child:
1:b6df1b5309e7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jan 07 12:01:55 2016 +0000
@@ -0,0 +1,61 @@
+/*
+
+2645_FSM_Puffin
+
+Sample code from ELEC2645 Week 16 Lab
+
+Demonstrates how to implement a puffin crossing using a FSM
+
+(c) Craig A. Evans, University of Leeds, Jan 2016
+
+*/
+
+#include "mbed.h"
+
+// K64F on-board LEDs
+DigitalOut r_led(LED_RED);
+DigitalOut g_led(LED_GREEN);
+DigitalOut b_led(LED_BLUE);
+
+// LEDs to display traffic light output
+// connect up external LEDs to these pins with appropriate current-limiting resistor
+// LSB                       MSB
+// car - green,amber, red  pedestrian - green, red
+BusOut output(PTB2,PTB3,PTB10,PTB11,PTC11);
+// BusIn to read inputs simutaneously
+// camera , pedestrian
+BusIn input(SW3,SW2);
+
+// struct for state
+struct State {
+    int output;  // output value
+    float time;    // time in state
+    int nextState[4];  // array of next states
+};
+typedef const struct State STyp;
+
+STyp fsm[4] = {
+    {0x0C,5.0,{0,1,0,1}},  // 0 - cars stop, pedestrian walk               5 seconds
+    {0x16,2.0,{2,2,2,2}},  // 1 - cars get ready to go, pedestrian stop    2 seconds
+    {0x11,10.0,{3,2,2,2}}, // 2 - cars go, pedestrian stop                10 seconds
+    {0x12,2.0,{0,0,0,0}}   // 3 - cars get ready to stop, pedestrian stop  2 seconds
+};
+
+int main()
+{
+    // on-board LEDs are active-low, so set pin high to turn them off.
+    r_led = 1;
+    g_led = 1;
+    b_led = 1;
+    
+    // on-board switches have external pull-ups, so turn off default internal pull-downs
+    input.mode(PullNone);
+    
+    int state = 2;  // start with cars green
+
+    while(1) {
+        output = fsm[state].output;  // set ouput depending on current state
+        wait(fsm[state].time);       // wait in that state for desired time           
+        state = fsm[state].nextState[input];  // read input (BusIn) and update curent state
+    }
+}