I have a very simple button class that uses an interrupt port to raise pressed and released events for a hardware button. Button.State reports accurately, but the interrupt never fires. Can someone look at my code and see if I’m making some stupid mistake somewhere?
public class HardwareButton
{
private InterruptPort port;
public delegate void OnSwitch(HardwareButton sender);
public event OnSwitch PressedEvent;
public event OnSwitch ReleasedEvent;
/// <summary>
/// Gets or sets the input state considered to be active
/// </summary>
public bool ActiveState { get; set; }
/// <summary>
/// Gets the current state of the button
/// </summary>
public bool State { get { return port.Read() == ActiveState; } }
public HardwareButton(Cpu.Pin input, Port.ResistorMode resistorMode = Port.ResistorMode.Disabled, bool activeState = false)
{
this.port = new InterruptPort(input, true, resistorMode, Port.InterruptMode.InterruptEdgeBoth);
this.port.OnInterrupt += inputPort_OnInterrupt;
this.ActiveState = activeState;
}
private void inputPort_OnInterrupt(uint port, uint state, DateTime time)
{
DebugHelper.Print("Button triggered");
if(this.State)
{
if (this.PressedEvent != null)
{
this.PressedEvent(this);
}
}
else
{
if(this.ReleasedEvent != null)
{
this.ReleasedEvent(this);
}
}
}
}
When I try to validate the interrupt port in a test project it works.
public partial class Program
{
void ProgramStarted()
{
InterruptPort port = new InterruptPort(GHI.Pins.G400.PA22, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
port.OnInterrupt += port_OnInterrupt;
Debug.Print("Program Started");
}
void port_OnInterrupt(uint data1, uint data2, DateTime time)
{
Debug.Print("Triggered");
}
}
I can’t see the difference in InterruptPort implementation.