I think the problem that I am having is being caused by debouncing but I am not sure why I am experiencing it since I believe I have code that should mitigate it.
I declare an Interrupt (heartBeatInput):
heartBeatInput = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
In my OnInterrupt event I check for the value of data2 to see if the pin is high or low. I have D1 pulled down to ground with a 4.7k resistor. I trigger the input by touching D1 to 3.3v and here is where I get confused. First of all, I do expect a little debouncing, but I a, seeing the Interrup event handled so many times that I believe it is causing am Out of Memory exception that I am seeing. I expect my heartBeatInput_OnInterrupt to not fire again until I call heartBeatInput.ClearInterrupt(). I call the ClearInterrupt method in my handleHeartBeat method. But it does fire, so I am checking for the value of data2.
Why is heartBeatInput_OnInterrupt firing again before the ClearInterrupt is called ? How can I better handle this problem ?
********************************************************* CODE **********************************************************
static void heartBeatInput_OnInterrupt(uint data1, uint data2, DateTime time)
{
//Debug.GC(true);
//Debug.EnableGCMessages(true);
try
{
Debug.Print(data2.ToString());
if (data2 == 1)
{
bool blnHandle = false;
TimeSpan ts = time - timeOfLastHeartbeat;
// TODO Make this a variable that is declared above
if (ts.Ticks > 1500000) //Must be greater than 1.5 seconds
{
blnHandle = true;
}
else
{
blnHandle = false;
}
//}
if (blnHandle == true)
{
Debug.Print("Handle = True");
handleHeartBeat(time.ToUniversalTime());
}
else
{
//Debug.Print("DO NOT HANDLE");
}
}
}
catch(Exception ex)
{
Debug.Print(ex.Message);
Thread.Sleep(4000);
}
//Debug.GC(true);
}
private static void handleHeartBeat(DateTime time)
{
totalRuntimeMilliseconds += getMillisecondsSinceLastHeartBeat(time);
totalNumberOfCycles++;
numberOfHeartBeatsSinceLastStateChange++;
TimeSpan ts = time - timeOfLastHeartbeat;
double totalMillisecondsSinceLastCycle = ts.Ticks / 10000.0;
Debug.Print(totalMillisecondsSinceLastCycle.ToString());
if (currentSystemState == SystemState.DOWN &&
numberOfHeartBeatsSinceLastStateChange > numberOfHeartBeatsRequiredToChangeState &&
totalMillisecondsSinceLastCycle < (cycleMillisecondsLength * 1.5))
{
setSystemSateToRun(time);
}
timeOfLastHeartbeat = time;
heartBeatInput.ClearInterrupt();
//Debug.GC(true);
//Debug.Print("Number of cycles: " + totalNumberOfCycles
// + " Total cycle time: " + totalRuntimeMilliseconds);
}