I decided to try modifying the ButtonApp sample to use interrupts instead of polling the state of the pushbutton switch.
I found an example in the book "Embedded Programming with the Microsoft .NET Micro Framework", and kinda mashed up my own ToggleButton app.
The basic idea is to have the pushbutton switch "toggle" the LED on and off with each successive press (i.e., the first time the button is pushed and released, the LED turns on, and the next time the button is pushed, the LED turns off, etc.).
Here's the code that I came up with. There's probably some unnecessary code in here, since I was trying various ways to make it work.
using System; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.Netduino; namespace ToggleButton { public class Program { static InterruptPort button; static OutputPort led; static bool ledState; public static void Main() { // Set the initial state of the LED to off (false). ledState = true; led = new OutputPort(Pins.ONBOARD_LED, ledState); button = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLevelLow); // Bind the interrupt handler to the pin's interrupt event. button.OnInterrupt += SwitchInterruptHandler; button.EnableInterrupt(); while (true) { Thread.Sleep(Timeout.Infinite); } } public static void SwitchInterruptHandler(UInt32 data1, UInt32 data2, DateTime time) { Debug.Print("+SwitchInterruptHandler"); button.DisableInterrupt(); // Invert the previous state of the LED. ledState = !ledState; // Set the LED to its new state. led.Write(ledState); button.EnableInterrupt(); button.ClearInterrupt(); Debug.Print("-SwitchInterruptHandler"); } } }
The problem is that the code seems to run fine under the debugger, but doesn't work as expected when it's not running in the debugger.
Can anybody spot what I'm doing wrong here?
Thanks,
-David