using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using System; using SecretLabs.NETMF.Hardware.NetduinoPlus; namespace StateMachineExample { public class Program { //StateMachineExample //Antonio Terrazas Lara //May 20th, 2012 #region static class variables static int i = new int(); static int j = new int(); static StateMachineEngine sm1; static StateMachineEngine sm2; static StateMachineEngine sm3; #endregion public static void Main() { //Create and run the State Machine engine sm1 = new StateMachineEngine("State Machine 1", CounterUp , 1, new Message("Initialize",null)); sm1.Run(); //Create and run the State Machine engine sm2 = new StateMachineEngine("State Machine 2", CounterDown, 1, new Message("Initialize", null)); sm2.Run(); //Create and run the State Machine engine sm3 = new StateMachineEngine("State Machine 3", Display, 1, new Message("Initialize", null)); sm3.Run(); //Create an interrupt to demonstrate the source of the event to change the Machines InterruptPort button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth); button.OnInterrupt += new NativeEventHandler(button_OnInterrupt); Thread.Sleep(Timeout.Infinite); } //Initializes all the state machines static void button_OnInterrupt(uint data1, uint data2, DateTime time) { if (data2 == 1) { sm1.QueueHdlr.Enque(new Message("Initialize", null)); sm2.QueueHdlr.Enque(new Message("Initialize", null)); sm3.QueueHdlr.Enque(new Message("Initialize", null)); } } #region State Machine 1 cases //CounterUp increases the i iterator public static void CounterUp(QueueHandler qh) { //Message contains the name of the state and object to send and //receive data usefull in the state Message msg = new Message(); //Fetchs the state msg = qh.Dequeue(); //Next Case... switch (msg.Name) { case "Initialize": { i = 0; msg.Name = "Increment"; qh.Clear(); qh.Enque(msg); break; } case "Increment": { i++; msg.Name = "Wait"; qh.Enque(msg); break; } case "Display": { Debug.Print(msg.Name + " " + i.ToString()); msg.Name = "Wait"; qh.Enque(msg); break; } case "Wait": { Thread.Sleep(500); msg.Name = "Increment"; qh.Enque(msg); break; } //Insert more cases here.... default: //Enters here on time out... IDLE state { } break; } } #endregion #region State Machine 2 cases //CounterDown decreases the j iterator public static void CounterDown(QueueHandler qh) { //Message contains the name of the state and object to send and //receive data usefull in the state Message msg = new Message(); //Fetchs the state msg = qh.Dequeue(); //Next Case... switch (msg.Name) { case "Initialize": { j = 0; msg.Name = "Decrement"; qh.Clear(); qh.Enque(msg); break; } case "Decrement": { j--; msg.Name = "Wait"; qh.Enque(msg); break; } case "Display": { Debug.Print(msg.Name + " " + j.ToString()); msg.Name = "Wait"; qh.Enque(msg); break; } case "Wait": { Thread.Sleep(1000); msg.Name = "Decrement"; qh.Enque(msg); break; } //Insert more cases here.... default: //Enters here on time out... IDLE state { } break; } } #endregion #region State Machine 3 cases //Display display the column names and the values of the i and j iterator public static void Display(QueueHandler qh) { //Message contains the name of the state and object to send and //receive data usefull in the state Message msg = new Message(); //Fetchs the state msg = qh.Dequeue(); //Next Case... switch (msg.Name) { case "Initialize": { Debug.Print("i j"); Debug.Print("----"); msg.Name = "Display"; qh.Clear(); qh.Enque(msg); break; } case "Display": { Debug.Print(i.ToString() + " " + j.ToString()); msg.Name = "Wait"; qh.Enque(msg); break; } case "Wait": { Thread.Sleep(1000); msg.Name = "Display"; qh.Enque(msg); break; } //Insert more cases here.... default: //Enters here on time out... IDLE state { } break; } } #endregion } }