using System;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using Toolbox.NETMF.Hardware;using Toolbox.NETMF;namespace NetduinoZandbak{ public class BaseBoard { #region Variables // Buttons private static InterruptPort btn1; private static InterruptPort btn2; // Switching modules. public static ModuleA ma; public static ModuleB mb; // Feedback led. private OutputPort Led; #endregion #region Constructor /// <summary> /// Main class.. /// Instance created in Program.cs /// </summary> public BaseBoard() { try { Led = new OutputPort(Pins.ONBOARD_LED, false); // Button 1 init btn1 = new InterruptPort(Pins.GPIO_PIN_D10, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); btn1.OnInterrupt += new NativeEventHandler(btn1_OnInterrupt); // Button 2 init btn2 = new InterruptPort(Pins.GPIO_PIN_D11, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth); btn2.OnInterrupt += new NativeEventHandler(btn2_OnInterrupt); Debug.Print("Ready......"); // Stanby mode and wait for interrupts... Thread.Sleep(Timeout.Infinite); } catch (Exception ex) { Debug.Print(ex.Message); } } #endregion #region Events /// <summary> /// Event handler when button 1 is pressed /// </summary> /// <param name="data1"></param> /// <param name="data2"></param> /// <param name="time"></param> void btn1_OnInterrupt(uint data1, uint data2, DateTime time) { try { if (data2 == 0) { // Pressed Debug.Print("Button 1 pressed on " + time.TimeOfDay.ToString()); Led.Write(!Led.Read()); // if mb exists, stop while loop. if (mb != null) mb.Stop(); if (ma == null) ma = new ModuleA(); // start module A loop ma.Start(); } } catch (Exception ex) { Debug.Print(ex.Message); } } /// <summary> /// Event handler when button 2 is pressed /// </summary> /// <param name="data1"></param> /// <param name="data2"></param> /// <param name="time"></param> void btn2_OnInterrupt(uint data1, uint data2, DateTime time) { try { if (data2 == 0) { // Pressed Debug.Print("Button 2 pressed on " + time.TimeOfDay.ToString()); Led.Write(!Led.Read()); // if ma exists, stop while loop. if (ma != null) ma.Stop(); if (mb == null) mb = new ModuleB(); // start module B loop mb.Start(); } } catch (Exception ex) { Debug.Print(ex.Message); } } #endregion }}
EDIT: forgot code for the modules. ModuleA and ModuleB are same structure...
public class ModuleA { public bool _stop = false; public string var = "A"; public ModuleA() { // Constructor for module A } public void Start() { _stop = false; while (_stop == false) { Thread.Sleep(200); Debug.Print("Do important stuff. Like reading port.... Module " + var); } } /// <summary> /// Halt main routine /// </summary> public void Stop() { _stop = true; } }